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
, ... ) {
37 if( !OtcpEvent
.TCPMalloc
) panic("no malloc");
38 return OtcpEvent
.TCPMalloc
39 ( OtcpEvent
.ClientData
, (OSK_UINT
)bytes
, "*", 0 );
42 void fbsd_free( void *data
, ... ) {
43 if( !OtcpEvent
.TCPFree
) panic("no free");
44 OtcpEvent
.TCPFree( OtcpEvent
.ClientData
, data
, "*", 0 );
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() {
74 void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers
) {
75 memcpy( &OtcpEvent
, EventHandlers
, sizeof(OtcpEvent
) );
76 if( OtcpEvent
.PacketSend
)
77 OS_DbgPrint(OSK_MID_TRACE
,("SendPacket handler registered: %x\n",
78 OtcpEvent
.PacketSend
));
81 void OskitDumpBuffer( OSK_PCHAR Data
, OSK_UINT Len
)
85 static const char* hex
= "0123456789abcdef";
87 for ( i
= 0; i
< Len
; i
++ )
90 int align3
= (align
<<1) + align
;
91 unsigned char c
= Data
[i
];
94 if ( i
) DbgPrint( line
);
95 _snprintf ( line
, sizeof(line
)-1, "%08x: \n", &Data
[i
] );
96 line
[sizeof(line
)-1] = '\0';
99 line
[10+align3
] = hex
[(c
>>4)&0xf];
100 line
[11+align3
] = hex
[c
&0xf];
109 /* From uipc_syscalls.c */
111 int OskitTCPSocket( void *context
,
118 int error
= socreate(domain
, &so
, type
, proto
);
120 so
->so_connection
= context
;
121 so
->so_state
= SS_NBIO
;
123 so
->so_q
= so
->so_q0
= NULL
;
131 int OskitTCPRecv( void *connection
,
136 struct uio uio
= { 0 };
137 struct iovec iov
= { 0 };
143 printf("so->so_state %x\n", ((struct socket
*)connection
)->so_state
);
145 if( Flags
& OSK_MSG_OOB
) tcp_flags
|= MSG_OOB
;
146 if( Flags
& OSK_MSG_DONTWAIT
) tcp_flags
|= MSG_DONTWAIT
;
147 if( Flags
& OSK_MSG_PEEK
) tcp_flags
|= MSG_PEEK
;
151 uio
.uio_rw
= UIO_READ
;
156 OS_DbgPrint(OSK_MID_TRACE
,("Reading %d bytes from TCP:\n", Len
));
158 error
= soreceive( connection
, NULL
, &uio
, NULL
, NULL
/* SCM_RIGHTS */,
162 *OutLen
= Len
- uio
.uio_resid
;
168 int OskitTCPBind( void *socket
, void *connection
,
169 void *nam
, OSK_UINT namelen
) {
171 struct socket
*so
= socket
;
173 struct sockaddr addr
;
175 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
178 addr
= *((struct sockaddr
*)nam
);
180 RtlZeroMemory(&sabuf
, sizeof(sabuf
));
181 sabuf
.m_data
= (void *)&addr
;
182 sabuf
.m_len
= sizeof(addr
);
184 addr
.sa_family
= addr
.sa_len
;
185 addr
.sa_len
= sizeof(struct sockaddr
);
187 error
= sobind(so
, &sabuf
);
189 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
193 int OskitTCPConnect( void *socket
, void *connection
,
194 void *nam
, OSK_UINT namelen
) {
195 struct socket
*so
= socket
;
198 struct sockaddr addr
;
200 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
202 so
->so_connection
= connection
;
204 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
209 OS_DbgPrint(OSK_MIN_TRACE
,("Nam: %x\n", nam
));
211 addr
= *((struct sockaddr
*)nam
);
213 RtlZeroMemory(&sabuf
, sizeof(sabuf
));
214 sabuf
.m_data
= (void *)&addr
;
215 sabuf
.m_len
= sizeof(addr
);
217 addr
.sa_family
= addr
.sa_len
;
218 addr
.sa_len
= sizeof(struct sockaddr
);
220 error
= soconnect(so
, &sabuf
);
225 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
231 so
->so_state
&= ~SS_ISCONNECTING
;
233 if (error
== ERESTART
)
237 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
241 int OskitTCPShutdown( void *socket
, int disconn_type
) {
242 return soshutdown( socket
, disconn_type
);
245 int OskitTCPClose( void *socket
) {
246 struct socket
*so
= socket
;
247 so
->so_connection
= 0;
252 int OskitTCPSend( void *socket
, OSK_PCHAR Data
, OSK_UINT Len
,
253 OSK_UINT
*OutLen
, OSK_UINT flags
) {
254 struct mbuf
* m
= m_devget( Data
, Len
, 0, NULL
, NULL
);
258 error
= sosend( socket
, NULL
, NULL
, m
, NULL
, 0 );
263 int OskitTCPAccept( void *socket
,
267 OSK_UINT
*OutAddrLen
,
268 OSK_UINT FinishAccepting
) {
269 struct socket
*head
= (void *)socket
;
270 struct sockaddr
*name
= (struct sockaddr
*)AddrOut
;
271 struct socket
**newso
= (struct socket
**)new_socket
;
272 struct socket
*so
= socket
;
273 struct sockaddr_in sa
;
276 int namelen
= 0, error
= 0, s
;
278 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Doing accept (Finish %d)\n",
281 *OutAddrLen
= AddrLen
;
284 /* that's a copyin actually */
285 namelen
= *OutAddrLen
;
290 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
292 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: head->so_options = %x, wanted bit %x\n",
293 head
->so_options
, SO_ACCEPTCONN
));
299 OS_DbgPrint(OSK_MID_TRACE
,("head->so_q = %x, head->so_state = %x\n",
300 head
->so_q
, head
->so_state
));
302 if ((head
->so_state
& SS_NBIO
) && head
->so_q
== NULL
) {
308 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
309 while (head
->so_q
== NULL
&& head
->so_error
== 0) {
310 if (head
->so_state
& SS_CANTRCVMORE
) {
311 head
->so_error
= ECONNABORTED
;
314 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
315 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
321 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
323 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
326 if (head
->so_error
) {
327 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
328 error
= head
->so_error
;
333 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
337 * At this point we know that there is at least one connection
338 * ready to be accepted. Remove it from the queue.
342 inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
344 ((struct sockaddr_in
*)AddrOut
)->sin_addr
.s_addr
=
345 inp
->inp_faddr
.s_addr
;
346 ((struct sockaddr_in
*)AddrOut
)->sin_port
= inp
->inp_fport
;
349 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
350 if( FinishAccepting
) {
351 head
->so_q
= so
->so_q
;
356 /*so->so_state &= ~SS_COMP;*/
358 mnam
.m_data
= (char *)&sa
;
359 mnam
.m_len
= sizeof(sa
);
361 (void) soaccept(so
, &mnam
);
363 so
->so_state
= SS_NBIO
| SS_ISCONNECTED
;
364 so
->so_q
= so
->so_q0
= NULL
;
368 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
370 /* check sa_len before it is destroyed */
371 memcpy( AddrOut
, &sa
, AddrLen
< sizeof(sa
) ? AddrLen
: sizeof(sa
) );
372 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
373 *OutAddrLen
= namelen
; /* copyout actually */
375 OS_DbgPrint(OSK_MID_TRACE
,("error = %d\n", error
));
379 OS_DbgPrint(OSK_MID_TRACE
,("OSKITTCP: Returning %d\n", error
));
385 * We have a packet. While we store the fields we want in host byte order
386 * outside the original packet, the bsd stack modifies them in place.
389 void OskitTCPReceiveDatagram( OSK_PCHAR Data
, OSK_UINT Len
,
390 OSK_UINT IpHeaderLen
) {
391 struct mbuf
*Ip
= m_devget( Data
, Len
, 0, NULL
, NULL
);
394 if( !Ip
) return; /* drop the segment */
396 //memcpy( Ip->m_data, Data, Len );
397 Ip
->m_pkthdr
.len
= IpHeaderLen
;
399 /* Do the transformations on the header that tcp_input expects */
400 iph
= mtod(Ip
, struct ip
*);
402 iph
->ip_len
-= sizeof(struct ip
);
404 OS_DbgPrint(OSK_MAX_TRACE
,
405 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len
,
408 tcp_input(Ip
, IpHeaderLen
);
410 /* The buffer Ip is freed by tcp_input */
413 int OskitTCPListen( void *socket
, int backlog
) {
416 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
417 error
= solisten( socket
, backlog
);
418 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
423 void OskitTCPSetAddress( void *socket
,
424 OSK_UINT LocalAddress
,
426 OSK_UINT RemoteAddress
,
427 OSK_UI16 RemotePort
) {
428 struct socket
*so
= socket
;
429 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
430 inp
->inp_laddr
.s_addr
= LocalAddress
;
431 inp
->inp_lport
= LocalPort
;
432 inp
->inp_faddr
.s_addr
= RemoteAddress
;
433 inp
->inp_fport
= RemotePort
;
436 void OskitTCPGetAddress( void *socket
,
437 OSK_UINT
*LocalAddress
,
439 OSK_UINT
*RemoteAddress
,
440 OSK_UI16
*RemotePort
) {
441 struct socket
*so
= socket
;
442 struct inpcb
*inp
= so
? (struct inpcb
*)so
->so_pcb
: NULL
;
444 *LocalAddress
= inp
->inp_laddr
.s_addr
;
445 *LocalPort
= inp
->inp_lport
;
446 *RemoteAddress
= inp
->inp_faddr
.s_addr
;
447 *RemotePort
= inp
->inp_fport
;
451 struct ifaddr
*ifa_iffind(struct sockaddr
*addr
, int type
)
453 if( OtcpEvent
.FindInterface
)
454 return OtcpEvent
.FindInterface( OtcpEvent
.ClientData
,
462 void oskittcp_die( const char *file
, int line
) {
463 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file
, line
);
467 /* Stuff supporting the BSD network-interface interface */
468 struct ifaddr
**ifnet_addrs
;
481 panic("if_attach\n");
491 * Handle interface watchdog timer routines. Called
492 * from softclock, we decrement timers (if set) and
493 * call the appropriate interface routine on expiration.
500 register struct ifnet
*ifp
;
503 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
) {
504 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
506 if (ifp
->if_watchdog
)
507 (*ifp
->if_watchdog
)(ifp
->if_unit
);
510 timeout(if_slowtimo
, (void *)0, hz
/ IFNET_SLOWHZ
);
515 * Locate an interface based on a complete address.
519 struct ifaddr
*ifa_ifwithaddr(addr
)
520 struct sockaddr
*addr
;
522 struct ifaddr
*ifaddr
= ifa_ifwithnet( addr
);
523 struct sockaddr_in
*addr_in
;
524 struct sockaddr_in
*faddr_in
;
527 OS_DbgPrint(OSK_MID_TRACE
,("No ifaddr\n"));
530 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr @ %x\n", ifaddr
));
533 addr_in
= (struct sockaddr_in
*)addr
;
534 faddr_in
= (struct sockaddr_in
*)ifaddr
->ifa_addr
;
536 if( faddr_in
->sin_addr
.s_addr
== addr_in
->sin_addr
.s_addr
)
543 * Locate the point to point interface with a given destination address.
547 ifa_ifwithdstaddr(addr
)
548 register struct sockaddr
*addr
;
550 OS_DbgPrint(OSK_MID_TRACE
,("Called\n"));
551 return ifa_iffind(addr
, IFF_POINTOPOINT
);
555 * Find an interface on a specific network. If many, choice
556 * is most specific found.
558 struct ifaddr
*ifa_ifwithnet(addr
)
559 struct sockaddr
*addr
;
561 struct sockaddr_in
*sin
;
562 struct ifaddr
*ifaddr
= ifa_iffind(addr
, IFF_UNICAST
);
566 sin
= (struct sockaddr_in
*)&ifaddr
->ifa_addr
;
568 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr->addr = %x\n",
569 sin
->sin_addr
.s_addr
));