~ubuntu-branches/ubuntu/dapper/postfix/dapper-security

« back to all changes in this revision

Viewing changes to src/util/inet_listen.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      inet_listen 3
 
4
/* SUMMARY
 
5
/*      start INET-domain listener
 
6
/* SYNOPSIS
 
7
/*      #include <listen.h>
 
8
/*
 
9
/*      int     inet_listen(addr, backlog, block_mode)
 
10
/*      const char *addr;
 
11
/*      int     backlog;
 
12
/*      int     block_mode;
 
13
/*
 
14
/*      int     inet_accept(fd)
 
15
/*      int     fd;
 
16
/* DESCRIPTION
 
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.
 
20
/*
 
21
/*      inet_accept() accepts a connection and sanitizes error results.
 
22
/*
 
23
/*      Arguments:
 
24
/* .IP addr
 
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.
 
28
/* .IP backlog
 
29
/*      This argument is passed on to the \fIlisten(2)\fR routine.
 
30
/* .IP block_mode
 
31
/*      Either NON_BLOCKING for a non-blocking socket, or BLOCKING for
 
32
/*      blocking mode.
 
33
/* .IP fd
 
34
/*      File descriptor returned by inet_listen().
 
35
/* DIAGNOSTICS
 
36
/*      Fatal errors: inet_listen() aborts upon any system call failure.
 
37
/*      inet_accept() leaves all error handling up to the caller.
 
38
/* LICENSE
 
39
/* .ad
 
40
/* .fi
 
41
/*      The Secure Mailer license must be distributed with this software.
 
42
/* AUTHOR(S)
 
43
/*      Wietse Venema
 
44
/*      IBM T.J. Watson Research
 
45
/*      P.O. Box 704
 
46
/*      Yorktown Heights, NY 10598, USA
 
47
/*--*/
 
48
 
 
49
/* System libraries. */
 
50
 
 
51
#include <sys_defs.h>
 
52
#include <sys/socket.h>
 
53
#include <netinet/in.h>
 
54
#include <arpa/inet.h>
 
55
#include <netdb.h>
 
56
#ifndef MAXHOSTNAMELEN
 
57
#include <sys/param.h>
 
58
#endif
 
59
#include <string.h>
 
60
#include <unistd.h>
 
61
 
 
62
/* Utility library. */
 
63
 
 
64
#include "mymalloc.h"
 
65
#include "msg.h"
 
66
#include "find_inet.h"
 
67
#include "inet_util.h"
 
68
#include "iostuff.h"
 
69
#include "listen.h"
 
70
#include "sane_accept.h"
 
71
 
 
72
/* Application-specific stuff. */
 
73
 
 
74
#ifndef INADDR_ANY
 
75
#define INADDR_ANY      0xffffffff
 
76
#endif
 
77
 
 
78
/* inet_listen - create inet-domain listener */
 
79
 
 
80
int     inet_listen(const char *addr, int backlog, int block_mode)
 
81
{
 
82
    struct sockaddr_in sin;
 
83
    int     sock;
 
84
    int     t = 1;
 
85
    char   *buf;
 
86
    char   *host;
 
87
    char   *port;
 
88
 
 
89
    /*
 
90
     * Translate address information to internal form.
 
91
     */
 
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);
 
97
    myfree(buf);
 
98
 
 
99
    /*
 
100
     * Create a listener socket.
 
101
     */
 
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");
 
112
    return (sock);
 
113
}
 
114
 
 
115
/* inet_accept - accept connection */
 
116
 
 
117
int     inet_accept(int fd)
 
118
{
 
119
    struct sockaddr_in sin;
 
120
    SOCKADDR_SIZE len = sizeof(sin);
 
121
 
 
122
    return (sane_accept(fd, (struct sockaddr *) & sin, &len));
 
123
}