1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
From feecde8c46cdb597a76df5e6ef02b854843a8a5c Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <gray@gnu.org>
Date: Thu, 21 Jun 2018 09:46:43 +0300
Subject: Fix endianness bug in string to IP conversion
* libmailutils/cidr/fromsa.c (_mu_inaddr_to_bytes)
(_mu_sockaddr_to_bytes): Fix improper endianness conversion.
* libmailutils/cidr/tosa.c (mu_cidr_to_sockaddr): Simplify conversion.
---
libmailutils/cidr/fromsa.c | 45 ++++++++++++++++++++-------------------------
libmailutils/cidr/tosa.c | 9 ++-------
2 files changed, 22 insertions(+), 32 deletions(-)
diff --git a/libmailutils/cidr/fromsa.c b/libmailutils/cidr/fromsa.c
index f57aadc..39d24fd 100644
--- a/libmailutils/cidr/fromsa.c
+++ b/libmailutils/cidr/fromsa.c
@@ -29,55 +29,50 @@
#include <mailutils/cidr.h>
#include <mailutils/errno.h>
-static void
-uint32_to_bytes (unsigned char *bytes, uint32_t u)
-{
- int i;
-
- for (i = 0; i < 4; i++)
- {
- bytes[i] = u & 0xff;
- u >>= 8;
- }
-}
-
int
_mu_inaddr_to_bytes (int af, void *buf, unsigned char *bytes)
{
- uint32_t u;
+ size_t len;
switch (af)
{
case AF_INET:
- memcpy (&u, buf, sizeof u);
- uint32_to_bytes (bytes, u);
- return 4;
-
+ len = 4;
+ break;
+
#ifdef MAILUTILS_IPV6
case AF_INET6:
- memcpy (bytes, buf, 16);
- return 16;
+ len = 16;
+ break;
#endif
+
+ default:
+ len = 0;
}
- return 0;
+ memcpy (bytes, buf, len);
+ return len;
}
int
_mu_sockaddr_to_bytes (unsigned char *bytes, struct sockaddr const *sa)
{
+ void *buf;
switch (sa->sa_family)
{
case AF_INET:
- uint32_to_bytes (bytes, ((struct sockaddr_in*)sa)->sin_addr.s_addr);
- return 4;
+ buf = &(((struct sockaddr_in*)sa)->sin_addr.s_addr);
+ break;
#ifdef MAILUTILS_IPV6
case AF_INET6:
- memcpy (bytes, &((struct sockaddr_in6*)sa)->sin6_addr, 16);
- return 16;
+ buf = &(((struct sockaddr_in6*)sa)->sin6_addr);
+ break;
#endif
+
+ default:
+ return 0;
}
- return 0;
+ return _mu_inaddr_to_bytes (sa->sa_family, buf, bytes);
}
int
diff --git a/libmailutils/cidr/tosa.c b/libmailutils/cidr/tosa.c
index 33715e1..2b372b1 100644
--- a/libmailutils/cidr/tosa.c
+++ b/libmailutils/cidr/tosa.c
@@ -42,19 +42,14 @@ mu_cidr_to_sockaddr (struct mu_cidr *cidr, struct sockaddr **psa)
} addr;
struct sockaddr *sa;
int socklen;
- int i;
-
+
memset (&addr, 0, sizeof (addr));
addr.sa.sa_family = cidr->family;
switch (cidr->family)
{
case AF_INET:
socklen = sizeof (addr.s_in);
- for (i = 0; i < cidr->len; i++)
- {
- addr.s_in.sin_addr.s_addr <<= 8;
- addr.s_in.sin_addr.s_addr |= cidr->address[i];
- }
+ memcpy (&addr.s_in.sin_addr.s_addr, cidr->address, 4);
break;
#ifdef MAILUTILS_IPV6
--
cgit v1.0-41-gc330
|