📄 gatedevice.c
字号:
if ((ret = pmlist_Find(ext_port, proto, int_ip)) != NULL) { trace(3, "Found port map to already exist. Replacing"); pmlist_Delete(ret); } new = pmlist_NewNode(atoi(bool_enabled), atol(int_duration), "", ext_port, int_port, proto, int_ip, desc); result = pmlist_PushBack(new); if (result==1) { ScheduleMappingExpiration(new,ca_event->DevUDN,ca_event->ServiceID); sprintf(num, "%d", pmlist_Size()); trace(3, "PortMappingNumberOfEntries: %d", pmlist_Size()); UpnpAddToPropertySet(&propSet, "PortMappingNumberOfEntries", num); UpnpNotifyExt(deviceHandle, ca_event->DevUDN, ca_event->ServiceID, propSet); ixmlDocument_free(propSet); trace(2, "AddPortMap: DevUDN: %s ServiceID: %s RemoteHost: %s Prot: %s ExtPort: %s Int: %s.%s", ca_event->DevUDN,ca_event->ServiceID,remote_host, proto, ext_port, int_ip, int_port); action_succeeded = 1; } else { if (result==718) { trace(1,"Failure in GateDeviceAddPortMapping: RemoteHost: %s Prot:%s ExtPort: %s Int: %s.%s\n", remote_host, proto, ext_port, int_ip, int_port); ca_event->ErrCode = 718; strcpy(ca_event->ErrStr, "ConflictInMappingEntry"); ca_event->ActionResult = NULL; } } } else { trace(1, "Failiure in GateDeviceAddPortMapping: Invalid Arguments!"); trace(1, " ExtPort: %s Proto: %s IntPort: %s IntIP: %s Dur: %s Ena: %s Desc: %s", ext_port, proto, int_port, int_ip, int_duration, bool_enabled, desc); ca_event->ErrCode = 402; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } if (action_succeeded) { ca_event->ErrCode = UPNP_E_SUCCESS; snprintf(resultStr, RESULT_LEN, "<u:%sResponse xmlns:u=\"%s\">\n%s\n</u:%sResponse>", ca_event->ActionName, "urn:schemas-upnp-org:service:WANIPConnection:1", "", ca_event->ActionName); ca_event->ActionResult = ixmlParseBuffer(resultStr); } if (ext_port) free(ext_port); if (int_port) free(int_port); if (proto) free(proto); if (int_ip) free(int_ip); if (bool_enabled) free(bool_enabled); if (desc) free(desc); if (remote_host) free(remote_host); return(ca_event->ErrCode);}int GetGenericPortMappingEntry(struct Upnp_Action_Request *ca_event){ char *mapindex = NULL; struct portMap *temp; char result_param[RESULT_LEN]; char resultStr[RESULT_LEN]; int action_succeeded = 0; if ((mapindex = GetFirstDocumentItem(ca_event->ActionRequest, "NewPortMappingIndex"))) { temp = pmlist_FindByIndex(atoi(mapindex)); if (temp) { snprintf(result_param, RESULT_LEN, "<NewRemoteHost>%s</NewRemoteHost><NewExternalPort>%s</NewExternalPort><NewProtocol>%s</NewProtocol><NewInternalPort>%s</NewInternalPort><NewInternalClient>%s</NewInternalClient><NewEnabled>%d</NewEnabled><NewPortMappingDescription>%s</NewPortMappingDescription><NewLeaseDuration>%li</NewLeaseDuration>", temp->m_RemoteHost, temp->m_ExternalPort, temp->m_PortMappingProtocol, temp->m_InternalPort, temp->m_InternalClient, temp->m_PortMappingEnabled, temp->m_PortMappingDescription, temp->m_PortMappingLeaseDuration); action_succeeded = 1; } if (action_succeeded) { ca_event->ErrCode = UPNP_E_SUCCESS; snprintf(resultStr, RESULT_LEN, "<u:%sResponse xmlns:u=\"%s\">\n%s\n</u:%sResponse>", ca_event->ActionName, "urn:schemas-upnp-org:service:WANIPConnection:1",result_param, ca_event->ActionName); ca_event->ActionResult = ixmlParseBuffer(resultStr); } else { ca_event->ErrCode = 713; strcpy(ca_event->ErrStr, "SpecifiedArrayIndexInvalid"); ca_event->ActionResult = NULL; } } else { trace(1, "Failure in GateDeviceGetGenericPortMappingEntry: Invalid Args"); ca_event->ErrCode = 402; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } if (mapindex) free (mapindex); return (ca_event->ErrCode); }int GetSpecificPortMappingEntry(struct Upnp_Action_Request *ca_event){ char *ext_port=NULL; char *proto=NULL; char result_param[RESULT_LEN]; char resultStr[RESULT_LEN]; int action_succeeded = 0; struct portMap *temp; if ((ext_port = GetFirstDocumentItem(ca_event->ActionRequest, "NewExternalPort")) && (proto = GetFirstDocumentItem(ca_event->ActionRequest,"NewProtocol"))) { if ((strcmp(proto, "TCP") == 0) || (strcmp(proto, "UDP") == 0)) { temp = pmlist_FindSpecific (ext_port, proto); if (temp) { snprintf(result_param, RESULT_LEN, "<NewInternalPort>%s</NewInternalPort><NewInternalClient>%s</NewInternalClient><NewEnabled>%d</NewEnabled><NewPortMappingDescription>%s</NewPortMappingDescription><NewLeaseDuration>%li</NewLeaseDuration>", temp->m_InternalPort, temp->m_InternalClient, temp->m_PortMappingEnabled, temp->m_PortMappingDescription, temp->m_PortMappingLeaseDuration); action_succeeded = 1; } if (action_succeeded) { ca_event->ErrCode = UPNP_E_SUCCESS; snprintf(resultStr, RESULT_LEN, "<u:%sResponse xmlns:u=\"%s\">\n%s\n</u:%sResponse>", ca_event->ActionName, "urn:schemas-upnp-org:service:WANIPConnection:1",result_param, ca_event->ActionName); ca_event->ActionResult = ixmlParseBuffer(resultStr); } else { trace(2, "GateDeviceGetSpecificPortMappingEntry: PortMapping Doesn't Exist..."); ca_event->ErrCode = 714; strcpy(ca_event->ErrStr, "NoSuchEntryInArray"); ca_event->ActionResult = NULL; } } else { trace(1, "Failure in GateDeviceGetSpecificPortMappingEntry: Invalid NewProtocol=%s\n",proto); ca_event->ErrCode = 402; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } } else { trace(1, "Failure in GateDeviceGetSpecificPortMappingEntry: Invalid Args"); ca_event->ErrCode = 402; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } return (ca_event->ErrCode);}int GetExternalIPAddress(struct Upnp_Action_Request *ca_event){ char resultStr[RESULT_LEN]; IXML_Document *result = NULL; ca_event->ErrCode = UPNP_E_SUCCESS; GetIpAddressStr(ExternalIPAddress, g_vars.extInterfaceName); snprintf(resultStr, RESULT_LEN, "<u:GetExternalIPAddressResponse xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">\n" "<NewExternalIPAddress>%s</NewExternalIPAddress>\n" "</u:GetExternalIPAddressResponse>", ExternalIPAddress); // Create a IXML_Document from resultStr and return with ca_event if ((result = ixmlParseBuffer(resultStr)) != NULL) { ca_event->ActionResult = result; ca_event->ErrCode = UPNP_E_SUCCESS; } else { trace(1, "Error parsing Response to ExternalIPAddress: %s", resultStr); ca_event->ActionResult = NULL; ca_event->ErrCode = 402; } return(ca_event->ErrCode);}int DeletePortMapping(struct Upnp_Action_Request *ca_event){ char *ext_port=NULL; char *proto=NULL; int result=0; char num[5]; char resultStr[RESULT_LEN]; IXML_Document *propSet= NULL; int action_succeeded = 0; struct portMap *temp; if (((ext_port = GetFirstDocumentItem(ca_event->ActionRequest, "NewExternalPort")) && (proto = GetFirstDocumentItem(ca_event->ActionRequest, "NewProtocol")))) { if ((strcmp(proto, "TCP") == 0) || (strcmp(proto, "UDP") == 0)) { if ((temp = pmlist_FindSpecific(ext_port, proto))) result = pmlist_Delete(temp); if (result==1) { trace(2, "DeletePortMap: Proto:%s Port:%s\n",proto, ext_port); sprintf(num,"%d",pmlist_Size()); UpnpAddToPropertySet(&propSet,"PortMappingNumberOfEntries", num); UpnpNotifyExt(deviceHandle, ca_event->DevUDN,ca_event->ServiceID,propSet); ixmlDocument_free(propSet); action_succeeded = 1; } else { trace(1, "Failure in GateDeviceDeletePortMapping: DeletePortMap: Proto:%s Port:%s\n",proto, ext_port); ca_event->ErrCode = 714; strcpy(ca_event->ErrStr, "NoSuchEntryInArray"); ca_event->ActionResult = NULL; } } else { trace(1, "Failure in GateDeviceDeletePortMapping: Invalid NewProtocol=%s\n",proto); ca_event->ErrCode = 402; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } } else { trace(1, "Failiure in GateDeviceDeletePortMapping: Invalid Arguments!"); ca_event->ErrCode = 402; strcpy(ca_event->ErrStr, "Invalid Args"); ca_event->ActionResult = NULL; } if (action_succeeded) { ca_event->ErrCode = UPNP_E_SUCCESS; snprintf(resultStr, RESULT_LEN, "<u:%sResponse xmlns:u=\"%s\">\n%s\n</u:%sResponse>", ca_event->ActionName, "urn:schemas-upnp-org:service:WANIPConnection:1", "", ca_event->ActionName); ca_event->ActionResult = ixmlParseBuffer(resultStr); } if (ext_port) free(ext_port); if (proto) free(proto); return(ca_event->ErrCode);}// From sampleutil.c included with libupnp char* GetFirstDocumentItem( IN IXML_Document * doc, IN const char *item ){ IXML_NodeList *nodeList = NULL; IXML_Node *textNode = NULL; IXML_Node *tmpNode = NULL; char *ret = NULL; nodeList = ixmlDocument_getElementsByTagName( doc, ( char * )item ); if( nodeList ) { if( ( tmpNode = ixmlNodeList_item( nodeList, 0 ) ) ) { textNode = ixmlNode_getFirstChild( tmpNode ); if (textNode != NULL) { ret = strdup( ixmlNode_getNodeValue( textNode ) ); } } } if( nodeList ) ixmlNodeList_free( nodeList ); return ret;}int ExpirationTimerThreadInit(void){ int retVal; ThreadPoolAttr attr; TPAttrInit( &attr ); TPAttrSetMaxThreads( &attr, MAX_THREADS ); TPAttrSetMinThreads( &attr, MIN_THREADS ); TPAttrSetJobsPerThread( &attr, JOBS_PER_THREAD ); TPAttrSetIdleTime( &attr, THREAD_IDLE_TIME ); if( ThreadPoolInit( &gExpirationThreadPool, &attr ) != UPNP_E_SUCCESS ) { return UPNP_E_INIT_FAILED; } if( ( retVal = TimerThreadInit( &gExpirationTimerThread, &gExpirationThreadPool ) ) != UPNP_E_SUCCESS ) { return retVal; } return 0;}int ExpirationTimerThreadShutdown(void){ return TimerThreadShutdown(&gExpirationTimerThread);}void free_expiration_event(expiration_event *event){ if (event->mapping!=NULL) event->mapping->expirationEventId = -1; free(event);}void ExpireMapping(void *input){ char num[5]; // Maximum number of port mapping entries 9999 IXML_Document *propSet = NULL; expiration_event *event = ( expiration_event * ) input; ithread_mutex_lock(&DevMutex); trace(2, "ExpireMapping: Proto:%s Port:%s\n", event->mapping->m_PortMappingProtocol, event->mapping->m_ExternalPort); //reset the event id before deleting the mapping so that pmlist_Delete //will not call CancelMappingExpiration event->mapping->expirationEventId = -1; pmlist_Delete(event->mapping); sprintf(num, "%d", pmlist_Size()); UpnpAddToPropertySet(&propSet, "PortMappingNumberOfEntries", num); UpnpNotifyExt(deviceHandle, event->DevUDN, event->ServiceID, propSet); ixmlDocument_free(propSet); trace(3, "ExpireMapping: UpnpNotifyExt(deviceHandle,%s,%s,propSet)\n PortMappingNumberOfEntries: %s", event->DevUDN, event->ServiceID, num); free_expiration_event(event); ithread_mutex_unlock(&DevMutex);}int ScheduleMappingExpiration(struct portMap *mapping, char *DevUDN, char *ServiceID){ int retVal = 0; ThreadPoolJob job; expiration_event *event; time_t curtime = time(NULL); if (mapping->m_PortMappingLeaseDuration > 0) { mapping->expirationTime = curtime + mapping->m_PortMappingLeaseDuration; } else { //client did not provide a duration, so use the default duration if (g_vars.duration==0) { return 1; //no default duration set } else if (g_vars.duration>0) { //relative duration mapping->expirationTime = curtime+g_vars.duration; } else { //g_vars.duration < 0 //absolute daily expiration time long int expclock = -1*g_vars.duration; struct tm *loctime = localtime(&curtime); long int curclock = loctime->tm_hour*3600 + loctime->tm_min*60 + loctime->tm_sec; long int diff = expclock-curclock; if (diff<60) //if exptime is in less than a minute (or in the past), schedule it in 24 hours instead diff += 24*60*60; mapping->expirationTime = curtime+diff; } } event = ( expiration_event * ) malloc( sizeof( expiration_event ) ); if( event == NULL ) { return 0; } event->mapping = mapping; if (strlen(DevUDN) < sizeof(event->DevUDN)) strcpy(event->DevUDN, DevUDN); else strcpy(event->DevUDN, ""); if (strlen(ServiceID) < sizeof(event->ServiceID)) strcpy(event->ServiceID, ServiceID); else strcpy(event->ServiceID, ""); TPJobInit( &job, ( start_routine ) ExpireMapping, event ); TPJobSetFreeFunction( &job, ( free_routine ) free_expiration_event ); if( ( retVal = TimerThreadSchedule( &gExpirationTimerThread, mapping->expirationTime, ABS_SEC, &job, SHORT_TERM, &( event->eventId ) ) ) != UPNP_E_SUCCESS ) { free( event ); mapping->expirationEventId = -1; return 0; } mapping->expirationEventId = event->eventId; trace(3,"ScheduleMappingExpiration: DevUDN: %s ServiceID: %s Proto: %s ExtPort: %s Int: %s.%s at: %s eventId: %d",event->DevUDN,event->ServiceID,mapping->m_PortMappingProtocol, mapping->m_ExternalPort, mapping->m_InternalClient, mapping->m_InternalPort, ctime(&(mapping->expirationTime)), event->eventId); return event->eventId;}int CancelMappingExpiration(int expirationEventId){ ThreadPoolJob job; if (expirationEventId<0) return 1; trace(3,"CancelMappingExpiration: eventId: %d",expirationEventId); if (TimerThreadRemove(&gExpirationTimerThread,expirationEventId,&job)==0) { free_expiration_event((expiration_event *)job.arg); } else { trace(1," TimerThreadRemove failed!"); } return 1;}void DeleteAllPortMappings(void){ IXML_Document *propSet = NULL; ithread_mutex_lock(&DevMutex); pmlist_FreeList(); UpnpAddToPropertySet(&propSet, "PortMappingNumberOfEntries", "0"); UpnpNotifyExt(deviceHandle, gateUDN, "urn:upnp-org:serviceId:WANIPConn1", propSet); ixmlDocument_free(propSet); trace(2, "DeleteAllPortMappings: UpnpNotifyExt(deviceHandle,%s,%s,propSet)\n PortMappingNumberOfEntries: %s", gateUDN, "urn:upnp-org:serviceId:WANIPConn1", "0"); ithread_mutex_unlock(&DevMutex);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -