Lines 29-83
Link Here
|
29 |
#include <mailutils/cidr.h> |
29 |
#include <mailutils/cidr.h> |
30 |
#include <mailutils/errno.h> |
30 |
#include <mailutils/errno.h> |
31 |
|
31 |
|
32 |
static void |
|
|
33 |
uint32_to_bytes (unsigned char *bytes, uint32_t u) |
34 |
{ |
35 |
int i; |
36 |
|
37 |
for (i = 0; i < 4; i++) |
38 |
{ |
39 |
bytes[i] = u & 0xff; |
40 |
u >>= 8; |
41 |
} |
42 |
} |
43 |
|
44 |
int |
32 |
int |
45 |
_mu_inaddr_to_bytes (int af, void *buf, unsigned char *bytes) |
33 |
_mu_inaddr_to_bytes (int af, void *buf, unsigned char *bytes) |
46 |
{ |
34 |
{ |
47 |
uint32_t u; |
35 |
size_t len; |
48 |
|
36 |
|
49 |
switch (af) |
37 |
switch (af) |
50 |
{ |
38 |
{ |
51 |
case AF_INET: |
39 |
case AF_INET: |
52 |
memcpy (&u, buf, sizeof u); |
40 |
len = 4; |
53 |
uint32_to_bytes (bytes, u); |
41 |
break; |
54 |
return 4; |
|
|
55 |
|
42 |
|
56 |
#ifdef MAILUTILS_IPV6 |
43 |
#ifdef MAILUTILS_IPV6 |
57 |
case AF_INET6: |
44 |
case AF_INET6: |
58 |
memcpy (bytes, buf, 16); |
45 |
len = 16; |
59 |
return 16; |
46 |
break; |
60 |
#endif |
47 |
#endif |
|
|
48 |
default: |
49 |
len = 0; |
61 |
} |
50 |
} |
62 |
return 0; |
51 |
memcpy (bytes, buf, len); |
|
|
52 |
return len; |
63 |
} |
53 |
} |
64 |
|
54 |
|
65 |
int |
55 |
int |
66 |
_mu_sockaddr_to_bytes (unsigned char *bytes, struct sockaddr const *sa) |
56 |
_mu_sockaddr_to_bytes (unsigned char *bytes, struct sockaddr const *sa) |
67 |
{ |
57 |
{ |
|
|
58 |
void *buf; |
68 |
switch (sa->sa_family) |
59 |
switch (sa->sa_family) |
69 |
{ |
60 |
{ |
70 |
case AF_INET: |
61 |
case AF_INET: |
71 |
uint32_to_bytes (bytes, ((struct sockaddr_in*)sa)->sin_addr.s_addr); |
62 |
buf = &(((struct sockaddr_in*)sa)->sin_addr.s_addr); |
72 |
return 4; |
63 |
break; |
73 |
|
64 |
|
74 |
#ifdef MAILUTILS_IPV6 |
65 |
#ifdef MAILUTILS_IPV6 |
75 |
case AF_INET6: |
66 |
case AF_INET6: |
76 |
memcpy (bytes, &((struct sockaddr_in6*)sa)->sin6_addr, 16); |
67 |
buf = &(((struct sockaddr_in6*)sa)->sin6_addr); |
77 |
return 16; |
68 |
break; |
78 |
#endif |
69 |
#endif |
|
|
70 |
default: |
71 |
return 0; |
79 |
} |
72 |
} |
80 |
return 0; |
73 |
return _mu_inaddr_to_bytes (sa->sa_family, buf, bytes); |
81 |
} |
74 |
} |
82 |
|
75 |
|
83 |
int |
76 |
int |