Hey! I'm available for hire (and I appreciate tips)  



You know what the problem with UPnP is?

It's that despite being a (mostly) HTTP API, you can't find any examples with curl!!!!

What the heck!?!?

Anyway, so I gleaned bits here and there from some examples embedded in code I found on the interwebs (and even some node.js code) to produce something a little more digestable:

How to find a UPnP Gateway with SSDP

I wanted to do a works-everywhere example using nc and tcpdump, but since the behavior varies quite wildly between OS X, Linux, and BSD, I had to chunk it out into a few different articles.

Take a look at either of these:

If you figure out how to get it working with netcat and tcpdump on Ubuntu Linux (or Raspbian on Raspberry PI), or other flavors of BSD or Linux, I'd love to hear about it in the comments.

How to add NAT port forwarding with curl

Template

curl '/Public_UPNP_C3' \
  -X 'POST' \
  -H 'Content-Type: text/xml; charset="utf-8"' \
  -H 'Connection: close' \
  -H 'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"' \
  -d '<?xml version="1.0"?>
<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></NewExternalPort>
  <NewProtocol></NewProtocol>
  <NewInternalPort></NewInternalPort>
  <NewInternalClient></NewInternalClient>
  <NewEnabled>1</NewEnabled>
  <NewPortMappingDescription></NewPortMappingDescription>
  <NewLeaseDuration></NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>'

Example

# For forwarding traffic to my Raspberry PI through my Netgear Router
curl 'http://10.0.0.1:5000/Public_UPNP_C3' \
  -X 'POST' \
  -H 'Content-Type: text/xml; charset="utf-8"' \
  -H 'Connection: close' \
  -H 'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"' \
  -d '<?xml version="1.0"?>
<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>443</NewExternalPort>
  <NewProtocol>TCP</NewProtocol>
  <NewInternalPort>443</NewInternalPort>
  <NewInternalClient>10.0.0.6</NewInternalClient>
  <NewEnabled>1</NewEnabled>
  <NewPortMappingDescription>node:nat:upnp</NewPortMappingDescription>
  <NewLeaseDuration>10</NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>'

By AJ ONeal

Was this useful to you? Share it!

Also, you can give me a tip or hire me.


Published

2015-2-5

Categories


Tags