1997-12-21 12:11:13 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
1997-12-21 12:11:13 +00:00
|
|
|
* Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1997-10-26 01:04:02 +00:00
|
|
|
*/
|
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <errno.h>
|
2002-08-27 20:11:58 +00:00
|
|
|
#include <stdarg.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <termios.h>
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <unistd.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "log.h"
|
1998-02-09 19:24:03 +00:00
|
|
|
#include "descriptor.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "server.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "prompt.h"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ncpaddr.h"
|
2001-08-18 19:16:56 +00:00
|
|
|
#include "probe.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
static int
|
2000-03-14 01:46:09 +00:00
|
|
|
server_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
|
1998-02-09 19:24:03 +00:00
|
|
|
{
|
|
|
|
struct server *s = descriptor2server(d);
|
1998-05-23 22:24:50 +00:00
|
|
|
struct prompt *p;
|
|
|
|
int sets;
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
sets = 0;
|
1998-02-13 05:10:26 +00:00
|
|
|
if (r && s->fd >= 0) {
|
1998-02-09 19:24:03 +00:00
|
|
|
if (*n < s->fd + 1)
|
|
|
|
*n = s->fd + 1;
|
1998-02-10 03:23:50 +00:00
|
|
|
FD_SET(s->fd, r);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "server: fdset(r) %d\n", s->fd);
|
1998-05-23 22:24:50 +00:00
|
|
|
sets++;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
for (p = log_PromptList(); p; p = p->next)
|
|
|
|
sets += descriptor_UpdateSet(&p->desc, r, w, e, n);
|
|
|
|
|
|
|
|
return sets;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
static int
|
2000-03-14 01:46:09 +00:00
|
|
|
server_IsSet(struct fdescriptor *d, const fd_set *fdset)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1998-02-09 19:24:03 +00:00
|
|
|
struct server *s = descriptor2server(d);
|
1998-05-23 22:24:50 +00:00
|
|
|
struct prompt *p;
|
|
|
|
|
|
|
|
if (s->fd >= 0 && FD_ISSET(s->fd, fdset))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (p = log_PromptList(); p; p = p->next)
|
|
|
|
if (descriptor_IsSet(&p->desc, fdset))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
static void
|
2000-03-14 01:46:09 +00:00
|
|
|
server_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
|
1998-02-09 19:24:03 +00:00
|
|
|
{
|
|
|
|
struct server *s = descriptor2server(d);
|
2001-08-14 16:05:52 +00:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)&ss;
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
|
|
|
#ifndef NOINET6
|
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
|
|
|
#endif
|
|
|
|
int ssize = sizeof ss, wfd;
|
1998-04-03 19:26:02 +00:00
|
|
|
struct prompt *p;
|
2001-08-14 16:05:52 +00:00
|
|
|
struct ncpaddr addr;
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) {
|
|
|
|
wfd = accept(s->fd, sa, &ssize);
|
|
|
|
if (wfd < 0)
|
|
|
|
log_Printf(LogERROR, "server_Read: accept(): %s\n", strerror(errno));
|
2001-02-13 12:33:34 +00:00
|
|
|
else if (sa->sa_len == 0) {
|
|
|
|
close(wfd);
|
|
|
|
wfd = -1;
|
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
} else
|
|
|
|
wfd = -1;
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
if (wfd >= 0)
|
1998-04-03 19:26:02 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_LOCAL:
|
1998-05-23 22:24:50 +00:00
|
|
|
log_Printf(LogPHASE, "Connected to local client.\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
break;
|
1998-05-23 22:24:50 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
case AF_INET:
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_setsa(&addr, sa);
|
|
|
|
if (ntohs(sin->sin_port) < 1024) {
|
|
|
|
log_Printf(LogALERT, "Rejected client connection from %s:%u"
|
|
|
|
"(invalid port number) !\n",
|
|
|
|
ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
|
|
|
|
close(wfd);
|
|
|
|
wfd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
log_Printf(LogPHASE, "Connected to client from %s:%u\n",
|
|
|
|
ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifndef NOINET6
|
|
|
|
case AF_INET6:
|
|
|
|
ncpaddr_setsa(&addr, sa);
|
|
|
|
if (ntohs(sin6->sin6_port) < 1024) {
|
1998-05-23 22:24:50 +00:00
|
|
|
log_Printf(LogALERT, "Rejected client connection from %s:%u"
|
|
|
|
"(invalid port number) !\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
|
1998-05-23 22:24:50 +00:00
|
|
|
close(wfd);
|
|
|
|
wfd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
log_Printf(LogPHASE, "Connected to client from %s:%u\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
|
1998-05-23 22:24:50 +00:00
|
|
|
break;
|
2001-08-14 16:05:52 +00:00
|
|
|
#endif
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
write(wfd, "Unrecognised access !\n", 22);
|
|
|
|
close(wfd);
|
|
|
|
wfd = -1;
|
1998-04-03 19:26:02 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
if (wfd >= 0) {
|
|
|
|
if ((p = prompt_Create(s, bundle, wfd)) == NULL) {
|
|
|
|
write(wfd, "Connection refused.\n", 20);
|
|
|
|
close(wfd);
|
|
|
|
} else {
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_LOCAL:
|
|
|
|
p->src.type = "local";
|
2001-01-26 01:41:34 +00:00
|
|
|
strncpy(p->src.from, s->cfg.sockname, sizeof p->src.from - 1);
|
1998-05-23 22:24:50 +00:00
|
|
|
p->src.from[sizeof p->src.from - 1] = '\0';
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
2001-08-14 16:05:52 +00:00
|
|
|
p->src.type = "ip";
|
1998-05-23 22:24:50 +00:00
|
|
|
snprintf(p->src.from, sizeof p->src.from, "%s:%u",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
|
1998-05-23 22:24:50 +00:00
|
|
|
break;
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
case AF_INET6:
|
|
|
|
p->src.type = "ip6";
|
|
|
|
snprintf(p->src.from, sizeof p->src.from, "%s:%u",
|
|
|
|
ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
|
|
|
|
break;
|
|
|
|
#endif
|
1998-05-23 22:24:50 +00:00
|
|
|
}
|
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
prompt_Required(p);
|
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
|
1998-08-02 13:01:16 +00:00
|
|
|
log_PromptListChanged = 0;
|
1998-05-23 22:24:50 +00:00
|
|
|
for (p = log_PromptList(); p; p = p->next)
|
1998-08-02 13:01:16 +00:00
|
|
|
if (descriptor_IsSet(&p->desc, fdset)) {
|
1998-05-23 22:24:50 +00:00
|
|
|
descriptor_Read(&p->desc, bundle, fdset);
|
1998-08-02 13:01:16 +00:00
|
|
|
if (log_PromptListChanged)
|
|
|
|
break;
|
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
|
|
|
|
1998-06-24 19:33:36 +00:00
|
|
|
static int
|
2004-09-05 01:46:52 +00:00
|
|
|
server_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
|
|
|
|
const fd_set *fdset __unused)
|
1998-02-09 19:24:03 +00:00
|
|
|
{
|
|
|
|
/* We never want to write here ! */
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogALERT, "server_Write: Internal error: Bad call !\n");
|
1998-06-24 19:33:36 +00:00
|
|
|
return 0;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct server server = {
|
|
|
|
{
|
|
|
|
SERVER_DESCRIPTOR,
|
|
|
|
server_UpdateSet,
|
|
|
|
server_IsSet,
|
|
|
|
server_Read,
|
|
|
|
server_Write
|
|
|
|
},
|
2004-09-05 01:46:52 +00:00
|
|
|
-1,
|
|
|
|
{ "", "", 0, 0 }
|
1998-02-09 19:24:03 +00:00
|
|
|
};
|
1998-01-29 00:44:16 +00:00
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
enum server_stat
|
|
|
|
server_Reopen(struct bundle *bundle)
|
|
|
|
{
|
|
|
|
char name[sizeof server.cfg.sockname];
|
2001-01-29 01:35:06 +00:00
|
|
|
struct stat st;
|
2001-01-26 01:41:34 +00:00
|
|
|
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);
|
2001-01-29 01:35:06 +00:00
|
|
|
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;
|
2001-01-26 01:41:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum server_stat
|
1998-05-01 19:26:12 +00:00
|
|
|
server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
2001-01-26 01:41:34 +00:00
|
|
|
struct sockaddr_un ifsun;
|
|
|
|
mode_t oldmask;
|
1998-01-29 00:44:16 +00:00
|
|
|
int s;
|
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
oldmask = (mode_t)-1; /* Silence compiler */
|
|
|
|
|
2015-01-28 21:33:49 +00:00
|
|
|
if (server.cfg.sockname[0] != '\0' && !strcmp(server.cfg.sockname, name))
|
2001-01-26 01:41:34 +00:00
|
|
|
server_Close(bundle);
|
1998-04-10 13:19:23 +00:00
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
memset(&ifsun, '\0', sizeof ifsun);
|
|
|
|
ifsun.sun_len = strlen(name);
|
|
|
|
if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Local: %s: Path too long\n", name);
|
2001-01-26 01:41:34 +00:00
|
|
|
return SERVER_INVALID;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
2001-01-26 01:41:34 +00:00
|
|
|
ifsun.sun_family = AF_LOCAL;
|
|
|
|
strcpy(ifsun.sun_path, name);
|
1997-06-25 19:30:05 +00:00
|
|
|
|
2002-01-16 14:03:52 +00:00
|
|
|
s = socket(PF_LOCAL, SOCK_STREAM, 0);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (s < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno));
|
2001-01-26 01:41:34 +00:00
|
|
|
goto failed;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
2001-01-26 01:41:34 +00:00
|
|
|
oldmask = umask(mask);
|
|
|
|
if (bind(s, (struct sockaddr *)&ifsun, sizeof ifsun) < 0) {
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
2001-01-26 01:41:34 +00:00
|
|
|
umask(oldmask);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
2001-01-26 01:41:34 +00:00
|
|
|
goto failed;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
2001-01-26 01:41:34 +00:00
|
|
|
umask(oldmask);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (listen(s, 5) != 0) {
|
2000-10-30 00:15:29 +00:00
|
|
|
log_Printf(LogERROR, "Local: Unable to listen to socket -"
|
|
|
|
" BUNDLE overload?\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
2001-01-26 01:41:34 +00:00
|
|
|
unlink(name);
|
|
|
|
goto failed;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1999-03-08 22:35:19 +00:00
|
|
|
server_Close(bundle);
|
1998-02-09 19:24:03 +00:00
|
|
|
server.fd = s;
|
2001-01-26 01:41:34 +00:00
|
|
|
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;
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogPHASE, "Listening at local socket %s.\n", name);
|
2001-01-26 01:41:34 +00:00
|
|
|
|
|
|
|
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;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
enum server_stat
|
|
|
|
server_TcpOpen(struct bundle *bundle, u_short port)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
2001-08-14 16:05:52 +00:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
2001-08-18 19:16:56 +00:00
|
|
|
#ifndef NOINET6
|
2001-08-14 16:05:52 +00:00
|
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
|
|
|
#endif
|
|
|
|
int s, sz;
|
1997-06-25 19:30:05 +00:00
|
|
|
|
2001-01-26 01:41:34 +00:00
|
|
|
if (server.cfg.port == port)
|
|
|
|
server_Close(bundle);
|
|
|
|
|
|
|
|
if (port == 0)
|
|
|
|
return SERVER_INVALID;
|
1997-09-09 21:51:39 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
memset(&ss, '\0', sizeof ss);
|
2001-08-18 19:16:56 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
if (probe.ipv6_available) {
|
|
|
|
sin6->sin6_family = AF_INET6;
|
|
|
|
sin6->sin6_port = htons(port);
|
2001-08-19 22:23:28 +00:00
|
|
|
sin6->sin6_len = (u_int8_t)sizeof ss;
|
2001-08-18 19:16:56 +00:00
|
|
|
sz = sizeof *sin6;
|
2002-01-16 14:03:52 +00:00
|
|
|
s = socket(PF_INET6, SOCK_STREAM, 0);
|
2001-08-18 19:16:56 +00:00
|
|
|
} else
|
2001-08-14 16:05:52 +00:00
|
|
|
#endif
|
2001-08-18 19:16:56 +00:00
|
|
|
{
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_port = htons(port);
|
2001-08-19 22:23:28 +00:00
|
|
|
sin->sin_len = (u_int8_t)sizeof ss;
|
2001-08-18 19:16:56 +00:00
|
|
|
sin->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
sz = sizeof *sin;
|
2002-01-16 14:03:52 +00:00
|
|
|
s = socket(PF_INET, SOCK_STREAM, 0);
|
2001-08-18 19:16:56 +00:00
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
if (s < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
|
2001-01-26 01:41:34 +00:00
|
|
|
goto failed;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
2001-08-14 16:05:52 +00:00
|
|
|
|
2006-09-18 03:50:30 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
if (probe.ipv6_available) {
|
|
|
|
int off = 0;
|
|
|
|
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
|
2001-08-14 16:05:52 +00:00
|
|
|
if (bind(s, (struct sockaddr *)&ss, sz) < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Tcp: bind: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
2001-01-26 01:41:34 +00:00
|
|
|
goto failed;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
if (listen(s, 5) != 0) {
|
1999-03-08 22:35:19 +00:00
|
|
|
log_Printf(LogERROR, "Tcp: Unable to listen to socket: %s\n",
|
|
|
|
strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
2001-01-26 01:41:34 +00:00
|
|
|
goto failed;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
server_Close(bundle);
|
1998-02-09 19:24:03 +00:00
|
|
|
server.fd = s;
|
2001-01-26 01:41:34 +00:00
|
|
|
server.cfg.port = port;
|
|
|
|
*server.cfg.sockname = '\0';
|
|
|
|
server.cfg.mask = 0;
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogPHASE, "Listening at port %d.\n", port);
|
2001-01-26 01:41:34 +00:00
|
|
|
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;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
int
|
2004-09-05 01:46:52 +00:00
|
|
|
server_Close(struct bundle *bundle __unused)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1998-02-09 19:24:03 +00:00
|
|
|
if (server.fd >= 0) {
|
2001-01-26 01:41:34 +00:00
|
|
|
if (*server.cfg.sockname != '\0') {
|
1999-03-08 22:35:19 +00:00
|
|
|
struct sockaddr_un un;
|
|
|
|
int sz = sizeof un;
|
|
|
|
|
|
|
|
if (getsockname(server.fd, (struct sockaddr *)&un, &sz) == 0 &&
|
|
|
|
un.sun_family == AF_LOCAL && sz == sizeof un)
|
2001-01-26 01:41:34 +00:00
|
|
|
unlink(un.sun_path);
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
1999-03-08 22:35:19 +00:00
|
|
|
close(server.fd);
|
1998-02-09 19:24:03 +00:00
|
|
|
server.fd = -1;
|
1998-04-03 19:26:02 +00:00
|
|
|
/* Drop associated prompts */
|
1998-05-23 22:24:50 +00:00
|
|
|
log_DestroyPrompts(&server);
|
2001-01-26 01:41:34 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
return 1;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
2001-01-26 01:41:34 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
return 0;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
2001-01-26 01:41:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|