- Disassociate the socket context before closing so we don't get signalled with...
[reactos.git] / reactos / lib / drivers / 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 KSPIN_LOCK OSKLock;
27
28 /* SPL */
29 unsigned cpl;
30 unsigned net_imask;
31 unsigned volatile ipending;
32 struct timeval boottime;
33
34 void clock_init();
35 int _snprintf(char * buf, size_t cnt, const char *fmt, ...);
36
37 void *fbsd_malloc( unsigned int bytes, char *file, unsigned line, ... ) {
38 if( !OtcpEvent.TCPMalloc ) panic("no malloc");
39 return OtcpEvent.TCPMalloc
40 ( OtcpEvent.ClientData, (OSK_UINT)bytes, (OSK_PCHAR)file, line );
41 }
42
43 void fbsd_free( void *data, char *file, unsigned line, ... ) {
44 if( !OtcpEvent.TCPFree ) panic("no free");
45 OtcpEvent.TCPFree( OtcpEvent.ClientData, data, (OSK_PCHAR)file, line );
46 }
47
48 void InitOskitTCP() {
49 OS_DbgPrint(OSK_MID_TRACE,("Init Called\n"));
50 KeInitializeSpinLock(&OSKLock);
51 OS_DbgPrint(OSK_MID_TRACE,("MB Init\n"));
52 mbinit();
53 OS_DbgPrint(OSK_MID_TRACE,("Rawip Init\n"));
54 rip_init();
55 raw_init();
56 OS_DbgPrint(OSK_MID_TRACE,("Route Init\n"));
57 route_init();
58 OS_DbgPrint(OSK_MID_TRACE,("Init clock\n"));
59 clock_init();
60 OS_DbgPrint(OSK_MID_TRACE,("Init TCP\n"));
61 tcp_init();
62 OS_DbgPrint(OSK_MID_TRACE,("Init routing\n"));
63 domaininit();
64 OS_DbgPrint(OSK_MID_TRACE,("Init Finished\n"));
65 tcp_iss = 1024;
66 }
67
68 void DeinitOskitTCP() {
69 }
70
71 void TimerOskitTCP( int FastTimer, int SlowTimer ) {
72 KIRQL OldIrql;
73
74 /* This function is a special case in which we cannot use OSKLock/OSKUnlock
75 * because we don't enter with the connection lock held */
76
77 OSKLockAndRaise(&OldIrql);
78 if ( SlowTimer ) {
79 tcp_slowtimo();
80 }
81 if ( FastTimer ) {
82 tcp_fasttimo();
83 }
84 OSKUnlockAndLower(OldIrql);
85 }
86
87 void RegisterOskitTCPEventHandlers( POSKITTCP_EVENT_HANDLERS EventHandlers ) {
88 memcpy( &OtcpEvent, EventHandlers, sizeof(OtcpEvent) );
89 }
90
91 void OskitDumpBuffer( OSK_PCHAR Data, OSK_UINT Len )
92 {
93 unsigned int i;
94 char line[81];
95 static const char* hex = "0123456789abcdef";
96
97 for ( i = 0; i < Len; i++ )
98 {
99 int align = i & 0xf;
100 int align3 = (align<<1) + align;
101 unsigned char c = Data[i];
102 if ( !align )
103 {
104 if ( i ) DbgPrint( line );
105 _snprintf ( line, sizeof(line)-1, "%08x: \n", &Data[i] );
106 line[sizeof(line)-1] = '\0';
107 }
108
109 line[10+align3] = hex[(c>>4)&0xf];
110 line[11+align3] = hex[c&0xf];
111 if ( !isprint(c) )
112 c = '.';
113 line[59+align] = c;
114 }
115 if ( Len & 0xf )
116 DbgPrint ( line );
117 }
118
119 /* From uipc_syscalls.c */
120
121 int OskitTCPSocket( void *context,
122 void **aso,
123 int domain,
124 int type,
125 int proto )
126 {
127 struct socket *so;
128
129 OSKLock();
130 int error = socreate(domain, &so, type, proto);
131 if( !error ) {
132 so->so_connection = context;
133 so->so_state |= SS_NBIO;
134 *aso = so;
135 }
136 OSKUnlock();
137
138 return error;
139 }
140
141 int OskitTCPRecv( void *connection,
142 OSK_PCHAR Data,
143 OSK_UINT Len,
144 OSK_UINT *OutLen,
145 OSK_UINT Flags ) {
146 struct uio uio = { 0 };
147 struct iovec iov = { 0 };
148 int error = 0;
149 int tcp_flags = 0;
150
151 OS_DbgPrint(OSK_MID_TRACE,
152 ("so->so_state %x\n", ((struct socket *)connection)->so_state));
153
154 if( Flags & OSK_MSG_OOB ) tcp_flags |= MSG_OOB;
155 if( Flags & OSK_MSG_DONTWAIT ) tcp_flags |= MSG_DONTWAIT;
156 if( Flags & OSK_MSG_PEEK ) tcp_flags |= MSG_PEEK;
157
158 iov.iov_len = Len;
159 iov.iov_base = (char *)Data;
160 uio.uio_iov = &iov;
161 uio.uio_iovcnt = 1;
162 uio.uio_offset = 0;
163 uio.uio_resid = Len;
164 uio.uio_segflg = UIO_SYSSPACE;
165 uio.uio_rw = UIO_READ;
166 uio.uio_procp = NULL;
167
168 OS_DbgPrint(OSK_MID_TRACE,("Reading %d bytes from TCP:\n", Len));
169
170 OSKLock();
171 error = soreceive( connection, NULL, &uio, NULL, NULL /* SCM_RIGHTS */,
172 &tcp_flags );
173 OSKUnlock();
174
175 *OutLen = Len - uio.uio_resid;
176
177 return error;
178 }
179
180 int OskitTCPBind( void *socket,
181 void *nam, OSK_UINT namelen ) {
182 int error = EFAULT;
183 struct socket *so = socket;
184 struct mbuf sabuf;
185 struct sockaddr addr;
186
187 OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
188
189 if (!socket)
190 return OSK_ESHUTDOWN;
191
192 if( nam )
193 addr = *((struct sockaddr *)nam);
194
195 RtlZeroMemory(&sabuf, sizeof(sabuf));
196 sabuf.m_data = (void *)&addr;
197 sabuf.m_len = sizeof(addr);
198
199 addr.sa_family = addr.sa_len;
200 addr.sa_len = sizeof(struct sockaddr);
201
202 OSKLock();
203 error = sobind(so, &sabuf);
204 OSKUnlock();
205
206 OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
207 return (error);
208 }
209
210 int OskitTCPConnect( void *socket, void *nam, OSK_UINT namelen ) {
211 struct socket *so = socket;
212 int error = EFAULT;
213 struct mbuf sabuf;
214 struct sockaddr addr;
215
216 OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
217
218 OSKLock();
219 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
220 error = EALREADY;
221 goto done;
222 }
223
224 OS_DbgPrint(OSK_MIN_TRACE,("Nam: %x\n", nam));
225 if( nam )
226 addr = *((struct sockaddr *)nam);
227
228 RtlZeroMemory(&sabuf, sizeof(sabuf));
229 sabuf.m_data = (void *)&addr;
230 sabuf.m_len = sizeof(addr);
231
232 addr.sa_family = addr.sa_len;
233 addr.sa_len = sizeof(struct sockaddr);
234
235 error = soconnect(so, &sabuf);
236
237 if (error == EINPROGRESS)
238 goto done;
239 else if (error)
240 goto bad;
241
242 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
243 error = EINPROGRESS;
244 goto done;
245 }
246
247 bad:
248 so->so_state &= ~SS_ISCONNECTING;
249
250 if (error == ERESTART)
251 error = EINTR;
252
253 done:
254 OSKUnlock();
255 OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
256 return (error);
257 }
258
259 int OskitTCPDisconnect(void *socket)
260 {
261 int error;
262
263 if (!socket)
264 return OSK_ESHUTDOWN;
265
266 OSKLock();
267 error = sodisconnect(socket);
268 OSKUnlock();
269
270 return error;
271 }
272
273 int OskitTCPShutdown( void *socket, int disconn_type ) {
274 int error;
275
276 if (!socket)
277 return OSK_ESHUTDOWN;
278
279 OSKLock();
280 error = soshutdown( socket, disconn_type );
281 OSKUnlock();
282
283 return error;
284 }
285
286 int OskitTCPClose( void *socket ) {
287 int error;
288 struct socket *so = socket;
289
290 if (!socket)
291 return OSK_ESHUTDOWN;
292
293 OSKLock();
294 /* We have to remove the socket context here otherwise we end up
295 * back in HandleSignalledConnection with a freed connection context
296 */
297 so->so_connection = NULL;
298 error = soclose( socket );
299 OSKUnlock();
300
301 return error;
302 }
303
304 int OskitTCPSend( void *socket, OSK_PCHAR Data, OSK_UINT Len,
305 OSK_UINT *OutLen, OSK_UINT flags ) {
306 int error;
307 struct uio uio;
308 struct iovec iov;
309
310 if (!socket)
311 return OSK_ESHUTDOWN;
312
313 iov.iov_len = Len;
314 iov.iov_base = (char *)Data;
315 uio.uio_iov = &iov;
316 uio.uio_iovcnt = 1;
317 uio.uio_offset = 0;
318 uio.uio_resid = Len;
319 uio.uio_segflg = UIO_SYSSPACE;
320 uio.uio_rw = UIO_WRITE;
321 uio.uio_procp = NULL;
322
323 OSKLock();
324 error = sosend( socket, NULL, &uio, NULL, NULL, 0 );
325 OSKUnlock();
326
327 *OutLen = Len - uio.uio_resid;
328
329 return error;
330 }
331
332 int OskitTCPAccept( void *socket,
333 void **new_socket,
334 void *context,
335 void *AddrOut,
336 OSK_UINT AddrLen,
337 OSK_UINT *OutAddrLen,
338 OSK_UINT FinishAccepting ) {
339 struct socket *head = (void *)socket;
340 struct sockaddr *name = (struct sockaddr *)AddrOut;
341 struct socket **newso = (struct socket **)new_socket;
342 struct socket *so = socket;
343 struct sockaddr_in sa;
344 struct mbuf mnam;
345 struct inpcb *inp;
346 int namelen = 0, error = 0, s;
347
348 if (!socket)
349 return OSK_ESHUTDOWN;
350
351 if (!new_socket || !AddrOut)
352 return OSK_EINVAL;
353
354 OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: Doing accept (Finish %d)\n",
355 FinishAccepting));
356
357 *OutAddrLen = AddrLen;
358
359 if (name)
360 /* that's a copyin actually */
361 namelen = *OutAddrLen;
362
363 OSKLock();
364
365 s = splnet();
366
367 #if 0
368 if ((head->so_options & SO_ACCEPTCONN) == 0) {
369 OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: head->so_options = %x, wanted bit %x\n",
370 head->so_options, SO_ACCEPTCONN));
371 error = EINVAL;
372 goto out;
373 }
374 #endif
375
376 OS_DbgPrint(OSK_MID_TRACE,("head->so_q = %x, head->so_state = %x\n",
377 head->so_q, head->so_state));
378
379 if ((head->so_state & SS_NBIO) && head->so_q == NULL) {
380 error = EWOULDBLOCK;
381 goto out;
382 }
383
384 /*
385 * At this point we know that there is at least one connection
386 * ready to be accepted. Remove it from the queue.
387 */
388 so = head->so_q;
389
390 inp = so ? (struct inpcb *)so->so_pcb : NULL;
391 if( inp && name ) {
392 ((struct sockaddr_in *)AddrOut)->sin_addr.s_addr =
393 inp->inp_faddr.s_addr;
394 ((struct sockaddr_in *)AddrOut)->sin_port = inp->inp_fport;
395 }
396
397 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
398 if( FinishAccepting && so ) {
399 head->so_q = so->so_q;
400 head->so_qlen--;
401
402 mnam.m_data = (char *)&sa;
403 mnam.m_len = sizeof(sa);
404
405 error = soaccept(so, &mnam);
406 if (error)
407 goto out;
408
409 so->so_state |= SS_NBIO | SS_ISCONNECTED;
410 so->so_q = so->so_q0 = NULL;
411 so->so_qlen = 0;
412 so->so_head = 0;
413 so->so_connection = context;
414
415 *newso = so;
416
417 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
418 if (name) {
419 /* check sa_len before it is destroyed */
420 memcpy( AddrOut, &sa, AddrLen < sizeof(sa) ? AddrLen : sizeof(sa) );
421 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
422 *OutAddrLen = namelen; /* copyout actually */
423 }
424 OS_DbgPrint(OSK_MID_TRACE,("error = %d\n", error));
425 }
426 out:
427 splx(s);
428 OSKUnlock();
429 OS_DbgPrint(OSK_MID_TRACE,("OSKITTCP: Returning %d\n", error));
430 return (error);
431 }
432
433 /* The story so far
434 *
435 * We have a packet. While we store the fields we want in host byte order
436 * outside the original packet, the bsd stack modifies them in place.
437 */
438
439 void OskitTCPReceiveDatagram( OSK_PCHAR Data, OSK_UINT Len,
440 OSK_UINT IpHeaderLen ) {
441 struct mbuf *Ip;
442 struct ip *iph;
443
444 OSKLock();
445 Ip = m_devget( (char *)Data, Len, 0, NULL, NULL );
446 if( !Ip )
447 {
448 OSKUnlock();
449 return; /* drop the segment */
450 }
451
452 //memcpy( Ip->m_data, Data, Len );
453 Ip->m_pkthdr.len = IpHeaderLen;
454
455 /* Do the transformations on the header that tcp_input expects */
456 iph = mtod(Ip, struct ip *);
457 NTOHS(iph->ip_len);
458 iph->ip_len -= sizeof(struct ip);
459
460 OS_DbgPrint(OSK_MAX_TRACE,
461 ("OskitTCPReceiveDatagram: %d (%d header) Bytes\n", Len,
462 IpHeaderLen));
463
464 tcp_input(Ip, IpHeaderLen);
465 OSKUnlock();
466
467 /* The buffer Ip is freed by tcp_input */
468 }
469
470 int OskitTCPSetSockOpt(void *socket,
471 int level,
472 int optname,
473 char *buffer,
474 int size)
475 {
476 struct mbuf *m;
477 int error;
478
479 if (!socket)
480 return OSK_ESHUTDOWN;
481
482 if (size >= MLEN)
483 return OSK_EINVAL;
484
485 OSKLock();
486 m = m_get(M_WAIT, MT_SOOPTS);
487 if (!m)
488 {
489 OSKUnlock();
490 return OSK_ENOMEM;
491 }
492
493 m->m_len = size;
494
495 memcpy(m->m_data, buffer, size);
496
497 /* m is freed by sosetopt */
498 error = sosetopt(socket, level, optname, m);
499 OSKUnlock();
500
501 return error;
502 }
503
504 int OskitTCPGetSockOpt(void *socket,
505 int level,
506 int optname,
507 char *buffer,
508 int *size)
509 {
510 int error, oldsize = *size;
511 struct mbuf *m;
512
513 if (!socket)
514 return OSK_ESHUTDOWN;
515
516 OSKLock();
517 error = sogetopt(socket, level, optname, &m);
518 if (!error)
519 {
520 *size = m->m_len;
521
522 if (!buffer || oldsize < m->m_len)
523 {
524 m_freem(m);
525 OSKUnlock();
526 return OSK_EINVAL;
527 }
528
529 memcpy(buffer, m->m_data, m->m_len);
530
531 m_freem(m);
532 }
533 OSKUnlock();
534
535 return error;
536 }
537
538 int OskitTCPListen( void *socket, int backlog ) {
539 int error;
540
541 if (!socket)
542 return OSK_ESHUTDOWN;
543
544 OS_DbgPrint(OSK_MID_TRACE,("Called, socket = %08x\n", socket));
545
546 OSKLock();
547 error = solisten( socket, backlog );
548 OSKUnlock();
549
550 OS_DbgPrint(OSK_MID_TRACE,("Ending: %08x\n", error));
551
552 return error;
553 }
554
555 int OskitTCPSetAddress( void *socket,
556 OSK_UINT LocalAddress,
557 OSK_UI16 LocalPort,
558 OSK_UINT RemoteAddress,
559 OSK_UI16 RemotePort ) {
560 struct socket *so = socket;
561 struct inpcb *inp;
562
563 if (!socket)
564 return OSK_ESHUTDOWN;
565
566 OSKLock();
567 inp = (struct inpcb *)so->so_pcb;
568 inp->inp_laddr.s_addr = LocalAddress;
569 inp->inp_lport = LocalPort;
570 inp->inp_faddr.s_addr = RemoteAddress;
571 inp->inp_fport = RemotePort;
572 OSKUnlock();
573
574 return 0;
575 }
576
577 int OskitTCPGetAddress( void *socket,
578 OSK_UINT *LocalAddress,
579 OSK_UI16 *LocalPort,
580 OSK_UINT *RemoteAddress,
581 OSK_UI16 *RemotePort ) {
582 struct socket *so = socket;
583 struct inpcb *inp;
584
585 if (!socket)
586 return OSK_ESHUTDOWN;
587
588 OSKLock();
589 inp = (struct inpcb *)so->so_pcb;
590 *LocalAddress = inp->inp_laddr.s_addr;
591 *LocalPort = inp->inp_lport;
592 *RemoteAddress = inp->inp_faddr.s_addr;
593 *RemotePort = inp->inp_fport;
594 OSKUnlock();
595
596 return 0;
597 }
598
599 int OskitTCPGetSocketError(void *socket) {
600 struct socket *so = socket;
601 int error;
602
603 if (!socket)
604 return OSK_ESHUTDOWN;
605
606 OSKLock();
607 error = so->so_error;
608 OSKUnlock();
609
610 return error;
611 }
612
613 struct ifaddr *ifa_iffind(struct sockaddr *addr, int type)
614 {
615 if( OtcpEvent.FindInterface )
616 return OtcpEvent.FindInterface( OtcpEvent.ClientData,
617 PF_INET,
618 type,
619 addr );
620 else
621 return NULL;
622 }
623
624 void oskittcp_die( const char *file, int line ) {
625 DbgPrint("\n\n*** OSKITTCP: Panic Called at %s:%d ***\n", file, line);
626 ASSERT(FALSE);
627 }
628
629 /* Stuff supporting the BSD network-interface interface */
630 struct ifaddr **ifnet_addrs;
631 struct ifnet *ifnet;
632
633 void
634 ifinit()
635 {
636 }
637
638
639 void
640 if_attach(ifp)
641 struct ifnet *ifp;
642 {
643 panic("if_attach\n");
644 }
645
646 struct ifnet *
647 ifunit(char *name)
648 {
649 return 0;
650 }
651
652 /*
653 * Handle interface watchdog timer routines. Called
654 * from softclock, we decrement timers (if set) and
655 * call the appropriate interface routine on expiration.
656 */
657 void
658 if_slowtimo(arg)
659 void *arg;
660 {
661 #if 0
662 register struct ifnet *ifp;
663 int s = splimp();
664
665 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
666 if (ifp->if_timer == 0 || --ifp->if_timer)
667 continue;
668 if (ifp->if_watchdog)
669 (*ifp->if_watchdog)(ifp->if_unit);
670 }
671 splx(s);
672 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
673 #endif
674 }
675
676 /*
677 * Locate an interface based on a complete address.
678 */
679
680 /*ARGSUSED*/
681 struct ifaddr *ifa_ifwithaddr(addr)
682 struct sockaddr *addr;
683 {
684 struct ifaddr *ifaddr = ifa_ifwithnet( addr );
685 struct sockaddr_in *addr_in;
686 struct sockaddr_in *faddr_in;
687
688 if( !ifaddr ) {
689 OS_DbgPrint(OSK_MID_TRACE,("No ifaddr\n"));
690 return NULL;
691 } else {
692 OS_DbgPrint(OSK_MID_TRACE,("ifaddr @ %x\n", ifaddr));
693 }
694
695 addr_in = (struct sockaddr_in *)addr;
696 faddr_in = (struct sockaddr_in *)ifaddr->ifa_addr;
697
698 if( faddr_in->sin_addr.s_addr == addr_in->sin_addr.s_addr )
699 return ifaddr;
700 else
701 return NULL;
702 }
703
704 /*
705 * Locate the point to point interface with a given destination address.
706 */
707 /*ARGSUSED*/
708 struct ifaddr *
709 ifa_ifwithdstaddr(addr)
710 register struct sockaddr *addr;
711 {
712 OS_DbgPrint(OSK_MID_TRACE,("Called\n"));
713 return ifa_iffind(addr, IFF_POINTOPOINT);
714 }
715
716 /*
717 * Find an interface on a specific network. If many, choice
718 * is most specific found.
719 */
720 struct ifaddr *ifa_ifwithnet(addr)
721 struct sockaddr *addr;
722 {
723 struct sockaddr_in *sin;
724 struct ifaddr *ifaddr = ifa_iffind(addr, IFF_UNICAST);
725
726 if( ifaddr )
727 {
728 sin = (struct sockaddr_in *)&ifaddr->ifa_addr;
729
730 OS_DbgPrint(OSK_MID_TRACE,("ifaddr->addr = %x\n",
731 sin->sin_addr.s_addr));
732 }
733
734 return ifaddr;
735 }
736