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
|
/*
* iptools.c -- Some useful TCP/IP utils
*
* iptools.c is a part of binkd project
*
* Copyright (C) 1997-1998 Dima Maloff, 5047/13
*
* 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. See COPYING.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#if defined(HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h>
#endif
#include "sys.h"
#include "Config.h"
#include "iphdr.h"
#include "common.h"
#include "iptools.h"
#include "tools.h"
#include "sem.h"
#include "rfc2553.h"
/*
* Sets non-blocking mode for a given socket
*/
void setsockopts (SOCKET s)
{
#if defined(FIONBIO)
#if defined(UNIX) || defined(IBMTCPIP) || defined(AMIGA)
int arg;
arg = 1;
if (ioctl (s, FIONBIO, (char *) &arg, sizeof arg) < 0)
Log (1, "ioctl (FIONBIO): %s", TCPERR ());
#elif defined(WIN32)
u_long arg;
arg = 1;
if (ioctlsocket (s, FIONBIO, &arg) < 0)
if (!binkd_exit && TCPERRNO != WSAENOTSOCK)
Log (1, "ioctlsocket (FIONBIO): %s", TCPERR ());
#endif
#endif
#if defined(UNIX) || defined(EMX) || defined(AMIGA)
if (fcntl (s, F_SETFL, O_NONBLOCK) == -1)
Log (1, "fcntl: %s", strerror (errno));
#endif
}
/*
* Find the appropriate port string to be used.
* Find_port ("") will return binkp's port from /etc/services or even
* (if there is no binkp entry) 24554.
* Returns NULL on error.
*/
char * find_port (char *s)
{
char *ps = NULL;
struct addrinfo *aiHead, hints;
int aiErr;
/* setup hints for getaddrinfo */
memset((void *)&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
aiErr = getaddrinfo(NULL, (s && *s) ? s : PRTCLNAME, &hints, &aiHead);
if (aiErr == 0)
{
ps = (s && *s) ? s : PRTCLNAME;
freeaddrinfo(aiHead);
}
else
if (s == NULL || *s == 0)
ps = DEF_PORT;
if (ps == NULL)
Log (1, "%s: incorrect port (getaddrinfo: %s)", s, gai_strerror(aiErr));
return ps;
}
int sockaddr_cmp_addr(const struct sockaddr *a, const struct sockaddr *b)
{
if (a->sa_family != b->sa_family)
return a->sa_family - b->sa_family;
if (a->sa_family == AF_INET)
return (((struct sockaddr_in*)a)->sin_addr.s_addr - ((struct sockaddr_in*)b)->sin_addr.s_addr);
#ifdef AF_INET6
else if (a->sa_family == AF_INET6)
return memcmp((char *) &(((struct sockaddr_in6*)a)->sin6_addr),
(char *) &(((struct sockaddr_in6*)b)->sin6_addr),
sizeof(((struct sockaddr_in6*)a)->sin6_addr));
#endif
else
{
Log(2, "Unsupported address family: %d", a->sa_family);
return -1;
}
}
int sockaddr_cmp_port(const struct sockaddr *a, const struct sockaddr *b)
{
if (a->sa_family != b->sa_family)
return a->sa_family - b->sa_family;
if (a->sa_family == AF_INET)
return (((struct sockaddr_in*)a)->sin_port - ((struct sockaddr_in*)b)->sin_port);
#ifdef AF_INET6
else if (a->sa_family == AF_INET6)
return (((struct sockaddr_in6*)a)->sin6_port - ((struct sockaddr_in6*)b)->sin6_port);
#endif
else
{
Log(2, "Unsupported address family: %d", a->sa_family);
return -1;
}
}
|