📄 upnpnat.cpp
字号:
break; } if(device_node.isEmpty()) { status=NAT_ERROR; last_error="Fail to find device \"urn:schemas-upnp-org:device:WANDevice:1 \"\n"; return false; } deviceList_node=device_node.getChildNode("deviceList",0); if(deviceList_node.isEmpty()) { status=NAT_ERROR; last_error=" Fail to find deviceList of device \"urn:schemas-upnp-org:device:WANDevice:1 \"\n"; return false; } // get urn:schemas-upnp-org:device:WANConnectionDevice:1 and it's servicelist num=deviceList_node.nChildNode("device"); for(i=0;i<num;i++) { device_node=deviceList_node.getChildNode("device",i); if(device_node.isEmpty()) break; deviceType_node=device_node.getChildNode("deviceType",0); if(strcmp(deviceType_node.getText(),DEVICE_TYPE_3)==0) break; } if(device_node.isEmpty()) { status=NAT_ERROR; last_error="Fail to find device \"urn:schemas-upnp-org:device:WANConnectionDevice:1\"\n"; return false; } XMLNode serviceList_node,service_node,serviceType_node; serviceList_node=device_node.getChildNode("serviceList",0); if(serviceList_node.isEmpty()) { status=NAT_ERROR; last_error=" Fail to find serviceList of device \"urn:schemas-upnp-org:device:WANDevice:1 \"\n"; return false; } num=serviceList_node.nChildNode("service"); const char * serviceType; bool is_found=false; for(i=0;i<num;i++) { service_node=serviceList_node.getChildNode("service",i); if(service_node.isEmpty()) break; serviceType_node=service_node.getChildNode("serviceType"); if(serviceType_node.isEmpty()) continue; serviceType=serviceType_node.getText(); if(strcmp(serviceType,SERVICE_WANIP)==0||strcmp(serviceType,SERVICE_WANPPP)==0) { is_found=true; break; } } if(!is_found) { status=NAT_ERROR; last_error="can't find \" SERVICE_WANIP \" or \" SERVICE_WANPPP \" service.\n"; return false; } this->service_type=serviceType; XMLNode controlURL_node=service_node.getChildNode("controlURL"); control_url=controlURL_node.getText(); //make the complete control_url; if(control_url.find("http://")==std::string::npos&&control_url.find("HTTP://")==std::string::npos) control_url=base_url+control_url; if(service_describe_url.find("http://")==std::string::npos&&service_describe_url.find("HTTP://")==std::string::npos) service_describe_url=base_url+service_describe_url; closesocket(tcp_socket_fd); status=NAT_GETCONTROL; return true; }bool UPNPNAT::add_port_mapping(unsigned short int _port,const char * _protocal){ int ret,i; std::string host,path; unsigned short int port; ret=parseUrl(control_url.c_str(),host,&port,path); if(!ret) { status=NAT_ERROR; last_error="Fail to parseURl: "+describe_url+"\n"; return false; } //connect ret=tcp_connect(host.c_str(),port); if(!ret) return false; get_localIP();//get local ip and put it in this->local_ip; char buff[MAX_BUFF_SIZE+1]; sprintf(buff,ADD_PORT_MAPPING_PARAMS,_port,_protocal,_port,local_ip.c_str()); std::string action_params=buff; sprintf(buff,SOAP_ACTION,ACTION_ADD,service_type.c_str(),action_params.c_str(),ACTION_ADD); std::string soap_message=buff; sprintf(buff,HTTP_HEADER_ACTION,path.c_str(),host.c_str(),port,service_type.c_str(),ACTION_ADD,soap_message.size()); std::string action_message=buff; std::string http_request=action_message+soap_message; //send request ret=send(tcp_socket_fd,http_request.c_str(),http_request.size(),0); //wait for response std::string response; while (ret=recv(tcp_socket_fd,buff,MAX_BUFF_SIZE,0) >0) { //buff[ret+1]=0; response+=buff; }// if(ret<0)// {// status=NAT_ERROR;// last_error="Fail to get response after send add port mapping request.\n";// return false;// } //std::string response=buff; //std::cout<<"add response:\n"<<buff<<std::endl; if(response.find(HTTP_OK)==std::string::npos) { status=NAT_ERROR; char temp[100]; sprintf(temp,"Fail to add port mapping (%s/%u)\n",_protocal,_port); last_error=temp; return false; } closesocket(tcp_socket_fd); status=NAT_ADD; return true; }bool UPNPNAT::del_port_mapping(unsigned short int _port,const char * _protocal){ int ret,i; std::string host,path; unsigned short int port; ret=parseUrl(control_url.c_str(),host,&port,path); if(!ret) { status=NAT_ERROR; last_error="Fail to parseURl: "+describe_url+"\n"; return false; } //connect ret=tcp_connect(host.c_str(),port); if(!ret) return false; //get_localIP();//get local ip and put it in this->local_ip; char buff[MAX_BUFF_SIZE+1]; sprintf(buff,DEL_PORT_MAPPING_PARAMS,_port,_protocal); std::string action_params=buff; sprintf(buff,SOAP_ACTION,ACTION_DEL,service_type.c_str(),action_params.c_str(),ACTION_DEL); std::string soap_message=buff; sprintf(buff,HTTP_HEADER_ACTION,path.c_str(),host.c_str(),port,service_type.c_str(),ACTION_DEL,soap_message.size()); std::string action_message=buff; std::string http_request=action_message+soap_message; //send request ret=send(tcp_socket_fd,http_request.c_str(),http_request.size(),0); //wait for response std::string response; while (ret=recv(tcp_socket_fd,buff,MAX_BUFF_SIZE,0) >0) { // buff[ret+1]=0; response+=buff; }// if(ret<0)// {// status=NAT_ERROR;// last_error="Fail to get response after send del port mapping request.\n";// return false;// }// std::string response=buff; if(response.find(HTTP_OK)==std::string::npos) { status=NAT_ERROR; char temp[100]; sprintf(temp,"Fail to del port mapping (%s/%u)\n",_protocal,_port); last_error=temp; return false; } closesocket(tcp_socket_fd); status=NAT_ADD; return true; }bool UPNPNAT::parse_mapping_info(){ //std::cout<<"mapping info cstr \n"<<mapping_info.c_str()<<std::endl; XMLNode node; node=XMLNode::parseString(mapping_info.c_str(),"SOAP-ENV:Envelope"); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"SOAP-ENV:Envelope\" node.)\n"; return false; } node=node.getChildNode("SOAP-ENV:Body"); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"SOAP-ENV:Body\" node.)\n"; return false; } node=node.getChildNode("u:GetGenericPortMappingEntryResponse"); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"u:GetGenericPortMappingEntryResponse\" node.)\n"; return false; } XMLNode child; child=node.getChildNode("NewExternalPort",0); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"NewExternalPort\" node.)\n"; return false; } std::string NewExternalPort=(child.getText()?child.getText():""); child=node.getChildNode("NewProtocol",0); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"NewProtocol\" node.)\n"; return false; } std::string NewProtocol=(child.getText()?child.getText():""); child=node.getChildNode("NewInternalPort",0); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"NewInternalPort\" node.)\n"; return false; } std::string NewInternalPort=(child.getText()?child.getText():""); child=node.getChildNode("NewInternalClient",0); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"NewInternalClient\" node.)\n"; return false; } std::string NewInternalClient=(child.getText()?child.getText():""); child=node.getChildNode("NewPortMappingDescription",0); if(node.isEmpty()) { status=NAT_ERROR; last_error="Fail to get mapping info from response data.(NO \"NewPortMappingDescription\" node.)\n"; return false; } std::string NewPortMappingDescription=(child.getText()?child.getText():""); char temp[100]; sprintf(temp,MAPPING_INFO,NewExternalPort.c_str(),NewProtocol.c_str(),NewPortMappingDescription.c_str(),NewInternalClient.c_str(),NewInternalPort.c_str()); std::string info=temp; mapping_infos.push_back(info); return true;}bool UPNPNAT::get_port_mapping(){ int ret,i; //connect to NAT std::string host,path; unsigned short int port; ret=parseUrl(control_url.c_str(),host,&port,path); if(!ret) { status=NAT_ERROR; last_error="Fail to parseURl: "+describe_url+"\n"; return false; } i=0; char buff[MAX_BUFF_SIZE+1]; while(true) { memset(buff,0,MAX_BUFF_SIZE+1); //connect ret=tcp_connect(host.c_str(),port); if(!ret) return false; sprintf(buff,GET_GEN_PARAMS,i); std::string action_params=buff; sprintf(buff,SOAP_ACTION,ACTION_GET_GEN,service_type.c_str(),action_params.c_str(),ACTION_GET_GEN); std::string soap_message=buff; sprintf(buff,HTTP_HEADER_ACTION,path.c_str(),host.c_str(),port,service_type.c_str(),ACTION_GET_GEN,soap_message.size()); std::string action_message=buff; std::string http_request=action_message+soap_message; //send request ret=send(tcp_socket_fd,http_request.c_str(),http_request.size(),0); //sleep(20); // char receiveBuff[MAX_BUFF_SIZE+1]; //receiveBuff[0]=0; //wait for response std::string response; memset(buff,0,MAX_BUFF_SIZE+1); while (ret=recv(tcp_socket_fd,buff,MAX_BUFF_SIZE,0) >0) { //receiveBuff[ret+1]=0; response+=buff; memset(buff,0,MAX_BUFF_SIZE+1); }// if(ret<0)// {// status=NAT_ERROR;// last_error="Fail to get response after send get port mapping info request.\n";// return false;// } //buff[ret]=0; //std::cout<<"mapping_info"<<response<<std::endl; //system("pause"); mapping_info=response; if(mapping_info.find(HTTP_OK)==std::string::npos) { status=NAT_ERROR; char temp[100]; sprintf(temp,"Totally %i mapping entries.\n",i); last_error=temp; return true; } if(!parse_mapping_info()) return false; std::cout<<"mapping["<<i<<"]:"; std::cout<<mapping_infos[i]<<std::endl; i++; //get next mapping entries status=NAT_GET; closesocket(tcp_socket_fd); } status=NAT_GET; return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -