3 #include <oskitdebug.h>
6 #include <sys/socket.h>
7 #include <sys/kernel.h>
8 #include <sys/filedesc.h>
10 #include <sys/fcntl.h>
13 #include <sys/protosw.h>
14 #include <sys/socket.h>
15 #include <sys/socketvar.h>
18 struct linker_set domain_set
;
20 OSKITTCP_EVENT_HANDLERS OtcpEvent
= { 0 };
22 OSK_UINT OskitDebugTraceLevel
= OSK_DEBUG_ULTRA
;
27 unsigned volatile ipending
;
28 struct timeval boottime
;
30 void *fbsd_malloc( unsigned int bytes
, ... ) {
31 if( !OtcpEvent
.TCPMalloc
) panic("no malloc");
32 return OtcpEvent
.TCPMalloc
33 ( OtcpEvent
.ClientData
, (OSK_UINT
)bytes
, "*", 0 );
36 void fbsd_free( void *data
, ... ) {
37 if( !OtcpEvent
.TCPFree
) panic("no free");
38 OtcpEvent
.TCPFree( OtcpEvent
.ClientData
, data
, "*", 0 );
42 OS_DbgPrint(OSK_MID_TRACE
,("Init Called\n"));
43 OS_DbgPrint(OSK_MID_TRACE
,("MB Init\n"));
45 OS_DbgPrint(OSK_MID_TRACE
,("Rawip Init\n"));
48 OS_DbgPrint(OSK_MID_TRACE
,("Route Init\n"));
50 OS_DbgPrint(OSK_MID_TRACE
,("Init fake freebsd scheduling\n"));
52 OS_DbgPrint(OSK_MID_TRACE
,("Init clock\n"));
54 OS_DbgPrint(OSK_MID_TRACE
,("Init TCP\n"));
56 OS_DbgPrint(OSK_MID_TRACE
,("Init routing\n"));
58 OS_DbgPrint(OSK_MID_TRACE
,("Init Finished\n"));
62 void DeinitOskitTCP() {
65 void TimerOskitTCP() {
70 void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers
) {
71 memcpy( &OtcpEvent
, EventHandlers
, sizeof(OtcpEvent
) );
72 if( OtcpEvent
.PacketSend
)
73 OS_DbgPrint(OSK_MID_TRACE
,("SendPacket handler registered: %x\n",
74 OtcpEvent
.PacketSend
));
77 void OskitDumpBuffer( OSK_PCHAR Data
, OSK_UINT Len
) {
80 for( i
= 0; i
< Len
; i
++ ) {
81 if( i
&& !(i
& 0xf) ) DbgPrint( "\n" );
82 if( !(i
& 0xf) ) DbgPrint( "%08x: ", (OSK_UINT
)(Data
+ i
) );
83 DbgPrint( " %02x", Data
[i
] );
88 /* From uipc_syscalls.c */
90 int OskitTCPSocket( void *context
,
97 int error
= socreate(domain
, &so
, type
, proto
);
99 so
->so_connection
= context
;
100 so
->so_state
= SS_NBIO
;
106 int OskitTCPRecv( void *connection
,
111 char *output_ptr
= Data
;
112 struct uio uio
= { 0 };
113 struct iovec iov
= { 0 };
120 if( Flags
& OSK_MSG_OOB
) tcp_flags
|= MSG_OOB
;
121 if( Flags
& OSK_MSG_DONTWAIT
) tcp_flags
|= MSG_DONTWAIT
;
122 if( Flags
& OSK_MSG_PEEK
) tcp_flags
|= MSG_PEEK
;
126 uio
.uio_rw
= UIO_READ
;
131 OS_DbgPrint(OSK_MID_TRACE
,("Reading %d bytes from TCP:\n", Len
));
133 error
= soreceive( connection
, NULL
, &uio
, NULL
, NULL
/* SCM_RIGHTS */,
137 OS_DbgPrint(OSK_MID_TRACE
,("Successful read from TCP:\n"));
138 *OutLen
= Len
- uio
.uio_resid
;
139 OskitDumpBuffer( Data
, *OutLen
);
146 getsockaddr(namp
, uaddr
, len
)
147 /* [<][>][^][v][top][bottom][index][help] */
148 struct sockaddr
**namp
;
155 if (len
> SOCK_MAXADDRLEN
)
157 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
158 error
= copyin(uaddr
, sa
, len
);
167 int OskitTCPBind( void *socket
, void *connection
,
168 void *nam
, OSK_UINT namelen
) {
170 struct socket
*so
= socket
;
171 struct mbuf sabuf
= { 0 };
172 struct sockaddr addr
;
174 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
177 addr
= *((struct sockaddr
*)nam
);
179 sabuf
.m_data
= (void *)&addr
;
180 sabuf
.m_len
= sizeof(addr
);
182 addr
.sa_family
= addr
.sa_len
;
183 addr
.sa_len
= sizeof(struct sockaddr
);
185 OskitDumpBuffer( (OSK_PCHAR
)&addr
, sizeof(addr
) );
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
;
196 struct connect_args _uap
= {
199 int error
= EFAULT
, s
;
200 struct mbuf sabuf
= { 0 };
201 struct sockaddr addr
;
203 OS_DbgPrint(OSK_MID_TRACE
,("Called, socket = %08x\n", socket
));
205 so
->so_connection
= connection
;
207 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
212 OS_DbgPrint(OSK_MIN_TRACE
,("Nam: %x\n", nam
));
214 addr
= *((struct sockaddr
*)nam
);
216 sabuf
.m_data
= (void *)&addr
;
217 sabuf
.m_len
= sizeof(addr
);
219 addr
.sa_family
= addr
.sa_len
;
220 addr
.sa_len
= sizeof(struct sockaddr
);
222 error
= soconnect(so
, &sabuf
);
227 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
233 so
->so_state
&= ~SS_ISCONNECTING
;
235 if (error
== ERESTART
)
239 OS_DbgPrint(OSK_MID_TRACE
,("Ending: %08x\n", error
));
243 int OskitTCPClose( void *socket
) {
244 struct socket
*so
= socket
;
245 so
->so_connection
= 0;
250 int OskitTCPSend( void *socket
, OSK_PCHAR Data
, OSK_UINT Len
,
251 OSK_UINT
*OutLen
, OSK_UINT flags
) {
252 struct mbuf
* m
= m_devget( Data
, Len
, 0, NULL
, NULL
);
256 OskitDumpBuffer( Data
, Len
);
257 error
= sosend( socket
, NULL
, NULL
, m
, NULL
, 0 );
262 int OskitTCPAccept( void *socket
,
265 OSK_UINT
*OutAddrLen
) {
269 nam
.m_data
= AddrOut
;
272 return soaccept( socket
, &nam
);
277 * We have a packet. While we store the fields we want in host byte order
278 * outside the original packet, the bsd stack modifies them in place.
281 void OskitTCPReceiveDatagram( OSK_PCHAR Data
, OSK_UINT Len
,
282 OSK_UINT IpHeaderLen
) {
283 struct mbuf
*Ip
= m_devget( Data
, Len
, 0, NULL
, NULL
);
286 if( !Ip
) return; /* drop the segment */
288 //memcpy( Ip->m_data, Data, Len );
289 Ip
->m_pkthdr
.len
= IpHeaderLen
;
291 /* Do the transformations on the header that tcp_input expects */
292 iph
= mtod(Ip
, struct ip
*);
294 iph
->ip_len
-= sizeof(struct ip
);
296 OS_DbgPrint(OSK_MAX_TRACE
,
297 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len
,
300 OskitDumpBuffer( Data
, Len
);
302 tcp_input(Ip
, IpHeaderLen
);
304 /* The buffer Ip is freed by tcp_input */
307 int OskitTCPListen( void *socket
, int backlog
) {
308 return solisten( socket
, backlog
);
311 void OskitTCPSetAddress( void *socket
,
312 OSK_UINT LocalAddress
,
314 OSK_UINT RemoteAddress
,
315 OSK_UI16 RemotePort
) {
316 struct socket
*so
= socket
;
317 struct inpcb
*inp
= so
->so_pcb
;
318 inp
->inp_laddr
.s_addr
= LocalAddress
;
319 inp
->inp_lport
= LocalPort
;
320 inp
->inp_faddr
.s_addr
= RemoteAddress
;
321 inp
->inp_fport
= RemotePort
;
322 DbgPrint("OSKIT: SET ADDR %x:%x -> %x:%x\n",
323 LocalAddress
, LocalPort
,
324 RemoteAddress
, RemotePort
);
327 void OskitTCPGetAddress( void *socket
,
328 OSK_UINT
*LocalAddress
,
330 OSK_UINT
*RemoteAddress
,
331 OSK_UI16
*RemotePort
) {
332 struct socket
*so
= socket
;
333 struct inpcb
*inp
= so
? so
->so_pcb
: 0;
335 *LocalAddress
= inp
->inp_laddr
.s_addr
;
336 *LocalPort
= inp
->inp_lport
;
337 *RemoteAddress
= inp
->inp_faddr
.s_addr
;
338 *RemotePort
= inp
->inp_fport
;
339 DbgPrint("OSKIT: GET ADDR %x:%x -> %x:%x\n",
340 *LocalAddress
, *LocalPort
,
341 *RemoteAddress
, *RemotePort
);
345 struct ifaddr
*ifa_iffind(struct sockaddr
*addr
, int type
)
347 if( OtcpEvent
.FindInterface
)
348 return OtcpEvent
.FindInterface( OtcpEvent
.ClientData
,
356 void oskittcp_die( const char *file
, int line
) {
357 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file
, line
);
361 /* Stuff supporting the BSD network-interface interface */
362 struct ifaddr
**ifnet_addrs
;
375 panic("if_attach\n");
385 * Handle interface watchdog timer routines. Called
386 * from softclock, we decrement timers (if set) and
387 * call the appropriate interface routine on expiration.
394 register struct ifnet
*ifp
;
397 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
) {
398 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
400 if (ifp
->if_watchdog
)
401 (*ifp
->if_watchdog
)(ifp
->if_unit
);
404 timeout(if_slowtimo
, (void *)0, hz
/ IFNET_SLOWHZ
);
409 * Locate an interface based on a complete address.
413 struct ifaddr
*ifa_ifwithaddr(addr
)
414 struct sockaddr
*addr
;
416 struct ifaddr
*ifaddr
= ifa_ifwithnet( addr
);
417 struct sockaddr_in
*addr_in
;
418 struct sockaddr_in
*faddr_in
;
421 OS_DbgPrint(OSK_MID_TRACE
,("No ifaddr\n"));
424 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr @ %x\n", ifaddr
));
427 addr_in
= (struct sockaddr_in
*)addr
;
428 faddr_in
= (struct sockaddr_in
*)ifaddr
->ifa_addr
;
430 if( faddr_in
->sin_addr
.s_addr
== addr_in
->sin_addr
.s_addr
)
437 * Locate the point to point interface with a given destination address.
441 ifa_ifwithdstaddr(addr
)
442 register struct sockaddr
*addr
;
444 OS_DbgPrint(OSK_MID_TRACE
,("Called\n"));
445 return ifa_iffind(addr
, IFF_POINTOPOINT
);
449 * Find an interface on a specific network. If many, choice
450 * is most specific found.
452 struct ifaddr
*ifa_ifwithnet(addr
)
453 struct sockaddr
*addr
;
455 struct sockaddr_in
*sin
;
456 struct ifaddr
*ifaddr
= ifa_iffind(addr
, IFF_UNICAST
);
460 sin
= (struct sockaddr
*)&ifaddr
->ifa_addr
;
462 OS_DbgPrint(OSK_MID_TRACE
,("ifaddr->addr = %x\n",
463 sin
->sin_addr
.s_addr
));