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
;
34 int _snprintf(char * buf
, size_t cnt
, const char *fmt
, ...);
36 void *fbsd_malloc( unsigned int bytes
, char *file
, unsigned line
, ... ) {
37 if( !OtcpEvent
.TCPMalloc
) panic("no malloc");
38 return OtcpEvent
.TCPMalloc
39 ( OtcpEvent
.ClientData
, (OSK_UINT
)bytes
, file
, line
);
42 void fbsd_free( void *data
, char *file
, unsigned line
, ... ) {
43 if( !OtcpEvent
.TCPFree
) panic("no free");
44 OtcpEvent
.TCPFree( OtcpEvent
.ClientData
, data
, file
, line
);
48 OS_DbgPrint(OSK_MID_TRACE
,("Init Called\n"));
49 OS_DbgPrint(OSK_MID_TRACE
,("MB Init\n"));
51 OS_DbgPrint(OSK_MID_TRACE
,("Rawip Init\n"));
54 OS_DbgPrint(OSK_MID_TRACE
,("Route Init\n"));
56 OS_DbgPrint(OSK_MID_TRACE
,("Init clock\n"));
58 OS_DbgPrint(OSK_MID_TRACE
,("Init TCP\n"));
60 OS_DbgPrint(OSK_MID_TRACE
,("Init routing\n"));
62 OS_DbgPrint(OSK_MID_TRACE
,("Init Finished\n"));
66 void DeinitOskitTCP() {
69 void TimerOskitTCP( int FastTimer
, int SlowTimer
) {
78 void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers
) {
79 memcpy( &OtcpEvent
, EventHandlers
, sizeof(OtcpEvent
) );
80 if( OtcpEvent
.PacketSend
)
81 OS_DbgPrint(OSK_MID_TRACE
,("SendPacket handler registered: %x\n",
82 OtcpEvent
.PacketSend
));
85 void OskitDumpBuffer( OSK_PCHAR Data
, OSK_UINT Len
)
89 static const char* hex
= "0123456789abcdef";
91 for ( i
= 0; i
< Len
; i
++ )
94 int align3
= (align
<<1) + align
;
95 unsigned char c
= Data
[i
];
98 if ( i
) DbgPrint( line
);
99 _snprintf ( line
, sizeof(line
)-1, "%08x: \n", &Data
[i
] );
100 line
[sizeof(line
)-1] = '\0';
103 line
[10+align3
] = hex
[(c
>>4)&0xf];
104 line
[11+align3
] = hex
[c
&0xf];
113 /* From uipc_syscalls.c */
115 int OskitTCPSocket( void *context
,
122 int error
= socreate(domain
, &so
, type
, proto
);
124 so
->so_connection
= context
;
125 so
->so_state
= SS_NBIO
;
127 so
->so_q
= so
->so_q0
= NULL
;
135 int OskitTCPRecv( void *connection
,
140 struct uio uio
= { 0 };
141 struct iovec iov
= { 0 };
147 OS_DbgPrint(OSK_MID_TRACE
,
148 ("so->so_state %x\n", ((struct socket
*)connection
)->so_state
));
150 if( Flags
& OSK_MSG_OOB
) tcp_flags
|= MSG_OOB
;
151 if( Flags
& OSK_MSG_DONTWAIT
) tcp_flags
|= MSG_DONTWAIT
;
152 if( Flags
& OSK_MSG_PEEK
) tcp_flags
|= MSG_PEEK
;
156 uio
.uio_rw
= UIO_READ
;
161 OS_DbgPrint(OSK_MID_TRACE
,("Reading %d bytes from TCP:\n", Len
));
163 error
= soreceive( connection
, NULL
, &uio
, NULL
, NULL
/* SCM_RIGHTS */,
167 *OutLen
= Len
- uio
.uio_resid
;
173 int OskitTCPBind( void *socket
, void *connection
,
174 void *nam
, OSK_UINT namelen
) {
176 struct socket
*so
= socket
;
178 struct sockaddr addr
;
180 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
183 addr
= *((struct sockaddr
*)nam
);
185 RtlZeroMemory(&sabuf
, sizeof(sabuf
));
186 sabuf
.m_data
= (void *)&addr
;
187 sabuf
.m_len
= sizeof(addr
);
189 addr
.sa_family
= addr
.sa_len
;
190 addr
.sa_len
= sizeof(struct sockaddr
);
192 error
= sobind(so
, &sabuf
);
194 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
198 int OskitTCPConnect( void *socket
, void *connection
,
199 void *nam
, OSK_UINT namelen
) {
200 struct socket
*so
= socket
;
203 struct sockaddr addr
;
205 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
207 so
->so_connection
= connection
;
209 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
214 OS_DbgPrint(OSK_MIN_TRACE
,("Nam: %x\n", nam
));
216 addr
= *((struct sockaddr
*)nam
);
218 RtlZeroMemory(&sabuf
, sizeof(sabuf
));
219 sabuf
.m_data
= (void *)&addr
;
220 sabuf
.m_len
= sizeof(addr
);
222 addr
.sa_family
= addr
.sa_len
;
223 addr
.sa_len
= sizeof(struct sockaddr
);
225 error
= soconnect(so
, &sabuf
);
230 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
236 so
->so_state
&= ~SS_ISCONNECTING
;
238 if (error
== ERESTART
)
242 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
246 int OskitTCPShutdown( void *socket
, int disconn_type
) {
247 return soshutdown( socket
, disconn_type
);
250 int OskitTCPClose( void *socket
) {
251 struct socket
*so
= socket
;
252 so
->so_connection
= 0;
257 int OskitTCPSend( void *socket
, OSK_PCHAR Data
, OSK_UINT Len
,
258 OSK_UINT
*OutLen
, OSK_UINT flags
) {
269 uio
.uio_segflg
= UIO_SYSSPACE
;
270 uio
.uio_rw
= UIO_WRITE
;
271 uio
.uio_procp
= NULL
;
273 error
= sosend( socket
, NULL
, &uio
, NULL
, NULL
, 0 );
274 if (OSK_EWOULDBLOCK
== error
) {
275 ((struct socket
*) socket
)->so_snd
.sb_flags
|= SB_WAIT
;
277 *OutLen
= uio
.uio_offset
;
282 int OskitTCPAccept( void *socket
,
286 OSK_UINT
*OutAddrLen
,
287 OSK_UINT FinishAccepting
) {
288 struct socket
*head
= (void *)socket
;
289 struct sockaddr
*name
= (struct sockaddr
*)AddrOut
;
290 struct socket
**newso
= (struct socket
**)new_socket
;
291 struct socket
*so
= socket
;
292 struct sockaddr_in sa
;
295 int namelen
= 0, error
= 0, s
;
297 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Doing accept (Finish %d)\n",
300 *OutAddrLen
= AddrLen
;
303 /* that's a copyin actually */
304 namelen
= *OutAddrLen
;
309 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
311 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: head->so_options = %x, wanted bit %x\n",
312 head
->so_options
, SO_ACCEPTCONN
));
318 OS_DbgPrint(OSK_MID_TRACE
,("head->so_q = %x, head->so_state = %x\n",
319 head
->so_q
, head
->so_state
));
321 if ((head
->so_state
& SS_NBIO
) && head
->so_q
== NULL
) {
327 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
328 while (head
->so_q
== NULL
&& head
->so_error
== 0) {
329 if (head
->so_state
& SS_CANTRCVMORE
) {
330 head
->so_error
= ECONNABORTED
;
333 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
334 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
340 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
342 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
345 if (head
->so_error
) {
346 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
347 error
= head
->so_error
;
352 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
356 * At this point we know that there is at least one connection
357 * ready to be accepted. Remove it from the queue.
361 inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
363 ((struct sockaddr_in
*)AddrOut
)->sin_addr
.s_addr
=
364 inp
->inp_faddr
.s_addr
;
365 ((struct sockaddr_in
*)AddrOut
)->sin_port
= inp
->inp_fport
;
368 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
369 if( FinishAccepting
) {
370 head
->so_q
= so
->so_q
;
375 /*so->so_state &= ~SS_COMP;*/
377 mnam
.m_data
= (char *)&sa
;
378 mnam
.m_len
= sizeof(sa
);
380 (void) soaccept(so
, &mnam
);
382 so
->so_state
= SS_NBIO
| SS_ISCONNECTED
;
383 so
->so_q
= so
->so_q0
= NULL
;
387 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
389 /* check sa_len before it is destroyed */
390 memcpy( AddrOut
, &sa
, AddrLen
< sizeof(sa
) ? AddrLen
: sizeof(sa
) );
391 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
392 *OutAddrLen
= namelen
; /* copyout actually */
394 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
398 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Returning %d\n", error
));
404 * We have a packet. While we store the fields we want in host byte order
405 * outside the original packet, the bsd stack modifies them in place.
408 void OskitTCPReceiveDatagram( OSK_PCHAR Data
, OSK_UINT Len
,
409 OSK_UINT IpHeaderLen
) {
410 struct mbuf
*Ip
= m_devget( Data
, Len
, 0, NULL
, NULL
);
413 if( !Ip
) return; /* drop the segment */
415 //memcpy( Ip->m_data, Data, Len );
416 Ip
->m_pkthdr
.len
= IpHeaderLen
;
418 /* Do the transformations on the header that tcp_input expects */
419 iph
= mtod(Ip
, struct ip
*);
421 iph
->ip_len
-= sizeof(struct ip
);
423 OS_DbgPrint(OSK_MAX_TRACE
,
424 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len
,
427 tcp_input(Ip
, IpHeaderLen
);
429 /* The buffer Ip is freed by tcp_input */
432 int OskitTCPListen( void *socket
, int backlog
) {
435 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
436 error
= solisten( socket
, backlog
);
437 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
442 void OskitTCPSetAddress( void *socket
,
443 OSK_UINT LocalAddress
,
445 OSK_UINT RemoteAddress
,
446 OSK_UI16 RemotePort
) {
447 struct socket
*so
= socket
;
448 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
449 inp
->inp_laddr
.s_addr
= LocalAddress
;
450 inp
->inp_lport
= LocalPort
;
451 inp
->inp_faddr
.s_addr
= RemoteAddress
;
452 inp
->inp_fport
= RemotePort
;
455 void OskitTCPGetAddress( void *socket
,
456 OSK_UINT
*LocalAddress
,
458 OSK_UINT
*RemoteAddress
,
459 OSK_UI16
*RemotePort
) {
460 struct socket
*so
= socket
;
461 struct inpcb
*inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
463 *LocalAddress
= inp
->inp_laddr
.s_addr
;
464 *LocalPort
= inp
->inp_lport
;
465 *RemoteAddress
= inp
->inp_faddr
.s_addr
;
466 *RemotePort
= inp
->inp_fport
;
470 struct ifaddr
*ifa_iffind(struct sockaddr
*addr
, int type
)
472 if( OtcpEvent
.FindInterface
)
473 return OtcpEvent
.FindInterface( OtcpEvent
.ClientData
,
481 void oskittcp_die( const char *file
, int line
) {
482 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file
, line
);
486 /* Stuff supporting the BSD network-interface interface */
487 struct ifaddr
**ifnet_addrs
;
500 panic("if_attach\n");
510 * Handle interface watchdog timer routines. Called
511 * from softclock, we decrement timers (if set) and
512 * call the appropriate interface routine on expiration.
519 register struct ifnet
*ifp
;
522 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
) {
523 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
525 if (ifp
->if_watchdog
)
526 (*ifp
->if_watchdog
)(ifp
->if_unit
);
529 timeout(if_slowtimo
, (void *)0, hz
/ IFNET_SLOWHZ
);
534 * Locate an interface based on a complete address.
538 struct ifaddr
*ifa_ifwithaddr(addr
)
539 struct sockaddr
*addr
;
541 struct ifaddr
*ifaddr
= ifa_ifwithnet( addr
);
542 struct sockaddr_in
*addr_in
;
543 struct sockaddr_in
*faddr_in
;
546 OS_DbgPrint(OSK_MID_TRACE
,("No ifaddr\n"));
549 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr @ %x\n", ifaddr
));
552 addr_in
= (struct sockaddr_in
*)addr
;
553 faddr_in
= (struct sockaddr_in
*)ifaddr
->ifa_addr
;
555 if( faddr_in
->sin_addr
.s_addr
== addr_in
->sin_addr
.s_addr
)
562 * Locate the point to point interface with a given destination address.
566 ifa_ifwithdstaddr(addr
)
567 register struct sockaddr
*addr
;
569 OS_DbgPrint(OSK_MID_TRACE
,("Called\n"));
570 return ifa_iffind(addr
, IFF_POINTOPOINT
);
574 * Find an interface on a specific network. If many, choice
575 * is most specific found.
577 struct ifaddr
*ifa_ifwithnet(addr
)
578 struct sockaddr
*addr
;
580 struct sockaddr_in
*sin
;
581 struct ifaddr
*ifaddr
= ifa_iffind(addr
, IFF_UNICAST
);
585 sin
= (struct sockaddr_in
*)&ifaddr
->ifa_addr
;
587 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr->addr = %x\n",
588 sin
->sin_addr
.s_addr
));