aboutsummaryrefslogtreecommitdiffstats
path: root/tools/vnet/vnet-module/varp_util.h
blob: d4610290887493899aea90aaadac72a4edd6e5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free software Foundation, Inc.,
 * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef _VNET_VARP_UTIL_H
#define _VNET_VARP_UTIL_H

#include "hash_table.h"

/** Size of a string buffer to store a varp address. */
#define VARP_ADDR_BUF 56

/** Size of a string buffer to store a vnet id. */
#define VNET_ID_BUF 56

#ifndef NIPQUAD
#define NIPQUAD(addr) \
	((unsigned char *)&addr)[0], \
	((unsigned char *)&addr)[1], \
	((unsigned char *)&addr)[2], \
	((unsigned char *)&addr)[3]
#endif

#ifndef NIP6
#define NIP6(addr) \
	ntohs((addr).s6_addr16[0]), \
	ntohs((addr).s6_addr16[1]), \
	ntohs((addr).s6_addr16[2]), \
	ntohs((addr).s6_addr16[3]), \
	ntohs((addr).s6_addr16[4]), \
	ntohs((addr).s6_addr16[5]), \
	ntohs((addr).s6_addr16[6]), \
	ntohs((addr).s6_addr16[7])
#endif


static inline const char *VarpAddr_ntoa(VarpAddr *addr, char buf[VARP_ADDR_BUF])
{
    switch(addr->family){
    default:
    case AF_INET:
        sprintf(buf, "%u.%u.%u.%u",
                NIPQUAD(addr->u.ip4));
        break;
    case AF_INET6:
        sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
                NIP6(addr->u.ip6));
        break;
    }
    return buf;
}

static inline const char *VnetId_ntoa(VnetId *vnet, char buf[VNET_ID_BUF])
{
    sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
            ntohs(vnet->u.vnet16[0]), \
            ntohs(vnet->u.vnet16[1]), \
            ntohs(vnet->u.vnet16[2]), \
            ntohs(vnet->u.vnet16[3]), \
            ntohs(vnet->u.vnet16[4]), \
            ntohs(vnet->u.vnet16[5]), \
            ntohs(vnet->u.vnet16[6]), \
            ntohs(vnet->u.vnet16[7]));
    return buf;
}

extern int VnetId_aton(const char *s, VnetId *vnet);

/** Convert an unsigned in host order to a vnet id.
 */
static inline struct VnetId toVnetId(uint32_t vnetid){
    struct VnetId vnet = {};
    vnet.u.vnet32[VNETID_SIZE32 - 1] = htonl(vnetid);
    return vnet;
}

static inline int VnetId_eq(VnetId *id1, VnetId *id2){
    return memcmp(id1, id2, sizeof(VnetId)) == 0;
}

#endif /* _VNET_VARP_UTIL_H */