📄 ssdpparser.c
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2000 Intel Corporation// All rights reserved.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,// this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.// * Neither name of Intel Corporation nor the names of its contributors// may be used to endorse or promote products derived from this software// without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.//// $Revision: 1.2 $// $Date: 2001/08/15 18:17:31 $#include "../../inc/tools/config.h"#if EXCLUDE_SSDP == 0#include "ssdplib.h"#include <string.h>#include <stdlib.h>#include <stdio.h>#include "genlib/http_client/http_client.h"static char Token_List[][25] = {"CACHE-CONTROL","ST","LOCATION","HOST","USN","NT","NTS","EXT","SERVER","MAN","MX","DATE"};static ParserFun FunList[NUM_TOKEN];char * StrTok(char ** Src, char * Del){ char * TmpPtr, *RetPtr; if(*Src != NULL) { RetPtr = *Src; TmpPtr = strstr(*Src,Del); if(TmpPtr != NULL) { *TmpPtr = '\0'; *Src = TmpPtr+ strlen(Del); } else *Src = NULL; return RetPtr; } return NULL;}////////////////////////////////////////////////////////////////////////////////////////////////// Function : CheckHdr(char * Cmd,Event * Evt)// Description : This function checks the first statement in the HTTP packet.// Parameters : Cmd : HTTP string.// Evt : Event structure defind in interface.h// Return value:///////////////////////////////////////////////////////////////////////////////////////////////int CheckHdr(char * Cmd,Event * Evt){ char Tmp[COMMAND_LEN],Seps[] = " ",*Token,*TmpPtr; TmpPtr =Tmp; strcpy(TmpPtr,Cmd); Token = StrTok((char **)&TmpPtr, Seps ); //Should be "HTTP" here if(strstr(Token,"M-SEARCH") != NULL) //This command is for service { Token = StrTok((char **)&TmpPtr, Seps ); //Should be "*" here if(strstr(Token,"*") == NULL) { Evt->ErrCode = E_HTTP_SYNTEX ; return -1; } else { Token = StrTok((char **)&TmpPtr, Seps ); //Should be "HTTP" here if( strstr(Token,"HTTP/1.1") == NULL) { Evt->ErrCode = E_HTTP_SYNTEX ; return -1; } else Evt->Cmd = SEARCH; } } else if (strstr(Token,"NOTIFY") != NULL) //This is for Client { Token = StrTok((char **)&TmpPtr, Seps ); //Should be "*" here if(strstr(Token,"*") == NULL) { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } else { Token = StrTok((char **)&TmpPtr, Seps ); //Should be "OK" here if( strstr(Token,"HTTP/1.1") == NULL) { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } } Evt->Cmd = NOTIFY; } else if(strstr(Token,"HTTP/1.1") != NULL) //This is for Client { Token = StrTok((char **)&TmpPtr, Seps ); //Should be "*" here if(strstr(Token,"200") == NULL) { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } else { Token = StrTok((char **)&TmpPtr, Seps ); //Should be "OK" here if( strstr(Token, "OK") != NULL) { Evt->Cmd = OK; return 1; } else { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } } } else { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } return 1;}////////////////////////////////////////////////////////////////////////////////////////////////// Function : Cache_Control(char * Cmd,Event * Evt)// Description : This function retrieves the Maximum-Age for the resources.// Parameters : Cmd : Cache_Control HTTP string.// Evt : Event structure defind in ssdplib.h, partially filled by all the parsing function.// Return value: 1 if True -1 if false.///////////////////////////////////////////////////////////////////////////////////////////////int Cache_Control(char * cmd,Event * Evt){ char *Token; Token = StrTok((char **)&cmd,"="); if(Token != NULL) { Token = StrTok((char **)&cmd,"=" ); if (Token != NULL) Evt->MaxAge = atoi(Token); else return -1 ; } else { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } return 1;}////////////////////////////////////////////////////////////////////////////////////////////////// Function : Location_Header(char * Cmd,Event * Evt)// Description : This function retrieves the location information(URL) for the resources.// Parameters : Cmd : Location header HTTP string.// Evt : Event structure defind in ssdplib.h, partially filled by all the parsing function.// Return value: 1 if True -1 if false.///////////////////////////////////////////////////////////////////////////////////////////////int Location_Header(char * cmd,Event * Evt){ if( cmd != NULL && strlen(cmd) >7) { strcpy(Evt->Location,cmd); } else { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } return 1; }////////////////////////////////////////////////////////////////////////////////////////////////// Function : Os(char * Cmd,Event * Evt)// Description : This function retrieves the OS info.// Parameters : Cmd : Request Id HTTP string.// Evt : Event structure defind in ssdplib.h, partially filled by all the parsing function.// Return value: 1 if True -1 if false.///////////////////////////////////////////////////////////////////////////////////////////////int Os(char * cmd,Event * Evt){ if(cmd != NULL && strlen(cmd) > 0) { strcpy(Evt->Os,cmd); } else { Evt->ErrCode = E_HTTP_SYNTEX; return -1; } return 1;}////////////////////////////////////////////////////////////////////////////////////////////////// Function : Unique_Service_Name(char * Cmd,Event * Evt)// Description : This function retrieves the name of resource.// Parameters : Cmd : Service Name string.// Evt : Event structure defind in ssdplib.h, partially filled by all the parsing function.// Return value: 1 if True -1 if false.///////////////////////////////////////////////////////////////////////////////////////////////int Unique_Service_Name(char * cmd, Event * Evt){ char *TempPtr, TempBuf[COMMAND_LEN], *Ptr,*ptr1,*ptr2,*ptr3, CommandFound = 0; if((TempPtr=strstr(cmd,"uuid:schemas")) != NULL) { ptr1= strstr(cmd,":device"); if(ptr1!= NULL) ptr2= strstr(ptr1+1,":"); else return -1; if(ptr2!= NULL) ptr3= strstr(ptr2+1,":"); else return -1; if(ptr3!= NULL) sprintf(Evt->UDN,"uuid:%s",ptr3+1); else return -1; ptr1 = strstr(cmd,":"); if(ptr1!= NULL) { strncpy(TempBuf,ptr1,ptr3-ptr1); TempBuf[ptr3-ptr1] = '\0'; sprintf(Evt->DeviceType,"urn%s",TempBuf); } else return -1; return 1; } if((TempPtr=strstr(cmd,"uuid")) != NULL) { //printf("cmd = %s\n",cmd); if( ( Ptr = strstr(cmd,"::") ) != NULL) { strncpy(Evt->UDN,TempPtr, Ptr-TempPtr); Evt->UDN[Ptr-TempPtr] ='\0'; } else strcpy(Evt->UDN,TempPtr); CommandFound = 1; } if(strstr(cmd,"urn:") != NULL && strstr(cmd,":service:") != NULL ) { if((TempPtr=strstr(cmd,"urn")) != NULL ) { strcpy(Evt->ServiceType,TempPtr); CommandFound = 1; } } if(strstr(cmd,"urn:") != NULL && strstr(cmd,":device:") != NULL) { if((TempPtr=strstr(cmd,"urn")) != NULL ) { strcpy(Evt->DeviceType,TempPtr); CommandFound = 1; } } if( CommandFound == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -