static void handlenewconnection(int listenfd, int is_ssl) { struct sockaddr_in their_addr; socklen_t tp = sizeof(struct sockaddr_in); int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp); addconnection(connfd, inet_ntoa(their_addr.sin_addr), is_ssl); }
static void handlenewconnection(int listenfd, int is_ssl) { struct sockaddr_in6 their_addr; socklen_t tp = sizeof(their_addr); char ipbuf[100]; int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp); if (tp == sizeof(struct sockaddr_in6)) inet_ntop(AF_INET6, &their_addr.sin6_addr, ipbuf, sizeof(ipbuf)); else if (tp == sizeof(struct sockaddr_in)) inet_ntop(AF_INET, &(((struct sockaddr_in *)&their_addr)->sin_addr), ipbuf, sizeof(ipbuf)); else *ipbuf = '\0'; if (connfd != -1) /* check for error condition */ addconnection(connfd, ipbuf, is_ssl); }
/* * multilisten * * Await and accept connections from listener applications. * * Parameters: * cmgr : pointer to connectionmgr_t : A pointer to the connection manager * object shared between the talker * and listener threads. * * Return Value: * The function does not return a value. * * Remarks: * */ void multilisten (connectionmgr_t * cmgr) { union sock sock, work, peer; int wsd, sd; socklen_t addlen, peerlen; listenerinfo_t * listenerinfo; pthread_t * listener; pthread_attr_t attr; int threadresult; int so_reuse = 1; time_t now; char buff[BUFSIZ]; int rv; signal (SIGPIPE, SIG_IGN); /* Watch return codes for pipe signal */ rv = pthread_attr_init(&attr); if (rv != 0) { perror ("attr_init"); exit (1); } rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (rv != 0) { perror ("attr_set"); exit (1); } sd = socket (AF_INET,SOCK_STREAM,0); if (sd == -1) { perror ("socket"); exit (1); } bzero ((char *) &sock, sizeof (sock)); sock.i.sin_family = AF_INET; sock.i.sin_port = htons (port); sock.i.sin_addr.s_addr = htonl (INADDR_ANY); setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,(char*)&so_reuse,sizeof(so_reuse)); rv = bind (sd, &(sock.s), sizeof (struct sockaddr)); if (rv == -1) { perror ("bind"); exit (1); } rv = listen (sd,2); if (rv == -1) { perror ("listen"); exit (1); } do { addlen=sizeof(work.s); memset(&work.s,0,addlen); wsd = accept (sd, &(work.s), &addlen); if (wsd == -1 ) { perror("accept"); exit(1); } peerlen = sizeof (struct sockaddr); getpeername (wsd, &(peer.s), &peerlen); time (&now); if (verbose >= 10) printf ("Connection from %s at %s", inet_ntoa (peer.i.sin_addr), ctime (&now)); /* Note that the listener thread proc will delete this memory unless an error occurs before the thread is started */ listenerinfo = (listenerinfo_t *) calloc (1, sizeof (listenerinfo_t)); listenerinfo->conn = newconnection (); if (addconnection (cmgr, listenerinfo->conn) != 0) { destroyconnection (listenerinfo->conn); free (listenerinfo); sprintf (buff, "*** Too many connections\r\n"); write (wsd, buff, strlen (buff)); close (wsd); break; } listenerinfo->cmgr = cmgr; listenerinfo->socketfd = wsd; listener = (pthread_t *) malloc (sizeof (pthread_t)); threadresult = pthread_create (listener, &attr, listenproc, (void *) listenerinfo); } while (1); }
int make_connection(Peer *peerList) { struct addrinfo hints, *res; Peer *elt, *tmp; int fd; char buf[HEADER_SIZE + PAYLOAD_SIZE]; int offset; int len = HEADER_SIZE+NUM_PEERS_SIZE+IP_SIZE+PORT_SIZE+MAC_SIZE+ID_SIZE; LL_FOREACH_SAFE(peerList, elt, tmp) { // make connection memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; // Use IPv4 hints.ai_socktype = SOCK_STREAM; //Use tcp if(getaddrinfo(elt->addr, elt->port, &hints, &res)) { ERROR("error: getaddrinfo\n", -1); } if((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) { ERROR("error: socket\n", -1); } if(connect(fd, res->ai_addr, res->ai_addrlen)) { continue; } // build packet *((uint16_t*)buf) = htons(LINKSTATE); offset = TYPE_SIZE; *((uint16_t*)(buf+offset)) = htons(len); offset += LENGTH_SIZE; *((uint16_t*)(buf+offset)) = htons(1); offset += NUM_PEERS_SIZE; *((uint32_t*)(buf+offset)) = htonl(host->local_ip); offset += IP_SIZE; *((uint16_t*)(buf+offset)) = htons(host->local_port); offset += PORT_SIZE; memcpy(buf+offset, host->local_mac, MAC_SIZE); offset += MAC_SIZE; *((unsigned long long*)(buf+offset)) = timestamp(); // send the new packet on its way writen(fd, buf, len); addconnection(elt); // clean up freeaddrinfo(res); LL_DELETE(peerList, elt); free(elt->addr); free(elt->port); free(elt); }