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
|
char *bind_rcs = "$Id: bind.c,v 2.8 1997/09/09 02:52:44 ACJC Exp $";
/* Written and copyright 1997 Anonymous Coders and Junkbusters Corporation.
* Distributed under the GNU General Public License; see the README file.
* This code comes with NO WARRANTY. http://www.junkbusters.com/ht/en/gpl.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#else
#include <unistd.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/signal.h>
#endif
extern int atoip();
long remote_ip_long;
char *remote_ip_str;
/*
* BIND-PORT (portnum)
* if success, return file descriptor
* if failure, returns -2 if address is in use, otherwise -1
*/
int bind_port (hostnam, portnum)
char *hostnam;
int portnum;
{
struct sockaddr_in inaddr;
int fd;
int one = 1;
memset ((char * ) &inaddr, '\0', sizeof inaddr);
inaddr.sin_family = AF_INET;
inaddr.sin_addr.s_addr = atoip(hostnam);
if(sizeof(inaddr.sin_port) == sizeof(short)) {
inaddr.sin_port = htons(portnum);
} else {
inaddr.sin_port = htonl(portnum);
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
return(-1);
}
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0) {
close (fd);
#ifdef _WIN32
if (errno == WSAEADDRINUSE)
#else
if (errno == EADDRINUSE)
#endif
{
return(-2);
} else {
return(-1);
}
}
while (listen(fd, 5) == -1) {
if (errno != EINTR) {
return(-1);
}
}
return fd;
}
/*
* ACCEPT-CONNECTION
* the argument, fd, is the value returned from bind_port
*
* when a connection is accepted, it returns the file descriptor
* for the connected port
*/
int accept_connection (fd)
int fd;
{
struct sockaddr raddr;
struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
int afd, raddrlen;
raddrlen = sizeof raddr;
do {
afd = accept (fd, &raddr, &raddrlen);
} while (afd < 1 && errno == EINTR);
if (afd < 0) {
return(-1);
}
remote_ip_str = inet_ntoa(rap->sin_addr);
remote_ip_long = ntohl(rap->sin_addr.s_addr);
return afd;
}
|