libnatpmp

An attempt to make a portable and fully compliant implementation of the protocol for the client side
Download

libnatpmp Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Thomas Bernard
  • Publisher web site:
  • http://miniupnp.free.fr/libnatpmp.html

libnatpmp Tags


libnatpmp Description

An attempt to make a portable and fully compliant implementation of the protocol for the client side libnatpmp is an attempt to make a portable and fully compliant implementation of the protocol for the client side. It is based on non blocking sockets and all calls of the API are asynchronous. libnatpmp library is therefore very easy to integrate the NAT-PMP code to any event driven code.NAT-PMP is the direct concurrent to the UPnP IGD specification. It is providing a way to do NAT traversal. It is part of the Bonjour protocol specifications. You can get the description of this simple protocol here or by reading the draft. At the moment it is mainly supported by Apple software and hardware.Use libnatpmp in your programsIt should be very easy to use libnatpmp in any C/C++ program. All the API is described in the natpmp.h file. · First of all, a variable of type natpmp_t must be allocated and initnatpmp() must be called.· sendpublicaddressrequest() or sendnewportmappingrequest() can be called.· For each request sent, readnatpmpresponseorretry() should be called as long as it returns NATPMP_TRYAGAIN.· If you are waiting for the socket (field s in the natpmp_t object) to be ready for reading using system calls such as select() or poll(), you can use getnatpmprequesttimeout() in order to know how long to wait before calling readnatpmpresponseorretry() anyway.· To free all used resources, closenatpmp() can be called.Sample codeError checking hSample codeError checking has been deliberately omitted. Please check all return codes in natpmp.h file. simple sampleThis very simple sample just ask for a redirection and return when the job is done. 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 %un", response.newportmapping.mappedpublicport, response.newportmapping.privateport, response.newportmapping.lifetime); closenatpmp(&natpmp); }as been deliberately omitted. Please check all return codes in natpmp.h file.simple sampleThis very simple sample just ask for a redirection and return when the job is done. 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 %un", response.newportmapping.mappedpublicport, response.newportmapping.privateport, response.newportmapping.lifetime); closenatpmp(&natpmp); }more complex sampleWe could imagine the init phase of a P2P software. During this phase, the software tries to get the public IP address and add a port mapping. At the end of the process, natpmpstate will be either Sdone or Serror. { natpmp_t natpmp; natpmpresp_t response; enum { Sinit=0, Ssendpub, Srecvpub, Ssendmap, Srecvmap, Sdone, Serror=1000 } natpmpstate = Sinit; int r; if(initnatpmp(&natpmp)


libnatpmp Related Software