]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libfreeswan/addrtypeof.c
(no commit message)
[people/ms/strongswan.git] / src / libfreeswan / addrtypeof.c
1 /*
2 * extract parts of an ip_address
3 * Copyright (C) 2000 Henry Spencer.
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
14 *
15 * RCSID $Id: addrtypeof.c,v 1.1 2004/03/15 20:35:25 as Exp $
16 */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21 - addrtypeof - get the type of an ip_address
22 */
23 int
24 addrtypeof(src)
25 const ip_address *src;
26 {
27 return src->u.v4.sin_family;
28 }
29
30 /*
31 - addrbytesptr - get pointer to the address bytes of an ip_address
32 */
33 size_t /* 0 for error */
34 addrbytesptr(src, dstp)
35 const ip_address *src;
36 const unsigned char **dstp; /* NULL means just a size query */
37 {
38 const unsigned char *p;
39 size_t n;
40
41 switch (src->u.v4.sin_family) {
42 case AF_INET:
43 p = (const unsigned char *)&src->u.v4.sin_addr.s_addr;
44 n = 4;
45 break;
46 case AF_INET6:
47 p = (const unsigned char *)&src->u.v6.sin6_addr;
48 n = 16;
49 break;
50 default:
51 return 0;
52 break;
53 }
54
55 if (dstp != NULL)
56 *dstp = p;
57 return n;
58 }
59
60 /*
61 - addrlenof - get length of the address bytes of an ip_address
62 */
63 size_t /* 0 for error */
64 addrlenof(src)
65 const ip_address *src;
66 {
67 return addrbytesptr(src, NULL);
68 }
69
70 /*
71 - addrbytesof - get the address bytes of an ip_address
72 */
73 size_t /* 0 for error */
74 addrbytesof(src, dst, dstlen)
75 const ip_address *src;
76 unsigned char *dst;
77 size_t dstlen;
78 {
79 const unsigned char *p;
80 size_t n;
81 size_t ncopy;
82
83 n = addrbytesptr(src, &p);
84 if (n == 0)
85 return 0;
86
87 if (dstlen > 0) {
88 ncopy = n;
89 if (ncopy > dstlen)
90 ncopy = dstlen;
91 memcpy(dst, p, ncopy);
92 }
93 return n;
94 }