~ubuntu-branches/ubuntu/hoary/postfix/hoary-security

« back to all changes in this revision

Viewing changes to src/util/find_inet.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-10-06 11:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041006115033-ooo6yfg6kmoteu04
Tags: upstream-2.1.3
ImportĀ upstreamĀ versionĀ 2.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      find_inet 3
 
4
/* SUMMARY
 
5
/*      inet-domain name services
 
6
/* SYNOPSIS
 
7
/*      #include <find_inet.h>
 
8
/*
 
9
/*      unsigned find_inet_addr(host)
 
10
/*      const char *host;
 
11
/*
 
12
/*      int     find_inet_port(port, proto)
 
13
/*      const char *port;
 
14
/*      const char *proto;
 
15
/* DESCRIPTION
 
16
/*      These functions translate network address information from
 
17
/*      between printable form to the internal the form used by the
 
18
/*      BSD TCP/IP network software.
 
19
/*
 
20
/*      find_inet_addr() translates a symbolic or numerical hostname.
 
21
/*
 
22
/*      find_inet_port() translates a symbolic or numerical port name.
 
23
/* BUGS
 
24
/*      find_inet_addr() ignores all but the first address listed for
 
25
/*      a symbolic hostname.
 
26
/* DIAGNOSTICS
 
27
/*      Lookup and conversion errors are fatal.
 
28
/* LICENSE
 
29
/* .ad
 
30
/* .fi
 
31
/*      The Secure Mailer license must be distributed with this software.
 
32
/* AUTHOR(S)
 
33
/*      Wietse Venema
 
34
/*      IBM T.J. Watson Research
 
35
/*      P.O. Box 704
 
36
/*      Yorktown Heights, NY 10598, USA
 
37
/*--*/
 
38
 
 
39
/* System libraries. */
 
40
 
 
41
#include <sys_defs.h>
 
42
#include <sys/socket.h>
 
43
#include <netinet/in.h>
 
44
#include <arpa/inet.h>
 
45
#include <netdb.h>
 
46
#include <stdlib.h>
 
47
#include <string.h>
 
48
 
 
49
/* Application-specific. */
 
50
 
 
51
#include "msg.h"
 
52
#include "stringops.h"
 
53
#include "find_inet.h"
 
54
 
 
55
#ifndef INADDR_NONE
 
56
#define INADDR_NONE 0xffffffff
 
57
#endif
 
58
 
 
59
/* find_inet_addr - translate numerical or symbolic host name */
 
60
 
 
61
unsigned find_inet_addr(const char *host)
 
62
{
 
63
    struct in_addr addr;
 
64
    struct hostent *hp;
 
65
 
 
66
    addr.s_addr = inet_addr(host);
 
67
    if ((addr.s_addr == INADDR_NONE) || (addr.s_addr == 0)) {
 
68
        if ((hp = gethostbyname(host)) == 0)
 
69
            msg_fatal("host not found: %s", host);
 
70
        if (hp->h_addrtype != AF_INET)
 
71
            msg_fatal("unexpected address family: %d", hp->h_addrtype);
 
72
        if (hp->h_length != sizeof(addr))
 
73
            msg_fatal("unexpected address length %d", hp->h_length);
 
74
        memcpy((char *) &addr, hp->h_addr, hp->h_length);
 
75
    }
 
76
    return (addr.s_addr);
 
77
}
 
78
 
 
79
/* find_inet_port - translate numerical or symbolic service name */
 
80
 
 
81
int     find_inet_port(const char *service, const char *protocol)
 
82
{
 
83
    struct servent *sp;
 
84
    int     port;
 
85
 
 
86
    if (alldig(service) && (port = atoi(service)) != 0) {
 
87
        return (htons(port));
 
88
    } else {
 
89
        if ((sp = getservbyname(service, protocol)) == 0)
 
90
            msg_fatal("unknown service: %s/%s", service, protocol);
 
91
        return (sp->s_port);
 
92
    }
 
93
}