1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
$Id: common.c,v 1.20 1998/08/13 14:53:30 thoth Exp $, part of
faucet and hose: network pipe utilities
Copyright (C) 1992-98 Robert Forsman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#ifndef NOUNIXSOCKETS
#include <sys/un.h>
#endif /*NOUNIXSOCKETS*/
#include <sys/time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "common.h"
#define EXITCODE_CONNECTION 127
#define EXITCODE_ARGS 126
/**********************************************************************/
int *fds=0;
int nfds;
int fdsize=0;
int how_shutdown = -2;
void add_fd(fd)
int fd;
{
if (fds==0) {
fds = (int*)malloc(sizeof(*fds)*(fdsize=4));
} else if (nfds >= fdsize) {
fds = (int*)realloc(fds, sizeof(*fds)*(fdsize*=2));
if (fds==0) {
fprintf(stderr, "%s: Out of memory\n", progname);
exit(1);
}
}
fds[nfds++] = fd;
if (fd>2)
/* We should reserve this spot in the file descriptor table.
If we don't it could get allocated by the socket(2) call and
we would have an awful mess on our hands. */
dup2(0, fd);
}
void reserve_fds(placeholder)
int placeholder; /* I usually pass in 0, assuming that
the process will have a stdin, even
if it's attached to /dev/null */
{
int i;
for (i=0; i<nfds; i++) {
if (!valid_descriptor(fds[i]))
dup2(0, fds[i]);
}
}
void dup_n(socket)
int socket;
{
int i;
#if 0
printf("I will redirect fds");
for (i=0; i<nfds; i++) {
printf(" %d", fds[i]);
}
printf(" and shutdown with %d\n", how_shutdown);
#endif
if (how_shutdown>=0)
shutdown(socket, how_shutdown!=0);
for (i=0; i<nfds; i++) {
dup2(socket, fds[i]);
}
}
/**********************************************************************/
int get_port(ss)
struct sockaddr_storage *ss;
/* Extract port number. Return value in host byte order. */
{
return ntohs((ss->ss_family == AF_INET6)
? ((struct sockaddr_in6 *) ss)->sin6_port
: ((struct sockaddr_in *) ss)->sin_port);
} /* get_port(struct sockaddr_storage *) */
void set_port(ss, port)
struct sockaddr_storage *ss;
int port;
/* Set port number. Input value in host byte order. */
{
switch (ss->ss_family) {
case AF_INET6:
((struct sockaddr_in6 *) ss)->sin6_port = htons(port);
break;
case AF_INET:
default:
((struct sockaddr_in *) ss)->sin_port = htons(port);
}
} /* set_port(struct sockaddr_storage *, short int) */
int name_to_inet_port(portname)
char *portname;
/* This procedure converts a character string to a port number. It looks
up the service by name and if there is none, then it converts the string
to a number with sscanf. Return value in host byte order! */
{
struct servent *p;
if (portname==NULL)
return 0;
p = getservbyname(portname,"tcp");
if (p!=NULL)
{
return p->s_port;
}
else
{
int port;
if (sscanf(portname,"%i",&port)!=1)
{
return 0;
}
else
return port;
}
}
struct sockaddr_storage ** /* addr_array */
convert_hostname(name, count_ret)
char *name;
int *count_ret;
{
//struct hostent *hp;
struct addrinfo hints, *ai, *aiptr;
struct sockaddr_storage **rval;
int rc, count = 0;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
hints.ai_family = AF_UNSPEC;
if ( (rc = getaddrinfo(name, NULL, &hints, &aiptr)) == 0 ) {
/* Count the relevant entries. */
for (ai = aiptr; ai; ai = ai->ai_next)
if ( ai->ai_family == AF_INET6 || ai->ai_family == AF_INET )
++count;
if (count) {
int i = 0;
*count_ret = count;
rval = (struct sockaddr_storage **) malloc(sizeof(*rval) * (count+1));
/* Recover the found address structures. */
for (ai = aiptr; ai; ai = ai->ai_next) {
if ( ai->ai_family != AF_INET6 && ai->ai_family != AF_INET )
continue;
rval[i] = (struct sockaddr_storage *) malloc(sizeof(**rval));
memcpy(rval[i], ai->ai_addr, ai->ai_addrlen);
++i;
}
rval[count] = NULL;
}
freeaddrinfo(aiptr);
if (count)
return rval;
}
#if 0
hp = gethostbyname(name);
if (hp != NULL) {
int i;
if (hp->h_length != sizeof(struct in_addr)) {
fprintf(stderr, "%s: Funky: (hp->h_length = %d) != (sizeof(struct in_addr) = %ld)\n", progname, hp->h_length, (long) sizeof(struct in_addr));
}
for (i = 0; hp->h_addr_list[i]; i++)
{ }
*count_ret = i;
rval = (struct sockaddr_storage **)malloc(sizeof(*rval) * (i+1));
for (i=0; i<*count_ret; i++) {
rval[i] = (struct sockaddr_storage *)malloc(hp->h_length);
memcpy((char*)rval[i], hp->h_addr_list[i], hp->h_length);
}
rval[*count_ret] = 0;
return rval;
} else {
#endif
#if 0
#ifndef HAVE_INET_ATON
int count, len;
unsigned int a1,a2,a3,a4;
#endif
rval = (struct sockaddr_storage **)malloc(2*sizeof(*rval));
rval[0] = (struct sockaddr_storage *)
malloc(sizeof(struct sockaddr_storage));
#ifdef HAVE_INET_ATON
if (0==inet_aton(name, rval[0])) {
*count_ret = 0;
free(rval[0]);
free(rval);
return 0;
}
#else
count = sscanf(name,"%i.%i.%i.%i%n", &a1, &a2, &a3, &a4, &len);
if (4!=count || 0!=name[len] )
return 0;
rval[0]->s_addr = (((((a1 << 8) | a2) << 8) | a3) << 8) | a4;
#endif
*count_ret = 1;
rval[1] = 0;
return rval;
}
#else
return NULL;
#endif
}
/* print an internet host address prettily */
void printhost(fp, addr)
FILE *fp;
struct sockaddr *addr;
{
int rc;
char hostip[INET6_ADDRSTRLEN];
rc = getnameinfo(addr, sizeof(struct sockaddr_storage),
hostip, sizeof(hostip), NULL, 0, NI_NUMERICHOST);
fprintf(fp, "%s", hostip);
}
#ifdef NO_STRERROR
/* Added for those systems without */
extern char *sys_errlist[];
extern int sys_nerr;
char *
strerror(num)
int num;
{
static char ebuf[40]; /* overflow this, baby */
if (num < sys_nerr)
return sys_errlist[num];
else
sprintf(ebuf, "Unknown error: %i\n", num);
return ebuf;
}
#endif
/* bind to a port on the local machine. */
int
bindlocal(fd, name, addrname, domain, reuseaddr)
int fd, domain;
char *name, *addrname;
int reuseaddr;
{
struct sockaddr *laddr;
int addrlen;
int countdown;
int rval, port;
if (reuseaddr && (domain == AF_INET || domain == AF_INET6)) {
#ifdef SO_REUSEADDR
/* The below fix is based on articles that came from comp.sys.hp.hpux
with the problem of having FIN_WAIT_2 statuses on sockets. But even
on Solaris the sockets with TIME_WAIT block the bind() call, so I
thought it would be a good idea to try the setsockopt() call.
1998/01/18 Thomas Endo <tendo@netcom.com> */
int enable = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&enable,
sizeof(enable)) < 0)
{
fprintf(stderr,"%s: error in setsockopt (%s)\n",progname,
strerror(errno));
exit(EXITCODE_CONNECTION);
}
#else
fprintf(stderr, "%s: Warning. SO_REUSEADDR is not available\n",
progname);
#endif
}
if (domain==AF_INET || domain==AF_INET6)
{
static struct sockaddr_storage srv;
static int initted=0;
laddr = (struct sockaddr*)&srv;
addrlen = sizeof(srv);
memset(&srv, 0, sizeof(srv));
if (!initted) {
srv.ss_family = domain;
if (addrname) {
int count, j = 0;
struct sockaddr_storage **addresses;
addresses = convert_hostname(addrname, &count);
if (addresses == 0) {
fprintf(stderr, "%s: Unable to convert %s to an internet address\n", progname, addrname);
errno=0;
return 0;
}
while (addresses[j] && addresses[j]->ss_family != AF_INET
&& addresses[j]->ss_family != AF_INET6)
++j;
if (addresses[j])
memcpy(&srv, addresses[j], sizeof(srv));
}
port = name_to_inet_port(name);
if (port==0)
{
fprintf(stderr, "%s: port %s unknown\n", progname, name);
errno = 0;
return 0;
}
set_port(&srv, port);
}
initted = 1; /* bindlocal is only called once in
each netpipes program */
}
#ifndef NOUNIXSOCKETS
else if (domain == AF_UNIX)
{
static struct sockaddr_un srv;
laddr = (struct sockaddr*)&srv;
addrlen = sizeof(srv);
srv.sun_family = AF_UNIX;
strncpy(srv.sun_path, name, sizeof(srv.sun_path));
srv.sun_path[sizeof(srv.sun_path) -1] = 0; /* NUL terminate that string*/
}
#endif
else
{
fprintf(stderr, "%s: unknown address family %d in bindlocal()\n",
progname, domain);
exit(EXITCODE_ARGS);
}
countdown= ((domain!=AF_INET && domain!=AF_INET6)|| reuseaddr)?1:10;
do {
rval = bind(fd, laddr, addrlen);
if (rval != 0)
{
if (errno==EADDRINUSE && --countdown>0)
{
fprintf(stderr,"%s: Address %s in use, sleeping 10.\n",
progname, name);
sleep (10);
fprintf(stderr,"%s: Trying again . . .\n", progname);
}
else
return 0;
}
} while (rval);
return 1;
}
/* check to see if the descriptor is assigned to a pipe/file/socket (valid)
or is unused (INvalid) */
int valid_descriptor(int fd)
{
int rval;
fd_set fds;
struct timeval tv;
tv.tv_sec = 0; tv.tv_usec = 0; /* POLL */
FD_ZERO(&fds);
FD_SET(fd, &fds);
rval = select(fd+1, &fds, (fd_set *)0, (fd_set *)0, &tv);
if (rval<0 && errno == EBADF) {
#ifdef DEBUG
fprintf(stderr, "%s: descriptor %d not in use\n",
progname, fd);
#endif
return 0;
} else {
#ifdef DEBUG
fprintf(stderr, "%s: descriptor %d already in use\n",
progname, fd);
#endif
return 1;
}
}
|