You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1815 lines
43 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. Copyright 2006 - 2011 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <sys/time.h>
  20. #include <netdb.h>
  21. #include <string.h>
  22. #include <sys/ioctl.h>
  23. #include <net/if.h>
  24. #include <sys/utsname.h>
  25. #include <netinet/in.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <semaphore.h>
  29. #include <malloc.h>
  30. #include <time.h>
  31. #include "ILibParsers.h"
  32. #define DEBUGSTATEMENT(x)
  33. #define UPNP_MAX_WAIT 2000000000
  34. extern int errno;
  35. static sem_t ILibChainLock;
  36. static int ILibChainLock_RefCounter = 0;
  37. static int malloc_counter = 0;
  38. void* dbg_malloc(int sz)
  39. {
  40. ++malloc_counter;
  41. return((void*)malloc(sz));
  42. }
  43. void dbg_free(void* ptr)
  44. {
  45. --malloc_counter;
  46. free(ptr);
  47. }
  48. int dbg_GetCount()
  49. {
  50. return(malloc_counter);
  51. }
  52. struct ILibStackNode
  53. {
  54. void *Data;
  55. struct ILibStackNode *Next;
  56. };
  57. struct HashNode
  58. {
  59. struct HashNode *Next;
  60. struct HashNode *Prev;
  61. int KeyHash;
  62. char *KeyValue;
  63. int KeyLength;
  64. void *Data;
  65. };
  66. int ILibGetLocalIPAddressList(int** pp_int)
  67. {
  68. char szBuffer[16*sizeof(struct ifreq)];
  69. struct ifconf ifConf;
  70. struct ifreq ifReq;
  71. int nResult;
  72. int LocalSock;
  73. struct sockaddr_in LocalAddr;
  74. int tempresults[16];
  75. int ctr=0;
  76. int i;
  77. /* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
  78. if ((LocalSock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  79. {
  80. DEBUGSTATEMENT(printf("Can't do that\r\n"));
  81. exit(1);
  82. }
  83. /* Get the interface configuration information... */
  84. ifConf.ifc_len = sizeof szBuffer;
  85. ifConf.ifc_ifcu.ifcu_buf = (caddr_t)szBuffer;
  86. nResult = ioctl(LocalSock, SIOCGIFCONF, &ifConf);
  87. if (nResult < 0)
  88. {
  89. DEBUGSTATEMENT(printf("ioctl error\r\n"));
  90. exit(1);
  91. }
  92. /* Cycle through the list of interfaces looking for IP addresses. */
  93. for (i = 0;(i < ifConf.ifc_len);)
  94. {
  95. struct ifreq *pifReq = (struct ifreq *)((caddr_t)ifConf.ifc_req + i);
  96. i += sizeof *pifReq;
  97. /* See if this is the sort of interface we want to deal with. */
  98. strcpy (ifReq.ifr_name, pifReq -> ifr_name);
  99. if (ioctl (LocalSock, SIOCGIFFLAGS, &ifReq) < 0)
  100. {
  101. DEBUGSTATEMENT(printf("Can't get flags\r\n"));
  102. exit(1);
  103. }
  104. /* Skip loopback, point-to-point and down interfaces, */
  105. /* except don't skip down interfaces */
  106. /* if we're trying to get a list of configurable interfaces. */
  107. if ((ifReq.ifr_flags & IFF_LOOPBACK) || (!(ifReq.ifr_flags & IFF_UP)))
  108. {
  109. continue;
  110. }
  111. if (pifReq -> ifr_addr.sa_family == AF_INET)
  112. {
  113. /* Get a pointer to the address... */
  114. memcpy (&LocalAddr, &pifReq -> ifr_addr, sizeof pifReq -> ifr_addr);
  115. if (LocalAddr.sin_addr.s_addr != htonl (INADDR_LOOPBACK))
  116. {
  117. tempresults[ctr] = LocalAddr.sin_addr.s_addr;
  118. ++ctr;
  119. }
  120. }
  121. }
  122. close(LocalSock);
  123. *pp_int = (int*)MALLOC(sizeof(int)*(ctr));
  124. memcpy(*pp_int,tempresults,sizeof(int)*ctr);
  125. return ctr;
  126. }
  127. struct ILibChain
  128. {
  129. void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime);
  130. void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset);
  131. void (*Destroy)(void* object);
  132. };
  133. struct LifeTimeMonitorData
  134. {
  135. long ExpirationTick;
  136. void *data;
  137. void (*CallbackPtr)(void *data);
  138. void (*DestroyPtr)(void *data);
  139. struct LifeTimeMonitorData *Next;
  140. };
  141. struct ILibLifeTime
  142. {
  143. void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime);
  144. void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset);
  145. void (*Destroy)(void* object);
  146. struct LifeTimeMonitorData *LM;
  147. void *Chain;
  148. sem_t SyncLock;
  149. };
  150. struct ILibBaseChain
  151. {
  152. int Terminate;
  153. int TerminateFlag;
  154. void *Object;
  155. void *Next;
  156. };
  157. void *ILibCreateChain()
  158. {
  159. struct ILibBaseChain *RetVal = (struct ILibBaseChain*)MALLOC(sizeof(struct ILibBaseChain));
  160. RetVal->Object = NULL;
  161. RetVal->Next = NULL;
  162. RetVal->Terminate = socket(AF_INET, SOCK_DGRAM, 0);
  163. RetVal->TerminateFlag = 0;
  164. if(ILibChainLock_RefCounter==0)
  165. {
  166. sem_init(&ILibChainLock,0,1);
  167. }
  168. return(RetVal);
  169. }
  170. void ILibAddToChain(void *Chain, void *object)
  171. {
  172. struct ILibBaseChain *chain = (struct ILibBaseChain*)Chain;
  173. while(chain->Next!=NULL)
  174. {
  175. chain = chain->Next;
  176. }
  177. if(chain->Object!=NULL)
  178. {
  179. chain->Next = (struct ILibBaseChain*)MALLOC(sizeof(struct ILibBaseChain));
  180. chain = chain->Next;
  181. }
  182. chain->Object = object;
  183. chain->Next = NULL;
  184. }
  185. void ILibForceUnBlockChain(void *Chain)
  186. {
  187. struct ILibBaseChain *c = (struct ILibBaseChain*)Chain;
  188. int temp;
  189. sem_wait(&ILibChainLock);
  190. temp = c->Terminate;
  191. c->Terminate = ~0;
  192. close(temp);
  193. sem_post(&ILibChainLock);
  194. }
  195. void ILibStartChain(void *Chain)
  196. {
  197. struct ILibBaseChain *c = (struct ILibBaseChain*)Chain;
  198. struct ILibBaseChain *temp;
  199. fd_set readset;
  200. fd_set errorset;
  201. fd_set writeset;
  202. struct timeval tv;
  203. int slct;
  204. srand((unsigned int)time(NULL));
  205. FD_ZERO(&readset);
  206. FD_ZERO(&errorset);
  207. FD_ZERO(&writeset);
  208. while(((struct ILibBaseChain*)Chain)->TerminateFlag==0)
  209. {
  210. slct = 0;
  211. FD_ZERO(&readset);
  212. FD_ZERO(&errorset);
  213. FD_ZERO(&writeset);
  214. tv.tv_sec = UPNP_MAX_WAIT;
  215. tv.tv_usec = 0;
  216. sem_wait(&ILibChainLock);
  217. if(((struct ILibBaseChain*)Chain)->Terminate==~0)
  218. {
  219. slct = -1;
  220. }
  221. else
  222. {
  223. FD_SET(((struct ILibBaseChain*)Chain)->Terminate,&errorset);
  224. }
  225. sem_post(&ILibChainLock);
  226. c = (struct ILibBaseChain*)Chain;
  227. while(c!=NULL && c->Object!=NULL)
  228. {
  229. if(((struct ILibChain*)c->Object)->PreSelect!=NULL)
  230. {
  231. ((struct ILibChain*)c->Object)->PreSelect(c->Object,&readset,&writeset,&errorset,(int*)&tv.tv_sec);
  232. }
  233. c = c->Next;
  234. }
  235. if(slct!=0)
  236. {
  237. tv.tv_sec = 0;
  238. }
  239. slct = select(FD_SETSIZE,&readset,&writeset,&errorset,&tv);
  240. if(slct==-1)
  241. {
  242. FD_ZERO(&readset);
  243. FD_ZERO(&writeset);
  244. FD_ZERO(&errorset);
  245. }
  246. if(((struct ILibBaseChain*)Chain)->Terminate==~0)
  247. {
  248. ((struct ILibBaseChain*)Chain)->Terminate = socket(AF_INET,SOCK_DGRAM,0);
  249. }
  250. c = (struct ILibBaseChain*)Chain;
  251. while(c!=NULL && c->Object!=NULL)
  252. {
  253. if(((struct ILibChain*)c->Object)->PostSelect!=NULL)
  254. {
  255. ((struct ILibChain*)c->Object)->PostSelect(c->Object,slct,&readset,&writeset,&errorset);
  256. }
  257. c = c->Next;
  258. }
  259. }
  260. c = (struct ILibBaseChain*)Chain;
  261. while(c!=NULL && c->Object!=NULL)
  262. {
  263. if(((struct ILibChain*)c->Object)->Destroy!=NULL)
  264. {
  265. ((struct ILibChain*)c->Object)->Destroy(c->Object);
  266. }
  267. FREE(c->Object);
  268. c = c->Next;
  269. }
  270. c = (struct ILibBaseChain*)Chain;
  271. while(c!=NULL)
  272. {
  273. temp = c->Next;
  274. FREE(c);
  275. c = temp;
  276. }
  277. if(ILibChainLock_RefCounter==1)
  278. {
  279. sem_destroy(&ILibChainLock);
  280. }
  281. --ILibChainLock_RefCounter;
  282. }
  283. void ILibStopChain(void *Chain)
  284. {
  285. ((struct ILibBaseChain*)Chain)->TerminateFlag = 1;
  286. ILibForceUnBlockChain(Chain);
  287. }
  288. void ILibDestructXMLNodeList(struct ILibXMLNode *node)
  289. {
  290. struct ILibXMLNode *temp;
  291. while(node!=NULL)
  292. {
  293. temp = node->Next;
  294. FREE(node);
  295. node = temp;
  296. }
  297. }
  298. void ILibDestructXMLAttributeList(struct ILibXMLAttribute *attribute)
  299. {
  300. struct ILibXMLAttribute *temp;
  301. while(attribute!=NULL)
  302. {
  303. temp = attribute->Next;
  304. FREE(attribute);
  305. attribute = temp;
  306. }
  307. }
  308. int ILibProcessXMLNodeList(struct ILibXMLNode *nodeList)
  309. {
  310. int RetVal = 0;
  311. struct ILibXMLNode *current = nodeList;
  312. struct ILibXMLNode *temp;
  313. void *TagStack;
  314. ILibCreateStack(&TagStack);
  315. while(current!=NULL)
  316. {
  317. if(current->StartTag!=0)
  318. {
  319. // Start Tag
  320. current->Parent = ILibPeekStack(&TagStack);
  321. ILibPushStack(&TagStack,current);
  322. }
  323. else
  324. {
  325. // Close Tag
  326. temp = (struct ILibXMLNode*)ILibPopStack(&TagStack);
  327. if(temp!=NULL)
  328. {
  329. if(temp->NameLength==current->NameLength && memcmp(temp->Name,current->Name,current->NameLength)==0)
  330. {
  331. if(current->Next!=NULL)
  332. {
  333. if(current->Next->StartTag!=0)
  334. {
  335. temp->Peer = current->Next;
  336. }
  337. }
  338. temp->ClosingTag = current;
  339. }
  340. else
  341. {
  342. // Illegal Close Tag Order
  343. RetVal = -2;
  344. break;
  345. }
  346. }
  347. else
  348. {
  349. // Illegal Close Tag
  350. RetVal = -1;
  351. break;
  352. }
  353. }
  354. current = current->Next;
  355. }
  356. if(TagStack!=NULL)
  357. {
  358. // Incomplete XML
  359. RetVal = -3;
  360. }
  361. return(RetVal);
  362. }
  363. int ILibReadInnerXML(struct ILibXMLNode *node, char **RetVal)
  364. {
  365. struct ILibXMLNode *x = node;
  366. int length = 0;
  367. void *TagStack;
  368. ILibCreateStack(&TagStack);
  369. do
  370. {
  371. if(x->StartTag!=0) {ILibPushStack(&TagStack,x);}
  372. x = x->Next;
  373. }while(!(x->StartTag==0 && ILibPopStack(&TagStack)==node && x->NameLength==node->NameLength && memcmp(x->Name,node->Name,node->NameLength)==0));
  374. length = (int)((char*)x->Reserved - (char*)node->Reserved - 1);
  375. if(length<0) {length=0;}
  376. *RetVal = (char*)node->Reserved;
  377. return(length);
  378. }
  379. struct ILibXMLAttribute *ILibGetXMLAttributes(struct ILibXMLNode *node)
  380. {
  381. struct ILibXMLAttribute *RetVal = NULL;
  382. struct ILibXMLAttribute *current = NULL;
  383. char *c;
  384. int EndReserved = (node->EmptyTag==0)?1:2;
  385. struct parser_result *xml;
  386. struct parser_result_field *field;
  387. struct parser_result *temp2;
  388. struct parser_result *temp3;
  389. c = (char*)node->Reserved - 1;
  390. while(*c!='<')
  391. {
  392. c = c -1;
  393. }
  394. c = c +1;
  395. xml = ILibParseStringAdv(c,0,(int)((char*)node->Reserved - c -EndReserved)," ",1);
  396. field = xml->FirstResult;
  397. if(field!=NULL) {field = field->NextResult;}
  398. while(field!=NULL)
  399. {
  400. if(RetVal==NULL)
  401. {
  402. RetVal = (struct ILibXMLAttribute*)MALLOC(sizeof(struct ILibXMLAttribute));
  403. RetVal->Next = NULL;
  404. }
  405. else
  406. {
  407. current = (struct ILibXMLAttribute*)MALLOC(sizeof(struct ILibXMLAttribute));
  408. current->Next = RetVal;
  409. RetVal = current;
  410. }
  411. temp2 = ILibParseStringAdv(field->data,0,field->datalength,":",1);
  412. if(temp2->NumResults==1)
  413. {
  414. RetVal->Prefix = NULL;
  415. RetVal->PrefixLength = 0;
  416. temp3 = ILibParseStringAdv(field->data,0,field->datalength,"=",1);
  417. }
  418. else
  419. {
  420. RetVal->Prefix = temp2->FirstResult->data;
  421. RetVal->PrefixLength = temp2->FirstResult->datalength;
  422. temp3 = ILibParseStringAdv(field->data,RetVal->PrefixLength+1,field->datalength-RetVal->PrefixLength-1,"=",1);
  423. }
  424. ILibDestructParserResults(temp2);
  425. RetVal->Name = temp3->FirstResult->data;
  426. RetVal->NameLength = temp3->FirstResult->datalength;
  427. RetVal->Value = temp3->LastResult->data;
  428. RetVal->ValueLength = temp3->LastResult->datalength;
  429. ILibDestructParserResults(temp3);
  430. field = field->NextResult;
  431. }
  432. ILibDestructParserResults(xml);
  433. return(RetVal);
  434. }
  435. struct ILibXMLNode *ILibParseXML(char *buffer, int offset, int length)
  436. {
  437. struct parser_result *xml;
  438. struct parser_result_field *field;
  439. struct parser_result *temp;
  440. struct parser_result *temp2;
  441. struct parser_result *temp3;
  442. char* TagName;
  443. int TagNameLength;
  444. int StartTag;
  445. int EmptyTag;
  446. int i;
  447. struct ILibXMLNode *RetVal = NULL;
  448. struct ILibXMLNode *current = NULL;
  449. struct ILibXMLNode *x = NULL;
  450. char *NSTag;
  451. int NSTagLength;
  452. xml = ILibParseString(buffer,offset,length,"<",1);
  453. field = xml->FirstResult;
  454. while(field!=NULL)
  455. {
  456. if(memcmp(field->data,"?",1)!=0)
  457. {
  458. EmptyTag = 0;
  459. if(memcmp(field->data,"/",1)==0)
  460. {
  461. StartTag = 0;
  462. field->data = field->data+1;
  463. field->datalength -= 1;
  464. temp2 = ILibParseString(field->data,0,field->datalength,">",1);
  465. }
  466. else
  467. {
  468. StartTag = -1;
  469. temp2 = ILibParseString(field->data,0,field->datalength,">",1);
  470. if(temp2->FirstResult->data[temp2->FirstResult->datalength-1]=='/')
  471. {
  472. EmptyTag = -1;
  473. }
  474. }
  475. temp = ILibParseString(temp2->FirstResult->data,0,temp2->FirstResult->datalength," ",1);
  476. temp3 = ILibParseString(temp->FirstResult->data,0,temp->FirstResult->datalength,":",1);
  477. if(temp3->NumResults==1)
  478. {
  479. NSTag = NULL;
  480. NSTagLength = 0;
  481. TagName = temp3->FirstResult->data;
  482. TagNameLength = temp3->FirstResult->datalength;
  483. }
  484. else
  485. {
  486. NSTag = temp3->FirstResult->data;
  487. NSTagLength = temp3->FirstResult->datalength;
  488. TagName = temp3->FirstResult->NextResult->data;
  489. TagNameLength = temp3->FirstResult->NextResult->datalength;
  490. }
  491. ILibDestructParserResults(temp3);
  492. for(i=0;i<TagNameLength;++i)
  493. {
  494. if( (TagName[i]==' ')||(TagName[i]=='/')||(TagName[i]=='>')||(TagName[i]=='\r')||(TagName[i]=='\n') )
  495. {
  496. if(i!=0)
  497. {
  498. if(TagName[i]=='/')
  499. {
  500. EmptyTag = -1;
  501. }
  502. TagNameLength = i;
  503. break;
  504. }
  505. }
  506. }
  507. if(TagNameLength!=0)
  508. {
  509. x = (struct ILibXMLNode*)MALLOC(sizeof(struct ILibXMLNode));
  510. x->Next = NULL;
  511. x->Name = TagName;
  512. x->NameLength = TagNameLength;
  513. x->StartTag = StartTag;
  514. x->NSTag = NSTag;
  515. x->NSLength = NSTagLength;
  516. x->Parent = NULL;
  517. x->Peer = NULL;
  518. x->ClosingTag = NULL;
  519. x->EmptyTag = 0;
  520. if(StartTag==0)
  521. {
  522. x->Reserved = field->data;
  523. do
  524. {
  525. (char*)x->Reserved -= 1;
  526. }while(*((char*)x->Reserved)=='<');
  527. }
  528. else
  529. {
  530. x->Reserved = temp2->LastResult->data;
  531. }
  532. if(RetVal==NULL)
  533. {
  534. RetVal = x;
  535. }
  536. else
  537. {
  538. current->Next = x;
  539. }
  540. current = x;
  541. if(EmptyTag!=0)
  542. {
  543. x = (struct ILibXMLNode*)MALLOC(sizeof(struct ILibXMLNode));
  544. x->Next = NULL;
  545. x->Name = TagName;
  546. x->NameLength = TagNameLength;
  547. x->StartTag = 0;
  548. x->NSTag = NSTag;
  549. x->NSLength = NSTagLength;
  550. x->Parent = NULL;
  551. x->Peer = NULL;
  552. x->ClosingTag = NULL;
  553. x->Reserved = current->Reserved;
  554. current->EmptyTag = -1;
  555. current->Next = x;
  556. current = x;
  557. }
  558. }
  559. ILibDestructParserResults(temp2);
  560. ILibDestructParserResults(temp);
  561. }
  562. field = field->NextResult;
  563. }
  564. ILibDestructParserResults(xml);
  565. return(RetVal);
  566. }
  567. void ILibCreateStack(void **TheStack)
  568. {
  569. *TheStack = NULL;
  570. }
  571. void ILibPushStack(void **TheStack, void *data)
  572. {
  573. struct ILibStackNode *RetVal = (struct ILibStackNode*)MALLOC(sizeof(struct ILibStackNode));
  574. RetVal->Data = data;
  575. RetVal->Next = *TheStack;
  576. *TheStack = RetVal;
  577. }
  578. void *ILibPopStack(void **TheStack)
  579. {
  580. void *RetVal = NULL;
  581. void *Temp;
  582. if(*TheStack!=NULL)
  583. {
  584. RetVal = ((struct ILibStackNode*)*TheStack)->Data;
  585. Temp = *TheStack;
  586. *TheStack = ((struct ILibStackNode*)*TheStack)->Next;
  587. FREE(Temp);
  588. }
  589. return(RetVal);
  590. }
  591. void *ILibPeekStack(void **TheStack)
  592. {
  593. void *RetVal = NULL;
  594. if(*TheStack!=NULL)
  595. {
  596. RetVal = ((struct ILibStackNode*)*TheStack)->Data;
  597. }
  598. return(RetVal);
  599. }
  600. void ILibClearStack(void **TheStack)
  601. {
  602. void *Temp = *TheStack;
  603. do
  604. {
  605. ILibPopStack(&Temp);
  606. }while(Temp!=NULL);
  607. *TheStack = NULL;
  608. }
  609. void ILibDestroyHashTree(void *tree)
  610. {
  611. struct HashNode *c = (struct HashNode*)tree;
  612. struct HashNode *n;
  613. while(c!=NULL)
  614. {
  615. n = c->Next;
  616. if(c->KeyValue!=NULL) {FREE(c->KeyValue);}
  617. FREE(c);
  618. c = n;
  619. }
  620. }
  621. void* ILibInitHashTree()
  622. {
  623. struct HashNode *RetVal = (struct HashNode*)MALLOC(sizeof(struct HashNode));
  624. memset(RetVal,0,sizeof(struct HashNode));
  625. return(RetVal);
  626. }
  627. int ILibGetHashValue(void *key, int keylength)
  628. {
  629. int HashValue=0;
  630. char TempValue[4];
  631. if(keylength<=4)
  632. {
  633. memset(TempValue,0,4);
  634. memcpy(TempValue,key,keylength);
  635. HashValue = *((int*)TempValue);
  636. }
  637. else
  638. {
  639. memcpy(TempValue,key,4);
  640. HashValue = *((int*)TempValue);
  641. memcpy(TempValue,(char*)key+(keylength-4),4);
  642. HashValue = HashValue^(*((int*)TempValue));
  643. if(keylength>=10)
  644. {
  645. memcpy(TempValue,(char*)key+(keylength/2),4);
  646. HashValue = HashValue^(*((int*)TempValue));
  647. }
  648. }
  649. return(HashValue);
  650. }
  651. struct HashNode* ILibFindEntry(void *hashtree, void *key, int keylength, int create)
  652. {
  653. struct HashNode *current = (struct HashNode*)hashtree;
  654. int HashValue = ILibGetHashValue(key,keylength);
  655. int done = 0;
  656. while(done==0)
  657. {
  658. if(current->KeyHash==HashValue)
  659. {
  660. if(current->KeyLength==keylength && memcmp(current->KeyValue,key,keylength)==0)
  661. {
  662. return(current);
  663. }
  664. }
  665. if(current->Next!=NULL)
  666. {
  667. current = current->Next;
  668. }
  669. else if(create!=0)
  670. {
  671. current->Next = (struct HashNode*)MALLOC(sizeof(struct HashNode));
  672. memset(current->Next,0,sizeof(struct HashNode));
  673. current->Next->Prev = current;
  674. current->Next->KeyHash = HashValue;
  675. current->Next->KeyValue = (void*)MALLOC(keylength);
  676. memcpy(current->Next->KeyValue,key,keylength);
  677. current->Next->KeyLength = keylength;
  678. return(current->Next);
  679. }
  680. else
  681. {
  682. return(NULL);
  683. }
  684. }
  685. return(NULL);
  686. }
  687. void ILibAddEntry(void* hashtree, char* key, int keylength, void *value)
  688. {
  689. struct HashNode* n = ILibFindEntry(hashtree,key,keylength,1);
  690. n->Data = value;
  691. }
  692. void* ILibGetEntry(void *hashtree, char* key, int keylength)
  693. {
  694. struct HashNode* n = ILibFindEntry(hashtree,key,keylength,0);
  695. if(n==NULL)
  696. {
  697. return(NULL);
  698. }
  699. else
  700. {
  701. return(n->Data);
  702. }
  703. }
  704. void ILibDeleteEntry(void *hashtree, char* key, int keylength)
  705. {
  706. struct HashNode* n = ILibFindEntry(hashtree,key,keylength,0);
  707. if(n!=NULL)
  708. {
  709. n->Prev->Next = n->Next;
  710. if(n->Next!=NULL)
  711. {
  712. n->Next->Prev = n->Prev;
  713. }
  714. FREE(n->KeyValue);
  715. FREE(n);
  716. }
  717. }
  718. int ILibGetLong(char *TestValue, int TestValueLength, long* NumericValue)
  719. {
  720. char* StopString;
  721. char* TempBuffer2 = (char*)MALLOC(1+sizeof(char)*19);
  722. char* TempBuffer = (char*)MALLOC(1+sizeof(char)*TestValueLength);
  723. memcpy(TempBuffer,TestValue,TestValueLength);
  724. TempBuffer[TestValueLength] = '\0';
  725. *NumericValue = strtol(TempBuffer,&StopString,10);
  726. if(*StopString!='\0')
  727. {
  728. FREE(TempBuffer);
  729. FREE(TempBuffer2);
  730. return(-1);
  731. }
  732. else
  733. {
  734. FREE(TempBuffer);
  735. FREE(TempBuffer2);
  736. if(errno!=ERANGE)
  737. {
  738. return(0);
  739. }
  740. else
  741. {
  742. return(-1);
  743. }
  744. }
  745. }
  746. int ILibGetULong(const char *TestValue, const int TestValueLength, unsigned long* NumericValue){
  747. char* StopString;
  748. char* TempBuffer2 = (char*)MALLOC(1+sizeof(char)*19);
  749. char* TempBuffer = (char*)MALLOC(1+sizeof(char)*TestValueLength);
  750. memcpy(TempBuffer,TestValue,TestValueLength);
  751. TempBuffer[TestValueLength] = '\0';
  752. *NumericValue = strtoul(TempBuffer,&StopString,10);
  753. if(*StopString!='\0')
  754. {
  755. FREE(TempBuffer);
  756. FREE(TempBuffer2);
  757. return(-1);
  758. }
  759. else
  760. {
  761. FREE(TempBuffer);
  762. FREE(TempBuffer2);
  763. if(errno!=ERANGE)
  764. {
  765. if(memcmp(TestValue,"-",1)==0)
  766. {
  767. return(-1);
  768. }
  769. return(0);
  770. }
  771. else
  772. {
  773. return(-1);
  774. }
  775. }
  776. }
  777. int ILibIsDelimiter(char* buffer, int offset, int buffersize, char* Delimiter, int DelimiterLength)
  778. {
  779. int i=0;
  780. int RetVal = 1;
  781. if(offset+DelimiterLength>buffersize)
  782. {
  783. return(0);
  784. }
  785. for(i=0;i<DelimiterLength;++i)
  786. {
  787. if(buffer[offset+i]!=Delimiter[i])
  788. {
  789. RetVal = 0;
  790. break;
  791. }
  792. }
  793. return(RetVal);
  794. }
  795. struct parser_result* ILibParseStringAdv(char* buffer, int offset, int length, char* Delimiter, int DelimiterLength)
  796. {
  797. struct parser_result* RetVal = (struct parser_result*)MALLOC(sizeof(struct parser_result));
  798. int i=0;
  799. char* Token = NULL;
  800. int TokenLength = 0;
  801. struct parser_result_field *p_resultfield;
  802. int Ignore = 0;
  803. char StringDelimiter=0;
  804. RetVal->FirstResult = NULL;
  805. RetVal->NumResults = 0;
  806. Token = buffer + offset;
  807. for(i=offset;i<length;++i)
  808. {
  809. if(StringDelimiter==0)
  810. {
  811. if(buffer[i]=='"')
  812. {
  813. StringDelimiter='"';
  814. Ignore=1;
  815. }
  816. else
  817. {
  818. if(buffer[i]=='\'')
  819. {
  820. StringDelimiter='\'';
  821. Ignore=1;
  822. }
  823. }
  824. }
  825. else
  826. {
  827. if(buffer[i]==StringDelimiter)
  828. {
  829. Ignore=((Ignore==0)?1:0);
  830. }
  831. }
  832. if(Ignore==0 && ILibIsDelimiter(buffer,i,length,Delimiter,DelimiterLength))
  833. {
  834. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  835. p_resultfield->data = Token;
  836. p_resultfield->datalength = TokenLength;
  837. p_resultfield->NextResult = NULL;
  838. if(RetVal->FirstResult != NULL)
  839. {
  840. RetVal->LastResult->NextResult = p_resultfield;
  841. RetVal->LastResult = p_resultfield;
  842. }
  843. else
  844. {
  845. RetVal->FirstResult = p_resultfield;
  846. RetVal->LastResult = p_resultfield;
  847. }
  848. ++RetVal->NumResults;
  849. i = i + DelimiterLength -1;
  850. Token = Token + TokenLength + DelimiterLength;
  851. TokenLength = 0;
  852. }
  853. else
  854. {
  855. ++TokenLength;
  856. }
  857. }
  858. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  859. p_resultfield->data = Token;
  860. p_resultfield->datalength = TokenLength;
  861. p_resultfield->NextResult = NULL;
  862. if(RetVal->FirstResult != NULL)
  863. {
  864. RetVal->LastResult->NextResult = p_resultfield;
  865. RetVal->LastResult = p_resultfield;
  866. }
  867. else
  868. {
  869. RetVal->FirstResult = p_resultfield;
  870. RetVal->LastResult = p_resultfield;
  871. }
  872. ++RetVal->NumResults;
  873. return(RetVal);
  874. }
  875. struct parser_result* ILibParseString(char* buffer, int offset, int length, char* Delimiter, int DelimiterLength)
  876. {
  877. struct parser_result* RetVal = (struct parser_result*)MALLOC(sizeof(struct parser_result));
  878. int i=0;
  879. char* Token = NULL;
  880. int TokenLength = 0;
  881. struct parser_result_field *p_resultfield;
  882. RetVal->FirstResult = NULL;
  883. RetVal->NumResults = 0;
  884. Token = buffer + offset;
  885. for(i=offset;i<length;++i)
  886. {
  887. if(ILibIsDelimiter(buffer,i,length,Delimiter,DelimiterLength))
  888. {
  889. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  890. p_resultfield->data = Token;
  891. p_resultfield->datalength = TokenLength;
  892. p_resultfield->NextResult = NULL;
  893. if(RetVal->FirstResult != NULL)
  894. {
  895. RetVal->LastResult->NextResult = p_resultfield;
  896. RetVal->LastResult = p_resultfield;
  897. }
  898. else
  899. {
  900. RetVal->FirstResult = p_resultfield;
  901. RetVal->LastResult = p_resultfield;
  902. }
  903. ++RetVal->NumResults;
  904. i = i + DelimiterLength -1;
  905. Token = Token + TokenLength + DelimiterLength;
  906. TokenLength = 0;
  907. }
  908. else
  909. {
  910. ++TokenLength;
  911. }
  912. }
  913. p_resultfield = (struct parser_result_field*)MALLOC(sizeof(struct parser_result_field));
  914. p_resultfield->data = Token;
  915. p_resultfield->datalength = TokenLength;
  916. p_resultfield->NextResult = NULL;
  917. if(RetVal->FirstResult != NULL)
  918. {
  919. RetVal->LastResult->NextResult = p_resultfield;
  920. RetVal->LastResult = p_resultfield;
  921. }
  922. else
  923. {
  924. RetVal->FirstResult = p_resultfield;
  925. RetVal->LastResult = p_resultfield;
  926. }
  927. ++RetVal->NumResults;
  928. return(RetVal);
  929. }
  930. void ILibDestructParserResults(struct parser_result *result)
  931. {
  932. struct parser_result_field *node = result->FirstResult;
  933. struct parser_result_field *temp;
  934. while(node!=NULL)
  935. {
  936. temp = node->NextResult;
  937. FREE(node);
  938. node = temp;
  939. }
  940. FREE(result);
  941. }
  942. void ILibDestructPacket(struct packetheader *packet)
  943. {
  944. struct packetheader_field_node *node = packet->FirstField;
  945. struct packetheader_field_node *nextnode;
  946. while(node!=NULL)
  947. {
  948. nextnode = node->NextField;
  949. if(node->UserAllocStrings!=0)
  950. {
  951. FREE(node->Field);
  952. FREE(node->FieldData);
  953. }
  954. FREE(node);
  955. node = nextnode;
  956. }
  957. if(packet->UserAllocStrings!=0)
  958. {
  959. if(packet->StatusData!=NULL) {FREE(packet->StatusData);}
  960. if(packet->Directive!=NULL) {FREE(packet->Directive);}
  961. if(packet->DirectiveObj!=NULL) {FREE(packet->DirectiveObj);}
  962. if(packet->Body!=NULL) FREE(packet->Body);
  963. }
  964. FREE(packet);
  965. }
  966. struct packetheader* ILibParsePacketHeader(char* buffer, int offset, int length)
  967. {
  968. struct packetheader *RetVal = (struct packetheader*)MALLOC(sizeof(struct packetheader));
  969. struct parser_result *_packet;
  970. struct parser_result *p;
  971. struct parser_result *StartLine;
  972. struct parser_result_field *HeaderLine;
  973. struct parser_result_field *f;
  974. char* tempbuffer;
  975. struct packetheader_field_node *node;
  976. int i=0;
  977. int FLNWS = -1;
  978. int FTNWS = -1;
  979. RetVal->UserAllocStrings = 0;
  980. RetVal->Directive = NULL;
  981. RetVal->DirectiveLength = 0;
  982. RetVal->Body = NULL;
  983. RetVal->BodyLength = 0;
  984. RetVal->FirstField = NULL;
  985. RetVal->LastField = NULL;
  986. RetVal->Source = NULL;
  987. p = (struct parser_result*)ILibParseString(buffer,offset,length,"\r\n",2);
  988. _packet = p;
  989. f = p->FirstResult;
  990. StartLine = (struct parser_result*)ILibParseString(f->data,0,f->datalength," ",1);
  991. HeaderLine = f->NextResult;
  992. if(memcmp(StartLine->FirstResult->data,
  993. "HTTP/",
  994. 5)==0)
  995. {
  996. /* Response Packet */
  997. p = (struct parser_result*)ILibParseString(StartLine->FirstResult->data,
  998. 0,
  999. StartLine->FirstResult->datalength,
  1000. "/",1);
  1001. RetVal->Version = p->LastResult->data;
  1002. RetVal->VersionLength = p->LastResult->datalength;
  1003. ILibDestructParserResults(p);
  1004. tempbuffer = (char*)MALLOC(1+sizeof(char)*(StartLine->FirstResult->NextResult->datalength));
  1005. memcpy(tempbuffer,StartLine->FirstResult->NextResult->data,
  1006. StartLine->FirstResult->NextResult->datalength);
  1007. tempbuffer[StartLine->FirstResult->NextResult->datalength] = '\0';
  1008. RetVal->StatusCode = (int)atoi(tempbuffer);
  1009. FREE(tempbuffer);
  1010. RetVal->StatusData = StartLine->FirstResult->NextResult->NextResult->data;
  1011. RetVal->StatusDataLength = StartLine->FirstResult->NextResult->NextResult->datalength;
  1012. }
  1013. else
  1014. {
  1015. /* Request Packet */
  1016. RetVal->Directive = StartLine->FirstResult->data;
  1017. RetVal->DirectiveLength = StartLine->FirstResult->datalength;
  1018. RetVal->DirectiveObj = StartLine->FirstResult->NextResult->data;
  1019. RetVal->DirectiveObjLength = StartLine->FirstResult->NextResult->datalength;
  1020. RetVal->StatusCode = -1;
  1021. p = (struct parser_result*)ILibParseString(StartLine->LastResult->data,
  1022. 0,
  1023. StartLine->LastResult->datalength,
  1024. "/",1);
  1025. RetVal->Version = p->LastResult->data;
  1026. RetVal->VersionLength = p->LastResult->datalength;
  1027. ILibDestructParserResults(p);
  1028. RetVal->Directive[RetVal->DirectiveLength] = '\0';
  1029. RetVal->DirectiveObj[RetVal->DirectiveObjLength] = '\0';
  1030. }
  1031. while(HeaderLine!=NULL)
  1032. {
  1033. if(HeaderLine->datalength==0)
  1034. {
  1035. break;
  1036. }
  1037. node = (struct packetheader_field_node*)MALLOC(sizeof(struct packetheader_field_node));
  1038. memset(node,0,sizeof(struct packetheader_field_node));
  1039. for(i=0;i<HeaderLine->datalength;++i)
  1040. {
  1041. if(*((HeaderLine->data)+i)==':')
  1042. {
  1043. node->Field = HeaderLine->data;
  1044. node->FieldLength = i;
  1045. node->FieldData = HeaderLine->data + i + 1;
  1046. node->FieldDataLength = (HeaderLine->datalength)-i-1;
  1047. break;
  1048. }
  1049. }
  1050. if(node->Field==NULL)
  1051. {
  1052. FREE(RetVal);
  1053. RetVal = NULL;
  1054. break;
  1055. }
  1056. FLNWS = 0;
  1057. FTNWS = node->FieldDataLength-1;
  1058. for(i=0;i<node->FieldDataLength;++i)
  1059. {
  1060. if(*((node->FieldData)+i)!=' ')
  1061. {
  1062. FLNWS = i;
  1063. break;
  1064. }
  1065. }
  1066. for(i=(node->FieldDataLength)-1;i>=0;--i)
  1067. {
  1068. if(*((node->FieldData)+i)!=' ')
  1069. {
  1070. FTNWS = i;
  1071. break;
  1072. }
  1073. }
  1074. node->FieldData = (node->FieldData) + FLNWS;
  1075. node->FieldDataLength = (FTNWS - FLNWS)+1;
  1076. node->Field[node->FieldLength] = '\0';
  1077. node->FieldData[node->FieldDataLength] = '\0';
  1078. node->UserAllocStrings = 0;
  1079. node->NextField = NULL;
  1080. if(RetVal->FirstField==NULL)
  1081. {
  1082. RetVal->FirstField = node;
  1083. RetVal->LastField = node;
  1084. }
  1085. else
  1086. {
  1087. RetVal->LastField->NextField = node;
  1088. }
  1089. RetVal->LastField = node;
  1090. HeaderLine = HeaderLine->NextResult;
  1091. }
  1092. ILibDestructParserResults(_packet);
  1093. ILibDestructParserResults(StartLine);
  1094. return(RetVal);
  1095. }
  1096. int ILibGetRawPacket(struct packetheader* packet,char **RetVal)
  1097. {
  1098. int i;
  1099. int BufferSize = 0;
  1100. char* Buffer;
  1101. struct packetheader_field_node *node;
  1102. if(packet->StatusCode!=-1)
  1103. {
  1104. BufferSize = 12 + packet->VersionLength + packet->StatusDataLength;
  1105. /* HTTP/1.1 200 OK\r\n */
  1106. }
  1107. else
  1108. {
  1109. BufferSize = packet->DirectiveLength + packet->DirectiveObjLength + 12;
  1110. /* GET / HTTP/1.1\r\n */
  1111. }
  1112. node = packet->FirstField;
  1113. while(node!=NULL)
  1114. {
  1115. BufferSize += node->FieldLength + node->FieldDataLength + 4;
  1116. node = node->NextField;
  1117. }
  1118. BufferSize += (3+packet->BodyLength);
  1119. *RetVal = (char*)MALLOC(BufferSize);
  1120. Buffer = *RetVal;
  1121. if(packet->StatusCode!=-1)
  1122. {
  1123. memcpy(Buffer,"HTTP/",5);
  1124. memcpy(Buffer+5,packet->Version,packet->VersionLength);
  1125. i = 5+packet->VersionLength;
  1126. i+=sprintf(Buffer+i," %d ",packet->StatusCode);
  1127. memcpy(Buffer+i,packet->StatusData,packet->StatusDataLength);
  1128. i+=packet->StatusDataLength;
  1129. memcpy(Buffer+i,"\r\n",2);
  1130. i+=2;
  1131. /* HTTP/1.1 200 OK\r\n */
  1132. }
  1133. else
  1134. {
  1135. memcpy(Buffer,packet->Directive,packet->DirectiveLength);
  1136. i = packet->DirectiveLength;
  1137. memcpy(Buffer+i," ",1);
  1138. i+=1;
  1139. memcpy(Buffer+i,packet->DirectiveObj,packet->DirectiveObjLength);
  1140. i+=packet->DirectiveObjLength;
  1141. memcpy(Buffer+i," HTTP/",6);
  1142. i+=6;
  1143. memcpy(Buffer+i,packet->Version,packet->VersionLength);
  1144. i+=packet->VersionLength;
  1145. memcpy(Buffer+i,"\r\n",2);
  1146. i+=2;
  1147. /* GET / HTTP/1.1\r\n */
  1148. }
  1149. node = packet->FirstField;
  1150. while(node!=NULL)
  1151. {
  1152. memcpy(Buffer+i,node->Field,node->FieldLength);
  1153. i+=node->FieldLength;
  1154. memcpy(Buffer+i,": ",2);
  1155. i+=2;
  1156. memcpy(Buffer+i,node->FieldData,node->FieldDataLength);
  1157. i+=node->FieldDataLength;
  1158. memcpy(Buffer+i,"\r\n",2);
  1159. i+=2;
  1160. BufferSize += node->FieldLength + node->FieldDataLength + 4;
  1161. node = node->NextField;
  1162. }
  1163. memcpy(Buffer+i,"\r\n",2);
  1164. i+=2;
  1165. memcpy(Buffer+i,packet->Body,packet->BodyLength);
  1166. i+=packet->BodyLength;
  1167. Buffer[i] = '\0';
  1168. return(i);
  1169. }
  1170. unsigned short ILibGetDGramSocket(int local, int *TheSocket)
  1171. {
  1172. unsigned short PortNum = -1;
  1173. struct sockaddr_in addr;
  1174. memset((char *)&(addr), 0, sizeof(addr));
  1175. addr.sin_family = AF_INET;
  1176. addr.sin_addr.s_addr = local;
  1177. *TheSocket = socket(AF_INET, SOCK_DGRAM, 0);
  1178. do
  1179. {
  1180. PortNum = (unsigned short)(50000 + ((unsigned short)rand() % 15000));
  1181. addr.sin_port = htons(PortNum);
  1182. }
  1183. while(bind(*TheSocket, (struct sockaddr *) &(addr), sizeof(addr)) < 0);
  1184. return(PortNum);
  1185. }
  1186. unsigned short ILibGetStreamSocket(int local, int *TheSocket)
  1187. {
  1188. unsigned short PortNum = -1;
  1189. struct sockaddr_in addr;
  1190. memset((char *)&(addr), 0, sizeof(addr));
  1191. addr.sin_family = AF_INET;
  1192. addr.sin_addr.s_addr = local;
  1193. *TheSocket = socket(AF_INET, SOCK_STREAM, 0);
  1194. do
  1195. {
  1196. PortNum = (unsigned short)(50000 + ((unsigned short)rand() % 15000));
  1197. addr.sin_port = htons(PortNum);
  1198. }
  1199. while(bind(*TheSocket, (struct sockaddr *) &(addr), sizeof(addr)) < 0);
  1200. return(PortNum);
  1201. }
  1202. struct packetheader *ILibCreateEmptyPacket()
  1203. {
  1204. struct packetheader *RetVal = (struct packetheader*)MALLOC(sizeof(struct packetheader));
  1205. RetVal->UserAllocStrings = -1;
  1206. RetVal->Directive = NULL;
  1207. RetVal->DirectiveLength = 0;
  1208. RetVal->DirectiveObj = NULL;
  1209. RetVal->DirectiveObjLength = 0;
  1210. RetVal->StatusCode = -1;
  1211. RetVal->StatusData = NULL;
  1212. RetVal->StatusDataLength = 0;
  1213. RetVal->Version = "1.0";
  1214. RetVal->VersionLength = 3;
  1215. RetVal->Body = NULL;
  1216. RetVal->BodyLength = 0;
  1217. RetVal->FirstField = NULL;
  1218. RetVal->LastField = NULL;
  1219. RetVal->Source = NULL;
  1220. RetVal->ReceivingAddress = 0;
  1221. RetVal->UserAllocStrings = 0;
  1222. return(RetVal);
  1223. }
  1224. void ILibSetStatusCode(struct packetheader *packet, int StatusCode, char *StatusData, int StatusDataLength)
  1225. {
  1226. packet->StatusCode = StatusCode;
  1227. packet->StatusData = (char*)MALLOC(StatusDataLength+1);
  1228. memcpy(packet->StatusData,StatusData,StatusDataLength);
  1229. packet->StatusData[StatusDataLength] = '\0';
  1230. packet->StatusDataLength = StatusDataLength;
  1231. }
  1232. void ILibSetDirective(struct packetheader *packet, char* Directive, int DirectiveLength, char* DirectiveObj, int DirectiveObjLength)
  1233. {
  1234. packet->Directive = (char*)MALLOC(DirectiveLength+1);
  1235. memcpy(packet->Directive,Directive,DirectiveLength);
  1236. packet->Directive[DirectiveLength] = '\0';
  1237. packet->DirectiveLength = DirectiveLength;
  1238. packet->DirectiveObj = (char*)MALLOC(DirectiveObjLength+1);
  1239. memcpy(packet->DirectiveObj,DirectiveObj,DirectiveObjLength);
  1240. packet->DirectiveObj[DirectiveObjLength] = '\0';
  1241. packet->DirectiveObjLength = DirectiveObjLength;
  1242. packet->UserAllocStrings = -1;
  1243. }
  1244. void ILibAddHeaderLine(struct packetheader *packet, char* FieldName, int FieldNameLength, char* FieldData, int FieldDataLength)
  1245. {
  1246. struct packetheader_field_node *node;
  1247. node = (struct packetheader_field_node*)MALLOC(sizeof(struct packetheader_field_node));
  1248. node->UserAllocStrings = -1;
  1249. node->Field = (char*)MALLOC(FieldNameLength+1);
  1250. memcpy(node->Field,FieldName,FieldNameLength);
  1251. node->Field[FieldNameLength] = '\0';
  1252. node->FieldLength = FieldNameLength;
  1253. node->FieldData = (char*)MALLOC(FieldDataLength+1);
  1254. memcpy(node->FieldData,FieldData,FieldDataLength);
  1255. node->FieldData[FieldDataLength] = '\0';
  1256. node->FieldDataLength = FieldDataLength;
  1257. node->NextField = NULL;
  1258. if(packet->LastField!=NULL)
  1259. {
  1260. packet->LastField->NextField = node;
  1261. packet->LastField = node;
  1262. }
  1263. else
  1264. {
  1265. packet->LastField = node;
  1266. packet->FirstField = node;
  1267. }
  1268. }
  1269. char* ILibGetHeaderLine(struct packetheader *packet, char* FieldName, int FieldNameLength)
  1270. {
  1271. char* RetVal = NULL;
  1272. struct packetheader_field_node *node = packet->FirstField;
  1273. int i;
  1274. while(node!=NULL)
  1275. {
  1276. if(strncasecmp(FieldName,node->Field,FieldNameLength)==0)
  1277. {
  1278. RetVal = (char*)MALLOC(node->FieldDataLength+1);
  1279. for(i=0;i<node->FieldDataLength;++i)
  1280. {
  1281. if(node->FieldData[i]!=' ') {break;}
  1282. }
  1283. if(i==node->FieldDataLength-1) {i = 0;}
  1284. memcpy(RetVal,node->FieldData+i,node->FieldDataLength-i);
  1285. RetVal[node->FieldDataLength-i] = '\0';
  1286. break;
  1287. }
  1288. node = node->NextField;
  1289. }
  1290. return(RetVal);
  1291. }
  1292. static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  1293. static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
  1294. /* encode 3 8-bit binary bytes as 4 '6-bit' characters */
  1295. void ILibencodeblock( unsigned char in[3], unsigned char out[4], int len )
  1296. {
  1297. out[0] = cb64[ in[0] >> 2 ];
  1298. out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
  1299. out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
  1300. out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
  1301. }
  1302. /* Base64 encode a stream adding padding and line breaks as per spec. */
  1303. int ILibBase64Encode(unsigned char* input, const int inputlen, unsigned char** output)
  1304. {
  1305. unsigned char* out;
  1306. unsigned char* in;
  1307. *output = (unsigned char*)malloc(((inputlen * 4) / 3) + 5);
  1308. out = *output;
  1309. in = input;
  1310. if (input == NULL || inputlen == 0)
  1311. {
  1312. *output = NULL;
  1313. return 0;
  1314. }
  1315. while ((in+3) <= (input+inputlen))
  1316. {
  1317. ILibencodeblock(in, out, 3);
  1318. in += 3;
  1319. out += 4;
  1320. }
  1321. if ((input+inputlen)-in == 1)
  1322. {
  1323. ILibencodeblock(in, out, 1);
  1324. out += 4;
  1325. }
  1326. else
  1327. if ((input+inputlen)-in == 2)
  1328. {
  1329. ILibencodeblock(in, out, 2);
  1330. out += 4;
  1331. }
  1332. *out = 0;
  1333. return (int)(out-*output);
  1334. }
  1335. /* Decode 4 '6-bit' characters into 3 8-bit binary bytes */
  1336. void ILibdecodeblock( unsigned char in[4], unsigned char out[3] )
  1337. {
  1338. out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
  1339. out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
  1340. out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
  1341. }
  1342. /* decode a base64 encoded stream discarding padding, line breaks and noise */
  1343. int ILibBase64Decode(unsigned char* input, const int inputlen, unsigned char** output)
  1344. {
  1345. unsigned char* inptr;
  1346. unsigned char* out;
  1347. unsigned char v;
  1348. unsigned char in[4];
  1349. int i, len;
  1350. if (input == NULL || inputlen == 0)
  1351. {
  1352. *output = NULL;
  1353. return 0;
  1354. }
  1355. *output = (unsigned char*)malloc(((inputlen * 3) / 4) + 4);
  1356. out = *output;
  1357. inptr = input;
  1358. while( inptr <= (input+inputlen) )
  1359. {
  1360. for( len = 0, i = 0; i < 4 && inptr <= (input+inputlen); i++ )
  1361. {
  1362. v = 0;
  1363. while( inptr <= (input+inputlen) && v == 0 ) {
  1364. v = (unsigned char) *inptr;
  1365. inptr++;
  1366. v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
  1367. if( v ) {
  1368. v = (unsigned char) ((v == '$') ? 0 : v - 61);
  1369. }
  1370. }
  1371. if( inptr <= (input+inputlen) ) {
  1372. len++;
  1373. if( v ) {
  1374. in[ i ] = (unsigned char) (v - 1);
  1375. }
  1376. }
  1377. else {
  1378. in[i] = 0;
  1379. }
  1380. }
  1381. if( len )
  1382. {
  1383. ILibdecodeblock( in, out );
  1384. out += len-1;
  1385. }
  1386. }
  1387. *out = 0;
  1388. return (int)(out-*output);
  1389. }
  1390. int ILibInPlaceXmlUnEscape(char* data)
  1391. {
  1392. char* end = data+strlen(data);
  1393. char* i = data; /* src */
  1394. char* j = data; /* dest */
  1395. while (j < end)
  1396. {
  1397. if (j[0] == '&' && j[1] == 'q' && j[2] == 'u' && j[3] == 'o' && j[4] == 't' && j[5] == ';') // &quot;
  1398. {
  1399. i[0] = '"';
  1400. j += 5;
  1401. }
  1402. else if (j[0] == '&' && j[1] == 'a' && j[2] == 'p' && j[3] == 'o' && j[4] == 's' && j[5] == ';') // &apos;
  1403. {
  1404. i[0] = '\'';
  1405. j += 5;
  1406. }
  1407. else if (j[0] == '&' && j[1] == 'a' && j[2] == 'm' && j[3] == 'p' && j[4] == ';') // &amp;
  1408. {
  1409. i[0] = '&';
  1410. j += 4;
  1411. }
  1412. else if (j[0] == '&' && j[1] == 'l' && j[2] == 't' && j[3] == ';') // &lt;
  1413. {
  1414. i[0] = '<';
  1415. j += 3;
  1416. }
  1417. else if (j[0] == '&' && j[1] == 'g' && j[2] == 't' && j[3] == ';') // &gt;
  1418. {
  1419. i[0] = '>';
  1420. j += 3;
  1421. }
  1422. else
  1423. {
  1424. i[0] = j[0];
  1425. }
  1426. i++;
  1427. j++;
  1428. }
  1429. i[0] = '\0';
  1430. return (int)(i - data);
  1431. }
  1432. int ILibXmlEscapeLength(const char* data)
  1433. {
  1434. int i = 0, j = 0;
  1435. while (data[i] != 0)
  1436. {
  1437. switch (data[i])
  1438. {
  1439. case '"':
  1440. j += 6;
  1441. break;
  1442. case '\'':
  1443. j += 6;
  1444. break;
  1445. case '<':
  1446. j += 4;
  1447. break;
  1448. case '>':
  1449. j += 4;
  1450. break;
  1451. case '&':
  1452. j += 5;
  1453. break;
  1454. default:
  1455. j++;
  1456. }
  1457. i++;
  1458. }
  1459. return j;
  1460. }
  1461. int ILibXmlEscape(char* outdata, const char* indata)
  1462. {
  1463. int i=0;
  1464. int inlen;
  1465. char* out;
  1466. out = outdata;
  1467. inlen = (int)strlen(indata);
  1468. for (i=0; i < inlen; i++)
  1469. {
  1470. if (indata[i] == '"')
  1471. {
  1472. memcpy(out, "&quot;", 6);
  1473. out = out + 6;
  1474. }
  1475. else
  1476. if (indata[i] == '\'')
  1477. {
  1478. memcpy(out, "&apos;", 6);
  1479. out = out + 6;
  1480. }
  1481. else
  1482. if (indata[i] == '<')
  1483. {
  1484. memcpy(out, "&lt;", 4);
  1485. out = out + 4;
  1486. }
  1487. else
  1488. if (indata[i] == '>')
  1489. {
  1490. memcpy(out, "&gt;", 4);
  1491. out = out + 4;
  1492. }
  1493. else
  1494. if (indata[i] == '&')
  1495. {
  1496. memcpy(out, "&amp;", 5);
  1497. out = out + 5;
  1498. }
  1499. else
  1500. {
  1501. out[0] = indata[i];
  1502. out++;
  1503. }
  1504. }
  1505. out[0] = 0;
  1506. return (int)(out - outdata);
  1507. }
  1508. void ILibLifeTime_Add(void *LifetimeMonitorObject,void *data, int seconds, void* Callback, void* Destroy)
  1509. {
  1510. int NeedUnBlock = 0;
  1511. struct timeval tv;
  1512. struct LifeTimeMonitorData *temp,*temp2;
  1513. struct LifeTimeMonitorData *ltms = (struct LifeTimeMonitorData*)MALLOC(sizeof(struct LifeTimeMonitorData));
  1514. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifetimeMonitorObject;
  1515. gettimeofday(&tv,NULL);
  1516. ltms->data = data;
  1517. ltms->ExpirationTick = tv.tv_sec + seconds;
  1518. ltms->CallbackPtr = Callback;
  1519. ltms->DestroyPtr = Destroy;
  1520. ltms->Next = NULL;
  1521. sem_wait(&(UPnPLifeTime->SyncLock));
  1522. if(UPnPLifeTime->LM==NULL)
  1523. {
  1524. UPnPLifeTime->LM = ltms;
  1525. NeedUnBlock = 1;
  1526. }
  1527. else
  1528. {
  1529. temp = UPnPLifeTime->LM;
  1530. temp2 = temp;
  1531. while(temp!=NULL)
  1532. {
  1533. if(ltms->ExpirationTick<=temp->ExpirationTick){break;}
  1534. temp2 = temp;
  1535. temp = temp->Next;
  1536. }
  1537. ltms->Next = temp;
  1538. if(temp!=temp2)
  1539. {
  1540. temp2->Next = ltms;
  1541. }
  1542. else
  1543. {
  1544. UPnPLifeTime->LM = ltms;
  1545. NeedUnBlock = 1;
  1546. }
  1547. }
  1548. if(NeedUnBlock!=0) {ILibForceUnBlockChain(UPnPLifeTime->Chain);}
  1549. sem_post(&(UPnPLifeTime->SyncLock));
  1550. }
  1551. void ILibLifeTime_Check(void *LifeTimeMonitorObject,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
  1552. {
  1553. struct timeval tv;
  1554. long CurrentTick;
  1555. struct LifeTimeMonitorData *Temp,*EVT,*Last=NULL;
  1556. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeMonitorObject;
  1557. int nexttick;
  1558. EVT = NULL;
  1559. sem_wait(&(UPnPLifeTime->SyncLock));
  1560. if(UPnPLifeTime->LM!=NULL)
  1561. {
  1562. gettimeofday(&tv,NULL);
  1563. CurrentTick = tv.tv_sec;
  1564. Temp = UPnPLifeTime->LM;
  1565. while(Temp!=NULL && Temp->ExpirationTick<=CurrentTick)
  1566. {
  1567. EVT = UPnPLifeTime->LM;
  1568. Last = Temp;
  1569. Temp = Temp->Next;
  1570. }
  1571. if(EVT != NULL)
  1572. {
  1573. if(Temp!=NULL)
  1574. {
  1575. UPnPLifeTime->LM = Temp;
  1576. Last->Next = NULL;
  1577. }
  1578. else
  1579. {
  1580. UPnPLifeTime->LM = NULL;
  1581. }
  1582. }
  1583. sem_post(&(UPnPLifeTime->SyncLock));
  1584. while(EVT!=NULL)
  1585. {
  1586. EVT->CallbackPtr(EVT->data);
  1587. Temp = EVT;
  1588. EVT = EVT->Next;
  1589. FREE(Temp);
  1590. }
  1591. if(UPnPLifeTime->LM!=NULL)
  1592. {
  1593. nexttick = UPnPLifeTime->LM->ExpirationTick-CurrentTick;
  1594. if(nexttick<*blocktime) {*blocktime=nexttick;}
  1595. }
  1596. }
  1597. else
  1598. {
  1599. sem_post(&(UPnPLifeTime->SyncLock));
  1600. }
  1601. }
  1602. void ILibLifeTime_Remove(void *LifeTimeToken, void *data)
  1603. {
  1604. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeToken;
  1605. struct LifeTimeMonitorData *first,*second;
  1606. sem_wait(&(UPnPLifeTime->SyncLock));
  1607. first = UPnPLifeTime->LM;
  1608. if(first==NULL)
  1609. {
  1610. sem_post(&(UPnPLifeTime->SyncLock));
  1611. return;
  1612. }
  1613. second = first->Next;
  1614. if(first->data==data)
  1615. {
  1616. UPnPLifeTime->LM = first->Next;
  1617. if(first->DestroyPtr!=NULL) {first->DestroyPtr(first->data);}
  1618. FREE(first);
  1619. }
  1620. else
  1621. {
  1622. while(second!=NULL)
  1623. {
  1624. if(second->data==data)
  1625. {
  1626. first->Next = second->Next;
  1627. if(second->DestroyPtr!=NULL) {second->DestroyPtr(second->data);}
  1628. FREE(second);
  1629. second = first->Next;
  1630. }
  1631. else
  1632. {
  1633. first = first->Next;
  1634. second = second->Next;
  1635. }
  1636. }
  1637. }
  1638. sem_post(&(UPnPLifeTime->SyncLock));
  1639. }
  1640. void ILibLifeTime_Flush(void *LifeTimeToken)
  1641. {
  1642. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeToken;
  1643. struct LifeTimeMonitorData *temp,*temp2;
  1644. sem_wait(&(UPnPLifeTime->SyncLock));
  1645. temp = UPnPLifeTime->LM;
  1646. while(temp!=NULL)
  1647. {
  1648. temp2 = temp->Next;
  1649. if(temp->DestroyPtr!=NULL) {temp->DestroyPtr(temp->data);}
  1650. FREE(temp);
  1651. temp = temp2;
  1652. }
  1653. UPnPLifeTime->LM = NULL;
  1654. sem_post(&(UPnPLifeTime->SyncLock));
  1655. }
  1656. void ILibLifeTime_Destroy(void *LifeTimeToken)
  1657. {
  1658. struct ILibLifeTime *UPnPLifeTime = (struct ILibLifeTime*)LifeTimeToken;
  1659. ILibLifeTime_Flush(LifeTimeToken);
  1660. sem_destroy(&(UPnPLifeTime->SyncLock));
  1661. }
  1662. void *ILibCreateLifeTime(void *Chain)
  1663. {
  1664. struct ILibLifeTime *RetVal = (struct ILibLifeTime*)MALLOC(sizeof(struct ILibLifeTime));
  1665. RetVal->LM = NULL;
  1666. RetVal->PreSelect = &ILibLifeTime_Check;
  1667. RetVal->PostSelect = NULL;
  1668. RetVal->Destroy = &ILibLifeTime_Destroy;
  1669. RetVal->Chain = Chain;
  1670. sem_init(&(RetVal->SyncLock),0,1);
  1671. ILibAddToChain(Chain,RetVal);
  1672. return((void*)RetVal);
  1673. }
  1674. int ILibFindEntryInTable(char *Entry, char **Table)
  1675. {
  1676. int i = 0;
  1677. while(Table[i]!=NULL)
  1678. {
  1679. if(strcmp(Entry,Table[i])==0)
  1680. {
  1681. return(i);
  1682. }
  1683. ++i;
  1684. }
  1685. return(-1);
  1686. }