1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// UPnP.java
// (C) 2009 by David Wieditz; d.wieditz@gmx.de
// first published 14.02.2009 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: $
// $LastChangedRevision: $
// $LastChangedBy: $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.net;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import net.sbbi.upnp.DiscoveryAdvertisement;
import net.sbbi.upnp.DiscoveryEventHandler;
import net.sbbi.upnp.devices.UPNPRootDevice;
import net.sbbi.upnp.impls.InternetGatewayDevice;
import net.sbbi.upnp.messages.UPNPResponseException;
import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.plasma.plasmaSwitchboardConstants;
import de.anomic.yacy.logging.Log;
public class UPnP {
public final static Log log = new Log("UPNP");
private static plasmaSwitchboard sb = plasmaSwitchboard.getSwitchboard();
private final static int discoveryTimeout = 5000; // seconds to receive a response from devices
private static InternetGatewayDevice[] IGDs = null;
// mapping variables
private final static String mappedName = "YaCy";
private final static String mappedProtocol = "TCP";
private static int mappedPort = 0;
private static String localHostIP = null;;
/* Discovery message sender IP /10.100.100.2 does not match device description IP /192.168.1.254 skipping message,
set the net.sbbi.upnp.ddos.matchip system property to false to avoid this check
static {
System.setProperty("net.sbbi.upnp.ddos.matchip", "false");
} */
public static boolean setIGDs(InternetGatewayDevice[] igds) {
if(IGDs == null) {
IGDs = igds; // set only once to prevent many same devices by advertisement events
return true;
}
return false;
}
private static boolean init() {
boolean init = true;
try {
if (IGDs == null) IGDs = InternetGatewayDevice.getDevices(discoveryTimeout);
localHostIP = InetAddress.getLocalHost().getHostAddress();
} catch (IOException e) {
init = false;
}
if (IGDs != null) {
for (InternetGatewayDevice IGD : IGDs) {
log.logInfo("found device: " + IGD.getIGDRootDevice().getFriendlyName());
}
} else {
log.logInfo("no device found");
init = false;
log.logInfo("listening for device");
Listener.register();
}
return init;
}
private static String getRemoteHost() {
return sb.getConfig(plasmaSwitchboardConstants.UPNP_REMOTEHOST, "");
}
/**
* add port mapping for configured port
*/
public static void addPortMapping() {
if (sb == null) return;
addPortMapping(Integer.parseInt(sb.getConfig("port", "0")));
}
/**
* add TCP port mapping to all IGDs on the network<br/>
* latest port mapping will be removed
* @param port
*/
public static void addPortMapping(final int port) { //TODO: don't map already mapped port again
if (port < 1) return;
if (mappedPort > 0) deletePortMapping(); // delete old mapping first
if (mappedPort == 0 && ((IGDs != null && localHostIP != null) || init())) {
for (InternetGatewayDevice IGD : IGDs) {
try {
boolean mapped = IGD.addPortMapping(mappedName, getRemoteHost(), port, port, localHostIP, 0, mappedProtocol);
String msg = "port " + port + " on device "+ IGD.getIGDRootDevice().getFriendlyName();
if (mapped) {
log.logInfo("mapped " + msg);
mappedPort = port;
}
else log.logWarning("could not map " + msg);
} catch (IOException e) {} catch (UPNPResponseException e) { log.logSevere("mapping error: " + e.getMessage()); }
}
}
}
/**
* delete current port mapping
*/
public static void deletePortMapping() {
if (mappedPort > 0 && IGDs != null && localHostIP != null) {
for (InternetGatewayDevice IGD : IGDs) {
try {
boolean unmapped = IGD.deletePortMapping(getRemoteHost(), mappedPort, mappedProtocol);
String msg = "port " + mappedPort + " on device "+ IGD.getIGDRootDevice().getFriendlyName();
if (unmapped) log.logInfo("unmapped " + msg);
else log.logWarning("could not unmap " + msg);
} catch (IOException e) {} catch (UPNPResponseException e) { log.logSevere("unmapping error: " + e.getMessage()); }
}
mappedPort = 0; // reset mapped port
}
}
/**
* @return mapped port or 0
*/
public static int getMappedPort() {
return mappedPort;
}
public static void main(String[] args) {
deletePortMapping(); // nothing
addPortMapping(40000); // map
addPortMapping(40000); // unmap, map
deletePortMapping(); // unmap
deletePortMapping(); // nothing
}
/**
* register devices that do not respond to discovery but advertise themselves
*/
public static class Listener {
private final static Handler handler = new Handler();
private final static String devicetype = "urn:schemas-upnp-org:device:InternetGatewayDevice:1";
public static void register() {
try {
DiscoveryAdvertisement.getInstance().registerEvent(DiscoveryAdvertisement.EVENT_SSDP_ALIVE, devicetype, handler);
// DiscoveryAdvertisement.getInstance().registerEvent(DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE, devicetype, handler);
} catch (IOException e) {}
}
public static void unregister() {
DiscoveryAdvertisement.getInstance().unRegisterEvent(DiscoveryAdvertisement.EVENT_SSDP_ALIVE, devicetype, handler);
// DiscoveryAdvertisement.getInstance().unRegisterEvent(DiscoveryAdvertisement.EVENT_SSDP_BYE_BYE, devicetype, handler);
}
private static class Handler implements DiscoveryEventHandler {
public void eventSSDPAlive(String usn, String udn, String nt, String maxAge, URL location) {
InternetGatewayDevice[] newIGD = { null };
boolean error = false;
String errorMsg = null;
try {
newIGD[0] = new InternetGatewayDevice(new UPNPRootDevice(location, maxAge, "", usn, udn));
} catch (UnsupportedOperationException e) {
error = true;
errorMsg = e.getMessage();
} catch (MalformedURLException e) {
error = true;
errorMsg = e.getMessage();
} catch (IllegalStateException e) {
error = true;
errorMsg = e.getMessage();
}
if (error && errorMsg != null)
log.logSevere("eventSSDPAlive: " + errorMsg);
if (newIGD[0] == null) return;
log.logInfo("discovered device: " + newIGD[0].getIGDRootDevice().getFriendlyName());
if (UPnP.setIGDs(newIGD) &&
plasmaSwitchboard.getSwitchboard().getConfigBool(plasmaSwitchboardConstants.UPNP_ENABLED, false))
UPnP.addPortMapping();
Listener.unregister();
}
public void eventSSDPByeBye(String usn, String udn, String nt) {}
}
}
}
|