main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include <iostream>
  2. #include "cwmp.nsmap"
  3. #include "httppost.h"
  4. #include "soapcwmpService.h"
  5. #include "plugin/threads.h"
  6. #include <vector>
  7. #include <algorithm>
  8. #include <signal.h>
  9. #include <thread>
  10. #include <fstream>
  11. int port = 8000;
  12. int POST_handler(struct soap *soap);
  13. struct http_post_handlers http_post_handlers[] = {
  14. {"POST", POST_handler},
  15. {NULL}
  16. };
  17. void handler(int s) {
  18. printf("Sassan Caught SIGPIPE....\n");
  19. }
  20. void *process_request(void *arg) {
  21. cwmpService *service = (cwmpService *) arg;
  22. THREAD_DETACH(THREAD_ID);
  23. if (service) {
  24. service->serve();
  25. service->destroy(); /* clean up */
  26. delete service;
  27. }
  28. return NULL;
  29. }
  30. //<editor-fold desc="zbx_keys">
  31. /*
  32. Searching for: InternetGatewayDevice.DeviceInfo.SoftwareVersion & Find Result: 18
  33. Searching for: InternetGatewayDevice.DeviceInfo.UpTime & Find Result: 21
  34. Searching for: InternetGatewayDevice.DeviceInfo.X_TGT_IMSI & Find Result: 22
  35. Searching for: InternetGatewayDevice.DeviceInfo.MemoryStatus.Free & Find Result: 28
  36. Searching for: InternetGatewayDevice.DeviceInfo.ProcessStatus.CPUUsage & Find Result: 29
  37. Searching for: InternetGatewayDevice.Time.CurrentLocalTime & Find Result: 229
  38. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR1 & Find Result: 464
  39. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR2 & Find Result: 465
  40. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.EnodeBId & Find Result: 469
  41. Searching for: InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.CellId & Find Result: 470
  42. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesSent & Find Result: 542
  43. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesReceived & Find Result: 543
  44. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsSent & Find Result: 544
  45. Searching for: InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsReceived & Find Result: 545
  46. Searching for: InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.Uptime & Find Result: 553
  47. Searching for: InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress & Find Result: 557
  48. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP1 & Find Result: 580
  49. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP2 & Find Result: 581
  50. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.TX_Power & Find Result: 585
  51. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.DL_MCS & Find Result: 591
  52. Searching for: InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.UL_MCS & Find Result: 592
  53. Searching for: InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMSI & Find Result: 612
  54. Searching for: InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMEI & Find Result: 613
  55. */
  56. std::string zbx_keys[] = {"InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.Uptime",
  57. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.UL_MCS",
  58. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.TX_Power",
  59. "InternetGatewayDevice.DeviceInfo.SoftwareVersion",
  60. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR2",
  61. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.SINR1",
  62. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP2",
  63. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.RSRP1",
  64. "InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress",
  65. "InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMSI",
  66. "InternetGatewayDevice.WEB_GUI.Overview.DeviceInfo.IMEI",
  67. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.EnodeBId",
  68. "InternetGatewayDevice.DeviceInfo.MemoryStatus.Free",
  69. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.EnodeBId",
  70. "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.DL_MCS",
  71. "InternetGatewayDevice.Time.CurrentLocalTime",
  72. "InternetGatewayDevice.DeviceInfo.ProcessStatus.CPUUsage",
  73. "InternetGatewayDevice.WANDevice.1.X_TGT_WANNetConfigInfo.CellId",
  74. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesSent",
  75. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.BytesReceived",
  76. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsReceived",
  77. "InternetGatewayDevice.WANDevice.1.WANEthernetInterfaceConfig.Stats.PacketsSent",
  78. "InternetGatewayDevice.DeviceInfo.UpTime",
  79. "InternetGatewayDevice.DeviceInfo.X_TGT_IMSI"
  80. };
  81. //</editor-fold>
  82. void ZBX_Send(char **Name, char **Value, char **imsi) {
  83. if (!strcmp(*Name, "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.DL_MCS") == 0 or
  84. !strcmp(*Name, "InternetGatewayDevice.WEB_GUI.Network.LTE_Setting.Status.UL_MCS") == 0) {
  85. if (!strcmp(*Value, "QPSK")) {
  86. *Value = "4";
  87. } else if (!strcmp(*Value, "QAM8")) {
  88. *Value = "8";
  89. } else if (!strcmp(*Value, "QAM16")) {
  90. *Value = "16";
  91. } else if (!strcmp(*Value, "QAM32")) {
  92. *Value = "32";
  93. } else if (!strcmp(*Value, "QAM64")) {
  94. *Value = "64";
  95. }
  96. std::ofstream zbx_data;
  97. time_t second;
  98. second = time(NULL);
  99. zbx_data.open("/var/tmp/zbx_sender_data", std::ios_base::app);
  100. zbx_data << "IMSI-" << *imsi << " " << *Name << " " << second << " \"" << *Value << "\"" << std::endl;
  101. }
  102. }
  103. int main() {
  104. signal(SIGPIPE, handler);
  105. std::cout << "TR069 Server Started (1.0)...\n" << std::endl;
  106. cwmpService service(SOAP_XML_INDENT);
  107. service.soap->send_timeout = service.soap->recv_timeout = 5;
  108. service.soap->transfer_timeout = 30;
  109. service.soap->keep_alive = 1;
  110. service.soap->tcp_keep_alive = 1;
  111. service.soap->bind_flags |= SO_REUSEADDR;
  112. service.soap->bind_flags |= SO_REUSEPORT;
  113. service.soap->bind_flags |= MSG_NOSIGNAL;
  114. soap_init2(service.soap, SOAP_IO_KEEPALIVE, SOAP_IO_KEEPALIVE);
  115. SOAP_SOCKET m = service.bind(NULL, port, 1); /* master socket */
  116. if (soap_valid_socket(m)) {
  117. while (soap_valid_socket(service.accept())) {
  118. THREAD_TYPE tid;
  119. void *arg = (void *) service.copy();
  120. if (arg)
  121. while (THREAD_CREATE(&tid, (void *(*)(void *)) process_request, arg))
  122. sleep(1);
  123. }
  124. }
  125. service.soap_stream_fault(std::cerr);
  126. service.destroy(); /* clean up */
  127. return 0;
  128. }
  129. int POST_handler(struct soap *soap) {
  130. // struct _cwmp__GetParameterValues *res = soap_new__cwmp__GetParameterValues(soap, -1);
  131. // struct ParameterNames *list = soap_new_ParameterNames(soap, -1);
  132. const char *buf;
  133. size_t len;
  134. buf = soap_http_get_body(soap, &len);
  135. // std::cerr<<"Sassan3 ID: "<<soap->header->cwmp__SupportedCWMPVersions<<std::endl;
  136. // std::cerr<<"Debug1: "<<len<<std::endl;
  137. // std::cerr<<"Debug2: "<<buf<<std::endl;
  138. /* if HTTP has a content type header or a HTTP body is present then return 404 */
  139. if (len == 0) {
  140. std::cerr << "Sassan 204...: " << buf << std::endl;
  141. return 204; // HTTP 400 Bad Request
  142. // }else {
  143. // std::cerr<<"Debug0: "<<buf<<std::endl;
  144. // std::cerr<<"Debug1: "<<len<<std::endl;
  145. // std::cerr<<"Debug2: "<<buf<<std::endl;
  146. }
  147. // if (soap->http_content || soap_http_has_body(soap))
  148. // return 404;
  149. // (void)soap_end_recv(soap);
  150. // /* populate the response with one array item */
  151. // if (res && list)
  152. // {
  153. // list->__size = 1;
  154. // if ((list->__ptrstring = (char**)soap_malloc(soap, list->__size * sizeof(char*))) != NULL)
  155. // {
  156. // list->__ptrstring[0] = soap_strdup(soap, "...");
  157. // res->ParameterNames = list;
  158. // }
  159. // }
  160. // /* send response */
  161. // soap->encodingStyle = "";
  162. // soap_serializeheader(soap);
  163. // soap_serialize__cwmp__GetParameterValues(soap, res);
  164. // if (soap_begin_count(soap))
  165. // return soap->error;
  166. // if (soap->mode & SOAP_IO_LENGTH)
  167. // {
  168. // if (soap_envelope_begin_out(soap)
  169. // || soap_putheader(soap)
  170. // || soap_body_begin_out(soap)
  171. // || soap_put__cwmp__GetParameterValues(soap, res, "cwmp:GetParameterValues", "")
  172. // || soap_body_end_out(soap)
  173. // || soap_envelope_end_out(soap))
  174. // return soap->error;
  175. // };
  176. // if (soap_end_count(soap)
  177. // || soap_response(soap, SOAP_OK)
  178. // || soap_envelope_begin_out(soap)
  179. // || soap_putheader(soap)
  180. // || soap_body_begin_out(soap)
  181. // || soap_put__cwmp__GetParameterValues(soap, res, "cwmp:GetParameterValues", "")
  182. // || soap_body_end_out(soap)
  183. // || soap_envelope_end_out(soap)
  184. // || soap_end_send(soap))
  185. // return soap->error;
  186. // return soap_closesock(soap);
  187. }
  188. int cwmpService::GetParameterNames(struct _cwmp__GetParameterNames *cwmp__GetParameterNames,
  189. struct _cwmp__GetParameterNamesResponse *cwmp__GetParameterNamesResponse) { return SOAP_OK; }
  190. int cwmpService::GetParameterValues(struct _cwmp__GetParameterValues *cwmp__GetParameterValues,
  191. struct _cwmp__GetParameterValuesResponse *cwmp__GetParameterValuesResponse) { return SOAP_OK; }
  192. int cwmpService::SetParameterValues(struct _cwmp__SetParameterValues *cwmp__SetParameterValues,
  193. struct _cwmp__SetParameterValuesResponse *cwmp__SetParameterValuesResponse) { return SOAP_OK; }
  194. int cwmpService::SetParameterAttributes(
  195. struct _cwmp__SetParameterAttributes *cwmp__SetParameterAttributes,
  196. struct _cwmp__SetParameterAttributesResponse *cwmp__SetParameterAttributesResponse) { return SOAP_OK; }
  197. int cwmpService::GetParameterAttributes(struct _cwmp__GetParameterAttributes *cwmp__GetParameterAttributes,
  198. struct _cwmp__GetParameterAttributesResponse *cwmp__GetParameterAttributesResponse) { return SOAP_OK; }
  199. int cwmpService::Download(struct _cwmp__Download *cwmp__Download,
  200. struct _cwmp__DownloadResponse *cwmp__DownloadResponse) { return SOAP_OK; }
  201. int cwmpService::Upload(
  202. struct _cwmp__Upload *cwmp__Upload,
  203. struct _cwmp__UploadResponse *cwmp__UploadResponse) { return SOAP_OK; }
  204. int cwmpService::AddObject(
  205. struct _cwmp__AddObject *cwmp__AddObject,
  206. struct _cwmp__AddObjectResponse *cwmp__AddObjectResponse) { return SOAP_OK; }
  207. int cwmpService::DeleteObject(
  208. struct _cwmp__DeleteObject *cwmp__DeleteObject,
  209. struct _cwmp__DeleteObjectResponse *cwmp__DeleteObjectResponse) { return SOAP_OK; }
  210. int cwmpService::GetRPCMethods(
  211. struct _cwmp__GetRPCMethods *cwmp__GetRPCMethods,
  212. struct _cwmp__GetRPCMethodsResponse *cwmp__GetRPCMethodsResponse) { return SOAP_OK; }
  213. int cwmpService::GetParameterValuesResponse(
  214. struct _cwmp__GetParameterValuesResponse *cwmp__GetParameterValuesResponse) { return SOAP_OK; }
  215. int cwmpService::Inform(struct _cwmp__Inform *cwmp__Inform,
  216. struct _cwmp__InformResponse *cwmp__InformResponse) {
  217. std::cerr << "Inform Recieved..." << cwmp__Inform->DeviceId->SerialNumber <<" at "<<time(NULL)<< std::endl;
  218. char *imsi;
  219. auto timestamp = std::chrono::high_resolution_clock::now();
  220. std::vector<std::thread> th;
  221. // int indexes[24];
  222. std::vector<int> ind;
  223. for (int i = 0 ; i < cwmp__Inform->ParameterList->__size; ++i) {
  224. if (!strcmp(cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Name,
  225. "InternetGatewayDevice.DeviceInfo.X_TGT_IMSI")) {
  226. imsi = cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Value;
  227. }
  228. std::string cpe = cwmp__Inform->DeviceId->SerialNumber;
  229. auto j = std::find(std::begin(zbx_keys), std::end(zbx_keys),
  230. cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Name);
  231. if (j != std::end(zbx_keys)) {
  232. th.push_back(std::thread(ZBX_Send, &cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Name,
  233. &cwmp__Inform->ParameterList->__ptrParameterValueStruct[i]->Value, &imsi));
  234. ind.push_back(i);
  235. }
  236. }
  237. for (auto &t : th) {
  238. t.join();
  239. }
  240. // std::ofstream influx_data;
  241. // time_t second;
  242. // second = time(NULL);
  243. // influx_data.open("/var/tmp/influxdb_data", std::ios_base::app);
  244. // influx_data << "tr069,imsi=" << imsi << " " ;
  245. // try {
  246. //
  247. // for (std::vector<int>::iterator it = ind.begin(); it != ind.end(); ++it){
  248. // influx_data << cwmp__Inform->ParameterList->__ptrParameterValueStruct[*it]->Name << "=\""
  249. // << cwmp__Inform->ParameterList->__ptrParameterValueStruct[*it]->Value << "\",";
  250. // }
  251. // influx_data<<std::endl;
  252. // }catch (std::exception e){std::cerr<<e.what()<<std::endl;}
  253. return SOAP_OK;
  254. }