diff options
| author | Anton <anton@picapica> | 2017-12-28 22:20:05 +0400 |
|---|---|---|
| committer | Anton <anton@picapica> | 2017-12-28 22:20:05 +0400 |
| commit | e963ba6aec1a0f211e1023ebb42afabe01997685 (patch) | |
| tree | 016017caa6335f81a9a4ab4618db43af64d7660c | |
| parent | 5a8663c16c1409c990824c0cf2358db237de6a61 (diff) | |
| download | pica-pica-e963ba6aec1a0f211e1023ebb42afabe01997685.tar.gz pica-pica-e963ba6aec1a0f211e1023ebb42afabe01997685.zip | |
implemented PICA_upnp_autoconfigure_ipv4()
| -rw-r--r-- | PICA_netconf.c | 58 | ||||
| -rw-r--r-- | PICA_netconf.h | 4 |
2 files changed, 62 insertions, 0 deletions
diff --git a/PICA_netconf.c b/PICA_netconf.c index c590b94..da48c52 100644 --- a/PICA_netconf.c +++ b/PICA_netconf.c @@ -1,5 +1,13 @@ #include "PICA_netconf.h" +#ifdef HAVE_LIBMINIUPNPC +#include <miniupnpc/miniupnpc.h> +#include <miniupnpc/miniwget.h> +#include <miniupnpc/upnpcommands.h> +#include <miniupnpc/upnperrors.h> +#include <stdio.h> +#endif + #define PREFIX4 htonl(0xf0000000U) #define PREFIX8 htonl(0xff000000U) #define PREFIX12 htonl(0xfff00000U) @@ -26,3 +34,53 @@ int PICA_is_reserved_addr_ipv4(in_addr_t addr) return 0; } +#ifdef HAVE_LIBMINIUPNPC +int PICA_upnp_autoconfigure_ipv4(int public_port, int local_port, char *public_ip) +{ + struct UPNPDev *devlist = NULL; + int error = 0; + struct UPNPUrls urls; + struct IGDdatas data; + char lanaddr[64]; + char ppstr[8]; + char lpstr[8]; + int ret; + + devlist = upnpDiscover(2000, 0, 0, 0, 0, 2, &error); + + ret = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr)); + + if (ret != 1) + { + freeUPNPDevlist(devlist); + return 0; + } + + ret = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, public_ip); + + if (ret != UPNPCOMMAND_SUCCESS) + { + freeUPNPDevlist(devlist); + FreeUPNPUrls(&urls); + return 0; + } + + sprintf(ppstr, "%u", public_port); + sprintf(lpstr, "%u", local_port); + + ret = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, + ppstr, lpstr, lanaddr, "Pica Pica", "TCP", 0, "0"); + + + if (ret != UPNPCOMMAND_SUCCESS) + ret = 0; + else + ret = 1; + + freeUPNPDevlist(devlist); + FreeUPNPUrls(&urls); + return ret; +} +#endif + + diff --git a/PICA_netconf.h b/PICA_netconf.h index bab5738..22c043a 100644 --- a/PICA_netconf.h +++ b/PICA_netconf.h @@ -17,5 +17,9 @@ typedef u_short in_port_t; int PICA_is_reserved_addr_ipv4(in_addr_t); +#ifdef HAVE_LIBMINIUPNPC +int PICA_upnp_autoconfigure_ipv4(int public_port, int local_port, char *public_ip); +#endif + #endif |
