summaryrefslogtreecommitdiff
path: root/tcpserver.c
diff options
context:
space:
mode:
authorDmitry Bogatov <KAction@debian.org>2018-11-29 05:18:28 +0000
committerDmitry Bogatov <KAction@debian.org>2018-11-29 05:18:28 +0000
commit35c0ccd3b0eedd2d14e26a109f5812a5c30c38aa (patch)
treea1dde23c236db3b8fd525282efac102daa1ae00f /tcpserver.c
parent78fbb41eaf3abb208abe6acfb06db005adeadf35 (diff)
Import Upstream version 0.84
Diffstat (limited to 'tcpserver.c')
-rw-r--r--tcpserver.c69
1 files changed, 50 insertions, 19 deletions
diff --git a/tcpserver.c b/tcpserver.c
index 3aa6f4e..012dc36 100644
--- a/tcpserver.c
+++ b/tcpserver.c
@@ -43,11 +43,17 @@ void drop_nomem()
}
void usage()
{
- if (verbosity) strerr_warn1("\
-tcpserver: usage: \
-tcpserver [ -qQvdDoOpPhHrR1 ] \
-[ -xrules.cdb ] \
-[ -bbacklog ] [ -climit ] [ -ttimeout ] [ -llocalname ] [ -ggid ] [ -uuid ] \
+ strerr_warn1("\
+tcpserver: usage: tcpserver \
+[ -1pPhHrRoOdDqQv ] \
+[ -c limit ] \
+[ -x rules.cdb ] \
+[ -B banner ] \
+[ -g gid ] \
+[ -u uid ] \
+[ -b backlog ] \
+[ -l localname ] \
+[ -t timeout ] \
host port program",0);
_exit(100);
}
@@ -57,13 +63,18 @@ stralloc *sa;
char *s;
{
char ch;
- while (ch = *s++) {
+ int i;
+
+ for (i = 0;i < 100;++i) {
+ ch = s[i];
+ if (!ch) return;
if (ch < 33) ch = '?';
if (ch > 126) ch = '?';
if (ch == '%') ch = '?'; /* logger stupidity */
if (ch == ':') ch = '?';
if (!stralloc_append(sa,&ch)) drop_nomem();
}
+ if (!stralloc_cats(sa,"...")) drop_nomem();
}
char strnum[FMT_ULONG];
@@ -71,6 +82,7 @@ char strnum2[FMT_ULONG];
stralloc tmp = {0};
ipalloc ia = {0};
+unsigned long limit = 40;
unsigned long numchildren = 0;
char tcpremoteip[IPFMT];
@@ -125,12 +137,19 @@ void printenv()
strerr_warn1(tmp.s,0);
}
+void printstatus()
+{
+ if (verbosity < 2) return;
+ strnum[fmt_ulong(strnum,numchildren)] = 0;
+ strnum2[fmt_ulong(strnum2,limit)] = 0;
+ strerr_warn4("tcpserver: status: ",strnum,"/",strnum2,0);
+}
+
void printpid()
{
if (verbosity < 2) return;
strnum[fmt_ulong(strnum,(unsigned long) getpid())] = 0;
- strnum2[fmt_ulong(strnum2,numchildren)] = 0;
- strerr_warn6("tcpserver: pid ",strnum," num ",strnum2," from ",tcpremoteip,0);
+ strerr_warn4("tcpserver: pid ",strnum," from ",tcpremoteip,0);
}
char printbuf[16];
@@ -150,12 +169,12 @@ void sigchld()
int pid;
while ((pid = wait_nohang(&wstat)) > 0) {
- if (numchildren) --numchildren;
if (verbosity >= 2) {
strnum[fmt_ulong(strnum,(unsigned long) pid)] = 0;
strnum2[fmt_ulong(strnum2,(unsigned long) wstat)] = 0;
strerr_warn4("tcpserver: end ",strnum," status ",strnum2,0);
}
+ if (numchildren) --numchildren; printstatus();
}
}
@@ -231,7 +250,8 @@ unsigned long timeout = 26;
unsigned long uid = 0;
unsigned long gid = 0;
char *forcelocal = 0;
-unsigned long limit = 40;
+
+char *banner = "";
void main(argc,argv)
int argc;
@@ -246,11 +266,12 @@ char **argv;
struct servent *se;
int j;
- while ((opt = getopt(argc,argv,"dDvqQhHrR1x:t:u:g:l:b:c:pPoO")) != opteof)
+ while ((opt = getopt(argc,argv,"dDvqQhHrR1x:t:u:g:l:b:B:c:pPoO")) != opteof)
switch(opt) {
case 'b': scan_ulong(optarg,&backlog); break;
case 'c': scan_ulong(optarg,&limit); break;
case 'x': fnrules = optarg; break;
+ case 'B': banner = optarg; break;
case 'd': flagdelay = 1; break;
case 'D': flagdelay = 0; break;
case 'v': verbosity = 2; break;
@@ -294,7 +315,7 @@ char **argv;
else {
se = getservbyname(portname,"tcp");
if (!se) {
- if (verbosity) strerr_warn3(FATAL,"unable to figure out number for port ",portname,0);
+ if (verbosity) strerr_warn3(FATAL,"unable to figure out port number for ",portname,0);
_exit(111);
}
salocal.sin_port = se->s_port;
@@ -309,10 +330,10 @@ char **argv;
case DNS_MEM:
die_nomem();
case DNS_HARD:
- if (verbosity) strerr_warn3(FATAL,"unable to figure out address for host ",hostname,0);
+ if (verbosity) strerr_warn3(FATAL,"unable to figure out IP address for ",hostname,0);
_exit(111);
case DNS_SOFT:
- if (verbosity) strerr_warn3(FATAL,"temporarily unable to figure out address for host ",hostname,0);
+ if (verbosity) strerr_warn3(FATAL,"temporarily unable to figure out IP address for ",hostname,0);
_exit(111);
}
if (!ia.len) {
@@ -386,13 +407,13 @@ char **argv;
byte_copy(&ipremote,4,&saremote.sin_addr);
sig_childblock();
+
+ ++numchildren; printstatus();
switch(fork()) {
- default:
- ++numchildren;
- break;
case -1:
if (verbosity) strerr_warn2(DROP,"unable to fork: ",&strerr_sys);
+ --numchildren; printstatus();
break;
case 0:
tcpremoteip[ip_fmt(tcpremoteip,&ipremote)] = 0;
@@ -408,10 +429,20 @@ char **argv;
setsockopt(t,IPPROTO_TCP,1,&opt,sizeof(opt)); /* 1 == TCP_NODELAY */
/* if it fails, bummer */
}
+
+ if (*banner) {
+ substdio ss;
+ char ssbuf[1];
+ substdio_fdbuf(&ss,write,t,ssbuf,sizeof ssbuf);
+ if (substdio_putsflush(&ss,banner) == -1) {
+ if (verbosity) strerr_warn2(DROP,"unable to print banner: ",&strerr_sys);
+ _exit(111);
+ }
+ }
dummy = sizeof(salocal);
if (getsockname(t,(struct sockaddr *) &salocal,&dummy) == -1) {
- if (verbosity) strerr_warn2(DROP,"unable to get local address",&strerr_sys);
+ if (verbosity) strerr_warn2(DROP,"unable to get local address: ",&strerr_sys);
_exit(111);
}
byte_copy(&iplocal,4,&salocal.sin_addr);
@@ -462,8 +493,8 @@ char **argv;
if (verbosity) strerr_warn2(DROP,"unable to set up descriptors: ",&strerr_sys);
_exit(111);
}
- sig_childunblock();
sig_childdefault();
+ sig_childunblock();
sig_termdefault();
sig_pipedefault();
execvp(*argv,argv);