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
) {
259 struct mbuf
* m
= m_devget( Data
, Len
, 0, NULL
, NULL
);
263 error
= sosend( socket
, NULL
, NULL
, m
, NULL
, 0 );
268 int OskitTCPAccept( void *socket
,
272 OSK_UINT
*OutAddrLen
,
273 OSK_UINT FinishAccepting
) {
274 struct socket
*head
= (void *)socket
;
275 struct sockaddr
*name
= (struct sockaddr
*)AddrOut
;
276 struct socket
**newso
= (struct socket
**)new_socket
;
277 struct socket
*so
= socket
;
278 struct sockaddr_in sa
;
281 int namelen
= 0, error
= 0, s
;
283 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Doing accept (Finish %d)\n",
286 *OutAddrLen
= AddrLen
;
289 /* that's a copyin actually */
290 namelen
= *OutAddrLen
;
295 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
297 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: head->so_options = %x, wanted bit %x\n",
298 head
->so_options
, SO_ACCEPTCONN
));
304 OS_DbgPrint(OSK_MID_TRACE
,("head->so_q = %x, head->so_state = %x\n",
305 head
->so_q
, head
->so_state
));
307 if ((head
->so_state
& SS_NBIO
) && head
->so_q
== NULL
) {
313 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
314 while (head
->so_q
== NULL
&& head
->so_error
== 0) {
315 if (head
->so_state
& SS_CANTRCVMORE
) {
316 head
->so_error
= ECONNABORTED
;
319 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
320 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
326 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
328 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
331 if (head
->so_error
) {
332 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
333 error
= head
->so_error
;
338 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
342 * At this point we know that there is at least one connection
343 * ready to be accepted. Remove it from the queue.
347 inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
349 ((struct sockaddr_in
*)AddrOut
)->sin_addr
.s_addr
=
350 inp
->inp_faddr
.s_addr
;
351 ((struct sockaddr_in
*)AddrOut
)->sin_port
= inp
->inp_fport
;
354 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
355 if( FinishAccepting
) {
356 head
->so_q
= so
->so_q
;
361 /*so->so_state &= ~SS_COMP;*/
363 mnam
.m_data
= (char *)&sa
;
364 mnam
.m_len
= sizeof(sa
);
366 (void) soaccept(so
, &mnam
);
368 so
->so_state
= SS_NBIO
| SS_ISCONNECTED
;
369 so
->so_q
= so
->so_q0
= NULL
;
373 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
375 /* check sa_len before it is destroyed */
376 memcpy( AddrOut
, &sa
, AddrLen
< sizeof(sa
) ? AddrLen
: sizeof(sa
) );
377 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
378 *OutAddrLen
= namelen
; /* copyout actually */
380 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
384 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Returning %d\n", error
));
390 * We have a packet. While we store the fields we want in host byte order
391 * outside the original packet, the bsd stack modifies them in place.
394 void OskitTCPReceiveDatagram( OSK_PCHAR Data
, OSK_UINT Len
,
395 OSK_UINT IpHeaderLen
) {
396 struct mbuf
*Ip
= m_devget( Data
, Len
, 0, NULL
, NULL
);
399 if( !Ip
) return; /* drop the segment */
401 //memcpy( Ip->m_data, Data, Len );
402 Ip
->m_pkthdr
.len
= IpHeaderLen
;
404 /* Do the transformations on the header that tcp_input expects */
405 iph
= mtod(Ip
, struct ip
*);
407 iph
->ip_len
-= sizeof(struct ip
);
409 OS_DbgPrint(OSK_MAX_TRACE
,
410 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len
,
413 tcp_input(Ip
, IpHeaderLen
);
415 /* The buffer Ip is freed by tcp_input */
418 int OskitTCPListen( void *socket
, int backlog
) {
421 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
422 error
= solisten( socket
, backlog
);
423 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
428 void OskitTCPSetAddress( void *socket
,
429 OSK_UINT LocalAddress
,
431 OSK_UINT RemoteAddress
,
432 OSK_UI16 RemotePort
) {
433 struct socket
*so
= socket
;
434 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
435 inp
->inp_laddr
.s_addr
= LocalAddress
;
436 inp
->inp_lport
= LocalPort
;
437 inp
->inp_faddr
.s_addr
= RemoteAddress
;
438 inp
->inp_fport
= RemotePort
;
441 void OskitTCPGetAddress( void *socket
,
442 OSK_UINT
*LocalAddress
,
444 OSK_UINT
*RemoteAddress
,
445 OSK_UI16
*RemotePort
) {
446 struct socket
*so
= socket
;
447 struct inpcb
*inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
449 *LocalAddress
= inp
->inp_laddr
.s_addr
;
450 *LocalPort
= inp
->inp_lport
;
451 *RemoteAddress
= inp
->inp_faddr
.s_addr
;
452 *RemotePort
= inp
->inp_fport
;
456 struct ifaddr
*ifa_iffind(struct sockaddr
*addr
, int type
)
458 if( OtcpEvent
.FindInterface
)
459 return OtcpEvent
.FindInterface( OtcpEvent
.ClientData
,
467 void oskittcp_die( const char *file
, int line
) {
468 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file
, line
);
472 /* Stuff supporting the BSD network-interface interface */
473 struct ifaddr
**ifnet_addrs
;
486 panic("if_attach\n");
496 * Handle interface watchdog timer routines. Called
497 * from softclock, we decrement timers (if set) and
498 * call the appropriate interface routine on expiration.
505 register struct ifnet
*ifp
;
508 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
) {
509 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
511 if (ifp
->if_watchdog
)
512 (*ifp
->if_watchdog
)(ifp
->if_unit
);
515 timeout(if_slowtimo
, (void *)0, hz
/ IFNET_SLOWHZ
);
520 * Locate an interface based on a complete address.
524 struct ifaddr
*ifa_ifwithaddr(addr
)
525 struct sockaddr
*addr
;
527 struct ifaddr
*ifaddr
= ifa_ifwithnet( addr
);
528 struct sockaddr_in
*addr_in
;
529 struct sockaddr_in
*faddr_in
;
532 OS_DbgPrint(OSK_MID_TRACE
,("No ifaddr\n"));
535 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr @ %x\n", ifaddr
));
538 addr_in
= (struct sockaddr_in
*)addr
;
539 faddr_in
= (struct sockaddr_in
*)ifaddr
->ifa_addr
;
541 if( faddr_in
->sin_addr
.s_addr
== addr_in
->sin_addr
.s_addr
)
548 * Locate the point to point interface with a given destination address.
552 ifa_ifwithdstaddr(addr
)
553 register struct sockaddr
*addr
;
555 OS_DbgPrint(OSK_MID_TRACE
,("Called\n"));
556 return ifa_iffind(addr
, IFF_POINTOPOINT
);
560 * Find an interface on a specific network. If many, choice
561 * is most specific found.
563 struct ifaddr
*ifa_ifwithnet(addr
)
564 struct sockaddr
*addr
;
566 struct sockaddr_in
*sin
;
567 struct ifaddr
*ifaddr
= ifa_iffind(addr
, IFF_UNICAST
);
571 sin
= (struct sockaddr_in
*)&ifaddr
->ifa_addr
;
573 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr->addr = %x\n",
574 sin
->sin_addr
.s_addr
));