ia64/xen-unstable

view tools/vnet/vnet-module/varp_util.h @ 17838:e5c9c8e6e726

tools: replace sprintf with snprintf where applicable

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 12 15:41:15 2008 +0100 (2008-06-12)
parents 71b0f00f6344
children
line source
1 /*
2 * Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free software Foundation, Inc.,
16 * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19 #ifndef _VNET_VARP_UTIL_H
20 #define _VNET_VARP_UTIL_H
22 #include "hash_table.h"
24 /** Size of a string buffer to store a varp address. */
25 #define VARP_ADDR_BUF 56
27 /** Size of a string buffer to store a vnet id. */
28 #define VNET_ID_BUF 56
30 #ifndef NIPQUAD
31 #define NIPQUAD(addr) \
32 ((unsigned char *)&addr)[0], \
33 ((unsigned char *)&addr)[1], \
34 ((unsigned char *)&addr)[2], \
35 ((unsigned char *)&addr)[3]
36 #endif
38 #ifndef NIP6
39 #define NIP6(addr) \
40 ntohs((addr).s6_addr16[0]), \
41 ntohs((addr).s6_addr16[1]), \
42 ntohs((addr).s6_addr16[2]), \
43 ntohs((addr).s6_addr16[3]), \
44 ntohs((addr).s6_addr16[4]), \
45 ntohs((addr).s6_addr16[5]), \
46 ntohs((addr).s6_addr16[6]), \
47 ntohs((addr).s6_addr16[7])
48 #endif
51 static inline const char *VarpAddr_ntoa(VarpAddr *addr, char buf[VARP_ADDR_BUF])
52 {
53 switch(addr->family){
54 default:
55 case AF_INET:
56 snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
57 NIPQUAD(addr->u.ip4));
58 break;
59 case AF_INET6:
60 snprintf(buf, sizeof(buf), "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
61 NIP6(addr->u.ip6));
62 break;
63 }
64 return buf;
65 }
67 static inline const char *VnetId_ntoa(VnetId *vnet, char buf[VNET_ID_BUF])
68 {
69 snprintf(buf, sizeof(buf), "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
70 ntohs(vnet->u.vnet16[0]), \
71 ntohs(vnet->u.vnet16[1]), \
72 ntohs(vnet->u.vnet16[2]), \
73 ntohs(vnet->u.vnet16[3]), \
74 ntohs(vnet->u.vnet16[4]), \
75 ntohs(vnet->u.vnet16[5]), \
76 ntohs(vnet->u.vnet16[6]), \
77 ntohs(vnet->u.vnet16[7]));
78 return buf;
79 }
81 extern int VnetId_aton(const char *s, VnetId *vnet);
83 /** Convert an unsigned in host order to a vnet id.
84 */
85 static inline struct VnetId toVnetId(uint32_t vnetid){
86 struct VnetId vnet = {};
87 vnet.u.vnet32[VNETID_SIZE32 - 1] = htonl(vnetid);
88 return vnet;
89 }
91 static inline int VnetId_eq(VnetId *id1, VnetId *id2){
92 return memcmp(id1, id2, sizeof(VnetId)) == 0;
93 }
95 #endif /* _VNET_VARP_UTIL_H */