English | Français
Dernière modification : 4 juilet 2008.
void redirect(uint16_t privateport, uint16_t publicport)
{
int r;
natpmp_t natpmp;
natpmpresp_t response;
initnatpmp(&natpmp);
sendnewportmappingrequest(&natpmp, NATPMP_PROTOCOL_TCP,
privateport, publicport, 3600);
do {
fd_set fds;
struct timeval timeout;
FD_ZERO(&fds);
FD_SET(natpmp.s, &fds);
getnatpmprequesttimeout(&natpmp, &timeout);
select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
r = readnatpmpresponseorretry(&natpmp, &response);
} while(r==NATPMP_TRYAGAIN);
printf("mapped public port %hu to localport %hu liftime %u\n",
response.newportmapping.mappedpublicport,
response.newportmapping.privateport,
response.newportmapping.lifetime);
closenatpmp(&natpmp);
}
{
natpmp_t natpmp;
natpmpresp_t response;
enum { Sinit=0, Ssendpub, Srecvpub, Ssendmap, Srecvmap, Sdone, Serror=1000 } natpmpstate = Sinit;
int r;
[...]
if(initnatpmp(&natpmp)<0)
natpmpstate = Serror;
else
natpmpstate = Ssendpub;
[...]
while(!finished_all_init_stuff) {
[...]
other init stuff :)
[...]
switch(natpmpstate) {
case Ssendpub:
if(sendpublicaddressrequest(&natpmp)<0);
natpmpstate = Serror;
else
natpmpstate = Srecvpub;
break;
case Srecvpub:
r = readnatpmpresponseorretry(&natpmp, &response);
if(r<0 && r!=NATPMP_TRYAGAIN)
natpmpstate = Serror;
else if(r!=NATPMP_TRYAGAIN) {
copy(publicaddress, response.publicaddress.addr);
natpmpstate = Ssendmap;
}
break;
case Ssendmap:
if(sendnewportmappingrequest(&natpmp, protocol, privateport, publicport, lifetime)<0);
natpmpstate = Serror;
else
natpmpstate = Srecvmap;
break;
case Srecvmap:
r = readnatpmpresponseorretry(&natpmp, &response);
if(r<0 && r!=NATPMP_TRYAGAIN)
natpmpstate = Serror;
else if(r!=NATPMP_TRYAGAIN) {
copy(publicport, response.newportmapping.mappedpublicport);
copy(privateport, response.newportmapping.privateport);
copy(mappinglifetime, response.newportmapping.lifetime);
natpmpclose(&natpmp);
natpmpstate = Sdone;
}
break;
}
[...]
}
[...]
}
Thomas Bernard
Pour me contacter, utilisez le forum MiniUPnP (en anglais) ou envoyez moi directement un email : miniupnp _AROBASE_ free _POINT_ fr