s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
ipme.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/param.h>
3#include <sys/time.h>
4#include <sys/ioctl.h>
5#include <sys/socket.h>
6#include <net/if.h>
7#include <netinet/in.h>
8#include <ifaddrs.h>
9#ifndef SIOCGIFCONF /* whatever works */
10#include <sys/sockio.h>
11#endif
12#include "hassalen.h"
13#include "byte.h"
14#include "ip.h"
15#include "ipalloc.h"
16#include "stralloc.h"
17#include "ipme.h"
18
23static int ipmeok = 0;
24ipalloc ipme = {0};
25
26int ipme_is4(struct ip4_address *ip)
27{
28 int i;
29
30 if (ipme_init() != 1) return -1;
31
32 for (i = 0; i < ipme.len; ++i)
33 if (ipme.ix[i].af == AF_INET && byte_equal(&ipme.ix[i].addr,4,ip))
34 return 1;
35 return 0;
36}
37
38int ipme_is6(struct ip6_address *ip)
39{
40 int i;
41
42 if (ipme_init() != 1) return -1;
43
44 for (i = 0; i < ipme.len; ++i)
45 if (ipme.ix[i].af == AF_INET6 && byte_equal(&ipme.ix[i].addr,16,ip))
46 return 1;
47 return 0;
48}
49
50int ipme_is(struct ip_mx *mxip)
51{
52 switch (mxip->af) {
53 case AF_INET: return ipme_is4(&mxip->addr.ip4);
54 case AF_INET6: return ipme_is6(&mxip->addr.ip6);
55 }
56 return 0;
57}
58
59/* @brief ipme_init uses now getifaddrs() instead of ioctl calls */
60
62{
63 struct ifaddrs *ifap, *ifa;
64 struct sockaddr_in *sin;
65 struct sockaddr_in6 *sin6;
66 struct ip_mx ix;
67
68 if (ipmeok) return 1;
69 if (!ipalloc_readyplus(&ipme,0)) return 0;
70 ipme.len = 0;
71 ix.pref = 0;
72
73 if (getifaddrs(&ifap)) return 0;
74
75 for (ifa = ifap; ifa; ifa = ifa->ifa_next)
76 if (ifa->ifa_addr) {
77 if (ifa->ifa_addr->sa_family == AF_INET) {
78 sin = (struct sockaddr_in *) ifa->ifa_addr;
79 byte_copy(&ix.addr.ip4,4,&sin->sin_addr);
80 ix.af = AF_INET;
81 if (!ipalloc_append(&ipme,&ix)) return 0;
82 }
83 if (ifa->ifa_addr->sa_family == AF_INET6) {
84 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
85 byte_copy(&ix.addr.ip6,16,&sin6->sin6_addr);
86 ix.af = AF_INET6;
87 if (!ipalloc_append(&ipme,&ix)) return 0;
88 }
89 }
90
91 freeifaddrs(ifap);
92 ipmeok = 1;
93
94 return 1;
95}
int ipalloc_append()
ipalloc ipme
Definition: ipme.c:24
int ipme_init()
Definition: ipme.c:61
int ipme_is6()
int ipme_is4()
int ipme_is()
Definition: ipalloc.h:8
struct ip6_address ip6
Definition: ipalloc.h:12
struct ip4_address ip4
Definition: ipalloc.h:11
union ip_mx::@0 addr
int pref
Definition: ipalloc.h:14
unsigned short af
Definition: ipalloc.h:9