diff options
author | iphydf <iphydf@users.noreply.github.com> | 2020-03-14 03:49:28 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2020-03-14 13:48:56 +0000 |
commit | 8795c5f987e2bd716d59a96418f1c7f6f9e83839 (patch) | |
tree | e3650c76847df0871e853964a9c54c16757b2205 | |
parent | 94d98ddd863279d5bf811d0107d73c373380dda2 (diff) |
Avoid casting back and forth between void-ptr.
In windows network code, we implement inet_pton and inet_ntop, which take
void pointers. We can do slightly better because we already know the type
when we call these functions, so we can avoid casting between void
pointer and the addr struct types.
-rw-r--r-- | toxav/toxav.c | 2 | ||||
-rw-r--r-- | toxav/toxav_old.c | 12 | ||||
-rw-r--r-- | toxcore/network.c | 62 |
3 files changed, 62 insertions, 14 deletions
diff --git a/toxav/toxav.c b/toxav/toxav.c index ba27fe27..597d7bea 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c | |||
@@ -131,7 +131,9 @@ ToxAV *toxav_new(Tox *tox, Toxav_Err_New *error) | |||
131 | 131 | ||
132 | // TODO(iphydf): Don't rely on toxcore internals. | 132 | // TODO(iphydf): Don't rely on toxcore internals. |
133 | Messenger *m; | 133 | Messenger *m; |
134 | //!TOKSTYLE- | ||
134 | m = *(Messenger **)tox; | 135 | m = *(Messenger **)tox; |
136 | //!TOKSTYLE+ | ||
135 | 137 | ||
136 | if (m->msi_packet) { | 138 | if (m->msi_packet) { |
137 | rc = TOXAV_ERR_NEW_MULTIPLE; | 139 | rc = TOXAV_ERR_NEW_MULTIPLE; |
diff --git a/toxav/toxav_old.c b/toxav/toxav_old.c index e5cfbadc..9ccd1365 100644 --- a/toxav/toxav_old.c +++ b/toxav/toxav_old.c | |||
@@ -24,7 +24,9 @@ | |||
24 | int toxav_add_av_groupchat(Tox *tox, audio_data_cb *audio_callback, void *userdata) | 24 | int toxav_add_av_groupchat(Tox *tox, audio_data_cb *audio_callback, void *userdata) |
25 | { | 25 | { |
26 | // TODO(iphydf): Don't rely on toxcore internals. | 26 | // TODO(iphydf): Don't rely on toxcore internals. |
27 | //!TOKSTYLE- | ||
27 | Messenger *m = *(Messenger **)tox; | 28 | Messenger *m = *(Messenger **)tox; |
29 | //!TOKSTYLE+ | ||
28 | return add_av_groupchat(m->log, tox, m->conferences_object, audio_callback, userdata); | 30 | return add_av_groupchat(m->log, tox, m->conferences_object, audio_callback, userdata); |
29 | } | 31 | } |
30 | 32 | ||
@@ -42,7 +44,9 @@ int toxav_join_av_groupchat(Tox *tox, uint32_t friendnumber, const uint8_t *data | |||
42 | audio_data_cb *audio_callback, void *userdata) | 44 | audio_data_cb *audio_callback, void *userdata) |
43 | { | 45 | { |
44 | // TODO(iphydf): Don't rely on toxcore internals. | 46 | // TODO(iphydf): Don't rely on toxcore internals. |
47 | //!TOKSTYLE- | ||
45 | Messenger *m = *(Messenger **)tox; | 48 | Messenger *m = *(Messenger **)tox; |
49 | //!TOKSTYLE+ | ||
46 | return join_av_groupchat(m->log, tox, m->conferences_object, friendnumber, data, length, audio_callback, userdata); | 50 | return join_av_groupchat(m->log, tox, m->conferences_object, friendnumber, data, length, audio_callback, userdata); |
47 | } | 51 | } |
48 | 52 | ||
@@ -63,7 +67,9 @@ int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, u | |||
63 | uint32_t sample_rate) | 67 | uint32_t sample_rate) |
64 | { | 68 | { |
65 | // TODO(iphydf): Don't rely on toxcore internals. | 69 | // TODO(iphydf): Don't rely on toxcore internals. |
70 | //!TOKSTYLE- | ||
66 | Messenger *m = *(Messenger **)tox; | 71 | Messenger *m = *(Messenger **)tox; |
72 | //!TOKSTYLE+ | ||
67 | return group_send_audio(m->conferences_object, groupnumber, pcm, samples, channels, sample_rate); | 73 | return group_send_audio(m->conferences_object, groupnumber, pcm, samples, channels, sample_rate); |
68 | } | 74 | } |
69 | 75 | ||
@@ -88,7 +94,9 @@ int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, u | |||
88 | int toxav_groupchat_enable_av(Tox *tox, uint32_t groupnumber, audio_data_cb *audio_callback, void *userdata) | 94 | int toxav_groupchat_enable_av(Tox *tox, uint32_t groupnumber, audio_data_cb *audio_callback, void *userdata) |
89 | { | 95 | { |
90 | // TODO(iphydf): Don't rely on toxcore internals. | 96 | // TODO(iphydf): Don't rely on toxcore internals. |
97 | //!TOKSTYLE- | ||
91 | Messenger *m = *(Messenger **)tox; | 98 | Messenger *m = *(Messenger **)tox; |
99 | //!TOKSTYLE+ | ||
92 | return groupchat_enable_av(m->log, tox, m->conferences_object, groupnumber, audio_callback, userdata); | 100 | return groupchat_enable_av(m->log, tox, m->conferences_object, groupnumber, audio_callback, userdata); |
93 | } | 101 | } |
94 | 102 | ||
@@ -100,7 +108,9 @@ int toxav_groupchat_enable_av(Tox *tox, uint32_t groupnumber, audio_data_cb *aud | |||
100 | int toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber) | 108 | int toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber) |
101 | { | 109 | { |
102 | // TODO(iphydf): Don't rely on toxcore internals. | 110 | // TODO(iphydf): Don't rely on toxcore internals. |
111 | //!TOKSTYLE- | ||
103 | Messenger *m = *(Messenger **)tox; | 112 | Messenger *m = *(Messenger **)tox; |
113 | //!TOKSTYLE+ | ||
104 | return groupchat_disable_av(m->conferences_object, groupnumber); | 114 | return groupchat_disable_av(m->conferences_object, groupnumber); |
105 | } | 115 | } |
106 | 116 | ||
@@ -109,6 +119,8 @@ int toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber) | |||
109 | bool toxav_groupchat_av_enabled(Tox *tox, uint32_t groupnumber) | 119 | bool toxav_groupchat_av_enabled(Tox *tox, uint32_t groupnumber) |
110 | { | 120 | { |
111 | // TODO(iphydf): Don't rely on toxcore internals. | 121 | // TODO(iphydf): Don't rely on toxcore internals. |
122 | //!TOKSTYLE- | ||
112 | Messenger *m = *(Messenger **)tox; | 123 | Messenger *m = *(Messenger **)tox; |
124 | //!TOKSTYLE+ | ||
113 | return groupchat_av_enabled(m->conferences_object, groupnumber); | 125 | return groupchat_av_enabled(m->conferences_object, groupnumber); |
114 | } | 126 | } |
diff --git a/toxcore/network.c b/toxcore/network.c index 67122ea3..d6d5bfd4 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -83,6 +83,26 @@ | |||
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) | ||
87 | { | ||
88 | return inet_ntop(family, addr, buf, bufsize); | ||
89 | } | ||
90 | |||
91 | static const char *inet_ntop6(int family, const struct in6_addr *addr, char *buf, size_t bufsize) | ||
92 | { | ||
93 | return inet_ntop(family, addr, buf, bufsize); | ||
94 | } | ||
95 | |||
96 | static int inet_pton4(int family, const char *addrString, struct in_addr *addrbuf) | ||
97 | { | ||
98 | return inet_pton(family, addrString, addrbuf); | ||
99 | } | ||
100 | |||
101 | static int inet_pton6(int family, const char *addrString, struct in6_addr *addrbuf) | ||
102 | { | ||
103 | return inet_pton(family, addrString, addrbuf); | ||
104 | } | ||
105 | |||
86 | #else | 106 | #else |
87 | #ifndef IPV6_V6ONLY | 107 | #ifndef IPV6_V6ONLY |
88 | #define IPV6_V6ONLY 27 | 108 | #define IPV6_V6ONLY 27 |
@@ -90,14 +110,14 @@ | |||
90 | 110 | ||
91 | #define TOX_EWOULDBLOCK WSAEWOULDBLOCK | 111 | #define TOX_EWOULDBLOCK WSAEWOULDBLOCK |
92 | 112 | ||
93 | static const char *inet_ntop(int family, const void *addr, char *buf, size_t bufsize) | 113 | static const char *inet_ntop4(int family, const struct in_addr *addr, char *buf, size_t bufsize) |
94 | { | 114 | { |
95 | if (family == AF_INET) { | 115 | if (family == AF_INET) { |
96 | struct sockaddr_in saddr; | 116 | struct sockaddr_in saddr; |
97 | memset(&saddr, 0, sizeof(saddr)); | 117 | memset(&saddr, 0, sizeof(saddr)); |
98 | 118 | ||
99 | saddr.sin_family = AF_INET; | 119 | saddr.sin_family = AF_INET; |
100 | saddr.sin_addr = *(const struct in_addr *)addr; | 120 | saddr.sin_addr = *addr; |
101 | 121 | ||
102 | DWORD len = bufsize; | 122 | DWORD len = bufsize; |
103 | 123 | ||
@@ -106,12 +126,19 @@ static const char *inet_ntop(int family, const void *addr, char *buf, size_t buf | |||
106 | } | 126 | } |
107 | 127 | ||
108 | return buf; | 128 | return buf; |
109 | } else if (family == AF_INET6) { | 129 | } |
130 | |||
131 | return nullptr; | ||
132 | } | ||
133 | |||
134 | static const char *inet_ntop6(int family, const struct in6_addr *addr, char *buf, size_t bufsize) | ||
135 | { | ||
136 | if (family == AF_INET6) { | ||
110 | struct sockaddr_in6 saddr; | 137 | struct sockaddr_in6 saddr; |
111 | memset(&saddr, 0, sizeof(saddr)); | 138 | memset(&saddr, 0, sizeof(saddr)); |
112 | 139 | ||
113 | saddr.sin6_family = AF_INET6; | 140 | saddr.sin6_family = AF_INET6; |
114 | saddr.sin6_addr = *(const struct in6_addr *)addr; | 141 | saddr.sin6_addr = *addr; |
115 | 142 | ||
116 | DWORD len = bufsize; | 143 | DWORD len = bufsize; |
117 | 144 | ||
@@ -125,7 +152,7 @@ static const char *inet_ntop(int family, const void *addr, char *buf, size_t buf | |||
125 | return nullptr; | 152 | return nullptr; |
126 | } | 153 | } |
127 | 154 | ||
128 | static int inet_pton(int family, const char *addrString, void *addrbuf) | 155 | static int inet_pton4(int family, const char *addrString, struct in_addr *addrbuf) |
129 | { | 156 | { |
130 | if (family == AF_INET) { | 157 | if (family == AF_INET) { |
131 | struct sockaddr_in saddr; | 158 | struct sockaddr_in saddr; |
@@ -137,10 +164,17 @@ static int inet_pton(int family, const char *addrString, void *addrbuf) | |||
137 | return 0; | 164 | return 0; |
138 | } | 165 | } |
139 | 166 | ||
140 | *(struct in_addr *)addrbuf = saddr.sin_addr; | 167 | *addrbuf = saddr.sin_addr; |
141 | 168 | ||
142 | return 1; | 169 | return 1; |
143 | } else if (family == AF_INET6) { | 170 | } |
171 | |||
172 | return 0; | ||
173 | } | ||
174 | |||
175 | static int inet_pton6(int family, const char *addrString, struct in6_addr *addrbuf) | ||
176 | { | ||
177 | if (family == AF_INET6) { | ||
144 | struct sockaddr_in6 saddr; | 178 | struct sockaddr_in6 saddr; |
145 | memset(&saddr, 0, sizeof(saddr)); | 179 | memset(&saddr, 0, sizeof(saddr)); |
146 | 180 | ||
@@ -150,7 +184,7 @@ static int inet_pton(int family, const char *addrString, void *addrbuf) | |||
150 | return 0; | 184 | return 0; |
151 | } | 185 | } |
152 | 186 | ||
153 | *(struct in6_addr *)addrbuf = saddr.sin6_addr; | 187 | *addrbuf = saddr.sin6_addr; |
154 | 188 | ||
155 | return 1; | 189 | return 1; |
156 | } | 190 | } |
@@ -1085,14 +1119,14 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
1085 | fill_addr4(ip->ip.v4, &addr); | 1119 | fill_addr4(ip->ip.v4, &addr); |
1086 | 1120 | ||
1087 | ip_str[0] = 0; | 1121 | ip_str[0] = 0; |
1088 | inet_ntop(family, &addr, ip_str, length); | 1122 | inet_ntop4(family, &addr, ip_str, length); |
1089 | } else if (net_family_is_ipv6(ip->family)) { | 1123 | } else if (net_family_is_ipv6(ip->family)) { |
1090 | /* returns hex-groups enclosed into square brackets */ | 1124 | /* returns hex-groups enclosed into square brackets */ |
1091 | struct in6_addr addr; | 1125 | struct in6_addr addr; |
1092 | fill_addr6(ip->ip.v6, &addr); | 1126 | fill_addr6(ip->ip.v6, &addr); |
1093 | 1127 | ||
1094 | ip_str[0] = '['; | 1128 | ip_str[0] = '['; |
1095 | inet_ntop(family, &addr, &ip_str[1], length - 3); | 1129 | inet_ntop6(family, &addr, &ip_str[1], length - 3); |
1096 | size_t len = strlen(ip_str); | 1130 | size_t len = strlen(ip_str); |
1097 | ip_str[len] = ']'; | 1131 | ip_str[len] = ']'; |
1098 | ip_str[len + 1] = 0; | 1132 | ip_str[len + 1] = 0; |
@@ -1116,12 +1150,12 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length) | |||
1116 | 1150 | ||
1117 | if (net_family_is_ipv4(ip->family)) { | 1151 | if (net_family_is_ipv4(ip->family)) { |
1118 | const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; | 1152 | const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; |
1119 | return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; | 1153 | return inet_ntop4(make_family(ip->family), addr, address, length) != nullptr; |
1120 | } | 1154 | } |
1121 | 1155 | ||
1122 | if (net_family_is_ipv6(ip->family)) { | 1156 | if (net_family_is_ipv6(ip->family)) { |
1123 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; | 1157 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; |
1124 | return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; | 1158 | return inet_ntop6(make_family(ip->family), addr, address, length) != nullptr; |
1125 | } | 1159 | } |
1126 | 1160 | ||
1127 | return false; | 1161 | return false; |
@@ -1135,7 +1169,7 @@ bool addr_parse_ip(const char *address, IP *to) | |||
1135 | 1169 | ||
1136 | struct in_addr addr4; | 1170 | struct in_addr addr4; |
1137 | 1171 | ||
1138 | if (inet_pton(AF_INET, address, &addr4) == 1) { | 1172 | if (inet_pton4(AF_INET, address, &addr4) == 1) { |
1139 | to->family = net_family_ipv4; | 1173 | to->family = net_family_ipv4; |
1140 | get_ip4(&to->ip.v4, &addr4); | 1174 | get_ip4(&to->ip.v4, &addr4); |
1141 | return true; | 1175 | return true; |
@@ -1143,7 +1177,7 @@ bool addr_parse_ip(const char *address, IP *to) | |||
1143 | 1177 | ||
1144 | struct in6_addr addr6; | 1178 | struct in6_addr addr6; |
1145 | 1179 | ||
1146 | if (inet_pton(AF_INET6, address, &addr6) == 1) { | 1180 | if (inet_pton6(AF_INET6, address, &addr6) == 1) { |
1147 | to->family = net_family_ipv6; | 1181 | to->family = net_family_ipv6; |
1148 | get_ip6(&to->ip.v6, &addr6); | 1182 | get_ip6(&to->ip.v6, &addr6); |
1149 | return true; | 1183 | return true; |