~ubuntu-branches/ubuntu/trusty/ntp/trusty-proposed

« back to all changes in this revision

Viewing changes to libntp/decodenetnum.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-04-03 07:21:01 UTC
  • mfrom: (4.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20130403072101-v3vt8xcs18b6uhjf
Tags: 1:4.2.6.p5+dfsg-2ubuntu1
* New upstream version, fixing build failure in raring.
* Merge with Debian; remaining changes:
  + debian/ntp.conf, debian/ntpdate.default: Change default server to
    ntp.ubuntu.com.
  + debian/ntpdate.ifup: Stop ntp before running ntpdate when an interface
    comes up, then start again afterwards.
  + debian/ntp.init, debian/rules: Only stop when entering single user mode.
  + Add enforcing AppArmor profile:
    - debian/control: Add Conflicts/Replaces on apparmor-profiles.
    - debian/control: Add Suggests on apparmor.
    - debian/ntp.dirs: Add apparmor directories.
    - debian/ntp.preinst: Force complain on certain upgrades.
    - debian/ntp.postinst: Reload apparmor profile.
    - debian/ntp.postrm: Remove the force-complain file.
    - add debian/apparmor-profile*.
    - debian/rules: install apparmor-profile and apparmor-profile.tunable.
    - debian/README.Debian: Add note on AppArmor.
  + debian/{control,rules}: Add and enable hardened build for PIE.
  + debian/apparmor-profile: Adjust location of drift files.
  + debian/rules, debian/ntp.dirs, debian/source_ntp.py: Add apport hook.
  + debian/ntpdate-debian: Disregard empty ntp.conf files.
  + debian/ntp.preinst: Remove empty /etc/ntp.conf on fresh intallation.
  + debian/ntpdate.ifup: Fix interaction with openntpd.
  + debian/source_ntp.py: Add filter on AppArmor profile names to prevent
    false positives from denials originating in other packages.
  + debian/apparmor-profile: Add samba4 ntp signing socket to ntpd apparmor
    profile.
  + debian/apparmor-profile: adjust for IPv6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include <netinet/in.h>
12
12
#endif
13
13
 
 
14
#include "ntp.h"
14
15
#include "ntp_stdlib.h"
15
16
#include "ntp_assert.h"
16
17
 
 
18
/*
 
19
 * decodenetnum         convert text IP address and port to sockaddr_u
 
20
 *
 
21
 * Returns 0 for failure, 1 for success.
 
22
 */
17
23
int
18
24
decodenetnum(
19
25
        const char *num,
21
27
        )
22
28
{
23
29
        struct addrinfo hints, *ai = NULL;
24
 
        register int err;
25
 
        register const char *cp;
 
30
        int err;
 
31
        u_short port;
 
32
        const char *cp;
 
33
        const char *port_str;
 
34
        char *pp;
 
35
        char *np;
26
36
        char name[80];
27
 
        char *np;
28
37
 
29
38
        NTP_REQUIRE(num != NULL);
30
39
        NTP_REQUIRE(strlen(num) < sizeof(name));
31
40
 
32
 
        if ('[' != num[0]) 
33
 
                cp = num;
34
 
        else {
 
41
        port_str = NULL;
 
42
        if ('[' != num[0]) {
 
43
                /*
 
44
                 * to distinguish IPv6 embedded colons from a port
 
45
                 * specification on an IPv4 address, assume all 
 
46
                 * legal IPv6 addresses have at least two colons.
 
47
                 */
 
48
                pp = strchr(num, ':');
 
49
                if (NULL == pp)
 
50
                        cp = num;       /* no colons */
 
51
                else if (NULL != strchr(pp + 1, ':'))
 
52
                        cp = num;       /* two or more colons */
 
53
                else {                  /* one colon */
 
54
                        strncpy(name, num, sizeof(name));
 
55
                        name[sizeof(name) - 1] = '\0';
 
56
                        cp = name;
 
57
                        pp = strchr(cp, ':');
 
58
                        *pp = '\0';
 
59
                        port_str = pp + 1;
 
60
                }
 
61
        } else {
35
62
                cp = num + 1;
36
63
                np = name; 
37
64
                while (*cp && ']' != *cp)
38
65
                        *np++ = *cp++;
39
66
                *np = 0;
 
67
                if (']' == cp[0] && ':' == cp[1] && '\0' != cp[2])
 
68
                        port_str = &cp[2];
40
69
                cp = name; 
41
70
        }
42
 
        memset(&hints, 0, sizeof(hints));
43
 
        hints.ai_flags = AI_NUMERICHOST;
44
 
        err = getaddrinfo(cp, NULL, &hints, &ai);
 
71
        ZERO(hints);
 
72
        hints.ai_flags = Z_AI_NUMERICHOST;
 
73
        err = getaddrinfo(cp, "ntp", &hints, &ai);
45
74
        if (err != 0)
46
75
                return 0;
47
 
        memcpy(netnum, ai->ai_addr, ai->ai_addrlen); 
 
76
        NTP_INSIST(ai->ai_addrlen <= sizeof(*netnum));
 
77
        memcpy(netnum, ai->ai_addr, ai->ai_addrlen);
48
78
        freeaddrinfo(ai);
 
79
        if (NULL == port_str || 1 != sscanf(port_str, "%hu", &port))
 
80
                port = NTP_PORT;
 
81
        SET_PORT(netnum, port);
49
82
        return 1;
50
83
}