miniupnpc

本文详细介绍了UPnP端口映射的实现过程及应用案例,包括使用upnpc命令行工具进行端口映射的方法,以及通过Java实现端口映射的具体步骤。此外,还提供了调试过程中遇到的问题及解决方案。

upnp端口映射:

http://bbs.youkuaiyun.com/topics/70382968

 

UPnP端口映射实现过程(二)

http://blog.youkuaiyun.com/jiuaiwo1314/article/details/7656446

UPnP端口映射实现过程(一)

http://blog.youkuaiyun.com/jiuaiwo1314/article/details/7656427

如何实现W5200E01-M3中的UPnP(通用即插即用) 端口转发(三)

http://blog.youkuaiyun.com/wiznet2012/article/details/7693430

 

MiniUPnP download zone

http://miniupnp.tuxfamily.org/files/

 

 

a command line upnp client that will allow me to write scripts to enable and disable port forwarding on my router

upnpc -a `ifconfig wlan0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1` 4668 4668 TCP
upnpc -a `ifconfig wlan0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1` 4678 4678 UDP

upnpc-static -a 192.168.99.40 4678 4678 UDP

M-SEARCH * HTTP/1.1\r\n
HOST: 239.255.255.250:1900\r\n
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n
MAN: \"ssdp:discover\"\r\n
MX: 2\r\n\r\n



HTTP/1.1 200 OK\r\n
Cache-Control: max-age=300\r\n
Date: Wed, 12 Mar 2014 10:06:19 GMT\r\n
Ext: \r\n
Location: http://192.168.99.1:1780/InternetGatewayDevice.xml\r\n
Server: POSIX UPnP/1.0 DD-WRT Linux/V24\r\n
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n
USN: uuid:40C596C5-9BF9-CB72-3586-660E4C29BD20::urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n\r\n

soapbody
<?xml version=\"1.0\"?>\r\n
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<s:Body>
<u:GetStatusInfo xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\"></u:GetStatusInfo>
</s:Body></s:Envelope>\r\n

soappostheader

POST /control?WANIPConnection HTTP/1.1\r\n
Host: 192.168.99.1:1780\r\n
User-Agent: Ubuntu/12.04, UPnP/1.0, MiniUPnPc/1.9\r\n
Content-Length: 271\r\n
Content-Type: text/xml\r\n
SOAPAction: \"urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo\"\r\n
Connection: Close\r\n
Cache-Control: no-cache\r\n
Pragma: no-cache\r\n\r\n

SOAP Response:
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n
<s:Body>\r\n
<u:GetStatusInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">\r\n
<NewConnectionStatus>Connected</NewConnectionStatus>\r\n
<NewLastConnectionError>ERROR_NONE</NewLastConnectionError>\r\n
<NewUptime>451042</NewUptime>\r\n
</u:GetStatusInfoResponse>\r\n
</s:Body>\r\n
</s:Envelope>\r\n

<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n
<s:Body>\r\n
<u:GetExternalIPAddressResponse xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">\r\n
<NewExternalIPAddress>192.168.110.83</NewExternalIPAddress>\r\n
</u:GetExternalIPAddressResponse>\r\n
</s:Body>\r\n
</s:Envelope>\r\n


(gdb) p *urls
$20 = {controlURL = 0x8113830 "http://192.168.99.1:1780/control?WANIPConnection",
  ipcondescURL = 0x81137e0 "http://192.168.99.1:1780/x_wanipconnection.xml",
  controlURL_CIF = 0x8113880 "http://192.168.99.1:1780/control?WANCommonInterfaceConfig",
  controlURL_6FC = 0x8113718 "http://192.168.99.1:1780/", rootdescURL = 0x81138e0 "http://192.168.99.1:1780/InternetGatewayDevice.xml"}

AddPortMapping
<?xml version=\"1.0\"?>\r\n
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<s:Body>
<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>4678</NewExternalPort>
<NewProtocol>UDP</NewProtocol>
<NewInternalPort>4678</NewInternalPort>
<NewInternalClient>192.168.99.48</NewInternalClient>
<NewEnabled>1</NewEnabled>
<NewPortMappingDescription>libminiupnpc</NewPortMappingDescription>
<NewLeaseDuration>0</NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>\r\n

重复添加错误的相应:
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">
<errorCode>402</errorCode>
<errorDescription>Invalid Argument</errorDescription>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>


GetSpecificPortMappingEntry:
<?xml version=\"1.0\"?>\r\n
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<s:Body>
<u:GetSpecificPortMappingEntry xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">
<NewRemoteHost></NewRemoteHost>
<NewExternalPort>4678</NewExternalPort>
<NewProtocol>UDP</NewProtocol>
</u:GetSpecificPortMappingEntry>
</s:Body>
</s:Envelope>


<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n
<s:Body>\r\n
<u:GetSpecificPortMappingEntryResponse xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">\r\n
<NewInternalPort>4678</NewInternalPort>\r\n
<NewInternalClient>192.168.99.40</NewInternalClient>\r\n
<NewEnabled>1</NewEnabled>\r\n
<NewPortMappingDescription>libminiupnpc</NewPortMappingDescription>\r\n
<NewLeaseDuration>0</NewLeaseDuration>\r\n
</u:GetSpecificPortMappingEntryResponse>\r\n
</s:Body>\r\n
</s:Envelope>

可以在谷歌上这样搜索关键字:upnp port forwarding c
miniupnp官网
http://miniupnp.tuxfamily.org/files/

java版本的upnp端口映射:
http://upnp-portmapper.sourceforge.net/
java -jar PortMapper-1.9.6.jar -h
usage: java -jar PortMapper.jar [-a <ip port external_port protocol> | -d
       <external_port protocol [...]> | -g | -h | -l | -r <port protocol [...]>
       | -s]    [-i <index>]    [-u <class name>]
 -a <ip port external_port protocol>   Add port forwarding
 -d <external_port protocol [...]>     Delete port forwarding
 -g                                    Start graphical user interface (default)
 -h                                    print this message
 -i <index>                            Router index (if more than one is found)
 -l                                    List forwardings
 -r <port protocol [...]>              Add all forwardings to the current host
 -s                                    Get Connection status
 -u <class name>                       UPnP library
Protocol is UDP or TCP
UPnP library class names:
- org.chris.portmapper.router.sbbi.SBBIRouterFactory (default)
- org.chris.portmapper.router.weupnp.WeUPnPRouterFactory
- org.chris.portmapper.router.dummy.DummyRouterFactory

转载于:https://www.cnblogs.com/jingzhishen/p/3572215.html

# $Id: Makefile,v 1.119 2014/11/26 10:42:03 nanard Exp $ # MiniUPnP Project # http://miniupnp.free.fr/ # http://miniupnp.tuxfamily.org/ # https://github.com/miniupnp/miniupnp # (c) 2005-2014 Thomas Bernard # to install use : # $ make DESTDIR=/tmp/dummylocation install # or # $ INSTALLPREFIX=/usr/local make install # or # $ make install (default INSTALLPREFIX is /usr) OS = $(shell uname -s) VERSION = $(shell cat VERSION) ifeq ($(OS), Darwin) JARSUFFIX=mac endif ifeq ($(OS), Linux) JARSUFFIX=linux endif ifneq (,$(findstring NT-5.1,$(OS))) JARSUFFIX=win32 endif HAVE_IPV6 ?= yes export HAVE_IPV6 CC ?= gcc #AR = gar #CFLAGS = -O -g -DDEBUG CFLAGS ?= -O CFLAGS += -Wall CFLAGS += -W -Wstrict-prototypes CFLAGS += -fno-common CFLAGS += -DMINIUPNPC_SET_SOCKET_TIMEOUT CFLAGS += -DMINIUPNPC_GET_SRC_ADDR CFLAGS += -D_BSD_SOURCE -D_POSIX_C_SOURCE=200112L CFLAGS += -ansi LDFLAGS += -Wl,-rpath,/lib:/usr/lib # -DNO_GETADDRINFO INSTALL = install SH = /bin/sh JAVA = java # see http://code.google.com/p/jnaerator/ #JNAERATOR = jnaerator-0.9.7.jar #JNAERATOR = jnaerator-0.9.8-shaded.jar #JNAERATORARGS = -library miniupnpc #JNAERATOR = jnaerator-0.10-shaded.jar JNAERATOR = jnaerator-0.11-shaded.jar JNAERATORARGS = -mode StandaloneJar -runtime JNAerator -library miniupnpc JNAERATORBASEURL = http://jnaerator.googlecode.com/files/ ifeq (SunOS, $(OS)) LDFLAGS=-lsocket -lnsl -lresolv endif # APIVERSION is used to build SONAME APIVERSION = 12 SRCS = igd_desc_parse.c miniupnpc.c minixml.c minisoap.c miniwget.c \ upnpc.c upnpcommands.c upnpreplyparse.c testminixml.c \ minixmlvalid.c testupnpreplyparse.c minissdpc.c \ upnperrors.c testigddescparse.c testminiwget.c \ connecthostport.c portlistingparse.c receivedata.c \ testportlistingparse.c miniupnpcmodule.c \ minihttptestserver.c util.c \ listdevices.c LIBOBJS = miniwget.o minixml.o igd_desc_parse.o minisoap.o \ miniupnpc.o upnpreplyparse.o upnpcommands.o upnperrors.o \ connecthostport.o portlistingparse.o receivedata.o util.o ifneq ($(OS), AmigaOS) CFLAGS := -fPIC $(CFLAGS) LIBOBJS := $(LIBOBJS) minissdpc.o endif OBJS = $(patsubst %.c,%.o,$(SRCS)) # HEADERS to install HEADERS = miniupnpc.h miniwget.h upnpcommands.h igd_desc_parse.h \ upnpreplyparse.h upnperrors.h miniupnpctypes.h \ portlistingparse.h \ declspec.h # library names LIBRARY = libminiupnpc.a ifeq ($(OS), Darwin) SHAREDLIBRARY = libminiupnpc.dylib SONAME = $(basename $(SHAREDLIBRARY)).$(APIVERSION).dylib CFLAGS := -D_DARWIN_C_SOURCE $(CFLAGS) else ifeq ($(JARSUFFIX), win32) SHAREDLIBRARY = miniupnpc.dll else # Linux/BSD/etc. SHAREDLIBRARY = libminiupnpc.so SONAME = $(SHAREDLIBRARY).$(APIVERSION) endif endif EXECUTABLES = upnpc-static listdevices EXECUTABLES_ADDTESTS = testminixml minixmlvalid testupnpreplyparse \ testigddescparse testminiwget testportlistingparse TESTMINIXMLOBJS = minixml.o igd_desc_parse.o testminixml.o TESTMINIWGETOBJS = miniwget.o testminiwget.o connecthostport.o receivedata.o TESTUPNPREPLYPARSE = testupnpreplyparse.o minixml.o upnpreplyparse.o TESTPORTLISTINGPARSE = testportlistingparse.o minixml.o portlistingparse.o TESTIGDDESCPARSE = testigddescparse.o igd_desc_parse.o minixml.o \ miniupnpc.o miniwget.o upnpcommands.o upnpreplyparse.o \ minisoap.o connecthostport.o receivedata.o \ portlistingparse.o ifneq ($(OS), AmigaOS) EXECUTABLES := $(EXECUTABLES) upnpc-shared TESTMINIWGETOBJS := $(TESTMINIWGETOBJS) minissdpc.o TESTIGDDESCPARSE := $(TESTIGDDESCPARSE) minissdpc.o endif LIBDIR ?= lib # install directories INSTALLPREFIX ?= $(PREFIX)/usr INSTALLDIRINC = $(INSTALLPREFIX)/include/miniupnpc INSTALLDIRLIB = $(INSTALLPREFIX)/$(LIBDIR) INSTALLDIRBIN = $(INSTALLPREFIX)/bin INSTALLDIRMAN = $(INSTALLPREFIX)/share/man FILESTOINSTALL = $(LIBRARY) $(EXECUTABLES) ifneq ($(OS), AmigaOS) FILESTOINSTALL := $(FILESTOINSTALL) $(SHAREDLIBRARY) endif .PHONY: install clean depend all check test everything \ installpythonmodule updateversion # validateminixml validateminiwget all: $(LIBRARY) $(EXECUTABLES) test: check check: validateminixml validateminiwget validateupnpreplyparse \ validateportlistingparse validateigddescparse everything: all $(EXECUTABLES_ADDTESTS) pythonmodule: $(LIBRARY) miniupnpcmodule.c setup.py python setup.py build touch $@ installpythonmodule: pythonmodule python setup.py install pythonmodule3: $(LIBRARY) miniupnpcmodule.c setup.py python3 setup.py build touch $@ installpythonmodule3: pythonmodule3 python3 setup.py install validateminixml: minixmlvalid @echo "minixml validation test" ./minixmlvalid touch $@ validateminiwget: testminiwget minihttptestserver testminiwget.sh @echo "miniwget validation test" ./testminiwget.sh touch $@ validateupnpreplyparse: testupnpreplyparse testupnpreplyparse.sh @echo "upnpreplyparse validation test" ./testupnpreplyparse.sh touch $@ validateportlistingparse: testportlistingparse @echo "portlistingparse validation test" ./testportlistingparse touch $@ validateigddescparse: testigddescparse @echo "igd desc parse validation test" ./testigddescparse testdesc/new_LiveBox_desc.xml testdesc/new_LiveBox_desc.values ./testigddescparse testdesc/linksys_WAG200G_desc.xml testdesc/linksys_WAG200G_desc.values touch $@ clean: $(RM) $(LIBRARY) $(SHAREDLIBRARY) $(EXECUTABLES) $(OBJS) miniupnpcstrings.h $(RM) $(EXECUTABLES_ADDTESTS) # clean python stuff $(RM) pythonmodule pythonmodule3 $(RM) validateminixml validateminiwget validateupnpreplyparse $(RM) validateigddescparse $(RM) minihttptestserver $(RM) -r build/ dist/ #python setup.py clean # clean jnaerator stuff $(RM) _jnaerator.* java/miniupnpc_$(OS).jar distclean: clean $(RM) $(JNAERATOR) java/*.jar java/*.class out.errors.txt updateversion: miniupnpc.h cp miniupnpc.h miniupnpc.h.bak sed 's/\(.*MINIUPNPC_API_VERSION\s\+\)[0-9]\+/\1$(APIVERSION)/' < miniupnpc.h.bak > miniupnpc.h install: updateversion $(FILESTOINSTALL) $(INSTALL) -d $(DESTDIR)$(INSTALLDIRINC) $(INSTALL) -m 644 $(HEADERS) $(DESTDIR)$(INSTALLDIRINC) $(INSTALL) -d $(DESTDIR)$(INSTALLDIRLIB) $(INSTALL) -m 644 $(LIBRARY) $(DESTDIR)$(INSTALLDIRLIB) ifneq ($(OS), AmigaOS) $(INSTALL) -m 644 $(SHAREDLIBRARY) $(DESTDIR)$(INSTALLDIRLIB)/$(SONAME) ln -fs $(SONAME) $(DESTDIR)$(INSTALLDIRLIB)/$(SHAREDLIBRARY) endif $(INSTALL) -d $(DESTDIR)$(INSTALLDIRBIN) ifeq ($(OS), AmigaOS) $(INSTALL) -m 755 upnpc-static $(DESTDIR)$(INSTALLDIRBIN)/upnpc else $(INSTALL) -m 755 upnpc-shared $(DESTDIR)$(INSTALLDIRBIN)/upnpc endif $(INSTALL) -m 755 external-ip.sh $(DESTDIR)$(INSTALLDIRBIN)/external-ip ifneq ($(OS), AmigaOS) $(INSTALL) -d $(DESTDIR)$(INSTALLDIRMAN)/man3 $(INSTALL) -m 644 man3/miniupnpc.3 $(DESTDIR)$(INSTALLDIRMAN)/man3/miniupnpc.3 ifeq ($(OS), Linux) gzip -f $(DESTDIR)$(INSTALLDIRMAN)/man3/miniupnpc.3 endif endif cleaninstall: $(RM) -r $(DESTDIR)$(INSTALLDIRINC) $(RM) $(DESTDIR)$(INSTALLDIRLIB)/$(LIBRARY) $(RM) $(DESTDIR)$(INSTALLDIRLIB)/$(SHAREDLIBRARY) depend: makedepend -Y -- $(CFLAGS) -- $(SRCS) 2>/dev/null $(LIBRARY): $(LIBOBJS) $(AR) crs $@ $? $(SHAREDLIBRARY): $(LIBOBJS) ifeq ($(OS), Darwin) # $(CC) -dynamiclib $(LDFLAGS) -Wl,-install_name,$(SONAME) -o $@ $^ $(CC) -dynamiclib $(LDFLAGS) -Wl,-install_name,$(INSTALLDIRLIB)/$(SONAME) -o $@ $^ else $(CC) -shared $(LDFLAGS) -Wl,-soname,$(SONAME) -o $@ $^ endif upnpc-static: upnpc.o $(LIBRARY) $(CC) $(LDFLAGS) -o $@ $^ upnpc-shared: upnpc.o $(SHAREDLIBRARY) $(CC) $(LDFLAGS) -o $@ $^ listdevices: listdevices.o $(LIBRARY) $(LDLIBS) $(CC) $(LDFLAGS) -o $@ $^ testminixml: $(TESTMINIXMLOBJS) testminiwget: $(TESTMINIWGETOBJS) minixmlvalid: minixml.o minixmlvalid.o testupnpreplyparse: $(TESTUPNPREPLYPARSE) testigddescparse: $(TESTIGDDESCPARSE) testportlistingparse: $(TESTPORTLISTINGPARSE) miniupnpcstrings.h: miniupnpcstrings.h.in updateminiupnpcstrings.sh VERSION $(SH) updateminiupnpcstrings.sh # ftp tool supplied with OpenBSD can download files from http. jnaerator-%.jar: wget $(JNAERATORBASEURL)/$@ || \ curl -o $@ $(JNAERATORBASEURL)/$@ || \ ftp $(JNAERATORBASEURL)/$@ jar: $(SHAREDLIBRARY) $(JNAERATOR) $(JAVA) -jar $(JNAERATOR) $(JNAERATORARGS) \ miniupnpc.h declspec.h upnpcommands.h upnpreplyparse.h \ igd_desc_parse.h miniwget.h upnperrors.h $(SHAREDLIBRARY) \ -package fr.free.miniupnp -o . -jar java/miniupnpc_$(JARSUFFIX).jar -v mvn_install: mvn install:install-file -Dfile=java/miniupnpc_$(JARSUFFIX).jar \ -DgroupId=com.github \ -DartifactId=miniupnp \ -Dversion=$(VERSION) \ -Dpackaging=jar \ -Dclassifier=$(JARSUFFIX) \ -DgeneratePom=true \ -DcreateChecksum=true # make .deb packages deb: /usr/share/pyshared/stdeb all (python setup.py --command-packages=stdeb.command bdist_deb) # install .deb packages ideb: (sudo dpkg -i deb_dist/*.deb) /usr/share/pyshared/stdeb: /usr/share/doc/python-all-dev (sudo apt-get install python-stdeb) /usr/share/doc/python-all-dev: (sudo apt-get install python-all-dev) minihttptestserver: minihttptestserver.o # DO NOT DELETE THIS LINE -- make depend depends on it. igd_desc_parse.o: igd_desc_parse.h miniupnpc.o: miniupnpc.h declspec.h igd_desc_parse.h minissdpc.h miniwget.h miniupnpc.o: minisoap.h minixml.h upnpcommands.h upnpreplyparse.h miniupnpc.o: portlistingparse.h miniupnpctypes.h connecthostport.h miniupnpc.o: receivedata.h minixml.o: minixml.h minisoap.o: minisoap.h miniupnpcstrings.h miniwget.o: miniupnpcstrings.h miniwget.h declspec.h connecthostport.h miniwget.o: receivedata.h upnpc.o: miniwget.h declspec.h miniupnpc.h igd_desc_parse.h upnpcommands.h upnpc.o: upnpreplyparse.h portlistingparse.h miniupnpctypes.h upnperrors.h upnpcommands.o: upnpcommands.h upnpreplyparse.h portlistingparse.h declspec.h upnpcommands.o: miniupnpctypes.h miniupnpc.h igd_desc_parse.h upnpreplyparse.o: upnpreplyparse.h minixml.h testminixml.o: minixml.h igd_desc_parse.h minixmlvalid.o: minixml.h testupnpreplyparse.o: upnpreplyparse.h minissdpc.o: minissdpc.h miniupnpc.h declspec.h igd_desc_parse.h codelength.h upnperrors.o: upnperrors.h declspec.h upnpcommands.h upnpreplyparse.h upnperrors.o: portlistingparse.h miniupnpctypes.h miniupnpc.h upnperrors.o: igd_desc_parse.h testigddescparse.o: igd_desc_parse.h minixml.h miniupnpc.h declspec.h testminiwget.o: miniwget.h declspec.h connecthostport.o: connecthostport.h portlistingparse.o: portlistingparse.h declspec.h miniupnpctypes.h minixml.h receivedata.o: receivedata.h testportlistingparse.o: portlistingparse.h declspec.h miniupnpctypes.h miniupnpcmodule.o: miniupnpc.h declspec.h igd_desc_parse.h upnpcommands.h miniupnpcmodule.o: upnpreplyparse.h portlistingparse.h miniupnpctypes.h miniupnpcmodule.o: upnperrors.h listdevices.o: miniupnpc.h declspec.h igd_desc_parse.h 帮我分析下这个makefile,他会产生什么target?
最新发布
09-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值