⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ilibssdpclient.c

📁 intel upnp stack source code
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * INTEL CONFIDENTIAL * Copyright (c) 2002, 2003 Intel Corporation.  All rights reserved. *  * The source code contained or described herein and all documents * related to the source code ("Material") are owned by Intel * Corporation or its suppliers or licensors.  Title to the * Material remains with Intel Corporation or its suppliers and * licensors.  The Material contains trade secrets and proprietary * and confidential information of Intel or its suppliers and * licensors. The Material is protected by worldwide copyright and * trade secret laws and treaty provisions.  No part of the Material * may be used, copied, reproduced, modified, published, uploaded, * posted, transmitted, distributed, or disclosed in any way without * Intel's prior express written permission.  * No license under any patent, copyright, trade secret or other * intellectual property right is granted to or conferred upon you * by disclosure or delivery of the Materials, either expressly, by * implication, inducement, estoppel or otherwise. Any license * under such intellectual property rights must be express and * approved by Intel in writing. *  * $Workfile: ILibSSDPClient.c * $Revision: #1.0.1799.42459 * $Author:   Intel Corporation, Intel Device Builder * $Date:     Wednesday, January 19, 2005 * * * */#if defined(WIN32) || defined(_WIN32_WCE)	#ifndef MICROSTACK_NO_STDAFX	#include "stdafx.h"	#endif	#define _CRTDBG_MAP_ALLOC	#include <math.h>	#include <winerror.h>#else	#include <fcntl.h>	#include <sys/types.h>	#include <sys/socket.h>	#include <netinet/in.h>	#include <arpa/inet.h>	#include <sys/time.h>	#include <netdb.h>	#include <sys/ioctl.h>	#include <net/if.h>	#include <sys/utsname.h>	#include <netinet/in.h>	#include <unistd.h>	#include <errno.h>	#include <semaphore.h>	#include <malloc.h>#endif#include <stdlib.h>#include <stdio.h>#include <stddef.h>#include <string.h>#if defined(WINSOCK2)	#include <winsock2.h>	#include <ws2tcpip.h>#elif defined(WINSOCK1)	#include <winsock.h>	#include <wininet.h>#endif#ifdef WIN32	#include <windows.h>	#include <winioctl.h>	#include <winbase.h>	#ifndef _WIN32_WCE		#include <crtdbg.h>	#endif#endif#ifndef _WIN32_WCE#include <time.h>#endif#include "ILibSSDPClient.h"#include "ILibParsers.h"#define UPNP_PORT 1900#define UPNP_GROUP "239.255.255.250"#define DEBUGSTATEMENT(x)struct SSDPClientModule{	void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime);	void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset);	void (*Destroy)(void* object);	void (*FunctionCallback)(void *sender, char* UDN, int Alive, char* LocationURL, int Timeout, UPnPSSDP_MESSAGE m, void *user);	char* DeviceURN;	int DeviceURNLength;	char* DeviceURN_Prefix;	int DeviceURN_PrefixLength;	int BaseDeviceVersionNumber;		int *IPAddress;	int NumIPAddress;#ifdef UPNP_1_1	void *HashTable;	void *TIMER;#	if defined(WIN32) || defined(_WIN32_WCE)	SOCKET UNICAST_Socket;#	else	int UNICAST_Socket;#	endif#endif	#if defined(WIN32) || defined(_WIN32_WCE)	SOCKET SSDPListenSocket;	SOCKET MSEARCH_Response_Socket;#else	int SSDPListenSocket;	int MSEARCH_Response_Socket;#endif	int Terminate;	void *Reserved;};#ifdef UPNP_1_1struct LocationInfo{	int BootID;	int ConfigID;	char* LocationURL;	unsigned short SearchPort;	int IsInteresting;	int Timeout;	char *UDN;	struct SSDPClientModule* parent;};void ILibSSDP_UnicastSearch(void *SSDPToken, char *IP, unsigned short Port){	struct SSDPClientModule *module = (struct SSDPClientModule*)SSDPToken;	char *buffer;	int bufferlength;	struct sockaddr_in dest_addr;	memset((char *)&(dest_addr),0,sizeof(dest_addr));	dest_addr.sin_family = AF_INET;	dest_addr.sin_addr.s_addr = inet_addr(IP);	dest_addr.sin_port = htons(Port);	buffer = (char*)malloc(105+module->DeviceURNLength);	bufferlength = sprintf(buffer,"M-SEARCH * HTTP/1.1\r\nMX: 3\r\nST: %s\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\n\r\n",module->DeviceURN);		printf("UNICASTING to %s:%u...\r\n",IP,Port);	sendto(module->UNICAST_Socket, buffer, bufferlength, 0, (struct sockaddr *) &dest_addr, sizeof(dest_addr));		free(buffer);}void ILibSSDP_LocationSink(void *obj){	struct LocationInfo* LI = (struct LocationInfo*)obj;	char *IP;	unsigned short Port;	char *Path;		struct sockaddr_in addr;	ILibParseUri(LI->LocationURL,&IP,&Port,&Path);		memset((char *)&addr, 0,sizeof(addr));	addr.sin_family = AF_INET;	addr.sin_addr.s_addr = inet_addr(IP);	addr.sin_port = htons(Port);		//Send UNICAST M-SEARCH to the device	ILibSSDP_UnicastSearch(LI->parent,IP,LI->SearchPort);	free(IP);	free(Path);}//void ILibSSDP_LocationSink_Destroy(void *obj)//{//	struct LocationInfo* LI = (struct LocationInfo*)obj;//	free(LI->LocationURL);//	free(LI);//}#endif#if defined(WIN32) || defined(_WIN32_WCE)static void ILibReadSSDP(SOCKET ReadSocket, struct SSDPClientModule *module)#elsestatic void ILibReadSSDP(int ReadSocket, struct SSDPClientModule *module)#endif{	int bytesRead = 0;	char* buffer = (char*)malloc(4096);	struct sockaddr_in addr;	unsigned int addrlen = sizeof(struct sockaddr_in);	struct packetheader *packet;	struct packetheader_field_node *node;	struct parser_result *pnode,*pnode2;	struct parser_result_field *prf;		char* Location = NULL;	char* UDN = NULL;	int Timeout = 0;	int Alive = 0;	int OK;	int rt;#ifdef UPNP_1_1	int BootID=0;	int ConfigID=0;	int MaxVersion=1;	unsigned short SearchPort = 1900;#endif		char *IP;	unsigned short PORT;	char *PATH;	int MATCH=0;#ifdef UPNP_1_1	struct LocationInfo *LI;#endif		do	{		bytesRead = recvfrom(ReadSocket, buffer, 4096, 0, (struct sockaddr *) &addr, &addrlen);		if(bytesRead<=0) 		{			break;		}		packet = ILibParsePacketHeader(buffer,0,bytesRead);		if(packet==NULL) {continue;}				if(packet->Directive==NULL)		{			/* M-SEARCH Response */			if(packet->StatusCode==200)			{				node = packet->FirstField;				while(node!=NULL)				{#ifdef UPNP_1_1					if(strncasecmp(node->Field,"BOOTID.UPNP.ORG",15)==0)					{						node->FieldData[node->FieldDataLength] = '\0';						BootID = atoi(node->FieldData);					}					if(strncasecmp(node->Field,"CONFIGID.UPNP.ORG",17)==0)					{						node->FieldData[node->FieldDataLength] = '\0';						ConfigID = atoi(node->FieldData);					}					if(strncasecmp(node->Field,"SEARCHPORT.UPNP.ORG",19)==0)					{						node->FieldData[node->FieldDataLength] = '\0';						SearchPort = (unsigned short)atoi(node->FieldData);					}#endif					if(strncasecmp(node->Field,"LOCATION",8)==0)					{						Location = node->FieldData;						Location[node->FieldDataLength] = 0;						//Location = (char*)malloc(node->FieldDataLength+1);						//memcpy(Location,node->FieldData,node->FieldDataLength);						//Location[node->FieldDataLength] = '\0';					}					if(strncasecmp(node->Field,"CACHE-CONTROL",13)==0)					{						pnode = ILibParseString(node->FieldData, 0, node->FieldDataLength, ",", 1);						prf = pnode->FirstResult;						while(prf!=NULL)						{							pnode2 = ILibParseString(prf->data, 0, prf->datalength, "=", 1);							pnode2->FirstResult->datalength = ILibTrimString(&(pnode2->FirstResult->data),pnode2->FirstResult->datalength);							pnode2->FirstResult->data[pnode2->FirstResult->datalength]=0;							if(strcasecmp(pnode2->FirstResult->data,"max-age")==0)							{								pnode2->LastResult->datalength = ILibTrimString(&(pnode2->LastResult->data),pnode2->LastResult->datalength);								pnode2->LastResult->data[pnode2->LastResult->datalength]=0;								Timeout = atoi(pnode2->LastResult->data);								ILibDestructParserResults(pnode2);								break;							}							prf = prf->NextResult;							ILibDestructParserResults(pnode2);						}						ILibDestructParserResults(pnode);					}					if(strncasecmp(node->Field,"USN",3)==0)					{						pnode = ILibParseString(node->FieldData, 0, node->FieldDataLength, "::", 2);						pnode->FirstResult->data[pnode->FirstResult->datalength] = '\0';						UDN = pnode->FirstResult->data+5;						ILibDestructParserResults(pnode);					}					node = node->NextField;				}				ILibParseUri(Location,&IP,&PORT,&PATH);				if(addr.sin_addr.s_addr==inet_addr(IP))				{#ifdef UPNP_1_1					LI = (struct LocationInfo*)ILibGetEntry(module->HashTable,Location,(int)strlen(Location));					if(LI!=NULL)					{						LI->IsInteresting = 1;						LI->Timeout = Timeout;					}					else					{						LI = (struct LocationInfo*)malloc(sizeof(struct LocationInfo));						memset(LI,0,sizeof(struct LocationInfo));						LI->ConfigID = ConfigID;						LI->LocationURL = (char*)malloc((int)strlen(Location)+1);						strcpy(LI->LocationURL,Location);						LI->SearchPort = SearchPort;						LI->parent = module;						LI->IsInteresting = 1;						LI->Timeout = Timeout;						LI->UDN = (char*)malloc(strlen(UDN)+1);						strcpy(LI->UDN,UDN);						ILibAddEntry(module->HashTable,Location,(int)strlen(Location),LI);					}#endif					if(module->FunctionCallback!=NULL)					{						module->FunctionCallback(module,UDN,-1,Location,Timeout,UPnPSSDP_MSEARCH,module->Reserved);					}				}				free(IP);				free(PATH);			}		}		else		{			/* Notify Packet */			if(strncasecmp(packet->Directive,"NOTIFY",6)==0)			{				OK = 0;				rt = 0;				node = packet->FirstField;#ifdef UPNP_1_1				// Find MaxVersion first				while(node!=NULL)				{					node->Field[node->FieldLength] = '\0';					if(strncasecmp(node->Field,"MAXVERSION.UPNP.ORG",19)==0)					{						node->FieldData[node->FieldDataLength] = '\0';						MaxVersion = atoi(node->FieldData);					}					node = node->NextField;				}				node = packet->FirstField;#endif				while(node!=NULL)				{					node->Field[node->FieldLength] = '\0';					if(strncasecmp(node->Field,"NT",2)==0 && node->FieldLength==2)					{						node->FieldData[node->FieldDataLength] = '\0';						if(strncasecmp(node->FieldData,module->DeviceURN_Prefix,module->DeviceURN_PrefixLength)==0)						{#ifdef UPNP_1_1							if(atoi(node->FieldData+module->DeviceURN_PrefixLength)>=module->BaseDeviceVersionNumber || MaxVersion >= module->BaseDeviceVersionNumber)							{								OK = -1;							}#else							if(atoi(node->FieldData+module->DeviceURN_PrefixLength)>=module->BaseDeviceVersionNumber)							{								OK = -1;							}#endif						}#ifndef UPNP_1_1						else if(strncasecmp(node->FieldData,"upnp:rootdevice",15)==0)						{							rt = -1;						}						else						{							break;						}#endif					}					if(strncasecmp(node->Field,"NTS",3)==0)					{						if(strncasecmp(node->FieldData,"ssdp:alive",10)==0)						{							Alive = -1;							rt = 0;						}						else						{							Alive = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -