-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPnP.h
197 lines (173 loc) · 6.33 KB
/
UPnP.h
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
/**
* @file UPnP.h
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* 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 3 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, see <https: //www.gnu.org/licenses/>.
*
*/
#ifndef __UPNP_H_
#define __UPNP_H_
#include "IPAddress.h"
#include "utils.h"
#include "TCPClient.h"
#include "UDPClient.h"
#define UPNP_DEBUG
#define UPNP_SSDP_PORT 1900
#define TCP_CONNECTION_TIMEOUT_MS 6000
#define PORT_MAPPING_INVALID_INDEX \
"<errorDescription>SpecifiedArrayIndexInvalid</errorDescription>"
#define PORT_MAPPING_INVALID_ACTION \
"<errorDescription>Invalid Action</errorDescription>"
#define RULE_PROTOCOL_TCP "TCP"
#define RULE_PROTOCOL_UDP "UDP"
#define MAX_NUM_OF_UPDATES_WITH_NO_EFFECT \
6 // after 6 tries of updatePortMappings we will execute the more extensive
// addPortMapping
#define UDP_TX_PACKET_MAX_SIZE \
1000 // reduce max UDP packet size to conserve memory (by default
// UDP_TX_PACKET_MAX_SIZE=8192)
#define UDP_TX_RESPONSE_MAX_SIZE 8192
// TODO: idealy the SOAP actions should be verified as supported by the IGD
// before they are used a struct can be created for each action and filled when
// the XML descriptor file is read
/*const char * SOAPActions [] = {
"AddPortMapping",
"GetSpecificPortMappingEntry",
"DeletePortMapping",
"GetGenericPortMappingEntry",
"GetExternalIPAddress"
};*/
/*
#define SOAP_ERROR_TAG "errorDescription";
const char * SOAPErrors [] = {
"SpecifiedArrayIndexInvalid",
"Invalid Action"
};*/
/*
enum soapActionResult {
// TODO
}*/
typedef struct _SOAPAction {
const char *name;
} SOAPAction;
typedef void (*callback_function)(void);
typedef struct _gatewayInfo {
// router info
IPAddress host;
int port; // this port is used when getting router capabilities and xml files
char * path; // this is the path that is used to retrieve router information
// from xml files
// info for actions
int actionPort; // this port is used when performing SOAP API actions
char *actionPath; // this is the path used to perform SOAP API actions
char *serviceTypeName; // i.e "WANPPPConnection:1" or "WANIPConnection:1"
} gatewayInfo;
typedef struct _upnpRule {
int index;
char *devFriendlyName;
IPAddress internalAddr;
int internalPort;
int externalPort;
char *protocol;
int leaseDuration;
} upnpRule;
typedef struct _upnpRuleNode {
_upnpRule *upnpRule;
_upnpRuleNode *next;
} upnpRuleNode;
typedef struct _ssdpDevice {
IPAddress host;
int port; // this port is used when getting router capabilities and xml files
char * path; // this is the path that is used to retrieve router information
// from xml files
} ssdpDevice;
typedef struct _ssdpDeviceNode {
_ssdpDevice *ssdpDevice;
_ssdpDeviceNode *next;
} ssdpDeviceNode;
typedef enum {
SUCCESS, // port mapping was added
ALREADY_MAPPED, // the port mapping is already found in the IGD
EMPTY_PORT_MAPPING_CONFIG,
NETWORK_ERROR,
TIMEOUT,
VERIFICATION_FAILED,
NOP // the check is delayed
} portMappingResult;
class UPnP {
public:
UPnP(unsigned long timeoutMs = 20000UL);
~UPnP();
// when the ruleIP is set to the current device IP, the IP of the rule will
// change if the device changes its IP this makes sure the traffic will be
// directed to the device even if the IP chnages
void addPortMappingConfig(IPAddress ruleIP /* can be NULL */, int rulePort,
const char * ruleProtocol, int ruleLeaseDuration,
const char * ruleFriendlyName);
portMappingResult commitPortMappings();
portMappingResult updatePortMappings(
unsigned long intervalMs,
callback_function fallback = NULL /* optional */);
bool printAllPortMappings();
void printPortMappingConfig(); // prints all the port mappings that were
// added using `addPortMappingConfig`
bool testConnectivity();
/* API extensions - additional methods to the UPnP API */
ssdpDeviceNode *listSsdpDevices(); // will create an object with all SSDP
// devices on the network
void printSsdpDevices(ssdpDeviceNode *ssdpDeviceNode); // will print all SSDP
// devices in teh list
gatewayInfo _gwInfo;
private:
bool connectUDP();
void broadcastMSearch(bool isSsdpAll = false);
ssdpDevice *waitForUnicastResponseToMSearch(IPAddress gatewayIP);
bool getGatewayInfo(gatewayInfo *deviceInfo);
bool isGatewayInfoValid(gatewayInfo *deviceInfo);
void clearGatewayInfo(gatewayInfo *deviceInfo);
bool connectToIGD(IPAddress host, int port);
bool getIGDEventURLs(gatewayInfo *deviceInfo);
bool addPortMappingEntry(gatewayInfo *deviceInfo, upnpRule *rule_ptr);
bool verifyPortMapping(gatewayInfo *deviceInfo, upnpRule *rule_ptr);
bool deletePortMapping(gatewayInfo *deviceInfo, upnpRule *rule_ptr);
bool applyActionOnSpecificPortMapping(SOAPAction *soapAction,
gatewayInfo *deviceInfo,
upnpRule *rule_ptr);
void removeAllPortMappingsFromIGD();
void upnpRulePrint(upnpRule *rule_ptr);
// char * getSpaceschar *(int num);
IPAddress getHost(char * url);
int getPort(char * url);
const char * getPath(char * url);
const char * getTagContent(const char * line, const char * tagName);
void ssdpDevicePrint(ssdpDevice *ssdpDevice);
/* members */
upnpRuleNode *_headRuleNode;
unsigned long _lastUpdateTime;
long _timeoutMs; // 0 for blocking operation
UDPClient _udpClient;
TCPClient _tcpClient;
unsigned long _consecutiveFails;
char buffer[2048];
char temp[255];
};
#endif // __UPNP_H_