You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

602 lines
19 KiB

7 years ago
7 years ago
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
​
7 years ago
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
7 years ago
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
7 years ago
7 years ago
7 years ago
7 years ago
​
​
​
​
7 years ago
​
7 years ago
  1. #include "upnpnat.h"
  2. #include "xmlParser.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <winsock2.h>
  6. #define LOG(X) m_pLog->Log("UPNPNAT", this, X)
  7. #define LOG_ERROR(X) m_pLog->Log("UPNPNAT", this, _LOG_ERROR, X)
  8. #define MAX_BUFF_SIZE 102400
  9. static bool parseUrl(const char * url, std::string & host, unsigned short * port, std::string & path)
  10. {
  11. std::string str_url = url;
  12. std::string::size_type pos1, pos2, pos3;
  13. pos1 = str_url.find("://");
  14. if (pos1 == std::string::npos)
  15. {
  16. return false;
  17. }
  18. pos1 = pos1 + 3;
  19. pos2 = str_url.find(":", pos1);
  20. if (pos2 == std::string::npos)
  21. {
  22. *port = 80;
  23. pos3 = str_url.find("/", pos1);
  24. if (pos3 == std::string::npos)
  25. {
  26. return false;
  27. }
  28. host = str_url.substr(pos1, pos3 - pos1);
  29. }
  30. else
  31. {
  32. host = str_url.substr(pos1, pos2 - pos1);
  33. pos3 = str_url.find("/", pos1);
  34. if (pos3 == std::string::npos)
  35. {
  36. return false;
  37. }
  38. std::string str_port = str_url.substr(pos2 + 1, pos3 - pos2 - 1);
  39. *port = (unsigned short) atoi(str_port.c_str());
  40. }
  41. if (pos3 + 1 >= str_url.size())
  42. {
  43. path = "/";
  44. }
  45. else
  46. {
  47. path = str_url.substr(pos3, str_url.size());
  48. }
  49. return true;
  50. }
  51. /******************************************************************
  52. ** Discovery Defines *
  53. *******************************************************************/
  54. #define HTTPMU_HOST_ADDRESS "192.168.0.255" //"239.255.255.250"
  55. #define HTTPMU_HOST_PORT 3702
  56. /*
  57. #define SEARCH_REQUEST_STRING "M-SEARCH * HTTP/1.1\r\nST:ssdp:all\r\nMX: 3\r\nMAN:\"ssdp:discover\"\r\nHOST: 239.255.255.250:1900\r\n\r\n"
  58. //*/
  59. /*
  60. #define SEARCH_REQUEST_STRING "M-SEARCH * HTTP/1.1\r\n" \
  61. "ST:ssdp:all\r\n" \
  62. "MX: 3\r\n" \
  63. "MAN:\"ssdp:discover\"\r\n" \
  64. "HOST: 239.255.255.250:3702\r\n" \
  65. "\r\n"
  66. //*/
  67. #define SEARCH_REQUEST_STRING \
  68. "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" \
  69. "<SOAP:Envelope xmlns:SOAP=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:DISCOVERY=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" " \
  70. "xmlns:ssdp=\"http://example.com/wsdp/sample/print\">\n" \
  71. "<SOAP:Header>\n" \
  72. "<wsa:MessageID>urn:uuid:ac9823ee-6813-498c-8c1b-6272a22353f7</wsa:MessageID>\n" \
  73. "<wsa:Action>\n" \
  74. "http ://schemas.xmlsoap.org/ws/2005/04/discovery/Probe\n" \
  75. "</wsa:Action>\n" \
  76. "<wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>\n" \
  77. "</SOAP:Header>\n" \
  78. "<SOAP:Body>\n" \
  79. "<DISCOVERY:Probe>\n" \
  80. "<DISCOVERY:Types>ssdp:any</DISCOVERY:Types>\n" \
  81. "</DISCOVERY:Probe>\n" \
  82. "</SOAP:Body>\n" \
  83. "</SOAP:Envelope>\r\n\r\n"
  84. #define HTTP_OK "200 OK"
  85. #define DEFAULT_HTTP_PORT 80
  86. /******************************************************************
  87. ** Device and Service Defines *
  88. *******************************************************************/
  89. #define DEVICE_TYPE_1 "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
  90. #define DEVICE_TYPE_2 "urn:schemas-upnp-org:device:WANDevice:1"
  91. #define DEVICE_TYPE_3 "urn:schemas-upnp-org:device:WANConnectionDevice:1"
  92. #define SERVICE_WANIP "urn:schemas-upnp-org:service:WANIPConnection:1"
  93. #define SERVICE_WANPPP "urn:schemas-upnp-org:service:WANPPPConnection:1"
  94. /******************************************************************
  95. ** Action Defines *
  96. *******************************************************************/
  97. #define HTTP_HEADER_ACTION \
  98. "POST %s HTTP/1.1\r\n" \
  99. "HOST: %s:%u\r\n" \
  100. "SOAPACTION:\"%s#%s\"\r\n" \
  101. "CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n" \
  102. "Content-Length: %d \r\n\r\n"
  103. #define SOAP_ACTION \
  104. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
  105. "<s:Envelope xmlns:s=" \
  106. "\"http://schemas.xmlsoap.org/soap/envelope/\" " \
  107. "s:encodingStyle=" \
  108. "\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" \
  109. "<s:Body>\r\n" \
  110. "<u:%s xmlns:u=\"%s\">\r\n%s" \
  111. "</u:%s>\r\n" \
  112. "</s:Body>\r\n" \
  113. "</s:Envelope>\r\n"
  114. #define PORT_MAPPING_LEASE_TIME "63072000" // two year
  115. #define ADD_PORT_MAPPING_PARAMS \
  116. "<NewRemoteHost></NewRemoteHost>\r\n" \
  117. "<NewExternalPort>%u</NewExternalPort>\r\n" \
  118. "<NewProtocol>%s</NewProtocol>\r\n" \
  119. "<NewInternalPort>%u</NewInternalPort>\r\n" \
  120. "<NewInternalClient>%s</NewInternalClient>\r\n" \
  121. "<NewEnabled>1</NewEnabled>\r\n" \
  122. "<NewPortMappingDescription>%s</NewPortMappingDescription>\r\n" \
  123. "<NewLeaseDuration>" PORT_MAPPING_LEASE_TIME "</NewLeaseDuration>\r\n"
  124. #define ACTION_ADD "AddPortMapping"
  125. //*********************************************************************************
  126. UPNPNAT::UPNPNAT()
  127. {
  128. m_pLog = basicLogCollection::getInstance();
  129. }
  130. UPNPNAT::~UPNPNAT() {}
  131. bool UPNPNAT::init(int time, int inter)
  132. {
  133. time_out = time;
  134. interval = inter;
  135. status = NAT_INIT;
  136. WORD wVersionRequested;
  137. WSADATA wsaData;
  138. int err;
  139. wVersionRequested = MAKEWORD(2, 2);
  140. err = WSAStartup(wVersionRequested, &wsaData);
  141. if (err != 0)
  142. {
  143. LOG_ERROR("WSAStartup failed with error");
  144. return false;
  145. };
  146. return true;
  147. }
  148. bool UPNPNAT::tcp_connect(const char * _host, unsigned short int _port)
  149. {
  150. int ret, i;
  151. tcp_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  152. struct sockaddr_in r_address;
  153. r_address.sin_family = AF_INET;
  154. r_address.sin_port = htons(_port);
  155. r_address.sin_addr.s_addr = inet_addr(_host);
  156. for (i = 1; i <= time_out; i++)
  157. {
  158. if (i > 1)
  159. {
  160. _sleep(1000);
  161. }
  162. ret = connect(tcp_socket_fd, (const struct sockaddr *) &r_address, sizeof(struct sockaddr_in));
  163. if (ret == 0)
  164. {
  165. status = NAT_TCP_CONNECTED;
  166. LOG("NAT TCP Connected");
  167. return true;
  168. }
  169. }
  170. status = NAT_ERROR;
  171. char temp[100];
  172. sprintf(temp, "Fail to connect to %s:%i (using TCP)", _host, _port);
  173. LOG_ERROR(temp);
  174. last_error = temp;
  175. return false;
  176. }
  177. bool UPNPNAT::discovery()
  178. {
  179. udp_socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  180. int i, ret;
  181. std::string send_buff = SEARCH_REQUEST_STRING;
  182. std::string recv_buff;
  183. char buff[MAX_BUFF_SIZE + 1]; // buff should be enough big
  184. struct sockaddr_in r_address;
  185. r_address.sin_family = AF_INET;
  186. r_address.sin_port = htons(HTTPMU_HOST_PORT);
  187. r_address.sin_addr.s_addr = inet_addr(HTTPMU_HOST_ADDRESS);
  188. bool bOptVal = true;
  189. int bOptLen = sizeof(bool);
  190. int iOptLen = sizeof(int);
  191. ret = setsockopt(udp_socket_fd, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
  192. LOG(send_buff.c_str());
  193. ret = sendto(udp_socket_fd, send_buff.c_str(), send_buff.size(), 0, (struct sockaddr *) &r_address, sizeof(struct sockaddr_in));
  194. sprintf(buff, "Sent %d Bytes", ret);
  195. LOG(buff);
  196. for (i = 1; i <= time_out; i++)
  197. {
  198. u_long val = 1;
  199. ioctlsocket(udp_socket_fd, FIONBIO, &val); // none block
  200. memset(buff, 0, sizeof(buff));
  201. ret = recvfrom(udp_socket_fd, buff, MAX_BUFF_SIZE, 0, NULL, NULL);
  202. if (ret == SOCKET_ERROR)
  203. {
  204. Sleep(1000);
  205. continue;
  206. }
  207. LOG("RESP:");
  208. // LOG(buff);
  209. recv_buff = buff;
  210. XMLNode node = XMLNode::parseString(buff, "root");
  211. if (node.isEmpty())
  212. {
  213. status = NAT_ERROR;
  214. LOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  215. continue;
  216. };
  217. XMLNode baseURL_node = node.getChildNode("Body", 0);
  218. std::string base_url;
  219. if (! baseURL_node.getText())
  220. {
  221. std::string::size_type index = recv_buff.find("/", 7);
  222. if (index == std::string::npos)
  223. {
  224. status = NAT_ERROR;
  225. LOG_ERROR("Fail to get base_URL from XMLNode \"URLBase\" or describe_url.");
  226. continue;
  227. };
  228. base_url = base_url.assign(buff, 0, index);
  229. }
  230. else
  231. {
  232. base_url = baseURL_node.getText();
  233. }
  234. ret = recv_buff.find(HTTP_OK);
  235. if (ret == std::string::npos)
  236. {
  237. continue; // invalid response
  238. }
  239. std::string::size_type begin = recv_buff.find("http://");
  240. if (begin == std::string::npos)
  241. {
  242. continue; // invalid response
  243. }
  244. std::string::size_type end = recv_buff.find("\r", begin);
  245. if (end == std::string::npos)
  246. {
  247. continue; // invalid response
  248. }
  249. describe_url = describe_url.assign(recv_buff, begin, end - begin);
  250. if (! get_description())
  251. {
  252. Sleep(1000);
  253. continue;
  254. }
  255. if (! parser_description())
  256. {
  257. Sleep(1000);
  258. continue;
  259. }
  260. closesocket(udp_socket_fd);
  261. status = NAT_FOUND; // find a router
  262. return true;
  263. }
  264. status = NAT_ERROR;
  265. last_error = "Fail to find an UPNP NAT.\n";
  266. LOG_ERROR("Fail to find an UPNP NAT.");
  267. return false; // no router finded
  268. }
  269. bool UPNPNAT::get_description()
  270. {
  271. std::string host, path;
  272. unsigned short int port;
  273. int ret = parseUrl(describe_url.c_str(), host, &port, path);
  274. if (! ret)
  275. {
  276. status = NAT_ERROR;
  277. last_error = "Failed to parseURl: " + describe_url + "\n";
  278. LOG_ERROR(last_error.c_str());
  279. return false;
  280. }
  281. // connect
  282. ret = tcp_connect(host.c_str(), port);
  283. if (! ret)
  284. {
  285. return false;
  286. }
  287. char request[200];
  288. sprintf(request, "GET %s HTTP/1.1\r\nHost: %s:%d\r\n\r\n", path.c_str(), host.c_str(), port);
  289. std::string http_request = request;
  290. // send request
  291. ret = send(tcp_socket_fd, http_request.c_str(), http_request.size(), 0);
  292. // get description xml file
  293. char buff[MAX_BUFF_SIZE + 1];
  294. memset(buff, 0, sizeof(buff));
  295. std::string response;
  296. while (ret = recv(tcp_socket_fd, buff, MAX_BUFF_SIZE, 0) > 0)
  297. {
  298. LOG(buff);
  299. response += buff;
  300. memset(buff, 0, sizeof(buff));
  301. }
  302. description_info = response;
  303. return true;
  304. }
  305. bool UPNPNAT::parser_description()
  306. {
  307. XMLNode node = XMLNode::parseString(description_info.c_str(), "root");
  308. if (node.isEmpty())
  309. {
  310. status = NAT_ERROR;
  311. last_error = "The device descripe XML file is not a valid XML file. Cann't find root element.\n";
  312. LOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  313. return false;
  314. }
  315. XMLNode baseURL_node = node.getChildNode("URLBase", 0);
  316. if (! baseURL_node.getText())
  317. {
  318. std::string::size_type index = describe_url.find("/", 7);
  319. if (index == std::string::npos)
  320. {
  321. status = NAT_ERROR;
  322. last_error = "Fail to get base_URL from XMLNode \"URLBase\" or describe_url.\n";
  323. LOG_ERROR("Fail to get base_URL from XMLNode \"URLBase\" or describe_url.");
  324. return false;
  325. }
  326. base_url = base_url.assign(describe_url, 0, index);
  327. }
  328. else
  329. {
  330. base_url = baseURL_node.getText();
  331. }
  332. int num, i;
  333. XMLNode device_node, deviceList_node, deviceType_node;
  334. num = node.nChildNode("device");
  335. for (i = 0; i < num; i++)
  336. {
  337. device_node = node.getChildNode("device", i);
  338. if (device_node.isEmpty())
  339. {
  340. break;
  341. }
  342. deviceType_node = device_node.getChildNode("deviceType", 0);
  343. if (strcmp(deviceType_node.getText(), DEVICE_TYPE_1) == 0)
  344. {
  345. break;
  346. }
  347. }
  348. if (device_node.isEmpty())
  349. {
  350. status = NAT_ERROR;
  351. last_error = "Fail to find device \"urn:schemas-upnp-org:device:InternetGatewayDevice:1\"\n";
  352. LOG_ERROR("Fail to find device \"urn:schemas-upnp-org:device:InternetGatewayDevice:1\"");
  353. return false;
  354. }
  355. deviceList_node = device_node.getChildNode("deviceList", 0);
  356. if (deviceList_node.isEmpty())
  357. {
  358. status = NAT_ERROR;
  359. last_error = "Fail to find deviceList of device \"urn:schemas-upnp-org:device:InternetGatewayDevice:1\"\n";
  360. LOG_ERROR("Fail to find deviceList of device \"urn:schemas-upnp-org:device:InternetGatewayDevice:1\"");
  361. return false;
  362. }
  363. // get urn:schemas-upnp-org:device:WANDevice:1 and it's devicelist
  364. num = deviceList_node.nChildNode("device");
  365. for (i = 0; i < num; i++)
  366. {
  367. device_node = deviceList_node.getChildNode("device", i);
  368. if (device_node.isEmpty())
  369. {
  370. break;
  371. }
  372. deviceType_node = device_node.getChildNode("deviceType", 0);
  373. if (strcmp(deviceType_node.getText(), DEVICE_TYPE_2) == 0)
  374. {
  375. break;
  376. }
  377. }
  378. if (device_node.isEmpty())
  379. {
  380. status = NAT_ERROR;
  381. last_error = "Fail to find device \"urn:schemas-upnp-org:device:WANDevice:1\"\n";
  382. LOG_ERROR("Fail to find device \"urn:schemas-upnp-org:device:WANDevice:1\"");
  383. return false;
  384. }
  385. deviceList_node = device_node.getChildNode("deviceList", 0);
  386. if (deviceList_node.isEmpty())
  387. {
  388. status = NAT_ERROR;
  389. last_error = "Fail to find deviceList of device \"urn:schemas-upnp-org:device:WANDevice:1\"\n";
  390. LOG_ERROR("Fail to find deviceList of device \"urn:schemas-upnp-org:device:WANDevice:1\"");
  391. return false;
  392. }
  393. // get urn:schemas-upnp-org:device:WANConnectionDevice:1 and it's servicelist
  394. num = deviceList_node.nChildNode("device");
  395. for (i = 0; i < num; i++)
  396. {
  397. device_node = deviceList_node.getChildNode("device", i);
  398. if (device_node.isEmpty())
  399. {
  400. break;
  401. }
  402. deviceType_node = device_node.getChildNode("deviceType", 0);
  403. if (strcmp(deviceType_node.getText(), DEVICE_TYPE_3) == 0)
  404. {
  405. break;
  406. }
  407. }
  408. if (device_node.isEmpty())
  409. {
  410. status = NAT_ERROR;
  411. last_error = "Fail to find device \"urn:schemas-upnp-org:device:WANConnectionDevice:1\"\n";
  412. LOG_ERROR("Fail to find device \"urn:schemas-upnp-org:device:WANConnectionDevice:1\"");
  413. return false;
  414. }
  415. XMLNode serviceList_node, service_node, serviceType_node;
  416. serviceList_node = device_node.getChildNode("serviceList", 0);
  417. if (serviceList_node.isEmpty())
  418. {
  419. status = NAT_ERROR;
  420. last_error = "Fail to find serviceList of device \"urn:schemas-upnp-org:device:WANDevice:1\"\n";
  421. LOG_ERROR("Fail to find serviceList of device \"urn:schemas-upnp-org:device:WANDevice:1\"");
  422. return false;
  423. }
  424. num = serviceList_node.nChildNode("service");
  425. const char * serviceType;
  426. bool is_found = false;
  427. for (i = 0; i < num; i++)
  428. {
  429. service_node = serviceList_node.getChildNode("service", i);
  430. if (service_node.isEmpty())
  431. {
  432. break;
  433. }
  434. serviceType_node = service_node.getChildNode("serviceType");
  435. if (serviceType_node.isEmpty())
  436. {
  437. continue;
  438. }
  439. serviceType = serviceType_node.getText();
  440. if (strcmp(serviceType, SERVICE_WANIP) == 0 || strcmp(serviceType, SERVICE_WANPPP) == 0)
  441. {
  442. is_found = true;
  443. break;
  444. }
  445. }
  446. if (! is_found)
  447. {
  448. status = NAT_ERROR;
  449. last_error = "can't find \" SERVICE_WANIP \" or \" SERVICE_WANPPP \" service.\n";
  450. LOG_ERROR("can't find \" SERVICE_WANIP \" or \" SERVICE_WANPPP \" service.");
  451. return false;
  452. }
  453. this->service_type = serviceType;
  454. XMLNode controlURL_node = service_node.getChildNode("controlURL");
  455. control_url = controlURL_node.getText();
  456. // make the complete control_url;
  457. if (control_url.find("http://") == std::string::npos && control_url.find("HTTP://") == std::string::npos)
  458. {
  459. control_url = base_url + control_url;
  460. }
  461. if (service_describe_url.find("http://") == std::string::npos && service_describe_url.find("HTTP://") == std::string::npos)
  462. {
  463. service_describe_url = base_url + service_describe_url;
  464. }
  465. closesocket(tcp_socket_fd);
  466. status = NAT_GETCONTROL;
  467. return true;
  468. }
  469. bool UPNPNAT::add_port_mapping(char * _description, char * _destination_ip, unsigned short int _port_ex, unsigned short int _port_in, char * _protocal)
  470. {
  471. int ret;
  472. std::string host, path;
  473. unsigned short int port;
  474. ret = parseUrl(control_url.c_str(), host, &port, path);
  475. if (! ret)
  476. {
  477. status = NAT_ERROR;
  478. last_error = "Fail to parseURl: " + describe_url;
  479. LOG_ERROR(last_error.c_str());
  480. return false;
  481. }
  482. // connect
  483. ret = tcp_connect(host.c_str(), port);
  484. if (! ret)
  485. {
  486. return false;
  487. }
  488. char buff[MAX_BUFF_SIZE + 1];
  489. sprintf(buff, ADD_PORT_MAPPING_PARAMS, _port_ex, _protocal, _port_in, _destination_ip, _description);
  490. std::string action_params = buff;
  491. sprintf(buff, SOAP_ACTION, ACTION_ADD, service_type.c_str(), action_params.c_str(), ACTION_ADD);
  492. std::string soap_message = buff;
  493. sprintf(buff, HTTP_HEADER_ACTION, path.c_str(), host.c_str(), port, service_type.c_str(), ACTION_ADD, soap_message.size());
  494. std::string action_message = buff;
  495. std::string http_request = action_message + soap_message;
  496. // send request
  497. ret = send(tcp_socket_fd, http_request.c_str(), http_request.size(), 0);
  498. // wait for response
  499. std::string response;
  500. memset(buff, 0, sizeof(buff));
  501. while (ret = recv(tcp_socket_fd, buff, MAX_BUFF_SIZE, 0) > 0)
  502. {
  503. response += buff;
  504. memset(buff, 0, sizeof(buff));
  505. }
  506. if (response.find(HTTP_OK) == std::string::npos)
  507. {
  508. status = NAT_ERROR;
  509. char temp[100];
  510. sprintf(temp, "Fail to add port mapping (%s/%s)", _description, _protocal);
  511. LOG_ERROR(temp);
  512. last_error = temp;
  513. return false;
  514. }
  515. closesocket(tcp_socket_fd);
  516. status = NAT_ADD;
  517. return true;
  518. }
  519. #undef LOG
  520. #undef LOG_ERROR