Send delayed ACKs after 200ms instead of 2500ms
[reactos.git] / reactos / drivers / lib / oskittcp / oskittcp / interface.c
1 #include <oskittcp.h>
2 #include <oskitdebug.h>
3 #include <net/raw_cb.h>
4
5 #include <sys/param.h>
6 #include <sys/systm.h>
7 #include <sys/socket.h>
8 #include <sys/kernel.h>
9 #include <sys/filedesc.h>
10 #include <sys/proc.h>
11 #include <sys/fcntl.h>
12 #include <sys/file.h>
13 #include <sys/mbuf.h>
14 #include <sys/protosw.h>
15 #include <sys/socket.h>
16 #include <sys/socketvar.h>
17 #include <sys/uio.h>
18
19 struct linker_set domain_set;
20
21 OSKITTCP_EVENT_HANDLERS OtcpEvent = { 0 };
22
23 //OSK_UINT OskitDebugTraceLevel = OSK_DEBUG_ULTRA;
24 OSK_UINT OskitDebugTraceLevel = 0;
25
26 /* SPL */
27 unsigned cpl;
28 unsigned net_imask;
29 unsigned volatile ipending;
30 struct timeval boottime;
31
32 void clock_init();
33 int isprint(int c);
34 int _snprintf(char * buf, size_t cnt, const char *fmt, ...);
35
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 );
40 }
41
42 void fbsd_free( void *data, ... ) {
43 if( !OtcpEvent.TCPFree ) panic("no free");
44 OtcpEvent.TCPFree( OtcpEvent.ClientData, data, "*", 0 );
45 }
46
47 void InitOskitTCP() {
48 OS_DbgPrint(OSK_MID_TRACE,("Init Called\n"));
49 OS_DbgPrint(OSK_MID_TRACE,("MB Init\n"));
50 mbinit();
51 OS_DbgPrint(OSK_MID_TRACE,("Rawip Init\n"));
52 rip_init();
53 raw_init();
54 OS_DbgPrint(OSK_MID_TRACE,("Route Init\n"));
55 route_init();
56 OS_DbgPrint(OSK_MID_TRACE,("Init clock\n"));
57 clock_init();
58 OS_DbgPrint(OSK_MID_TRACE,("Init TCP\n"));
59 tcp_init();
60 OS_DbgPrint(OSK_MID_TRACE,("Init routing\n"));
61 domaininit();
62 OS_DbgPrint(OSK_MID_TRACE,("Init Finished\n"));
63 tcp_iss = 1024;
64 }
65
66 void DeinitOskitTCP() {
67 }
68
69 void TimerOskitTCP( int FastTimer, int SlowTimer ) {
70 if ( SlowTimer ) {
71 tcp_slowtimo();
72 }
73 if ( FastTimer ) {
74 tcp_fasttimo();
75 }
76 }
77
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));
83 }
84
85 void OskitDumpBuffer( OSK_PCHAR Data, OSK_UINT Len )
86 {
87 unsigned int i;
88 char line[81];
89 static const char* hex = "0123456789abcdef";
90
91 for ( i = 0; i < Len; i++ )
92 {
93 int align = i & 0xf;
94 int align3 = (align<<1) + align;
95 unsigned char c = Data[i];
96 if ( !align )
97 {
98 if ( i ) DbgPrint( line );
99 _snprintf ( line, sizeof(line)-1, "%08x: \n", &Data[i] );
100 line[sizeof(line)-1] = '\0';
101 }
102
103 line[10+align3] = hex[(c>>4)&0xf];
104 line[11+align3] = hex[c&0xf];
105 if ( !isprint(c) )
106 c = '.';
107 line[59+align] = c;
108 }
109 if ( Len & 0xf )
110 DbgPrint ( line );
111 }
112
113 /* From uipc_syscalls.c */
114
115 int OskitTCPSocket( void *context,
116 void **aso,
117 int domain,
118 int type,
119 int proto )
120 {
121 struct socket *so;
122 int error = socreate(domain, &so, type, proto);
123 if( !error ) {
124 so->so_connection = context;
125 so->so_state = SS_NBIO;
126 so->so_error = 0;
127 so->so_q = so->so_q0 = NULL;
128 so->so_qlen = 0;
129 so->so_head = NULL;
130 *aso = so;
131 }
132 return error;
133 }
134
135 int OskitTCPRecv( void *connection,
136 OSK_PCHAR Data,
137 OSK_UINT Len,
138 OSK_UINT *OutLen,
139 OSK_UINT Flags ) {
140 struct uio uio = { 0 };
141 struct iovec iov = { 0 };
142 int error = 0;
143 int tcp_flags = 0;
144
145 *OutLen = 0;
146
147 OS_DbgPrint(OSK_MID_TRACE,
148 ("so->so_state %x\n", ((struct socket *)connection)->so_state));
149
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;
153
154 uio.uio_resid = Len;
155 uio.uio_iov = &iov;
156 uio.uio_rw = UIO_READ;
157 uio.uio_iovcnt = 1;
158 iov.iov_len = Len;
159 iov.iov_base = Data;
160
161 OS_DbgPrint(OSK_MID_TRACE,("Reading %d bytes from TCP:\n", Len));
162
163 error = soreceive( connection, NULL, &uio, NULL, NULL /* SCM_RIGHTS */,
164 &tcp_flags );
165
166 if( error == 0 ) {
167 *OutLen = Len - uio.uio_resid;
168 }
169
170 return error;
171 }
172
173 int OskitTCPBind( void *socket, void *connection,
174 void *nam, OSK_UINT namelen ) {
175 int error = EFAULT;
176 struct socket *so = socket;
177 struct mbuf sabuf;
178 struct sockaddr addr;
179
180 OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
181
182 if( nam )
183 addr = *((struct sockaddr *)nam);
184
185 RtlZeroMemory(&sabuf, sizeof(sabuf));
186 sabuf.m_data = (void *)&addr;
187 sabuf.m_len = sizeof(addr);
188
189 addr.sa_family = addr.sa_len;
190 addr.sa_len = sizeof(struct sockaddr);
191
192 error = sobind(so, &sabuf);
193
194 OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
195 return (error);
196 }
197
198 int OskitTCPConnect( void *socket, void *connection,
199 void *nam, OSK_UINT namelen ) {
200 struct socket *so = socket;
201 int error = EFAULT;
202 struct mbuf sabuf;
203 struct sockaddr addr;
204
205 OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
206
207 so->so_connection = connection;
208
209 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
210 error = EALREADY;
211 goto done;
212 }
213
214 OS_DbgPrint(OSK_MIN_TRACE,("Nam: %x\n", nam));
215 if( nam )
216 addr = *((struct sockaddr *)nam);
217
218 RtlZeroMemory(&sabuf, sizeof(sabuf));
219 sabuf.m_data = (void *)&addr;
220 sabuf.m_len = sizeof(addr);
221
222 addr.sa_family = addr.sa_len;
223 addr.sa_len = sizeof(struct sockaddr);
224
225 error = soconnect(so, &sabuf);
226
227 if (error)
228 goto bad;
229
230 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
231 error = EINPROGRESS;
232 goto done;
233 }
234
235 bad:
236 so->so_state &= ~SS_ISCONNECTING;
237
238 if (error == ERESTART)
239 error = EINTR;
240
241 done:
242 OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
243 return (error);
244 }
245
246 int OskitTCPShutdown( void *socket, int disconn_type ) {
247 return soshutdown( socket, disconn_type );
248 }
249
250 int OskitTCPClose( void *socket ) {
251 struct socket *so = socket;
252 so->so_connection = 0;
253 soclose( so );
254 return 0;
255 }
256
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 );
260 int error = 0;
261 if ( !m )
262 return ENOBUFS;
263 error = sosend( socket, NULL, NULL, m, NULL, 0 );
264 *OutLen = Len;
265 return error;
266 }
267
268 int OskitTCPAccept( void *socket,
269 void **new_socket,
270 void *AddrOut,
271 OSK_UINT AddrLen,
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;
279 struct mbuf mnam;
280 struct inpcb *inp;
281 int namelen = 0, error = 0, s;
282
283 OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: Doing accept (Finish %d)\n",
284 FinishAccepting));
285
286 *OutAddrLen = AddrLen;
287
288 if (name)
289 /* that's a copyin actually */
290 namelen = *OutAddrLen;
291
292 s = splnet();
293
294 #if 0
295 if ((head->so_options & SO_ACCEPTCONN) == 0) {
296 splx(s);
297 OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: head->so_options = %x, wanted bit %x\n",
298 head->so_options, SO_ACCEPTCONN));
299 error = EINVAL;
300 goto out;
301 }
302 #endif
303
304 OS_DbgPrint(OSK_MID_TRACE,("head->so_q = %x, head->so_state = %x\n",
305 head->so_q, head->so_state));
306
307 if ((head->so_state & SS_NBIO) && head->so_q == NULL) {
308 splx(s);
309 error = EWOULDBLOCK;
310 goto out;
311 }
312
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;
317 break;
318 }
319 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
320 error = tsleep((caddr_t)&head->so_timeo, PSOCK | PCATCH,
321 "accept", 0);
322 if (error) {
323 splx(s);
324 goto out;
325 }
326 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
327 }
328 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
329
330 #if 0
331 if (head->so_error) {
332 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
333 error = head->so_error;
334 head->so_error = 0;
335 splx(s);
336 goto out;
337 }
338 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
339 #endif
340
341 /*
342 * At this point we know that there is at least one connection
343 * ready to be accepted. Remove it from the queue.
344 */
345 so = head->so_q;
346
347 inp = so ? (struct inpcb *)so->so_pcb : NULL;
348 if( inp ) {
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;
352 }
353
354 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
355 if( FinishAccepting ) {
356 head->so_q = so->so_q;
357 head->so_qlen--;
358
359 *newso = so;
360
361 /*so->so_state &= ~SS_COMP;*/
362
363 mnam.m_data = (char *)&sa;
364 mnam.m_len = sizeof(sa);
365
366 (void) soaccept(so, &mnam);
367
368 so->so_state = SS_NBIO | SS_ISCONNECTED;
369 so->so_q = so->so_q0 = NULL;
370 so->so_qlen = 0;
371 so->so_head = 0;
372
373 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
374 if (name) {
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 */
379 }
380 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
381 splx(s);
382 }
383 out:
384 OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: Returning %d\n", error));
385 return (error);
386 }
387
388 /* The story so far
389 *
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.
392 */
393
394 void OskitTCPReceiveDatagram( OSK_PCHAR Data, OSK_UINT Len,
395 OSK_UINT IpHeaderLen ) {
396 struct mbuf *Ip = m_devget( Data, Len, 0, NULL, NULL );
397 struct ip *iph;
398
399 if( !Ip ) return; /* drop the segment */
400
401 //memcpy( Ip->m_data, Data, Len );
402 Ip->m_pkthdr.len = IpHeaderLen;
403
404 /* Do the transformations on the header that tcp_input expects */
405 iph = mtod(Ip, struct ip *);
406 NTOHS(iph->ip_len);
407 iph->ip_len -= sizeof(struct ip);
408
409 OS_DbgPrint(OSK_MAX_TRACE,
410 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len,
411 IpHeaderLen));
412
413 tcp_input(Ip, IpHeaderLen);
414
415 /* The buffer Ip is freed by tcp_input */
416 }
417
418 int OskitTCPListen( void *socket, int backlog ) {
419 int error;
420
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));
424
425 return error;
426 }
427
428 void OskitTCPSetAddress( void *socket,
429 OSK_UINT LocalAddress,
430 OSK_UI16 LocalPort,
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;
439 }
440
441 void OskitTCPGetAddress( void *socket,
442 OSK_UINT *LocalAddress,
443 OSK_UI16 *LocalPort,
444 OSK_UINT *RemoteAddress,
445 OSK_UI16 *RemotePort ) {
446 struct socket *so = socket;
447 struct inpcb *inp = so ? (struct inpcb *)so->so_pcb : NULL;
448 if( inp ) {
449 *LocalAddress = inp->inp_laddr.s_addr;
450 *LocalPort = inp->inp_lport;
451 *RemoteAddress = inp->inp_faddr.s_addr;
452 *RemotePort = inp->inp_fport;
453 }
454 }
455
456 struct ifaddr *ifa_iffind(struct sockaddr *addr, int type)
457 {
458 if( OtcpEvent.FindInterface )
459 return OtcpEvent.FindInterface( OtcpEvent.ClientData,
460 PF_INET,
461 type,
462 addr );
463 else
464 return NULL;
465 }
466
467 void oskittcp_die( const char *file, int line ) {
468 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file, line);
469 *((int *)0) = 0;
470 }
471
472 /* Stuff supporting the BSD network-interface interface */
473 struct ifaddr **ifnet_addrs;
474 struct ifnet *ifnet;
475
476 void
477 ifinit()
478 {
479 }
480
481
482 void
483 if_attach(ifp)
484 struct ifnet *ifp;
485 {
486 panic("if_attach\n");
487 }
488
489 struct ifnet *
490 ifunit(char *name)
491 {
492 return 0;
493 }
494
495 /*
496 * Handle interface watchdog timer routines. Called
497 * from softclock, we decrement timers (if set) and
498 * call the appropriate interface routine on expiration.
499 */
500 void
501 if_slowtimo(arg)
502 void *arg;
503 {
504 #if 0
505 register struct ifnet *ifp;
506 int s = splimp();
507
508 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
509 if (ifp->if_timer == 0 || --ifp->if_timer)
510 continue;
511 if (ifp->if_watchdog)
512 (*ifp->if_watchdog)(ifp->if_unit);
513 }
514 splx(s);
515 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
516 #endif
517 }
518
519 /*
520 * Locate an interface based on a complete address.
521 */
522
523 /*ARGSUSED*/
524 struct ifaddr *ifa_ifwithaddr(addr)
525 struct sockaddr *addr;
526 {
527 struct ifaddr *ifaddr = ifa_ifwithnet( addr );
528 struct sockaddr_in *addr_in;
529 struct sockaddr_in *faddr_in;
530
531 if( !ifaddr ) {
532 OS_DbgPrint(OSK_MID_TRACE,("No ifaddr\n"));
533 return NULL;
534 } else {
535 OS_DbgPrint(OSK_MID_TRACE,("ifaddr @ %x\n", ifaddr));
536 }
537
538 addr_in = (struct sockaddr_in *)addr;
539 faddr_in = (struct sockaddr_in *)ifaddr->ifa_addr;
540
541 if( faddr_in->sin_addr.s_addr == addr_in->sin_addr.s_addr )
542 return ifaddr;
543 else
544 return NULL;
545 }
546
547 /*
548 * Locate the point to point interface with a given destination address.
549 */
550 /*ARGSUSED*/
551 struct ifaddr *
552 ifa_ifwithdstaddr(addr)
553 register struct sockaddr *addr;
554 {
555 OS_DbgPrint(OSK_MID_TRACE,("Called\n"));
556 return ifa_iffind(addr, IFF_POINTOPOINT);
557 }
558
559 /*
560 * Find an interface on a specific network. If many, choice
561 * is most specific found.
562 */
563 struct ifaddr *ifa_ifwithnet(addr)
564 struct sockaddr *addr;
565 {
566 struct sockaddr_in *sin;
567 struct ifaddr *ifaddr = ifa_iffind(addr, IFF_UNICAST);
568
569 if( ifaddr )
570 {
571 sin = (struct sockaddr_in *)&ifaddr->ifa_addr;
572
573 OS_DbgPrint(OSK_MID_TRACE,("ifaddr->addr = %x\n",
574 sin->sin_addr.s_addr));
575 }
576
577 return ifaddr;
578 }
579