/* * Accept a new Telnet connection. */ static void telnet_accept(void *vp) { ndesc_t *nd = vp; int addrlen, s; struct sockaddr_in addr; telnet_t *tp; void *ip; nd_enable(nd, ND_R); addrlen = sizeof (addr); s = accept(nd_fd(nd), (struct sockaddr *)&addr, &addrlen); if (s == -1) { switch (errno) { default: warning("telnet_accept: accept() errno = %d. ip: %s\n", errno, inet_ntoa(addr.sin_addr)); case EWOULDBLOCK: case EINTR: case EPROTO: return; } } enable_nbio(s); enable_oobinline(s); enable_nodelay(s); enable_lowdelay(s); enable_keepalive(s); set_rcvsize(s, TELNET_RCVBUF_SIZE); set_sndsize(s, TELNET_SNDBUF_SIZE); tp = telnet_alloc(); tp->t_nd = nd_attach(s, telnet_read, telnet_write, telnet_exception, NULL, telnet_shutdown, tp); ip = (void *)new_player(tp, &addr, addrlen); if (ip == NULL) { telnet_shutdown(tp->t_nd, tp); } else { telnet_attach(tp, ip); nd_enable(tp->t_nd, ND_R | ND_X); } }
/* * Initialize the UDP Manager. */ udpsvc_t * udpsvc_init(int port) { int s; struct sockaddr_in addr; struct udpsvc_t *svc; s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (s == -1) fatal("udp_init: socket() error = %d.\n", errno); enable_reuseaddr(s); memset(&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; addr.sin_port = htons((u_short)port); addr.sin_addr.s_addr = INADDR_ANY; if (bind(s, (struct sockaddr *)&addr, sizeof (addr)) == -1) { if (errno == EADDRINUSE) { (void)fprintf(stderr, "UDP Socket already bound!\n"); debug_message("UDP Socket already bound!\n"); (void)close(s); return 0; } else { fatal("udp_init: bind() error = %d.\n", errno); } } enable_nbio(s); svc = xalloc(sizeof(*udpsvc)); svc->nq = nq_alloc(UDPSVC_RAWQ_SIZE); svc->nd = nd_attach(s, udpsvc_read, NULL, NULL, NULL, udpsvc_shutdown, svc); nd_enable(svc->nd, ND_R); return svc; }
/* * Initialize the Telnet server. */ void telnet_init(u_short port) { int s; struct sockaddr_in addr; ndesc_t *nd; s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s == -1) fatal("telnet_init: socket() error = %d.\n", errno); enable_reuseaddr(s); memset(&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; if (bind(s, (struct sockaddr *)&addr, sizeof (addr)) == -1) { if (errno == EADDRINUSE) { (void)fprintf(stderr, "Socket already bound!\n"); debug_message("Socket already bound!\n"); exit(errno); } else { fatal("telnet_init: bind() error = %d.\n", errno); } } if (listen(s, 5) == -1) fatal("telnet_init: listen() error = %d.\n", errno); enable_nbio(s); nd = nd_attach(s, telnet_ready, NULL, NULL, NULL, telnet_shutdown, NULL); nd_enable(nd, ND_R); }