5
/* start INET-domain listener
9
/* int inet_listen(addr, backlog, block_mode)
14
/* int inet_accept(fd)
17
/* The \fBinet_listen\fR routine starts a listener in the INET domain
18
/* on the specified address, with the specified backlog, and returns
19
/* the resulting file descriptor.
21
/* inet_accept() accepts a connection and sanitizes error results.
25
/* The communication endpoint to listen on. The syntax is "host:port".
26
/* Host and port may be specified in symbolic form or numerically.
27
/* A null host field means listen on all network interfaces.
29
/* This argument is passed on to the \fIlisten(2)\fR routine.
31
/* Either NON_BLOCKING for a non-blocking socket, or BLOCKING for
34
/* File descriptor returned by inet_listen().
36
/* Fatal errors: inet_listen() aborts upon any system call failure.
37
/* inet_accept() leaves all error handling up to the caller.
41
/* The Secure Mailer license must be distributed with this software.
44
/* IBM T.J. Watson Research
46
/* Yorktown Heights, NY 10598, USA
49
/* System libraries. */
52
#include <sys/socket.h>
53
#include <netinet/in.h>
54
#include <arpa/inet.h>
56
#ifndef MAXHOSTNAMELEN
57
#include <sys/param.h>
62
/* Utility library. */
66
#include "find_inet.h"
67
#include "inet_util.h"
70
#include "sane_accept.h"
72
/* Application-specific stuff. */
75
#define INADDR_ANY 0xffffffff
78
/* inet_listen - create inet-domain listener */
80
int inet_listen(const char *addr, int backlog, int block_mode)
82
struct sockaddr_in sin;
90
* Translate address information to internal form.
92
buf = inet_parse(addr, &host, &port);
93
memset((char *) &sin, 0, sizeof(sin));
94
sin.sin_family = AF_INET;
95
sin.sin_port = find_inet_port(port, "tcp");
96
sin.sin_addr.s_addr = (*host ? find_inet_addr(host) : INADDR_ANY);
100
* Create a listener socket.
102
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
103
msg_fatal("socket: %m");
104
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &t, sizeof(t)) < 0)
105
msg_fatal("setsockopt: %m");
106
if (bind(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0)
107
msg_fatal("bind %s port %d: %m", sin.sin_addr.s_addr == INADDR_ANY ?
108
"INADDR_ANY" : inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
109
non_blocking(sock, block_mode);
110
if (listen(sock, backlog) < 0)
111
msg_fatal("listen: %m");
115
/* inet_accept - accept connection */
117
int inet_accept(int fd)
119
struct sockaddr_in sin;
120
SOCKADDR_SIZE len = sizeof(sin);
122
return (sane_accept(fd, (struct sockaddr *) & sin, &len));