/* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ int x11req(struct ChanSess * chansess) { int fd; if (!svr_pubkey_allows_x11fwd()) { return DROPBEAR_FAILURE; } /* we already have an x11 connection */ if (chansess->x11listener != NULL) { return DROPBEAR_FAILURE; } chansess->x11singleconn = buf_getbool(ses.payload); chansess->x11authprot = buf_getstring(ses.payload, NULL); chansess->x11authcookie = buf_getstring(ses.payload, NULL); chansess->x11screennum = buf_getint(ses.payload); /* create listening socket */ fd = socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { goto fail; } /* allocate port and bind */ chansess->x11port = bindport(fd); if (chansess->x11port < 0) { goto fail; } /* listen */ if (listen(fd, 20) < 0) { goto fail; } /* set non-blocking */ setnonblocking(fd); /* listener code will handle the socket now. * No cleanup handler needed, since listener_remove only happens * from our cleanup anyway */ chansess->x11listener = new_listener( &fd, 1, 0, chansess, x11accept, NULL); if (chansess->x11listener == NULL) { goto fail; } return DROPBEAR_SUCCESS; fail: /* cleanup */ m_free(chansess->x11authprot); m_free(chansess->x11authcookie); close(fd); return DROPBEAR_FAILURE; }
void sv_core_httpd(void) { int sv; if (chdir(MY_BBS_HOME)) return; signal(SIGPIPE, dopipesig); signal(SIGBUS, dosigbus); /* shm_utmp = (struct UTMPFILE *) get_old_shm(UTMP_SHMKEY, sizeof (struct UTMPFILE)); if (shm_utmp == NULL) { printf("shm_utmp error "); exit(-1); } */ if ((sv = bindport(SERVER_PORT)) == -1) return; while (1) { int maxfd; fd_set rfs, wfs; FD_ZERO(&rfs); FD_ZERO(&wfs); FD_SET(sv, &rfs); maxfd = conn_cullselect(&rfs, &wfs); if (sv > maxfd) maxfd = sv; if (select(maxfd + 1, &rfs, &wfs, NULL, NULL) == -1) { if (errno == EINTR) continue; break; } if (FD_ISSET(sv, &rfs)) { int cl; if ((cl = getcl(sv)) == -1) break; if (fcntl(cl, F_SETFL, O_NONBLOCK) >= 0) conn_insert(cl); else close(cl); } conn_upkeep(&rfs, &wfs); } return; }
int SocketDgram::sharedOpen(const Addr& local, int protocolFamily) { bool error = false; if(local == Addr::sapAny) { if(protocolFamily == PF_INET) { if(bindport(this->getHandle(), INADDR_ANY, protocolFamily) == -1) error = true; } } else if(OS::bind(this->getHandle(), reinterpret_cast<sockaddr*> (local.getAddr()), local.getSize()) == -1) error = true; if(error) this->close(); return error ? -1:0; }
static struct descriptor_set listen_on_ifaces(const char* hostnames, struct clientinfo* clntinfo) { char* part =0, *portdel =0; char* hostnames2; size_t hostnames2size; int offset, port, shandle, i; struct descriptor_set d_set; offset = 0; clntinfo->boundsocket_niface = 0; FD_ZERO(&d_set.set); d_set.maxfd = 0; /* we have to make the string suitable for quotstrtok by appending a * WHITESPACE character */ hostnames2size = strlen(hostnames) + 2; hostnames2 = (char*) malloc(hostnames2size); enough_mem(hostnames2); snprintf(hostnames2, hostnames2size, "%s ", hostnames); while ((part = quotstrtok(hostnames2, WHITESPACES, &offset))) { /* do some counting */ clntinfo->boundsocket_niface++; free(part); } clntinfo->boundsocket_list = (int*) malloc(sizeof(int) * clntinfo->boundsocket_niface); enough_mem(clntinfo->boundsocket_list); offset = 0; i = 0; while ((part = quotstrtok(hostnames2, WHITESPACES, &offset))) { portdel = strchr(part, ':'); if (!portdel) { jlog(3, "Invalid IP/Port specification: %s", part); free(part); continue; } *portdel = (char) 0; errno = 0; port = strtol(portdel+1, (char**) 0, 10); if (errno || port > 65535 || port <= 0) { jlog(4, "Invalid port specification: %s. " "Using default value %d", portdel, DEFAULTBINDPORT); port = DEFAULTBINDPORT; } portdel = (char*) 0; jlog(9, "binding to %s, port %d", part, port); shandle = bindport(part, port); free(part); part = (char*) 0; if (shandle < 0) { jlog(8, "Could not bind: %s", strerror(errno)); free(hostnames2); FD_ZERO(&d_set.set); d_set.maxfd = -1; return d_set; } FD_SET(shandle, &d_set.set); d_set.maxfd = MAX_VAL(d_set.maxfd, shandle); clntinfo->boundsocket_list[ i++ ] = shandle; } free(hostnames2); hostnames2 = (char*) 0; if (clntinfo->boundsocket_niface == 0) { jlog(2, "No interfaces found to bind to"); FD_ZERO(&d_set.set); d_set.maxfd = -1; return d_set; } return d_set; }