2 #include <oskitdebug.h>
3 #include <net/raw_cb.h>
7 #include <sys/socket.h>
8 #include <sys/kernel.h>
9 #include <sys/filedesc.h>
11 #include <sys/fcntl.h>
14 #include <sys/protosw.h>
15 #include <sys/socket.h>
16 #include <sys/socketvar.h>
19 struct linker_set domain_set
;
21 OSKITTCP_EVENT_HANDLERS OtcpEvent
= { 0 };
23 //OSK_UINT OskitDebugTraceLevel = OSK_DEBUG_ULTRA;
24 OSK_UINT OskitDebugTraceLevel
= 0;
29 unsigned volatile ipending
;
30 struct timeval boottime
;
33 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...);
35 void *fbsd_malloc( unsigned int bytes
, char *file
, unsigned line
, ... ) {
36 if( !OtcpEvent
.TCPMalloc
) panic("no malloc");
37 return OtcpEvent
.TCPMalloc
38 ( OtcpEvent
.ClientData
, (OSK_UINT
)bytes
, (OSK_PCHAR
)file
, line
);
41 void fbsd_free( void *data
, char *file
, unsigned line
, ... ) {
42 if( !OtcpEvent
.TCPFree
) panic("no free");
43 OtcpEvent
.TCPFree( OtcpEvent
.ClientData
, data
, (OSK_PCHAR
)file
, line
);
47 OS_DbgPrint(OSK_MID_TRACE
,("Init Called\n"));
48 OS_DbgPrint(OSK_MID_TRACE
,("MB Init\n"));
50 OS_DbgPrint(OSK_MID_TRACE
,("Rawip Init\n"));
53 OS_DbgPrint(OSK_MID_TRACE
,("Route Init\n"));
55 OS_DbgPrint(OSK_MID_TRACE
,("Init clock\n"));
57 OS_DbgPrint(OSK_MID_TRACE
,("Init TCP\n"));
59 OS_DbgPrint(OSK_MID_TRACE
,("Init routing\n"));
61 OS_DbgPrint(OSK_MID_TRACE
,("Init Finished\n"));
65 void DeinitOskitTCP() {
68 void TimerOskitTCP( int FastTimer
, int SlowTimer
) {
77 void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers
) {
78 memcpy( &OtcpEvent
, EventHandlers
, sizeof(OtcpEvent
) );
79 if( OtcpEvent
.PacketSend
)
80 OS_DbgPrint(OSK_MID_TRACE
,("SendPacket handler registered: %x\n",
81 OtcpEvent
.PacketSend
));
84 void OskitDumpBuffer( OSK_PCHAR Data
, OSK_UINT Len
)
88 static const char* hex
= "0123456789abcdef";
90 for ( i
= 0; i
< Len
; i
++ )
93 int align3
= (align
<<1) + align
;
94 unsigned char c
= Data
[i
];
97 if ( i
) DbgPrint( line
);
98 _snprintf ( line
, sizeof(line
)-1, "%08x: \n", &Data
[i
] );
99 line
[sizeof(line
)-1] = '\0';
102 line
[10+align3
] = hex
[(c
>>4)&0xf];
103 line
[11+align3
] = hex
[c
&0xf];
112 /* From uipc_syscalls.c */
114 int OskitTCPSocket( void *context
,
121 int error
= socreate(domain
, &so
, type
, proto
);
123 so
->so_connection
= context
;
124 so
->so_state
= SS_NBIO
;
126 so
->so_q
= so
->so_q0
= NULL
;
134 int OskitTCPRecv( void *connection
,
139 struct uio uio
= { 0 };
140 struct iovec iov
= { 0 };
146 OS_DbgPrint(OSK_MID_TRACE
,
147 ("so->so_state %x\n", ((struct socket
*)connection
)->so_state
));
149 if( Flags
& OSK_MSG_OOB
) tcp_flags
|= MSG_OOB
;
150 if( Flags
& OSK_MSG_DONTWAIT
) tcp_flags
|= MSG_DONTWAIT
;
151 if( Flags
& OSK_MSG_PEEK
) tcp_flags
|= MSG_PEEK
;
155 uio
.uio_rw
= UIO_READ
;
158 iov
.iov_base
= (char *)Data
;
160 OS_DbgPrint(OSK_MID_TRACE
,("Reading %d bytes from TCP:\n", Len
));
162 error
= soreceive( connection
, NULL
, &uio
, NULL
, NULL
/* SCM_RIGHTS */,
166 *OutLen
= Len
- uio
.uio_resid
;
172 int OskitTCPBind( void *socket
, void *connection
,
173 void *nam
, OSK_UINT namelen
) {
175 struct socket
*so
= socket
;
177 struct sockaddr addr
;
179 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
182 addr
= *((struct sockaddr
*)nam
);
184 RtlZeroMemory(&sabuf
, sizeof(sabuf
));
185 sabuf
.m_data
= (void *)&addr
;
186 sabuf
.m_len
= sizeof(addr
);
188 addr
.sa_family
= addr
.sa_len
;
189 addr
.sa_len
= sizeof(struct sockaddr
);
191 error
= sobind(so
, &sabuf
);
193 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
197 int OskitTCPConnect( void *socket
, void *connection
,
198 void *nam
, OSK_UINT namelen
) {
199 struct socket
*so
= socket
;
202 struct sockaddr addr
;
204 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
206 so
->so_connection
= connection
;
208 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
213 OS_DbgPrint(OSK_MIN_TRACE
,("Nam: %x\n", nam
));
215 addr
= *((struct sockaddr
*)nam
);
217 RtlZeroMemory(&sabuf
, sizeof(sabuf
));
218 sabuf
.m_data
= (void *)&addr
;
219 sabuf
.m_len
= sizeof(addr
);
221 addr
.sa_family
= addr
.sa_len
;
222 addr
.sa_len
= sizeof(struct sockaddr
);
224 error
= soconnect(so
, &sabuf
);
229 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
235 so
->so_state
&= ~SS_ISCONNECTING
;
237 if (error
== ERESTART
)
241 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
245 int OskitTCPShutdown( void *socket
, int disconn_type
) {
246 return soshutdown( socket
, disconn_type
);
249 int OskitTCPClose( void *socket
) {
250 struct socket
*so
= socket
;
251 so
->so_connection
= 0;
256 int OskitTCPSend( void *socket
, OSK_PCHAR Data
, OSK_UINT Len
,
257 OSK_UINT
*OutLen
, OSK_UINT flags
) {
263 iov
.iov_base
= (char *)Data
;
268 uio
.uio_segflg
= UIO_SYSSPACE
;
269 uio
.uio_rw
= UIO_WRITE
;
270 uio
.uio_procp
= NULL
;
272 error
= sosend( socket
, NULL
, &uio
, NULL
, NULL
, 0 );
273 if (OSK_EWOULDBLOCK
== error
) {
274 ((struct socket
*) socket
)->so_snd
.sb_flags
|= SB_WAIT
;
276 *OutLen
= uio
.uio_offset
;
281 int OskitTCPAccept( void *socket
,
285 OSK_UINT
*OutAddrLen
,
286 OSK_UINT FinishAccepting
) {
287 struct socket
*head
= (void *)socket
;
288 struct sockaddr
*name
= (struct sockaddr
*)AddrOut
;
289 struct socket
**newso
= (struct socket
**)new_socket
;
290 struct socket
*so
= socket
;
291 struct sockaddr_in sa
;
294 int namelen
= 0, error
= 0, s
;
296 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Doing accept (Finish %d)\n",
299 *OutAddrLen
= AddrLen
;
302 /* that's a copyin actually */
303 namelen
= *OutAddrLen
;
308 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
310 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: head->so_options = %x, wanted bit %x\n",
311 head
->so_options
, SO_ACCEPTCONN
));
317 OS_DbgPrint(OSK_MID_TRACE
,("head->so_q = %x, head->so_state = %x\n",
318 head
->so_q
, head
->so_state
));
320 if ((head
->so_state
& SS_NBIO
) && head
->so_q
== NULL
) {
326 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
327 while (head
->so_q
== NULL
&& head
->so_error
== 0) {
328 if (head
->so_state
& SS_CANTRCVMORE
) {
329 head
->so_error
= ECONNABORTED
;
332 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
333 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
339 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
341 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
344 if (head
->so_error
) {
345 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
346 error
= head
->so_error
;
351 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
355 * At this point we know that there is at least one connection
356 * ready to be accepted. Remove it from the queue.
360 inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
362 ((struct sockaddr_in
*)AddrOut
)->sin_addr
.s_addr
=
363 inp
->inp_faddr
.s_addr
;
364 ((struct sockaddr_in
*)AddrOut
)->sin_port
= inp
->inp_fport
;
367 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
368 if( FinishAccepting
&& so
) {
369 head
->so_q
= so
->so_q
;
374 /*so->so_state &= ~SS_COMP;*/
376 mnam
.m_data
= (char *)&sa
;
377 mnam
.m_len
= sizeof(sa
);
379 (void) soaccept(so
, &mnam
);
381 so
->so_state
= SS_NBIO
| SS_ISCONNECTED
;
382 so
->so_q
= so
->so_q0
= NULL
;
386 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
388 /* check sa_len before it is destroyed */
389 memcpy( AddrOut
, &sa
, AddrLen
< sizeof(sa
) ? AddrLen
: sizeof(sa
) );
390 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
391 *OutAddrLen
= namelen
; /* copyout actually */
393 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
397 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Returning %d\n", error
));
403 * We have a packet. While we store the fields we want in host byte order
404 * outside the original packet, the bsd stack modifies them in place.
407 void OskitTCPReceiveDatagram( OSK_PCHAR Data
, OSK_UINT Len
,
408 OSK_UINT IpHeaderLen
) {
409 struct mbuf
*Ip
= m_devget( (char *)Data
, Len
, 0, NULL
, NULL
);
412 if( !Ip
) return; /* drop the segment */
414 //memcpy( Ip->m_data, Data, Len );
415 Ip
->m_pkthdr
.len
= IpHeaderLen
;
417 /* Do the transformations on the header that tcp_input expects */
418 iph
= mtod(Ip
, struct ip
*);
420 iph
->ip_len
-= sizeof(struct ip
);
422 OS_DbgPrint(OSK_MAX_TRACE
,
423 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len
,
426 tcp_input(Ip
, IpHeaderLen
);
428 /* The buffer Ip is freed by tcp_input */
431 int OskitTCPListen( void *socket
, int backlog
) {
434 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
435 error
= solisten( socket
, backlog
);
436 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
441 void OskitTCPSetAddress( void *socket
,
442 OSK_UINT LocalAddress
,
444 OSK_UINT RemoteAddress
,
445 OSK_UI16 RemotePort
) {
446 struct socket
*so
= socket
;
447 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
448 inp
->inp_laddr
.s_addr
= LocalAddress
;
449 inp
->inp_lport
= LocalPort
;
450 inp
->inp_faddr
.s_addr
= RemoteAddress
;
451 inp
->inp_fport
= RemotePort
;
454 void OskitTCPGetAddress( void *socket
,
455 OSK_UINT
*LocalAddress
,
457 OSK_UINT
*RemoteAddress
,
458 OSK_UI16
*RemotePort
) {
459 struct socket
*so
= socket
;
460 struct inpcb
*inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
462 *LocalAddress
= inp
->inp_laddr
.s_addr
;
463 *LocalPort
= inp
->inp_lport
;
464 *RemoteAddress
= inp
->inp_faddr
.s_addr
;
465 *RemotePort
= inp
->inp_fport
;
469 struct ifaddr
*ifa_iffind(struct sockaddr
*addr
, int type
)
471 if( OtcpEvent
.FindInterface
)
472 return OtcpEvent
.FindInterface( OtcpEvent
.ClientData
,
480 void oskittcp_die( const char *file
, int line
) {
481 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file
, line
);
485 /* Stuff supporting the BSD network-interface interface */
486 struct ifaddr
**ifnet_addrs
;
499 panic("if_attach\n");
509 * Handle interface watchdog timer routines. Called
510 * from softclock, we decrement timers (if set) and
511 * call the appropriate interface routine on expiration.
518 register struct ifnet
*ifp
;
521 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
) {
522 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
524 if (ifp
->if_watchdog
)
525 (*ifp
->if_watchdog
)(ifp
->if_unit
);
528 timeout(if_slowtimo
, (void *)0, hz
/ IFNET_SLOWHZ
);
533 * Locate an interface based on a complete address.
537 struct ifaddr
*ifa_ifwithaddr(addr
)
538 struct sockaddr
*addr
;
540 struct ifaddr
*ifaddr
= ifa_ifwithnet( addr
);
541 struct sockaddr_in
*addr_in
;
542 struct sockaddr_in
*faddr_in
;
545 OS_DbgPrint(OSK_MID_TRACE
,("No ifaddr\n"));
548 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr @ %x\n", ifaddr
));
551 addr_in
= (struct sockaddr_in
*)addr
;
552 faddr_in
= (struct sockaddr_in
*)ifaddr
->ifa_addr
;
554 if( faddr_in
->sin_addr
.s_addr
== addr_in
->sin_addr
.s_addr
)
561 * Locate the point to point interface with a given destination address.
565 ifa_ifwithdstaddr(addr
)
566 register struct sockaddr
*addr
;
568 OS_DbgPrint(OSK_MID_TRACE
,("Called\n"));
569 return ifa_iffind(addr
, IFF_POINTOPOINT
);
573 * Find an interface on a specific network. If many, choice
574 * is most specific found.
576 struct ifaddr
*ifa_ifwithnet(addr
)
577 struct sockaddr
*addr
;
579 struct sockaddr_in
*sin
;
580 struct ifaddr
*ifaddr
= ifa_iffind(addr
, IFF_UNICAST
);
584 sin
= (struct sockaddr_in
*)&ifaddr
->ifa_addr
;
586 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr->addr = %x\n",
587 sin
->sin_addr
.s_addr
));