enum server_stat server_Reopen(struct bundle *bundle) { char name[sizeof server.cfg.sockname]; struct stat st; u_short port; mode_t mask; enum server_stat ret; if (server.cfg.sockname[0] != '\0') { strcpy(name, server.cfg.sockname); mask = server.cfg.mask; server_Close(bundle); if (server.cfg.sockname[0] != '\0' && stat(server.cfg.sockname, &st) == 0) if (!(st.st_mode & S_IFSOCK) || unlink(server.cfg.sockname) != 0) return SERVER_FAILED; ret = server_LocalOpen(bundle, name, mask); } else if (server.cfg.port != 0) { port = server.cfg.port; server_Close(bundle); ret = server_TcpOpen(bundle, port); } else ret = SERVER_UNSET; return ret; }
static void BringDownServer(int signo) { /* Drops all child prompts too ! */ if (server_Close(SignalBundle)) log_Printf(LogPHASE, "Closed server socket\n"); }
void AbortProgram(int excode) { if (SignalBundle) server_Close(SignalBundle); log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode)); if (SignalBundle) { bundle_Close(SignalBundle, NULL, CLOSE_STAYDOWN); bundle_Destroy(SignalBundle); } log_Close(); exit(excode); }
int server_Clear(struct bundle *bundle) { int ret; ret = server_Close(bundle); server.fd = -1; server.cfg.port = 0; *server.cfg.sockname = '\0'; server.cfg.mask = 0; return ret; }
enum server_stat server_TcpOpen(struct bundle *bundle, u_short port) { struct sockaddr_storage ss; struct sockaddr_in *sin = (struct sockaddr_in *)&ss; #ifndef NOINET6 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss; #endif int s, sz; if (server.cfg.port == port) server_Close(bundle); if (port == 0) return SERVER_INVALID; memset(&ss, '\0', sizeof ss); #ifndef NOINET6 if (probe.ipv6_available) { sin6->sin6_family = AF_INET6; sin6->sin6_port = htons(port); sin6->sin6_len = (u_int8_t)sizeof ss; sz = sizeof *sin6; s = socket(PF_INET6, SOCK_STREAM, 0); } else #endif { sin->sin_family = AF_INET; sin->sin_port = htons(port); sin->sin_len = (u_int8_t)sizeof ss; sin->sin_addr.s_addr = INADDR_ANY; sz = sizeof *sin; s = socket(PF_INET, SOCK_STREAM, 0); } if (s < 0) { log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno)); goto failed; } setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s); if (bind(s, (struct sockaddr *)&ss, sz) < 0) { log_Printf(LogWARN, "Tcp: bind: %s\n", strerror(errno)); close(s); goto failed; } if (listen(s, 5) != 0) { log_Printf(LogERROR, "Tcp: Unable to listen to socket: %s\n", strerror(errno)); close(s); goto failed; } server_Close(bundle); server.fd = s; server.cfg.port = port; *server.cfg.sockname = '\0'; server.cfg.mask = 0; log_Printf(LogPHASE, "Listening at port %d.\n", port); return SERVER_OK; failed: if (server.fd == -1) { server.fd = -1; server.cfg.port = port; *server.cfg.sockname = '\0'; server.cfg.mask = 0; } return SERVER_FAILED; }
enum server_stat server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask) { struct sockaddr_un ifsun; mode_t oldmask; int s; oldmask = (mode_t)-1; /* Silence compiler */ if (server.cfg.sockname && !strcmp(server.cfg.sockname, name)) server_Close(bundle); memset(&ifsun, '\0', sizeof ifsun); ifsun.sun_len = strlen(name); if (ifsun.sun_len > sizeof ifsun.sun_path - 1) { log_Printf(LogERROR, "Local: %s: Path too long\n", name); return SERVER_INVALID; } ifsun.sun_family = AF_LOCAL; strcpy(ifsun.sun_path, name); s = socket(PF_LOCAL, SOCK_STREAM, 0); if (s < 0) { log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno)); goto failed; } setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s); if (mask != (mode_t)-1) oldmask = umask(mask); if (bind(s, (struct sockaddr *)&ifsun, sizeof ifsun) < 0) { if (mask != (mode_t)-1) umask(oldmask); log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno)); close(s); goto failed; } if (mask != (mode_t)-1) umask(oldmask); if (listen(s, 5) != 0) { log_Printf(LogERROR, "Local: Unable to listen to socket -" " BUNDLE overload?\n"); close(s); unlink(name); goto failed; } server_Close(bundle); server.fd = s; server.cfg.port = 0; strncpy(server.cfg.sockname, ifsun.sun_path, sizeof server.cfg.sockname - 1); server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0'; server.cfg.mask = mask; log_Printf(LogPHASE, "Listening at local socket %s.\n", name); return SERVER_OK; failed: if (server.fd == -1) { server.fd = -1; server.cfg.port = 0; strncpy(server.cfg.sockname, ifsun.sun_path, sizeof server.cfg.sockname - 1); server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0'; server.cfg.mask = mask; } return SERVER_FAILED; }