diff options
author | iphydf <iphydf@users.noreply.github.com> | 2020-03-14 15:09:34 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2020-03-22 11:06:45 +0000 |
commit | f148d5bd0ab39d0dab4a35b831e70fdb3dfe8e4d (patch) | |
tree | 983c287e799b7176062b0b2f6b1ff7dd7865a84a | |
parent | d5ae583c2f09cb3fa0a9f5bf2bc60a6d5ecbd4d4 (diff) |
Minor cleanups in network code.
Remove useless parameter, replace with assert (just to be sure). Also
replaced some memsets with 0-initialiser and memcpy with assignment.
-rw-r--r-- | toxcore/network.c | 181 |
1 files changed, 80 insertions, 101 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index da51fdc3..d3284c4d 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -83,24 +83,24 @@ | |||
83 | 83 | ||
84 | #define TOX_EWOULDBLOCK EWOULDBLOCK | 84 | #define TOX_EWOULDBLOCK EWOULDBLOCK |
85 | 85 | ||
86 | static const char *inet_ntop4(int family, const struct in_addr *addr, char *buf, size_t bufsize) | 86 | static const char *inet_ntop4(const struct in_addr *addr, char *buf, size_t bufsize) |
87 | { | 87 | { |
88 | return inet_ntop(family, addr, buf, bufsize); | 88 | return inet_ntop(AF_INET, addr, buf, bufsize); |
89 | } | 89 | } |
90 | 90 | ||
91 | static const char *inet_ntop6(int family, const struct in6_addr *addr, char *buf, size_t bufsize) | 91 | static const char *inet_ntop6(const struct in6_addr *addr, char *buf, size_t bufsize) |
92 | { | 92 | { |
93 | return inet_ntop(family, addr, buf, bufsize); | 93 | return inet_ntop(AF_INET6, addr, buf, bufsize); |
94 | } | 94 | } |
95 | 95 | ||
96 | static int inet_pton4(int family, const char *addrString, struct in_addr *addrbuf) | 96 | static int inet_pton4(const char *addrString, struct in_addr *addrbuf) |
97 | { | 97 | { |
98 | return inet_pton(family, addrString, addrbuf); | 98 | return inet_pton(AF_INET, addrString, addrbuf); |
99 | } | 99 | } |
100 | 100 | ||
101 | static int inet_pton6(int family, const char *addrString, struct in6_addr *addrbuf) | 101 | static int inet_pton6(const char *addrString, struct in6_addr *addrbuf) |
102 | { | 102 | { |
103 | return inet_pton(family, addrString, addrbuf); | 103 | return inet_pton(AF_INET6, addrString, addrbuf); |
104 | } | 104 | } |
105 | 105 | ||
106 | #else | 106 | #else |
@@ -110,86 +110,66 @@ static int inet_pton6(int family, const char *addrString, struct in6_addr *addrb | |||
110 | 110 | ||
111 | #define TOX_EWOULDBLOCK WSAEWOULDBLOCK | 111 | #define TOX_EWOULDBLOCK WSAEWOULDBLOCK |
112 | 112 | ||
113 | static const char *inet_ntop4(int family, const struct in_addr *addr, char *buf, size_t bufsize) | 113 | static const char *inet_ntop4(const struct in_addr *addr, char *buf, size_t bufsize) |
114 | { | 114 | { |
115 | if (family == AF_INET) { | 115 | struct sockaddr_in saddr = {0}; |
116 | struct sockaddr_in saddr; | ||
117 | memset(&saddr, 0, sizeof(saddr)); | ||
118 | 116 | ||
119 | saddr.sin_family = AF_INET; | 117 | saddr.sin_family = AF_INET; |
120 | saddr.sin_addr = *addr; | 118 | saddr.sin_addr = *addr; |
121 | 119 | ||
122 | DWORD len = bufsize; | 120 | DWORD len = bufsize; |
123 | 121 | ||
124 | if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { | 122 | if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { |
125 | return nullptr; | 123 | return nullptr; |
126 | } | ||
127 | |||
128 | return buf; | ||
129 | } | 124 | } |
130 | 125 | ||
131 | return nullptr; | 126 | return buf; |
132 | } | 127 | } |
133 | 128 | ||
134 | static const char *inet_ntop6(int family, const struct in6_addr *addr, char *buf, size_t bufsize) | 129 | static const char *inet_ntop6(const struct in6_addr *addr, char *buf, size_t bufsize) |
135 | { | 130 | { |
136 | if (family == AF_INET6) { | 131 | struct sockaddr_in6 saddr = {0}; |
137 | struct sockaddr_in6 saddr; | ||
138 | memset(&saddr, 0, sizeof(saddr)); | ||
139 | 132 | ||
140 | saddr.sin6_family = AF_INET6; | 133 | saddr.sin6_family = AF_INET6; |
141 | saddr.sin6_addr = *addr; | 134 | saddr.sin6_addr = *addr; |
142 | 135 | ||
143 | DWORD len = bufsize; | 136 | DWORD len = bufsize; |
144 | 137 | ||
145 | if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { | 138 | if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { |
146 | return nullptr; | 139 | return nullptr; |
147 | } | ||
148 | |||
149 | return buf; | ||
150 | } | 140 | } |
151 | 141 | ||
152 | return nullptr; | 142 | return buf; |
153 | } | 143 | } |
154 | 144 | ||
155 | static int inet_pton4(int family, const char *addrString, struct in_addr *addrbuf) | 145 | static int inet_pton4(const char *addrString, struct in_addr *addrbuf) |
156 | { | 146 | { |
157 | if (family == AF_INET) { | 147 | struct sockaddr_in saddr = {0}; |
158 | struct sockaddr_in saddr; | ||
159 | memset(&saddr, 0, sizeof(saddr)); | ||
160 | 148 | ||
161 | INT len = sizeof(saddr); | 149 | INT len = sizeof(saddr); |
162 | 150 | ||
163 | if (WSAStringToAddress((LPTSTR)addrString, AF_INET, nullptr, (LPSOCKADDR)&saddr, &len)) { | 151 | if (WSAStringToAddress((LPTSTR)addrString, AF_INET, nullptr, (LPSOCKADDR)&saddr, &len)) { |
164 | return 0; | 152 | return 0; |
165 | } | ||
166 | |||
167 | *addrbuf = saddr.sin_addr; | ||
168 | |||
169 | return 1; | ||
170 | } | 153 | } |
171 | 154 | ||
172 | return 0; | 155 | *addrbuf = saddr.sin_addr; |
156 | |||
157 | return 1; | ||
173 | } | 158 | } |
174 | 159 | ||
175 | static int inet_pton6(int family, const char *addrString, struct in6_addr *addrbuf) | 160 | static int inet_pton6(const char *addrString, struct in6_addr *addrbuf) |
176 | { | 161 | { |
177 | if (family == AF_INET6) { | 162 | struct sockaddr_in6 saddr = {0}; |
178 | struct sockaddr_in6 saddr; | ||
179 | memset(&saddr, 0, sizeof(saddr)); | ||
180 | |||
181 | INT len = sizeof(saddr); | ||
182 | 163 | ||
183 | if (WSAStringToAddress((LPTSTR)addrString, AF_INET6, nullptr, (LPSOCKADDR)&saddr, &len)) { | 164 | INT len = sizeof(saddr); |
184 | return 0; | ||
185 | } | ||
186 | 165 | ||
187 | *addrbuf = saddr.sin6_addr; | 166 | if (WSAStringToAddress((LPTSTR)addrString, AF_INET6, nullptr, (LPSOCKADDR)&saddr, &len)) { |
188 | 167 | return 0; | |
189 | return 1; | ||
190 | } | 168 | } |
191 | 169 | ||
192 | return 0; | 170 | *addrbuf = saddr.sin6_addr; |
171 | |||
172 | return 1; | ||
193 | } | 173 | } |
194 | 174 | ||
195 | #endif | 175 | #endif |
@@ -222,8 +202,33 @@ static int inet_pton6(int family, const char *addrString, struct in6_addr *addrb | |||
222 | #error "TOX_INET_ADDRSTRLEN should be greater or equal to INET_ADDRSTRLEN (#INET_ADDRSTRLEN)" | 202 | #error "TOX_INET_ADDRSTRLEN should be greater or equal to INET_ADDRSTRLEN (#INET_ADDRSTRLEN)" |
223 | #endif | 203 | #endif |
224 | 204 | ||
225 | static int make_proto(int proto); | 205 | static int make_proto(int proto) |
226 | static int make_socktype(int type); | 206 | { |
207 | switch (proto) { | ||
208 | case TOX_PROTO_TCP: | ||
209 | return IPPROTO_TCP; | ||
210 | |||
211 | case TOX_PROTO_UDP: | ||
212 | return IPPROTO_UDP; | ||
213 | |||
214 | default: | ||
215 | return proto; | ||
216 | } | ||
217 | } | ||
218 | |||
219 | static int make_socktype(int type) | ||
220 | { | ||
221 | switch (type) { | ||
222 | case TOX_SOCK_STREAM: | ||
223 | return SOCK_STREAM; | ||
224 | |||
225 | case TOX_SOCK_DGRAM: | ||
226 | return SOCK_DGRAM; | ||
227 | |||
228 | default: | ||
229 | return type; | ||
230 | } | ||
231 | } | ||
227 | 232 | ||
228 | static int make_family(Family tox_family) | 233 | static int make_family(Family tox_family) |
229 | { | 234 | { |
@@ -1086,7 +1091,7 @@ void ip_copy(IP *target, const IP *source) | |||
1086 | return; | 1091 | return; |
1087 | } | 1092 | } |
1088 | 1093 | ||
1089 | memcpy(target, source, sizeof(IP)); | 1094 | *target = *source; |
1090 | } | 1095 | } |
1091 | 1096 | ||
1092 | /* copies an ip_port structure (careful about direction!) */ | 1097 | /* copies an ip_port structure (careful about direction!) */ |
@@ -1096,7 +1101,7 @@ void ipport_copy(IP_Port *target, const IP_Port *source) | |||
1096 | return; | 1101 | return; |
1097 | } | 1102 | } |
1098 | 1103 | ||
1099 | memcpy(target, source, sizeof(IP_Port)); | 1104 | *target = *source; |
1100 | } | 1105 | } |
1101 | 1106 | ||
1102 | /* ip_ntoa | 1107 | /* ip_ntoa |
@@ -1116,23 +1121,23 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
1116 | } | 1121 | } |
1117 | 1122 | ||
1118 | if (ip) { | 1123 | if (ip) { |
1119 | const int family = make_family(ip->family); | ||
1120 | |||
1121 | if (net_family_is_ipv4(ip->family)) { | 1124 | if (net_family_is_ipv4(ip->family)) { |
1122 | /* returns standard quad-dotted notation */ | 1125 | /* returns standard quad-dotted notation */ |
1123 | struct in_addr addr; | 1126 | struct in_addr addr; |
1124 | fill_addr4(ip->ip.v4, &addr); | 1127 | fill_addr4(ip->ip.v4, &addr); |
1125 | 1128 | ||
1126 | ip_str[0] = 0; | 1129 | ip_str[0] = 0; |
1127 | inet_ntop4(family, &addr, ip_str, length); | 1130 | assert(make_family(ip->family) == AF_INET); |
1131 | inet_ntop4(&addr, ip_str, length); | ||
1128 | } else if (net_family_is_ipv6(ip->family)) { | 1132 | } else if (net_family_is_ipv6(ip->family)) { |
1129 | /* returns hex-groups enclosed into square brackets */ | 1133 | /* returns hex-groups enclosed into square brackets */ |
1130 | struct in6_addr addr; | 1134 | struct in6_addr addr; |
1131 | fill_addr6(ip->ip.v6, &addr); | 1135 | fill_addr6(ip->ip.v6, &addr); |
1132 | 1136 | ||
1133 | ip_str[0] = '['; | 1137 | ip_str[0] = '['; |
1134 | inet_ntop6(family, &addr, &ip_str[1], length - 3); | 1138 | assert(make_family(ip->family) == AF_INET6); |
1135 | size_t len = strlen(ip_str); | 1139 | inet_ntop6(&addr, &ip_str[1], length - 3); |
1140 | const size_t len = strlen(ip_str); | ||
1136 | ip_str[len] = ']'; | 1141 | ip_str[len] = ']'; |
1137 | ip_str[len + 1] = 0; | 1142 | ip_str[len + 1] = 0; |
1138 | } else { | 1143 | } else { |
@@ -1155,12 +1160,14 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length) | |||
1155 | 1160 | ||
1156 | if (net_family_is_ipv4(ip->family)) { | 1161 | if (net_family_is_ipv4(ip->family)) { |
1157 | const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; | 1162 | const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; |
1158 | return inet_ntop4(make_family(ip->family), addr, address, length) != nullptr; | 1163 | assert(make_family(ip->family) == AF_INET); |
1164 | return inet_ntop4(addr, address, length) != nullptr; | ||
1159 | } | 1165 | } |
1160 | 1166 | ||
1161 | if (net_family_is_ipv6(ip->family)) { | 1167 | if (net_family_is_ipv6(ip->family)) { |
1162 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; | 1168 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; |
1163 | return inet_ntop6(make_family(ip->family), addr, address, length) != nullptr; | 1169 | assert(make_family(ip->family) == AF_INET6); |
1170 | return inet_ntop6(addr, address, length) != nullptr; | ||
1164 | } | 1171 | } |
1165 | 1172 | ||
1166 | return false; | 1173 | return false; |
@@ -1174,7 +1181,7 @@ bool addr_parse_ip(const char *address, IP *to) | |||
1174 | 1181 | ||
1175 | struct in_addr addr4; | 1182 | struct in_addr addr4; |
1176 | 1183 | ||
1177 | if (inet_pton4(AF_INET, address, &addr4) == 1) { | 1184 | if (inet_pton4(address, &addr4) == 1) { |
1178 | to->family = net_family_ipv4; | 1185 | to->family = net_family_ipv4; |
1179 | get_ip4(&to->ip.v4, &addr4); | 1186 | get_ip4(&to->ip.v4, &addr4); |
1180 | return true; | 1187 | return true; |
@@ -1182,7 +1189,7 @@ bool addr_parse_ip(const char *address, IP *to) | |||
1182 | 1189 | ||
1183 | struct in6_addr addr6; | 1190 | struct in6_addr addr6; |
1184 | 1191 | ||
1185 | if (inet_pton6(AF_INET6, address, &addr6) == 1) { | 1192 | if (inet_pton6(address, &addr6) == 1) { |
1186 | to->family = net_family_ipv6; | 1193 | to->family = net_family_ipv6; |
1187 | get_ip6(&to->ip.v6, &addr6); | 1194 | get_ip6(&to->ip.v6, &addr6); |
1188 | return true; | 1195 | return true; |
@@ -1424,34 +1431,6 @@ bool bind_to_port(Socket sock, Family family, uint16_t port) | |||
1424 | return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0; | 1431 | return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0; |
1425 | } | 1432 | } |
1426 | 1433 | ||
1427 | static int make_socktype(int type) | ||
1428 | { | ||
1429 | switch (type) { | ||
1430 | case TOX_SOCK_STREAM: | ||
1431 | return SOCK_STREAM; | ||
1432 | |||
1433 | case TOX_SOCK_DGRAM: | ||
1434 | return SOCK_DGRAM; | ||
1435 | |||
1436 | default: | ||
1437 | return type; | ||
1438 | } | ||
1439 | } | ||
1440 | |||
1441 | static int make_proto(int proto) | ||
1442 | { | ||
1443 | switch (proto) { | ||
1444 | case TOX_PROTO_TCP: | ||
1445 | return IPPROTO_TCP; | ||
1446 | |||
1447 | case TOX_PROTO_UDP: | ||
1448 | return IPPROTO_UDP; | ||
1449 | |||
1450 | default: | ||
1451 | return proto; | ||
1452 | } | ||
1453 | } | ||
1454 | |||
1455 | Socket net_socket(Family domain, int type, int protocol) | 1434 | Socket net_socket(Family domain, int type, int protocol) |
1456 | { | 1435 | { |
1457 | const int platform_domain = make_family(domain); | 1436 | const int platform_domain = make_family(domain); |