diff options
author | Bruno Cabral <bcabral@uw.edu> | 2015-08-21 04:15:24 +0000 |
---|---|---|
committer | Bruno Cabral <bcabral@uw.edu> | 2015-08-21 04:15:24 +0000 |
commit | ecc90530eb2b3f4d0d5037d8d8b69722dda2a2cb (patch) | |
tree | e43497b563f2d7ee7856fb8f2f57bb8f2598b371 /src/nat/nat.c | |
parent | 946e39d3696847334b5316902c1eeb84d4c34cb0 (diff) | |
download | gnunet-ecc90530eb2b3f4d0d5037d8d8b69722dda2a2cb.tar.gz gnunet-ecc90530eb2b3f4d0d5037d8d8b69722dda2a2cb.zip |
Fix crashes and memory leaks.
Diffstat (limited to 'src/nat/nat.c')
-rw-r--r-- | src/nat/nat.c | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/nat/nat.c b/src/nat/nat.c index 42fe843e3..e51c001f2 100644 --- a/src/nat/nat.c +++ b/src/nat/nat.c | |||
@@ -407,7 +407,7 @@ struct GNUNET_NAT_Handle | |||
407 | int use_stun; | 407 | int use_stun; |
408 | 408 | ||
409 | /** | 409 | /** |
410 | * How often should se check STUN ? | 410 | * How often should we check STUN ? |
411 | */ | 411 | */ |
412 | struct GNUNET_TIME_Relative stun_frequency; | 412 | struct GNUNET_TIME_Relative stun_frequency; |
413 | 413 | ||
@@ -1109,7 +1109,8 @@ list_interfaces (void *cls, | |||
1109 | * @param cls the NAT handle | 1109 | * @param cls the NAT handle |
1110 | * @param result , the status | 1110 | * @param result , the status |
1111 | */ | 1111 | */ |
1112 | static void stun_request_callback(void *cls, | 1112 | static void |
1113 | stun_request_callback(void *cls, | ||
1113 | enum GNUNET_NAT_StatusCode result) | 1114 | enum GNUNET_NAT_StatusCode result) |
1114 | { | 1115 | { |
1115 | 1116 | ||
@@ -1129,16 +1130,18 @@ static void stun_request_callback(void *cls, | |||
1129 | }; | 1130 | }; |
1130 | 1131 | ||
1131 | /** | 1132 | /** |
1132 | * Check if STUN can decode the packet | 1133 | * CHECK if is a valid STUN packet sending to GNUNET_NAT_stun_handle_packet. |
1134 | * It also check if it can handle the packet based on the NAT handler. | ||
1135 | * You don't need to call anything else to check if the packet is valid, | ||
1133 | * | 1136 | * |
1134 | * @param cls the NAT handle | 1137 | * @param cls the NAT handle |
1135 | * @param data, packet | 1138 | * @param data, packet |
1136 | * @param len, packet lenght | 1139 | * @param len, packet length |
1137 | * | 1140 | * |
1138 | * @return GNUNET_NO if it can't decode, GNUNET_YES if is a packet | 1141 | * @return #GNUNET_NO if it can't decode, #GNUNET_YES if is a packet |
1139 | */ | 1142 | */ |
1140 | int | 1143 | int |
1141 | GNUNET_NAT_try_decode_stun_packet(void *cls, const void *data, size_t len) | 1144 | GNUNET_NAT_is_valid_stun_packet(void *cls, const void *data, size_t len) |
1142 | { | 1145 | { |
1143 | struct GNUNET_NAT_Handle *h = cls; | 1146 | struct GNUNET_NAT_Handle *h = cls; |
1144 | struct sockaddr_in answer; | 1147 | struct sockaddr_in answer; |
@@ -1147,6 +1150,10 @@ GNUNET_NAT_try_decode_stun_packet(void *cls, const void *data, size_t len) | |||
1147 | if(!h->waiting_stun) | 1150 | if(!h->waiting_stun) |
1148 | return GNUNET_NO; | 1151 | return GNUNET_NO; |
1149 | 1152 | ||
1153 | /*We dont have STUN installed*/ | ||
1154 | if(!h->use_stun) | ||
1155 | return GNUNET_NO; | ||
1156 | |||
1150 | /* Empty the answer structure */ | 1157 | /* Empty the answer structure */ |
1151 | memset(&answer, 0, sizeof(struct sockaddr_in)); | 1158 | memset(&answer, 0, sizeof(struct sockaddr_in)); |
1152 | 1159 | ||
@@ -1154,7 +1161,7 @@ GNUNET_NAT_try_decode_stun_packet(void *cls, const void *data, size_t len) | |||
1154 | int valid = GNUNET_NAT_stun_handle_packet(data,len, &answer); | 1161 | int valid = GNUNET_NAT_stun_handle_packet(data,len, &answer); |
1155 | if(valid) | 1162 | if(valid) |
1156 | { | 1163 | { |
1157 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1164 | LOG (GNUNET_ERROR_TYPE_INFO, |
1158 | "Stun server returned IP %s , with port %d \n", inet_ntoa(answer.sin_addr), ntohs(answer.sin_port)); | 1165 | "Stun server returned IP %s , with port %d \n", inet_ntoa(answer.sin_addr), ntohs(answer.sin_port)); |
1159 | /* ADD IP AS VALID*/ | 1166 | /* ADD IP AS VALID*/ |
1160 | add_to_address_list (h, LAL_EXTERNAL_IP, (const struct sockaddr *) &answer, | 1167 | add_to_address_list (h, LAL_EXTERNAL_IP, (const struct sockaddr *) &answer, |
@@ -1183,20 +1190,25 @@ process_stun (void *cls, | |||
1183 | { | 1190 | { |
1184 | struct GNUNET_NAT_Handle *h = cls; | 1191 | struct GNUNET_NAT_Handle *h = cls; |
1185 | 1192 | ||
1186 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1193 | h->stun_task = NULL; |
1187 | "I will do a STUN request\n"); | ||
1188 | 1194 | ||
1189 | 1195 | ||
1190 | h->stun_task = NULL; | ||
1191 | h->waiting_stun = GNUNET_YES; | ||
1192 | 1196 | ||
1193 | struct StunServerList* elem = h->actual_stun_server; | 1197 | struct StunServerList* elem = h->actual_stun_server; |
1194 | 1198 | ||
1195 | /* Make the request */ | 1199 | /* Make the request */ |
1196 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1200 | LOG (GNUNET_ERROR_TYPE_INFO, |
1197 | "I will request the stun server %s:%i !\n", elem->address, elem->port); | 1201 | "I will request the stun server %s:%i !\n", elem->address, elem->port); |
1198 | 1202 | ||
1199 | GNUNET_NAT_stun_make_request(elem->address, elem->port, h->socket, &stun_request_callback, NULL); | 1203 | if(GNUNET_OK == GNUNET_NAT_stun_make_request(elem->address, elem->port, h->socket, &stun_request_callback, NULL)) |
1204 | { | ||
1205 | h->waiting_stun = GNUNET_YES; | ||
1206 | } | ||
1207 | else | ||
1208 | { | ||
1209 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
1210 | "STUN request failed %s:%i !\n", elem->address, elem->port); | ||
1211 | } | ||
1200 | 1212 | ||
1201 | h->stun_task = | 1213 | h->stun_task = |
1202 | GNUNET_SCHEDULER_add_delayed (h->stun_frequency, | 1214 | GNUNET_SCHEDULER_add_delayed (h->stun_frequency, |
@@ -1588,7 +1600,8 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1588 | } | 1600 | } |
1589 | 1601 | ||
1590 | /* ENABLE STUN ONLY ON UDP*/ | 1602 | /* ENABLE STUN ONLY ON UDP*/ |
1591 | if(!is_tcp && (NULL != sock) && h->use_stun ) { | 1603 | if(!is_tcp && (NULL != sock) && h->use_stun ) |
1604 | { | ||
1592 | h->socket = sock; | 1605 | h->socket = sock; |
1593 | h->actual_stun_server = NULL; | 1606 | h->actual_stun_server = NULL; |
1594 | 1607 | ||
@@ -1772,6 +1785,11 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h) | |||
1772 | GNUNET_SCHEDULER_cancel (h->dns_task); | 1785 | GNUNET_SCHEDULER_cancel (h->dns_task); |
1773 | h->dns_task = NULL; | 1786 | h->dns_task = NULL; |
1774 | } | 1787 | } |
1788 | if (NULL != h->stun_task) | ||
1789 | { | ||
1790 | GNUNET_SCHEDULER_cancel (h->stun_task); | ||
1791 | h->stun_task = NULL; | ||
1792 | } | ||
1775 | if (NULL != h->server_proc) | 1793 | if (NULL != h->server_proc) |
1776 | { | 1794 | { |
1777 | if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) | 1795 | if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) |