Changeset 13625 for trunk/libtransmission/natpmp.c
 Timestamp:
 Dec 5, 2012, 5:29:46 PM (9 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/libtransmission/natpmp.c
r13263 r13625 3 3 * 4 4 * This file is licensed by the GPL version 2. Works owned by the 5 * Transmission project are granted a special exemption to clause 2 (b)5 * Transmission project are granted a special exemption to clause 2 (b) 6 6 * so that the bulk of its code can remain under the MIT license. 7 7 * This exemption does not extend to derived works not owned by … … 15 15 #include <inttypes.h> 16 16 17 #include <event2/util.h> /* evutil_inet_ntop () */17 #include <event2/util.h> /* evutil_inet_ntop () */ 18 18 19 19 #define ENABLE_STRNATPMPERR … … 30 30 31 31 static const char * 32 getKey ( void ) { return _( "Port Forwarding (NATPMP)"); }32 getKey (void) { return _ ("Port Forwarding (NATPMP)"); } 33 33 34 34 typedef enum … … 64 64 65 65 static void 66 logVal (const char * func,67 int ret 68 { 69 if ( ret == NATPMP_TRYAGAIN)66 logVal (const char * func, 67 int ret) 68 { 69 if (ret == NATPMP_TRYAGAIN) 70 70 return; 71 if ( ret >= 0)72 tr_ninf ( getKey( ), _( "%s succeeded (%d)" ), func, ret);71 if (ret >= 0) 72 tr_ninf (getKey (), _ ("%s succeeded (%d)"), func, ret); 73 73 else 74 tr_ndbg (75 getKey (),74 tr_ndbg ( 75 getKey (), 76 76 "%s failed. Natpmp returned %d (%s); errno is %d (%s)", 77 func, ret, strnatpmperr ( ret ), errno, tr_strerror( errno ));77 func, ret, strnatpmperr (ret), errno, tr_strerror (errno)); 78 78 } 79 79 80 80 struct tr_natpmp* 81 tr_natpmpInit ( void)81 tr_natpmpInit (void) 82 82 { 83 83 struct tr_natpmp * nat; 84 84 85 nat = tr_new0 ( struct tr_natpmp, 1);85 nat = tr_new0 (struct tr_natpmp, 1); 86 86 nat>state = TR_NATPMP_DISCOVER; 87 87 nat>public_port = 0; … … 92 92 93 93 void 94 tr_natpmpClose ( tr_natpmp * nat)95 { 96 if ( nat)97 { 98 if ( nat>natpmp.s >= 0)99 tr_netCloseSocket ( nat>natpmp.s);100 tr_free ( nat);94 tr_natpmpClose (tr_natpmp * nat) 95 { 96 if (nat) 97 { 98 if (nat>natpmp.s >= 0) 99 tr_netCloseSocket (nat>natpmp.s); 100 tr_free (nat); 101 101 } 102 102 } 103 103 104 104 static int 105 canSendCommand ( const struct tr_natpmp * nat)106 { 107 return tr_time () >= nat>command_time;105 canSendCommand (const struct tr_natpmp * nat) 106 { 107 return tr_time () >= nat>command_time; 108 108 } 109 109 110 110 static void 111 setCommandTime ( struct tr_natpmp * nat)112 { 113 nat>command_time = tr_time () + COMMAND_WAIT_SECS;111 setCommandTime (struct tr_natpmp * nat) 112 { 113 nat>command_time = tr_time () + COMMAND_WAIT_SECS; 114 114 } 115 115 116 116 int 117 tr_natpmpPulse ( struct tr_natpmp * nat, tr_port private_port, bool is_enabled, tr_port * public_port)117 tr_natpmpPulse (struct tr_natpmp * nat, tr_port private_port, bool is_enabled, tr_port * public_port) 118 118 { 119 119 int ret; 120 120 121 if ( is_enabled && ( nat>state == TR_NATPMP_DISCOVER ))122 { 123 int val = initnatpmp ( &nat>natpmp, 0, 0);124 logVal ( "initnatpmp", val);125 val = sendpublicaddressrequest ( &nat>natpmp);126 logVal ( "sendpublicaddressrequest", val);121 if (is_enabled && (nat>state == TR_NATPMP_DISCOVER)) 122 { 123 int val = initnatpmp (&nat>natpmp, 0, 0); 124 logVal ("initnatpmp", val); 125 val = sendpublicaddressrequest (&nat>natpmp); 126 logVal ("sendpublicaddressrequest", val); 127 127 nat>state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB; 128 128 nat>has_discovered = true; 129 setCommandTime ( nat);130 } 131 132 if ( ( nat>state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ))129 setCommandTime (nat); 130 } 131 132 if ((nat>state == TR_NATPMP_RECV_PUB) && canSendCommand (nat)) 133 133 { 134 134 natpmpresp_t response; 135 const int val = readnatpmpresponseorretry ( &nat>natpmp, &response);136 logVal ( "readnatpmpresponseorretry", val);137 if ( val >= 0)135 const int val = readnatpmpresponseorretry (&nat>natpmp, &response); 136 logVal ("readnatpmpresponseorretry", val); 137 if (val >= 0) 138 138 { 139 139 char str[128]; 140 evutil_inet_ntop ( AF_INET, &response.pnu.publicaddress.addr, str, sizeof( str ));141 tr_ninf ( getKey( ), _( "Found public address \"%s\"" ), str);140 evutil_inet_ntop (AF_INET, &response.pnu.publicaddress.addr, str, sizeof (str)); 141 tr_ninf (getKey (), _ ("Found public address \"%s\""), str); 142 142 nat>state = TR_NATPMP_IDLE; 143 143 } 144 else if ( val != NATPMP_TRYAGAIN)144 else if (val != NATPMP_TRYAGAIN) 145 145 { 146 146 nat>state = TR_NATPMP_ERR; … … 148 148 } 149 149 150 if ( ( nat>state == TR_NATPMP_IDLE )  ( nat>state == TR_NATPMP_ERR ))151 { 152 if ( nat>is_mapped && ( !is_enabled  ( nat>private_port != private_port ) ))150 if ((nat>state == TR_NATPMP_IDLE)  (nat>state == TR_NATPMP_ERR)) 151 { 152 if (nat>is_mapped && (!is_enabled  (nat>private_port != private_port))) 153 153 nat>state = TR_NATPMP_SEND_UNMAP; 154 154 } 155 155 156 if ( ( nat>state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ))157 { 158 const int val = sendnewportmappingrequest (&nat>natpmp, NATPMP_PROTOCOL_TCP,156 if ((nat>state == TR_NATPMP_SEND_UNMAP) && canSendCommand (nat)) 157 { 158 const int val = sendnewportmappingrequest (&nat>natpmp, NATPMP_PROTOCOL_TCP, 159 159 nat>private_port, 160 160 nat>public_port, 161 0 162 logVal ( "sendnewportmappingrequest", val);161 0); 162 logVal ("sendnewportmappingrequest", val); 163 163 nat>state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP; 164 setCommandTime ( nat);165 } 166 167 if ( nat>state == TR_NATPMP_RECV_UNMAP)164 setCommandTime (nat); 165 } 166 167 if (nat>state == TR_NATPMP_RECV_UNMAP) 168 168 { 169 169 natpmpresp_t resp; 170 const int val = readnatpmpresponseorretry ( &nat>natpmp, &resp);171 logVal ( "readnatpmpresponseorretry", val);172 if ( val >= 0)170 const int val = readnatpmpresponseorretry (&nat>natpmp, &resp); 171 logVal ("readnatpmpresponseorretry", val); 172 if (val >= 0) 173 173 { 174 174 const int private_port = resp.pnu.newportmapping.privateport; 175 175 176 tr_ninf ( getKey( ), _( "no longer forwarding port %d" ), private_port);177 178 if ( nat>private_port == private_port)176 tr_ninf (getKey (), _ ("no longer forwarding port %d"), private_port); 177 178 if (nat>private_port == private_port) 179 179 { 180 180 nat>private_port = 0; … … 184 184 } 185 185 } 186 else if ( val != NATPMP_TRYAGAIN)186 else if (val != NATPMP_TRYAGAIN) 187 187 { 188 188 nat>state = TR_NATPMP_ERR; … … 190 190 } 191 191 192 if ( nat>state == TR_NATPMP_IDLE)193 { 194 if ( is_enabled && !nat>is_mapped && nat>has_discovered)192 if (nat>state == TR_NATPMP_IDLE) 193 { 194 if (is_enabled && !nat>is_mapped && nat>has_discovered) 195 195 nat>state = TR_NATPMP_SEND_MAP; 196 196 197 else if ( nat>is_mapped && tr_time( ) >= nat>renew_time)197 else if (nat>is_mapped && tr_time () >= nat>renew_time) 198 198 nat>state = TR_NATPMP_SEND_MAP; 199 199 } 200 200 201 if ( ( nat>state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ))202 { 203 const int val = sendnewportmappingrequest ( &nat>natpmp, NATPMP_PROTOCOL_TCP, private_port, private_port, LIFETIME_SECS);204 logVal ( "sendnewportmappingrequest", val);201 if ((nat>state == TR_NATPMP_SEND_MAP) && canSendCommand (nat)) 202 { 203 const int val = sendnewportmappingrequest (&nat>natpmp, NATPMP_PROTOCOL_TCP, private_port, private_port, LIFETIME_SECS); 204 logVal ("sendnewportmappingrequest", val); 205 205 nat>state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP; 206 setCommandTime ( nat);207 } 208 209 if ( nat>state == TR_NATPMP_RECV_MAP)206 setCommandTime (nat); 207 } 208 209 if (nat>state == TR_NATPMP_RECV_MAP) 210 210 { 211 211 natpmpresp_t resp; 212 const int val = readnatpmpresponseorretry ( &nat>natpmp, &resp);213 logVal ( "readnatpmpresponseorretry", val);214 if ( val >= 0)212 const int val = readnatpmpresponseorretry (&nat>natpmp, &resp); 213 logVal ("readnatpmpresponseorretry", val); 214 if (val >= 0) 215 215 { 216 216 nat>state = TR_NATPMP_IDLE; 217 217 nat>is_mapped = true; 218 nat>renew_time = tr_time ( ) + ( resp.pnu.newportmapping.lifetime / 2);218 nat>renew_time = tr_time () + (resp.pnu.newportmapping.lifetime / 2); 219 219 nat>private_port = resp.pnu.newportmapping.privateport; 220 220 nat>public_port = resp.pnu.newportmapping.mappedpublicport; 221 tr_ninf ( getKey( ), _( "Port %d forwarded successfully" ), nat>private_port);222 } 223 else if ( val != NATPMP_TRYAGAIN)221 tr_ninf (getKey (), _ ("Port %d forwarded successfully"), nat>private_port); 222 } 223 else if (val != NATPMP_TRYAGAIN) 224 224 { 225 225 nat>state = TR_NATPMP_ERR; … … 227 227 } 228 228 229 switch ( nat>state)229 switch (nat>state) 230 230 { 231 231 case TR_NATPMP_IDLE:
Note: See TracChangeset
for help on using the changeset viewer.