summaryrefslogtreecommitdiff
path: root/libmailutils/cidr/tosa.c
diff options
context:
space:
mode:
Diffstat (limited to 'libmailutils/cidr/tosa.c')
-rw-r--r--libmailutils/cidr/tosa.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/libmailutils/cidr/tosa.c b/libmailutils/cidr/tosa.c
new file mode 100644
index 000000000..c14a995da
--- /dev/null
+++ b/libmailutils/cidr/tosa.c
@@ -0,0 +1,78 @@
1/* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <netdb.h>
27#include <string.h>
28#include <stdlib.h>
29#include <mailutils/cidr.h>
30#include <mailutils/errno.h>
31
32int
33mu_cidr_to_sockaddr (struct mu_cidr *cidr, struct sockaddr **psa)
34{
35 union
36 {
37 struct sockaddr sa;
38 struct sockaddr_in s_in;
39#ifdef MAILUTILS_IPV6
40 struct sockaddr_in6 s_in6;
41#endif
42 } addr;
43 struct sockaddr *sa;
44 int socklen;
45 int i;
46
47 memset (&addr, 0, sizeof (addr));
48 addr.sa.sa_family = cidr->family;
49 switch (cidr->family)
50 {
51 case AF_INET:
52 socklen = sizeof (addr.s_in);
53 for (i = 0; i < cidr->len; i++)
54 {
55 addr.s_in.sin_addr.s_addr <<= 8;
56 addr.s_in.sin_addr.s_addr |= cidr->address[i];
57 }
58 break;
59
60#ifdef MAILUTILS_IPV6
61 case AF_INET6:
62 socklen = sizeof (addr.s_in6);
63 memcpy (&addr.s_in6.sin6_addr, cidr->address, 16);
64 break;
65#endif
66
67 default:
68 return MU_ERR_FAMILY;
69 }
70
71 sa = malloc (socklen);
72 if (!sa)
73 return ENOMEM;
74 memcpy (sa, &addr, socklen);
75 *psa = sa;
76 return 0;
77}
78

Return to:

Send suggestions and report system problems to the System administrator.