network.c 10.0 KB
Newer Older
1 2 3
/*
 * network.c: network helper APIs for libvirt
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2009-2010 Red Hat, Inc.
5 6 7 8 9 10 11
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#include <config.h>
M
Matthew Booth 已提交
12
#include <arpa/inet.h>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

#include "memory.h"
#include "network.h"

/*
 * Helpers to extract the IP arrays from the virSocketAddrPtr
 * That part is the less portable of the module
 */
typedef unsigned char virIPv4Addr[4];
typedef virIPv4Addr *virIPv4AddrPtr;
typedef unsigned short virIPv6Addr[8];
typedef virIPv6Addr *virIPv6AddrPtr;

static int getIPv4Addr(virSocketAddrPtr addr, virIPv4AddrPtr tab) {
    unsigned long val;
    int i;

30
    if ((addr == NULL) || (tab == NULL) || (addr->data.stor.ss_family != AF_INET))
31 32
        return(-1);

33
    val = ntohl(addr->data.inet4.sin_addr.s_addr);
34 35

    for (i = 0;i < 4;i++) {
36
        (*tab)[3 - i] = val & 0xFF;
37 38 39 40 41 42 43 44 45
        val >>= 8;
    }

    return(0);
}

static int getIPv6Addr(virSocketAddrPtr addr, virIPv6AddrPtr tab) {
    int i;

46
    if ((addr == NULL) || (tab == NULL) || (addr->data.stor.ss_family != AF_INET6))
47 48 49
        return(-1);

    for (i = 0;i < 8;i++) {
50 51
        (*tab)[i] = ((addr->data.inet6.sin6_addr.s6_addr[2 * i] << 8) |
                     addr->data.inet6.sin6_addr.s6_addr[2 * i + 1]);
52 53 54 55 56 57 58 59
    }

    return(0);
}

/**
 * virSocketParseAddr:
 * @val: a numeric network address IPv4 or IPv6
60
 * @addr: where to store the return value, optional.
61
 * @family: address family to pass down to getaddrinfo
62 63 64 65
 *
 * Mostly a wrapper for getaddrinfo() extracting the address storage
 * from the numeric string like 1.2.3.4 or 2001:db8:85a3:0:0:8a2e:370:7334
 *
M
Matthew Booth 已提交
66
 * Returns the length of the network address or -1 in case of error.
67 68
 */
int
69
virSocketParseAddr(const char *val, virSocketAddrPtr addr, int family) {
70 71 72 73
    int len;
    struct addrinfo hints;
    struct addrinfo *res = NULL;

74
    if (val == NULL)
75 76 77
        return(-1);

    memset(&hints, 0, sizeof(hints));
78 79
    hints.ai_family = family;
    hints.ai_flags = AI_NUMERICHOST;
80 81 82 83 84
    if ((getaddrinfo(val, NULL, &hints, &res) != 0) || (res ==  NULL)) {
        return(-1);
    }

    len = res->ai_addrlen;
85 86 87 88
    if (addr != NULL) {
        memcpy(&addr->data.stor, res->ai_addr, len);
        addr->len = res->ai_addrlen;
    }
89 90 91 92 93 94 95 96

    freeaddrinfo(res);
    return(len);
}

/*
 * virSocketParseIpv4Addr:
 * @val: an IPv4 numeric address
97
 * @addr: the location to store the result
98 99 100
 *
 * Extract the address storage from an IPv4 numeric address
 *
101
 * Returns the length of the network address or -1 in case of error.
102 103 104 105 106 107 108 109 110
 */
int
virSocketParseIpv4Addr(const char *val, virSocketAddrPtr addr) {
    return(virSocketParseAddr(val, addr, AF_INET));
}

/*
 * virSocketParseIpv6Addr:
 * @val: an IPv6 numeric address
111
 * @addr: the location to store the result
112 113 114
 *
 * Extract the address storage from an IPv6 numeric address
 *
115
 * Returns the length of the network address or -1 in case of error.
116 117 118 119 120 121
 */
int
virSocketParseIpv6Addr(const char *val, virSocketAddrPtr addr) {
    return(virSocketParseAddr(val, addr, AF_INET6));
}

M
Matthew Booth 已提交
122 123
/*
 * virSocketFormatAddr:
E
Eric Blake 已提交
124
 * @addr: an initialized virSocketAddrPtr
M
Matthew Booth 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
 *
 * Returns a string representation of the given address
 * Returns NULL on any error
 * Caller must free the returned string
 */
char *
virSocketFormatAddr(virSocketAddrPtr addr) {
    char   *out;
    size_t outlen;
    void   *inaddr;

    if (addr == NULL)
        return NULL;

139
    if (addr->data.stor.ss_family == AF_INET) {
M
Matthew Booth 已提交
140
        outlen = INET_ADDRSTRLEN;
141
        inaddr = &addr->data.inet4.sin_addr;
M
Matthew Booth 已提交
142 143
    }

144
    else if (addr->data.stor.ss_family == AF_INET6) {
M
Matthew Booth 已提交
145
        outlen = INET6_ADDRSTRLEN;
146
        inaddr = &addr->data.inet6.sin6_addr;
M
Matthew Booth 已提交
147 148 149 150 151 152 153 154 155
    }

    else {
        return NULL;
    }

    if (VIR_ALLOC_N(out, outlen) < 0)
        return NULL;

156
    if (inet_ntop(addr->data.stor.ss_family, inaddr, out, outlen) == NULL) {
M
Matthew Booth 已提交
157 158 159 160 161 162 163 164 165
        VIR_FREE(out);
        return NULL;
    }

    return out;
}

/*
 * virSocketSetPort:
E
Eric Blake 已提交
166
 * @addr: an initialized virSocketAddrPtr
M
Matthew Booth 已提交
167 168 169 170 171 172 173 174 175 176 177
 * @port: the port number to set
 *
 * Set the transport layer port of the given virtSocketAddr
 *
 * Returns 0 on success, -1 on failure
 */
int
virSocketSetPort(virSocketAddrPtr addr, int port) {
    if (addr == NULL)
        return -1;

178 179
    port = htons(port);

180 181
    if(addr->data.stor.ss_family == AF_INET) {
        addr->data.inet4.sin_port = port;
M
Matthew Booth 已提交
182 183
    }

184 185
    else if(addr->data.stor.ss_family == AF_INET6) {
        addr->data.inet6.sin6_port = port;
M
Matthew Booth 已提交
186 187 188 189 190 191 192 193 194 195 196
    }

    else {
        return -1;
    }

    return 0;
}

/*
 * virSocketGetPort:
E
Eric Blake 已提交
197
 * @addr: an initialized virSocketAddrPtr
M
Matthew Booth 已提交
198 199 200 201 202 203 204 205 206
 *
 * Returns the transport layer port of the given virtSocketAddr
 * Returns -1 if @addr is invalid
 */
int
virSocketGetPort(virSocketAddrPtr addr) {
    if (addr == NULL)
        return -1;

207 208
    if(addr->data.stor.ss_family == AF_INET) {
        return ntohs(addr->data.inet4.sin_port);
M
Matthew Booth 已提交
209 210
    }

211 212
    else if(addr->data.stor.ss_family == AF_INET6) {
        return ntohs(addr->data.inet6.sin6_port);
M
Matthew Booth 已提交
213 214 215 216 217
    }

    return -1;
}

218 219 220 221 222 223 224 225 226
/**
 * virSocketAddrIsNetmask:
 * @netmask: the netmask address
 *
 * Check that @netmask is a proper network mask
 *
 * Returns 0 in case of success and -1 in case of error
 */
int virSocketAddrIsNetmask(virSocketAddrPtr netmask) {
227 228 229 230
    int n = virSocketGetNumNetmaskBits(netmask);
    if (n < 0)
        return -1;
    return 0;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}

/**
 * virSocketCheckNetmask:
 * @addr1: a first network address
 * @addr2: a second network address
 * @netmask: the netmask address
 *
 * Check that @addr1 and @addr2 pertain to the same @netmask address
 * range and returns the size of the range
 *
 * Returns 1 in case of success and 0 in case of failure and
 *         -1 in case of error
 */
int virSocketCheckNetmask(virSocketAddrPtr addr1, virSocketAddrPtr addr2,
                          virSocketAddrPtr netmask) {
    int i;

    if ((addr1 == NULL) || (addr2 == NULL) || (netmask == NULL))
        return(-1);
251 252
    if ((addr1->data.stor.ss_family != addr2->data.stor.ss_family) ||
        (addr1->data.stor.ss_family != netmask->data.stor.ss_family))
253 254 255 256 257
        return(-1);

    if (virSocketAddrIsNetmask(netmask) != 0)
        return(-1);

258
    if (addr1->data.stor.ss_family == AF_INET) {
259 260 261 262 263 264 265 266 267 268 269 270
        virIPv4Addr t1, t2, tm;

        if ((getIPv4Addr(addr1, &t1) < 0) ||
            (getIPv4Addr(addr2, &t2) < 0) ||
            (getIPv4Addr(netmask, &tm) < 0))
            return(-1);

        for (i = 0;i < 4;i++) {
            if ((t1[i] & tm[i]) != (t2[i] & tm[i]))
                return(0);
        }

271
    } else if (addr1->data.stor.ss_family == AF_INET6) {
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        virIPv6Addr t1, t2, tm;

        if ((getIPv6Addr(addr1, &t1) < 0) ||
            (getIPv6Addr(addr2, &t2) < 0) ||
            (getIPv6Addr(netmask, &tm) < 0))
            return(-1);

        for (i = 0;i < 8;i++) {
            if ((t1[i] & tm[i]) != (t2[i] & tm[i]))
                return(0);
        }

    } else {
        return(-1);
    }
    return(1);
}

/**
 * virSocketGetRange:
 * @start: start of an IP range
 * @end: end of an IP range
 *
 * Check the order of the 2 addresses and compute the range, this
 * will return 1 for identical addresses. Errors can come from incompatible
 * addresses type, excessive range (>= 2^^16) where the two addresses are
 * unrelated or inverted start and end.
 *
 * Returns the size of the range or -1 in case of failure
 */
int virSocketGetRange(virSocketAddrPtr start, virSocketAddrPtr end) {
    int ret = 0, i;

    if ((start == NULL) || (end == NULL))
        return(-1);
307
    if (start->data.stor.ss_family != end->data.stor.ss_family)
308 309
        return(-1);

310
    if (start->data.stor.ss_family == AF_INET) {
311 312 313 314 315 316 317 318 319 320 321 322 323 324
        virIPv4Addr t1, t2;

        if ((getIPv4Addr(start, &t1) < 0) ||
            (getIPv4Addr(end, &t2) < 0))
            return(-1);

        for (i = 0;i < 2;i++) {
            if (t1[i] != t2[i])
                return(-1);
        }
        ret = (t2[2] - t1[2]) * 256 + (t2[3] - t1[3]);
        if (ret < 0)
            return(-1);
        ret++;
325
    } else if (start->data.stor.ss_family == AF_INET6) {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        virIPv6Addr t1, t2;

        if ((getIPv6Addr(start, &t1) < 0) ||
            (getIPv6Addr(end, &t2) < 0))
            return(-1);

        for (i = 0;i < 7;i++) {
            if (t1[i] != t2[i])
                return(-1);
        }
        ret = t2[7] - t1[7];
        if (ret < 0)
            return(-1);
        ret++;
    } else {
        return(-1);
    }
    return(ret);
}
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360


/**
 * virGetNumNetmaskBits
 * @netmask: the presumed netmask
 *
 * Get the number of netmask bits in a netmask.
 *
 * Returns the number of bits in the netmask or -1 if an error occurred
 * or the netmask is invalid.
 */
int virSocketGetNumNetmaskBits(const virSocketAddrPtr netmask)
{
    int i, j;
    int c = 0;

361
    if (netmask->data.stor.ss_family == AF_INET) {
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
        virIPv4Addr tm;
        uint8_t bit;

        if (getIPv4Addr(netmask, &tm) < 0)
            return -1;

        for (i = 0; i < 4; i++)
            if (tm[i] == 0xff)
                c += 8;
            else
                break;

        if (c == 8 * 4)
            return c;

        j = i << 3;
        while (j < (8 * 4)) {
            bit = 1 << (7 - (j & 7));
            if ((tm[j >> 3] & bit)) {
                c++;
            } else
                break;
            j++;
        }

        while (j < (8 * 4)) {
            bit = 1 << (7 - (j & 7));
            if ((tm[j >> 3] & bit))
                return -1;
            j++;
        }

        return c;
395
    } else if (netmask->data.stor.ss_family == AF_INET6) {
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        virIPv6Addr tm;
        uint16_t bit;

        if (getIPv6Addr(netmask, &tm) < 0)
            return -1;

        for (i = 0; i < 8; i++)
            if (tm[i] == 0xffff)
                c += 16;
            else
                break;

        if (c == 16 * 8)
            return c;

        j = i << 4;
        while (j < (16 * 8)) {
            bit = 1 << (15 - (j & 0xf));
            if ((tm[j >> 4] & bit)) {
                c++;
            } else
                break;
            j++;
        }

        while (j < (16 * 8)) {
            bit = 1 << (15 - (j & 0xf));
            if ((tm[j >> 4]) & bit)
                return -1;
            j++;
        }

        return c;
    }
    return -1;
}