Discussion:
[OpenWrt-Users] UPnP client
Dom Latter
2012-12-07 10:28:39 UTC
Permalink
Hi, first post here...

I'm using Attitude Adjustment (12.09-beta, r33312) on a TP-LINK WR-842ND.

The main thing to note is that I am using it "behind" an existing router.

The aim is to be able to "drop" it into a domestic network set up and
have it automatically configure itself to make itself visible / available
to me, on the outside, on port 1234, with minimal (or no) configuration [1].

Three stages to make this happen:
1. Connect to LAN with DHCP
2. Dynamic DNS client to set up a known hostname
3. UPnP to set up port forwarding on existing router

Stage 1 is straightforward, set to DHCP, everything works.

Stage 2 - I've set "option interface 'lan'" inside a fairly normal
/etc/config/ddns, seems to work fine with no-ip.com

Stage 3 has been more interesting. I've installed miniupnpc through
opkg. At first I tried to hook the upnpc commands into the dhcp exit
(i.e to run them after obtaining an IP address). Then I tried rc.local.
Finally I ended up with an /etc/rc.local that looks a bit like:
sleep 10
IP=$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
date > /tmp/dhcp-log
echo "++++++ ADD 1234 for $IP" >> /tmp/dhcp-log
upnpc -a $IP 1234 1234 tcp >> /tmp/dhcp-log 2>&1

Without the "sleep 10" it doesn't work - for some reason upnp doesn't seem
to work until things have had time to settle down. Does anyone know why?
Is there a way of knowing *when* the existing router is ready?

A couple of other questions:
Will this distro be out of beta soon?

I am logging in to /tmp, as above - what's the right way of doing it?


[1] - yes, many domestic routers will need to have UPnP enabled first,
but it should be reasonably easy to walk / talk someone through that.
Jo-Philipp Wich
2012-12-07 10:40:35 UTC
Permalink
Use hotplug to trigger tasks like that, example:

# /etc/hotplug.d/iface/90-upnpc:

. /lib/functions/network.sh

if [ "$ACTION" = ifup ] && [ "$INTERFACE" != lo ]; then
local ipaddr
network_get_ipaddr ipaddr "$INTERFACE" || exit 0

logger -t 90-upnpc "Adding port forward 1234 to address $ipaddr"
/usr/bin/upnpc -a "$ipaddr" 1234 1234 tcp
fi


~ Jow
Dom Latter
2012-12-07 13:40:47 UTC
Permalink
Many thanks Jo-Philipp!

I've been looking around the site but have not yet found documentation for "this
sort of thing" - any hints?
Post by Jo-Philipp Wich
. /lib/functions/network.sh
if [ "$ACTION" = ifup ] && [ "$INTERFACE" != lo ]; then
I have changed this to:
if [ "$ACTION" = ifup ] && [ "$INTERFACE" = lan ]; then
as otherwise it seemed to be doing it to the loopback (127.0.0.1) interface as well.

However...

I still have to have a "sleep" in the script - otherwise it does not find
a valid IGD. It seems to need about ten or a bit more seconds, but I am
giving it 60 to be on the safe side.

Also, I am back to logging in /tmp:
/usr/bin/upnpc -a "$ipaddr" 1234 1234 tcp >> /tmp/pnpc.log
as I am not sure how to use logger to capture all the output of upnpc - if I use
backticks I get just the first line.

Another question on upnpc - when it does work, the "Application Name" in the
router's port forwarding table lists "miniupnpc". I would like to change
this to a string of my choosing (e.g. "AskDomAboutThis" would be good...)
Is this possible? I'll recompile if I have to. OTOH I might just hack the
binary with a text editor - I assume if it's the same length string it
will work fine!

regards

Dom
Jo-Philipp Wich
2012-12-07 14:52:06 UTC
Permalink
Hi.
Post by Dom Latter
I've been looking around the site but have not yet found
documentation for "this sort of thing" - any hints?
Not really, sorry. You'll find docs for it if you know how to name it
and wehat to look for.
Post by Dom Latter
I have changed this to: if [ "$ACTION" = ifup ] && [ "$INTERFACE"
= lan ]; then as otherwise it seemed to be doing it to the
loopback (127.0.0.1) interface as well.
My fault, "lo" should've been "loopback".
Post by Dom Latter
I still have to have a "sleep" in the script - otherwise it does
not find a valid IGD. It seems to need about ten or a bit more
seconds, but I am giving it 60 to be on the safe side.
Odd, maybe DNS takes a while to settle.
Post by Dom Latter
Also, I am back to logging in /tmp: /usr/bin/upnpc -a "$ipaddr"
1234 1234 tcp >> /tmp/pnpc.log as I am not sure how to use logger
to capture all the output of upnpc - if I use backticks I get just
the first line.
/usr/bin/upnpc -a "$ipaddr" 1234 1234 tcp | logger -t 90-upnpc -
Post by Dom Latter
Another question on upnpc - when it does work, the "Application
Name" in the router's port forwarding table lists "miniupnpc". I
would like to change this to a string of my choosing (e.g.
"AskDomAboutThis" would be good...) Is this possible?
I suppose you have to recompile or binary-patch to change it.

~ Jow
Dom Latter
2012-12-07 16:05:37 UTC
Permalink
Post by Jo-Philipp Wich
Post by Dom Latter
I have changed this to: if [ "$ACTION" = ifup ] && [ "$INTERFACE"
= lan ]; then as otherwise it seemed to be doing it to the
loopback (127.0.0.1) interface as well.
My fault, "lo" should've been "loopback".
No worries, but is there a reason for "!= loopback" rather than
"= lan"?
Post by Jo-Philipp Wich
Post by Dom Latter
I still have to have a "sleep" in the script - otherwise it does
not find a valid IGD. It seems to need about ten or a bit more
seconds, but I am giving it 60 to be on the safe side.
Odd, maybe DNS takes a while to settle.
I think DNS is okay. I was wondering if it's something to do with
the clock, because it takes a while to sync in to real time.

So I made the script do this:
date >> /tmp/pnp
ping -c 1 latter.org | grep PING >> /tmp/pnp
/usr/bin/upnpc -s 2>&1 >> /tmp/pnp
date >> /tmp/pnp
sleep 3

a few times before doing anything else.
(Piping into logger doesn't seem to work for some reason).

From the the first time, ping resolves latter.org correctly.

Also, the system clock gets set to real time correctly,
before it is able to find any IGD routers.

I have two others on the network The "real" router is a Linksys,
and then there is a Netgear I am using as a wifi access point.
(The openwrt box is a TPLink).

upnpc "finds" the netgear before it is able to see the Linksys.

All very odd. I think I will stick with "sleep 60" and move on.
Post by Jo-Philipp Wich
/usr/bin/upnpc -a "$ipaddr" 1234 1234 tcp | logger -t 90-upnpc -
Tried it - just get "-" in the log.
Post by Jo-Philipp Wich
Post by Dom Latter
Another question on upnpc - when it does work, the "Application
Name" in the router's port forwarding table lists "miniupnpc". I
would like to change this to a string of my choosing (e.g.
"AskDomAboutThis" would be good...) Is this possible?
I suppose you have to recompile or binary-patch to change it.
I've been looking at the source and it is not obvious where to
change it! The actual C function seems to accept "desc" as a
parameter which is the string I want to change... I will carry on.
Jo-Philipp Wich
2012-12-08 16:48:25 UTC
Permalink
No worries, but is there a reason for "!= loopback" rather than "=
lan"?
Not really.
All very odd. I think I will stick with "sleep 60" and move on.
Ok.
Tried it - just get "-" in the log.
Then leave out the "-".

- -- 8< --
root at uplink:~# echo -en "abc\ndef\nghi\n" | logger -t test
root at uplink:~# logread | grep test
Dec 8 17:45:31 uplink user.notice test: abc
Dec 8 17:45:31 uplink user.notice test: def
Dec 8 17:45:31 uplink user.notice test: ghi
root at uplink:~#
- -- >8 --

~ Jow
Dom Latter
2012-12-09 00:49:59 UTC
Permalink
Post by Jo-Philipp Wich
Post by Dom Latter
Tried it - just get "-" in the log.
Then leave out the "-".
Sorry, that's basic shell stuff that I ought to work out for myself.

Thanks.

Daniel Dickinson
2012-12-07 20:45:30 UTC
Permalink
Hi Dom,
Post by Dom Latter
Another question on upnpc - when it does work, the "Application Name" in the
router's port forwarding table lists "miniupnpc". I would like to change
this to a string of my choosing (e.g. "AskDomAboutThis" would be good...)
Is this possible? I'll recompile if I have to. OTOH I might just hack the
binary with a text editor - I assume if it's the same length string it
miniupnpd hardcodes most of the strings it sends so changing the name of
the icon that appears in the, e.g., Windows 7 Network folder requires
changing the source and recompiling (or binary patching). Given that
it's the same author(s) I suspect it may not be a strong that is
configurable at runtime (well excepting binary patching).

Regards,

Daniel


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
URL: <http://lists.openwrt.org/pipermail/openwrt-users/attachments/20121207/91e87f28/attachment.sig>
Loading...