static void ENET_scan_for_adapters(void) { struct enet_list elist ; struct enet_member * em ; MONITOR_RLOCK ; LEVEL_DEBUG("ENET SCAN!"); enet_list_init( &elist ) ; Find_ENET_all( &elist ) ; em = elist.head ; if ( em == NULL ) { } else { while ( em != NULL ) { struct port_in * pnew = AllocPort( NULL ) ; if ( pnew == NULL ) { break ; } if ( GOOD(OWServer_Enet_setup( em->name, em->version, pnew )) ) { // Add the device, but no need to check for bad match Add_InFlight( NULL, pnew ) ; } em = em->next ; } } enet_list_kill( &elist ) ; MONITOR_RUNLOCK ; }
static int tcpsock_listen(Socket *sock, int backlog) { TCPSocket *tcpsock = (TCPSocket*) sock; semWait(&tcpsock->lock); if (tcpsock->state != TCP_CLOSED) { semSignal(&tcpsock->lock); ERRNO = EALREADY; return -1; }; if (sock->domain == AF_INET) { struct sockaddr_in *inname = (struct sockaddr_in*) &tcpsock->sockname; if (inname->sin_family == AF_UNSPEC) { inname->sin_family = AF_INET; memset(&inname->sin_addr, 0, 4); inname->sin_port = AllocPort(); }; } else { struct sockaddr_in6 *inname = (struct sockaddr_in6*) &tcpsock->sockname; if (inname->sin6_family == AF_UNSPEC) { inname->sin6_family = AF_INET6; memset(&inname->sin6_addr, 0, 16); inname->sin6_port = AllocPort(); }; }; tcpsock->state = TCP_LISTENING; semSignal(&tcpsock->lock); return 0; };
static int tcpsock_connect(Socket *sock, const struct sockaddr *addr, size_t size) { TCPSocket *tcpsock = (TCPSocket*) sock; if (addr->sa_family != sock->domain) { ERRNO = EAFNOSUPPORT; return -1; }; if (size != INET_SOCKADDR_LEN) { ERRNO = EAFNOSUPPORT; return -1; }; semWait(&tcpsock->lock); if (tcpsock->state == TCP_CONNECTING) { semSignal(&tcpsock->lock); ERRNO = EALREADY; return -1; }; if (tcpsock->state != TCP_CLOSED) { semSignal(&tcpsock->lock); ERRNO = EISCONN; return -1; }; uint16_t srcport, dstport; if (sock->domain == AF_INET) { const struct sockaddr_in *inaddr = (const struct sockaddr_in*) addr; struct sockaddr_in *inname = (struct sockaddr_in*) &tcpsock->sockname; if (inname->sin_family == AF_UNSPEC) { inname->sin_family = AF_INET; getDefaultAddr4(&inname->sin_addr, &inaddr->sin_addr, sock->ifname); inname->sin_port = AllocPort(); srcport = inname->sin_port; } else { srcport = inname->sin_port; }; dstport = inaddr->sin_port; } else { const struct sockaddr_in6 *inaddr = (const struct sockaddr_in6*) addr; struct sockaddr_in6 *inname = (struct sockaddr_in6*) &tcpsock->sockname; if (inname->sin6_family == AF_UNSPEC) { inname->sin6_family = AF_INET6; getDefaultAddr6(&inname->sin6_addr, &inaddr->sin6_addr, sock->ifname); inname->sin6_port = AllocPort(); srcport = inname->sin6_port; } else { srcport = inname->sin6_port; } dstport = inaddr->sin6_port; }; memcpy(&tcpsock->peername, addr, INET_SOCKADDR_LEN); tcpsock->state = TCP_CONNECTING; tcpsock->nextSeqNo = (uint32_t) getRandom(); tcpsock->currentOut = CreateOutbound(&tcpsock->sockname, &tcpsock->peername, 0); tcpsock->expectedAck = tcpsock->nextSeqNo; TCPSegment *syn = tcpsock->currentOut->segment; syn->srcport = srcport; syn->dstport = dstport; syn->seqno = htonl(tcpsock->nextSeqNo-1); syn->dataOffsetNS = 0x50; syn->flags = TCP_SYN; syn->winsz = htons(TCP_BUFFER_SIZE); ChecksumOutbound(tcpsock->currentOut); KernelThreadParams pars; memset(&pars, 0, sizeof(KernelThreadParams)); pars.stackSize = DEFAULT_STACK_SIZE; pars.name = "TCP Thread"; tcpsock->thread = CreateKernelThread(tcpThread, &pars, tcpsock); semSignal(&tcpsock->lock); uint8_t bitmap = 0; Semaphore *semConn = &tcpsock->semConnected; if (semPoll(1, &semConn, &bitmap, SEM_W_FILE(sock->fp->oflags), 0) == 0) { // we caught an interrupt before the connection was completed ERRNO = EINPROGRESS; return -1; } else { // report the error synchronously if there is one; otherwise success if (tcpsock->sockErr == 0) { return 0; } else { ERRNO = tcpsock->sockErr; return -1; }; }; };