// // Main // int main(int argc, char *argv[]) { int lfd, i, opt, len; FD_ZERO(&s_fds); fd_max = 0; while(-1 != (opt = getopt_long(argc, argv, "hp:r:", long_opts, &i)) ) { switch (opt) { case 'h': usage(argv[0]); break; case 'p': // open ports if ((atoi(optarg) > 65535) || (atoi (optarg) < 1)) { fprintf(stderr, "bad port number: (1-65535)\n"); exit(0); } lfd = getSocket(optarg); // getSocket handles errors internally printf(" - lfd = %d fd_max = %d\n", lfd, fd_max); FD_SET(lfd, &s_fds); break; case 'r': // set root exit_msg(access(optarg, F_OK) , "Root directory not found"); server_root = optarg; break; default: fprintf(stderr, "Invalid option.\n"); usage(argv[0]); break; } } // while for long_opts if (fd_max == 0) { lfd = getSocket(DEFAULT_PORT); FD_SET(lfd, &s_fds); } // for consistency ensure server root ends with '/' len = strlen(server_root); if (server_root[len-1] != '/') strncat(server_root, "/", len); printf("Starting web server at %s \n", server_root); wait_connect( ); return 0; }
int mail_tcp_connect_with_local_address_timeout(const char * server, uint16_t port, const char * local_address, uint16_t local_port, time_t timeout) { #ifndef HAVE_IPV6 struct hostent * remotehost; struct sockaddr_in sa; #else /* HAVE_IPV6 */ struct addrinfo hints, *res, *ai; struct addrinfo la_hints; char port_str[6]; #endif #ifdef WIN32 SOCKET s; long r; #else int s; int r; if ('/' == server[0]) return mail_unix_connect_socket(server); #endif #ifndef HAVE_IPV6 s = socket(PF_INET, SOCK_STREAM, 0); if (s == -1) goto err; if ((local_address != NULL) || (local_port != 0)) { struct sockaddr_in la; la.sin_family = AF_INET; la.sin_port = htons(local_port); if (local_address == NULL) { la.sin_addr.s_addr = htonl(INADDR_ANY); } r = inet_aton(local_address, &la.sin_addr); if (r == 0) goto close_socket; r = bind(s, (struct sockaddr *) &la, sizeof(struct sockaddr_in)); if (r == -1) goto close_socket; } remotehost = gethostbyname(server); if (remotehost == NULL) goto close_socket; sa.sin_family = AF_INET; sa.sin_port = htons(port); memcpy(&sa.sin_addr, remotehost->h_addr, remotehost->h_length); r = prepare_fd(s); if (r == -1) { goto close_socket; } r = connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)); r = wait_connect(s, r, timeout); if (r == -1) { goto close_socket; } #else /* HAVE_IPV6 */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; memset(&la_hints, 0, sizeof(la_hints)); la_hints.ai_family = AF_UNSPEC; la_hints.ai_socktype = SOCK_STREAM; la_hints.ai_flags = AI_PASSIVE; /* convert port from integer to string. */ snprintf(port_str, sizeof(port_str), "%d", port); res = NULL; if (getaddrinfo(server, port_str, &hints, &res) != 0) goto err; s = -1; for (ai = res; ai != NULL; ai = ai->ai_next) { s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (s == -1) continue; // Christopher Lyon Anderson - prevent SigPipe #ifdef SO_NOSIGPIPE int kOne = 1; int err = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &kOne, sizeof(kOne)); if (err != 0) continue; #endif if ((local_address != NULL) || (local_port != 0)) { char local_port_str[6]; char * p_local_port_str; struct addrinfo * la_res; if (local_port != 0) { snprintf(local_port_str, sizeof(local_port_str), "%d", local_port); p_local_port_str = local_port_str; } else { p_local_port_str = NULL; } la_res = NULL; r = getaddrinfo(local_address, p_local_port_str, &la_hints, &la_res); if (r != 0) goto close_socket; r = bind(s, (struct sockaddr *) la_res->ai_addr, la_res->ai_addrlen); if (la_res != NULL) freeaddrinfo(la_res); if (r == -1) goto close_socket; } r = prepare_fd(s); if (r == -1) { goto close_socket; } r = connect(s, ai->ai_addr, ai->ai_addrlen); r = wait_connect(s, r, timeout); if (r != -1) { r = verify_sock_errors(s); } if (r == -1) { if (ai->ai_next) { #ifdef WIN32 closesocket(s); #else close(s); #endif continue; } else { goto close_socket; } } /* if we're here, we're good */ break; } if (res != NULL) freeaddrinfo(res); if (ai == NULL) goto err; #endif return s; close_socket: #ifdef HAVE_IPV6 if (res != NULL) freeaddrinfo(res); #endif #ifdef WIN32 closesocket(s); #else close(s); #endif err: return -1; }
static int wwwconnect(const char *server, const char *proxy, int pport, char *ip, const char *localip, int ctimeout, struct mirdat *mdat, int logerr, unsigned int can_whitelist) { int socketfd, port, ret; unsigned int ips = 0, ignored = 0; #ifdef HAVE_GETADDRINFO struct addrinfo hints, *res = NULL, *rp, *loadbal_rp = NULL; char port_s[6], loadbal_ipaddr[46]; uint32_t loadbal = 1, minsucc = 0xffffffff, minfail = 0xffffffff; struct mirdat_ip *md; #else struct sockaddr_in name; struct hostent *host; unsigned char *ia; int i; #endif char ipaddr[46]; const char *hostpt; if(ip) strcpy(ip, "???"); if(proxy) { hostpt = proxy; if(!(port = pport)) { const struct servent *webcache = getservbyname("webcache", "TCP"); if(webcache) port = ntohs(webcache->s_port); else port = 8080; #ifndef C_WINDOWS endservent(); #endif } } else { hostpt = server; port = 80; } #ifdef HAVE_GETADDRINFO memset(&hints, 0, sizeof(hints)); #ifdef SUPPORT_IPv6 hints.ai_family = AF_UNSPEC; #else hints.ai_family = AF_INET; #endif hints.ai_socktype = SOCK_STREAM; snprintf(port_s, sizeof(port_s), "%d", port); port_s[sizeof(port_s) - 1] = 0; ret = getaddrinfo(hostpt, port_s, &hints, &res); if(ret) { logg("%cCan't get information about %s: %s\n", logerr ? '!' : '^', hostpt, gai_strerror(ret)); return -1; } for(rp = res; rp; rp = rp->ai_next) { void *addr; ips++; #ifdef SUPPORT_IPv6 if(rp->ai_family == AF_INET6) addr = &((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr; else #endif addr = &((struct sockaddr_in *) rp->ai_addr)->sin_addr; if(!inet_ntop(rp->ai_family, addr, ipaddr, sizeof(ipaddr))) { logg("%cinet_ntop() failed\n", logerr ? '!' : '^'); freeaddrinfo(res); return -1; } if(mdat && (ret = mirman_check(addr, rp->ai_family, mdat, &md))) { if(ret == 1) logg("*Ignoring mirror %s (due to previous errors)\n", ipaddr); else logg("*Ignoring mirror %s (has connected too many times with an outdated version)\n", ipaddr); ignored++; if(!loadbal || rp->ai_next) continue; } if(mdat && loadbal) { if(!ret) { if(!md) { loadbal_rp = rp; strncpy(loadbal_ipaddr, ipaddr, sizeof(loadbal_ipaddr)); } else { if(md->succ < minsucc && md->fail <= minfail) { minsucc = md->succ; minfail = md->fail; loadbal_rp = rp; strncpy(loadbal_ipaddr, ipaddr, sizeof(loadbal_ipaddr)); } if(rp->ai_next) continue; } } if(!loadbal_rp) { if(!rp->ai_next) { loadbal = 0; rp = res; } continue; } rp = loadbal_rp; strncpy(ipaddr, loadbal_ipaddr, sizeof(ipaddr)); } else if(loadbal_rp == rp) { continue; } if(ip) strcpy(ip, ipaddr); if(rp != res) logg("Trying host %s (%s)...\n", hostpt, ipaddr); socketfd = getclientsock(localip, rp->ai_family); if(socketfd < 0) { freeaddrinfo(res); return -1; } #ifdef SO_ERROR if(wait_connect(socketfd, rp->ai_addr, rp->ai_addrlen, ctimeout) == -1) { #else if(connect(socketfd, rp->ai_addr, rp->ai_addrlen) == -1) { #endif logg("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr); closesocket(socketfd); if(loadbal) { loadbal = 0; rp = res; } continue; } else { if(mdat) { if(rp->ai_family == AF_INET) mdat->currip[0] = *((uint32_t *) addr); else memcpy(mdat->currip, addr, 4 * sizeof(uint32_t)); mdat->af = rp->ai_family; } freeaddrinfo(res); return socketfd; } } freeaddrinfo(res); #else /* IPv4 */ if((host = gethostbyname(hostpt)) == NULL) { logg("%cCan't get information about %s: %s\n", logerr ? '!' : '^', hostpt, ghbn_err(h_errno)); return -1; } for(i = 0; host->h_addr_list[i] != 0; i++) { /* this dirty hack comes from pink - Nosuid TCP/IP ping 1.6 */ ia = (unsigned char *) host->h_addr_list[i]; sprintf(ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]); ips++; if(mdat && (ret = mirman_check(&((struct in_addr *) ia)->s_addr, AF_INET, mdat, NULL))) { if(ret == 1) logg("*Ignoring mirror %s (due to previous errors)\n", ipaddr); else logg("*Ignoring mirror %s (has connected too many times with an outdated version)\n", ipaddr); ignored++; continue; } if(ip) strcpy(ip, ipaddr); if(i > 0) logg("Trying host %s (%s)...\n", hostpt, ipaddr); memset ((char *) &name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_addr = *((struct in_addr *) host->h_addr_list[i]); name.sin_port = htons(port); socketfd = getclientsock(localip, AF_INET); if(socketfd < 0) return -1; #ifdef SO_ERROR if(wait_connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in), ctimeout) == -1) { #else if(connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) == -1) { #endif logg("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr); closesocket(socketfd); continue; } else { if(mdat) { mdat->currip[0] = ((struct in_addr *) ia)->s_addr; mdat->af = AF_INET; } return socketfd; } } #endif if(mdat && can_whitelist && ips && (ips == ignored)) mirman_whitelist(mdat, 1); return -2; } static const char *readbline(int fd, char *buf, int bufsize, int filesize, int *bread) { char *pt; int ret, end; if(!*bread) { if(bufsize < filesize) lseek(fd, -bufsize, SEEK_END); *bread = read(fd, buf, bufsize - 1); if(!*bread || *bread == -1) return NULL; buf[*bread] = 0; } pt = strrchr(buf, '\n'); if(!pt) return NULL; *pt = 0; pt = strrchr(buf, '\n'); if(pt) { return ++pt; } else if(*bread == filesize) { return buf; } else { *bread -= strlen(buf) + 1; end = filesize - *bread; if(end < bufsize) { if((ret = lseek(fd, 0, SEEK_SET)) != -1) ret = read(fd, buf, end); } else { if((ret = lseek(fd, end - bufsize, SEEK_SET)) != -1) ret = read(fd, buf, bufsize - 1); } if(!ret || ret == -1) return NULL; buf[ret] = 0; *bread += ret; pt = strrchr(buf, '\n'); if(!pt) return buf; *pt = 0; pt = strrchr(buf, '\n'); if(pt) return ++pt; else if(strlen(buf)) return buf; else return NULL; } } static unsigned int fmt_base64(char *dest, const char *src, unsigned int len) { unsigned short bits = 0,temp = 0; unsigned long written = 0; unsigned int i; const char base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; for(i = 0; i < len; i++) { temp <<= 8; temp += src[i]; bits += 8; while(bits > 6) { dest[written] = base64[((temp >> (bits - 6)) & 63)]; written++; bits -= 6; } } if(bits) { temp <<= (6 - bits); dest[written] = base64[temp & 63]; written++; } while(written & 3) { dest[written] = '='; written++; } return written; }
void new_connect(t_server *server) { wait_connect(server); server->connection++; printf("connected\n"); }
static int ConnectionObject_init(ConnectionObject *self, PyObject *args, PyObject *kwargs) { drizzle_con_st *con; //Not support PyObject *ssl = NULL; char *host = DRIZZLE_DEFAULT_TCP_HOST; char *user = NULL, *passwd = NULL, *db = NULL, *unix_socket = NULL; unsigned int port = 3306; unsigned int client_flag = 0; int connect_timeout = 0; static char *kwlist[] = { "host", "user", "passwd", "db", "port", "unix_socket", "connect_timeout", NULL } ; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisi:connect", kwlist, &host, &user, &passwd, &db, &port, &unix_socket, &connect_timeout )){ return -1; } DEBUG("init %p", self); //set NON_BLOKING /* drizzle_add_options(self->drizzle, DRIZZLE_NON_BLOCKING); */ Py_BEGIN_ALLOW_THREADS ; if (connect_timeout) { drizzle_set_timeout(drizzle, connect_timeout); DEBUG("set timeout %d", connect_timeout); } DEBUG("host %s", host); DEBUG("port %d", port); DEBUG("user %s", user); DEBUG("passwd %s", passwd); DEBUG("db %s", db); if (!unix_socket) { con = drizzle_con_add_tcp(drizzle, NULL, host, port, user, passwd, db, DRIZZLE_CON_MYSQL); } else { con = drizzle_con_add_uds(drizzle, NULL, unix_socket, user, passwd, db, DRIZZLE_CON_MYSQL); } Py_END_ALLOW_THREADS; DEBUG("created drizzle conn %p", con); DEBUG("connect %p", con); if (!con) { PyErr_SetString(PyExc_IOError, drizzle_error(drizzle)); RDEBUG("drizzle connection failed"); return -1; } self->con = con; if (wait_connect(con) == -1) { return -1; } return 0; }
int main (int argc, char *argv[]) { struct sockaddr_in servaddr; int port = 7777; int c; int err; if ((err = ossmix_init ()) < 0) { fprintf (stderr, "ossmix_init() failed, err=%d\n", err); exit (EXIT_FAILURE); } if ((err = ossmix_connect (NULL, 0)) < 0) /* Force local connection */ { fprintf (stderr, "ossmix_connect() failed, err=%d\n", err); exit (EXIT_FAILURE); } while ((c = getopt (argc, argv, "vp:")) != EOF) { switch (c) { case 'p': /* TCP/IP port */ port = atoi (optarg); if (port <= 0) port = 9876; break; case 'v': /* Verbose */ verbose++; break; } } printf ("Listening socket %d\n", port); if ((listenfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror ("socket"); exit (-1); } memset (&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl (INADDR_ANY); servaddr.sin_port = htons (port); if (bind (listenfd, (struct sockaddr *) &servaddr, sizeof (servaddr)) == -1) { perror ("bind"); exit (-1); } while (1) { if (!wait_connect ()) exit (-1); handle_connection (connfd); close (connfd); } // close (listenfd); /* Not reached */ exit (0); }