~ubuntu-branches/ubuntu/maverick/bind9/maverick

« back to all changes in this revision

Viewing changes to lib/isc/netaddr.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-04-19 10:21:58 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050419102158-kp20cp1bqf4ivpzy
Tags: 1:9.3.1-2ubuntu1
resync with debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3
 
 * Copyright (C) 1999-2001  Internet Software Consortium.
 
3
 * Copyright (C) 1999-2002  Internet Software Consortium.
4
4
 *
5
5
 * Permission to use, copy, modify, and distribute this software for any
6
6
 * purpose with or without fee is hereby granted, provided that the above
15
15
 * PERFORMANCE OF THIS SOFTWARE.
16
16
 */
17
17
 
18
 
/* $Id: netaddr.c,v 1.18.2.2 2004/03/09 06:11:49 marka Exp $ */
 
18
/* $Id: netaddr.c,v 1.18.12.9 2004/05/15 03:46:12 jinmei Exp $ */
19
19
 
20
20
#include <config.h>
21
21
 
37
37
        if (a->family != b->family)
38
38
                return (ISC_FALSE);
39
39
 
 
40
        if (a->zone != b->zone)
 
41
                return (ISC_FALSE);
 
42
 
40
43
        switch (a->family) {
41
44
        case AF_INET:
42
45
                if (a->type.in.s_addr != b->type.in.s_addr)
43
46
                        return (ISC_FALSE);
44
47
                break;
45
48
        case AF_INET6:
46
 
                if (memcmp(&a->type.in6, &b->type.in6, sizeof a->type.in6)
47
 
                    != 0)
 
49
                if (memcmp(&a->type.in6, &b->type.in6,
 
50
                           sizeof(a->type.in6)) != 0 ||
 
51
                    a->zone != b->zone)
48
52
                        return (ISC_FALSE);
49
53
                break;
50
54
        default:
67
71
        if (a->family != b->family)
68
72
                return (ISC_FALSE);
69
73
 
 
74
        if (a->zone != b->zone)
 
75
                return (ISC_FALSE);
 
76
 
70
77
        switch (a->family) {
71
78
        case AF_INET:
72
79
                pa = (const unsigned char *) &a->type.in;
112
119
 
113
120
isc_result_t
114
121
isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
115
 
        char abuf[sizeof "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255"];
 
122
        char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
 
123
        char zbuf[sizeof("%4294967295")];
116
124
        unsigned int alen;
 
125
        int zlen;
117
126
        const char *r;
 
127
        const void *type;
118
128
 
119
129
        REQUIRE(netaddr != NULL);
120
130
 
121
 
        r = inet_ntop(netaddr->family, &netaddr->type, abuf, sizeof abuf);
 
131
        switch (netaddr->family) {
 
132
        case AF_INET:
 
133
                type = &netaddr->type.in;
 
134
                break;
 
135
        case AF_INET6:
 
136
                type = &netaddr->type.in6;
 
137
                break;
 
138
        default:
 
139
                return (ISC_R_FAILURE);
 
140
        }
 
141
        r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
122
142
        if (r == NULL)
123
143
                return (ISC_R_FAILURE);
124
144
 
125
145
        alen = strlen(abuf);
126
146
        INSIST(alen < sizeof(abuf));
127
147
 
128
 
        if (alen > isc_buffer_availablelength(target))
 
148
        zlen = 0;
 
149
        if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
 
150
                zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
 
151
                if (zlen < 0)
 
152
                        return (ISC_R_FAILURE);
 
153
                INSIST((unsigned int)zlen < sizeof(zbuf));
 
154
        }
 
155
 
 
156
        if (alen + zlen > isc_buffer_availablelength(target))
129
157
                return (ISC_R_NOSPACE);
130
158
 
131
159
        isc_buffer_putmem(target, (unsigned char *)abuf, alen);
 
160
        isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
132
161
 
133
162
        return (ISC_R_SUCCESS);
134
163
}
135
164
 
136
165
void
137
 
isc_netaddr_format(isc_netaddr_t *na, char *array, unsigned int size) {
 
166
isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
138
167
        isc_result_t result;
139
168
        isc_buffer_t buf;
140
169
 
205
234
 
206
235
void
207
236
isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina) {
208
 
        memset(netaddr, 0, sizeof *netaddr);
 
237
        memset(netaddr, 0, sizeof(*netaddr));
209
238
        netaddr->family = AF_INET;
210
239
        netaddr->type.in = *ina;
211
240
}
212
241
 
213
242
void
214
243
isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
215
 
        memset(netaddr, 0, sizeof *netaddr);
 
244
        memset(netaddr, 0, sizeof(*netaddr));
216
245
        netaddr->family = AF_INET6;
217
246
        netaddr->type.in6 = *ina6;
218
247
}
219
248
 
220
249
void
 
250
isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone) {
 
251
        /* we currently only support AF_INET6. */
 
252
        REQUIRE(netaddr->family == AF_INET6);
 
253
 
 
254
        netaddr->zone = zone;
 
255
}
 
256
 
 
257
isc_uint32_t
 
258
isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
 
259
        return (netaddr->zone);
 
260
}
 
261
 
 
262
void
221
263
isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
222
264
        int family = s->type.sa.sa_family;
223
265
        t->family = family;
224
266
        switch (family) {
225
 
        case AF_INET:
 
267
        case AF_INET:
226
268
                t->type.in = s->type.sin.sin_addr;
227
 
                break;
228
 
        case AF_INET6:
 
269
                t->zone = 0;
 
270
                break;
 
271
        case AF_INET6:
229
272
                memcpy(&t->type.in6, &s->type.sin6.sin6_addr, 16);
230
 
                break;
231
 
        default:
232
 
                INSIST(0);
233
 
        }
 
273
#ifdef ISC_PLATFORM_HAVESCOPEID
 
274
                t->zone = s->type.sin6.sin6_scope_id;
 
275
#else
 
276
                t->zone = 0;
 
277
#endif
 
278
                break;
 
279
        default:
 
280
                INSIST(0);
 
281
        }
234
282
}
235
283
 
236
284
void
237
285
isc_netaddr_any(isc_netaddr_t *netaddr) {
238
 
        memset(netaddr, 0, sizeof *netaddr);
 
286
        memset(netaddr, 0, sizeof(*netaddr));
239
287
        netaddr->family = AF_INET;
240
288
        netaddr->type.in.s_addr = INADDR_ANY;
241
289
}
242
290
 
243
291
void
244
292
isc_netaddr_any6(isc_netaddr_t *netaddr) {
245
 
        memset(netaddr, 0, sizeof *netaddr);
 
293
        memset(netaddr, 0, sizeof(*netaddr));
246
294
        netaddr->family = AF_INET6;
247
295
        netaddr->type.in6 = in6addr_any;
248
296
}
269
317
        }
270
318
}
271
319
 
 
320
isc_boolean_t
 
321
isc_netaddr_islinklocal(isc_netaddr_t *na) {
 
322
        switch (na->family) {
 
323
        case AF_INET:
 
324
                return (ISC_FALSE);
 
325
        case AF_INET6:
 
326
                return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na->type.in6)));
 
327
        default:
 
328
                return (ISC_FALSE);
 
329
        }
 
330
}
 
331
 
 
332
isc_boolean_t
 
333
isc_netaddr_issitelocal(isc_netaddr_t *na) {
 
334
        switch (na->family) {
 
335
        case AF_INET:
 
336
                return (ISC_FALSE);
 
337
        case AF_INET6:
 
338
                return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na->type.in6)));
 
339
        default:
 
340
                return (ISC_FALSE);
 
341
        }
 
342
}
 
343
 
272
344
void
273
345
isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
274
346
        isc_netaddr_t *src;