~ubuntu-branches/ubuntu/raring/iproute/raring

« back to all changes in this revision

Viewing changes to lib/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt, Andreas Henriksson, Justin B Rye, Alexander Wirt
  • Date: 2008-05-11 11:18:29 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080511111829-rfewew7s6kiev0bh
Tags: 20080417-1
[ Andreas Henriksson ]
* New upstream release, v2.6.25 a.k.a. snapshot 20080417.
  - Initial documentation for xfrm (Partially fixes #451337)
  - Fixes manpage error caught by lintian!
* Fix typos (syntax error) in ip(8) manpage.
  - Introduced by upstream, caught by lintian yet again!
* Don't ship useless headers in iproute-dev (Closes: #467557)
* Cherry-pick "Fix bad hash calculation because of signed address" from
  upstream. (Closes: #480173)

[ Justin B Rye ]
* Update package description (Closes: #464521)

[ Alexander Wirt ]
* Fix typo in short package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
        return 0;
48
48
}
49
49
 
50
 
/* a valid netmask must be 2^n - 1 (n = 1..31) */
51
 
static int is_valid_netmask(const inet_prefix *addr)
 
50
int mask2bits(__u32 netmask)
52
51
{
53
 
        uint32_t host;
54
 
 
55
 
        if (addr->family != AF_INET)
56
 
                return 0;
57
 
 
58
 
        host = ~ntohl(addr->data[0]);
59
 
 
60
 
        return (host & (host + 1)) == 0;
 
52
        unsigned bits = 0;
 
53
        __u32 mask = ntohl(netmask);
 
54
        __u32 host = ~mask;
 
55
 
 
56
        /* a valid netmask must be 2^n - 1 */
 
57
        if ((host & (host + 1)) != 0)
 
58
                return -1;
 
59
 
 
60
        for (; mask; mask <<= 1)
 
61
                ++bits;
 
62
        return bits;
61
63
}
62
64
 
63
65
static int get_netmask(unsigned *val, const char *arg, int base)
68
70
                return 0;
69
71
 
70
72
        /* try coverting dotted quad to CIDR */
71
 
        if (!get_addr_1(&addr, arg, AF_INET)) {
72
 
                u_int32_t mask;
73
 
 
74
 
                *val=0;
75
 
                for (mask = ntohl(addr.data[0]); mask; mask <<= 1)
76
 
                        (*val)++;
77
 
 
78
 
                if (is_valid_netmask(&addr))
 
73
        if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) {
 
74
                int b = mask2bits(addr.data[0]);
 
75
                
 
76
                if (b >= 0) {
 
77
                        *val = b;
79
78
                        return 0;
 
79
                }
80
80
        }
81
81
 
82
82
        return -1;
518
518
struct namerec
519
519
{
520
520
        struct namerec *next;
 
521
        const char *name;
521
522
        inet_prefix addr;
522
 
        char        *name;
523
523
};
524
524
 
525
 
static struct namerec *nht[256];
 
525
#define NHASH 257
 
526
static struct namerec *nht[NHASH];
526
527
 
527
 
char *resolve_address(const char *addr, int len, int af)
 
528
static const char *resolve_address(const void *addr, int len, int af)
528
529
{
529
530
        struct namerec *n;
530
531
        struct hostent *h_ent;
539
540
                len = 4;
540
541
        }
541
542
 
542
 
        hash = addr[len-1] ^ addr[len-2] ^ addr[len-3] ^ addr[len-4];
 
543
        hash = *(__u32 *)(addr + len - 4) % NHASH;
543
544
 
544
545
        for (n = nht[hash]; n; n = n->next) {
545
546
                if (n->addr.family == af &&
573
574
{
574
575
#ifdef RESOLVE_HOSTNAMES
575
576
        if (resolve_hosts) {
576
 
                char *n;
 
577
                const char *n;
 
578
 
577
579
                if (len <= 0) {
578
580
                        switch (af) {
579
581
                        case AF_INET: