igd_desc_parse.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* $Id: igd_desc_parse.c,v 1.8 2008/04/23 11:51:06 nanard Exp $ */
  2. /* Project : miniupnp
  3. * http://miniupnp.free.fr/
  4. * Author : Thomas Bernard
  5. * Copyright (c) 2005-2008 Thomas Bernard
  6. * This software is subject to the conditions detailed in the
  7. * LICENCE file provided in this distribution.
  8. * */
  9. #include "igd_desc_parse.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. /* TODO : rewrite this code so it correctly handle descriptions with
  13. * both WANIPConnection and/or WANPPPConnection */
  14. /* Start element handler :
  15. * update nesting level counter and copy element name */
  16. void IGDstartelt(void * d, const char * name, int l)
  17. {
  18. struct IGDdatas * datas = (struct IGDdatas *)d;
  19. memcpy( datas->cureltname, name, l);
  20. datas->cureltname[l] = '\0';
  21. datas->level++;
  22. if( (l==7) && !memcmp(name, "service", l) ) {
  23. datas->controlurl_tmp[0] = '\0';
  24. datas->eventsuburl_tmp[0] = '\0';
  25. datas->scpdurl_tmp[0] = '\0';
  26. datas->servicetype_tmp[0] = '\0';
  27. }
  28. }
  29. /* End element handler :
  30. * update nesting level counter and update parser state if
  31. * service element is parsed */
  32. void IGDendelt(void * d, const char * name, int l)
  33. {
  34. struct IGDdatas * datas = (struct IGDdatas *)d;
  35. datas->level--;
  36. /*printf("endelt %2d %.*s\n", datas->level, l, name);*/
  37. if( (l==7) && !memcmp(name, "service", l) )
  38. {
  39. /*
  40. if( datas->state < 1
  41. && !strcmp(datas->servicetype,
  42. // "urn:schemas-upnp-org:service:WANIPConnection:1") )
  43. "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"))
  44. datas->state ++;
  45. */
  46. if(0==strcmp(datas->servicetype_tmp,
  47. "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1")) {
  48. memcpy(datas->controlurl_CIF, datas->controlurl_tmp, MINIUPNPC_URL_MAXSIZE);
  49. memcpy(datas->eventsuburl_CIF, datas->eventsuburl_tmp, MINIUPNPC_URL_MAXSIZE);
  50. memcpy(datas->scpdurl_CIF, datas->scpdurl_tmp, MINIUPNPC_URL_MAXSIZE);
  51. memcpy(datas->servicetype_CIF, datas->servicetype_tmp, MINIUPNPC_URL_MAXSIZE);
  52. } else if(0==strcmp(datas->servicetype_tmp,
  53. "urn:schemas-upnp-org:service:WANIPConnection:1")
  54. || 0==strcmp(datas->servicetype_tmp,
  55. "urn:schemas-upnp-org:service:WANPPPConnection:1") ) {
  56. memcpy(datas->controlurl, datas->controlurl_tmp, MINIUPNPC_URL_MAXSIZE);
  57. memcpy(datas->eventsuburl, datas->eventsuburl_tmp, MINIUPNPC_URL_MAXSIZE);
  58. memcpy(datas->scpdurl, datas->scpdurl_tmp, MINIUPNPC_URL_MAXSIZE);
  59. memcpy(datas->servicetype, datas->servicetype_tmp, MINIUPNPC_URL_MAXSIZE);
  60. }
  61. }
  62. }
  63. /* Data handler :
  64. * copy data depending on the current element name and state */
  65. void IGDdata(void * d, const char * data, int l)
  66. {
  67. struct IGDdatas * datas = (struct IGDdatas *)d;
  68. char * dstmember = 0;
  69. /*printf("%2d %s : %.*s\n",
  70. datas->level, datas->cureltname, l, data); */
  71. if( !strcmp(datas->cureltname, "URLBase") )
  72. dstmember = datas->urlbase;
  73. else if( !strcmp(datas->cureltname, "serviceType") )
  74. dstmember = datas->servicetype_tmp;
  75. else if( !strcmp(datas->cureltname, "controlURL") )
  76. dstmember = datas->controlurl_tmp;
  77. else if( !strcmp(datas->cureltname, "eventSubURL") )
  78. dstmember = datas->eventsuburl_tmp;
  79. else if( !strcmp(datas->cureltname, "SCPDURL") )
  80. dstmember = datas->scpdurl_tmp;
  81. /* else if( !strcmp(datas->cureltname, "deviceType") )
  82. dstmember = datas->devicetype_tmp;*/
  83. if(dstmember)
  84. {
  85. if(l>=MINIUPNPC_URL_MAXSIZE)
  86. l = MINIUPNPC_URL_MAXSIZE-1;
  87. memcpy(dstmember, data, l);
  88. dstmember[l] = '\0';
  89. }
  90. }
  91. void printIGD(struct IGDdatas * d)
  92. {
  93. printf("urlbase = %s\n", d->urlbase);
  94. printf("WAN Device (Common interface config) :\n");
  95. /*printf(" deviceType = %s\n", d->devicetype_CIF);*/
  96. printf(" serviceType = %s\n", d->servicetype_CIF);
  97. printf(" controlURL = %s\n", d->controlurl_CIF);
  98. printf(" eventSubURL = %s\n", d->eventsuburl_CIF);
  99. printf(" SCPDURL = %s\n", d->scpdurl_CIF);
  100. printf("WAN Connection Device (IP or PPP Connection):\n");
  101. /*printf(" deviceType = %s\n", d->devicetype);*/
  102. printf(" servicetype = %s\n", d->servicetype);
  103. printf(" controlURL = %s\n", d->controlurl);
  104. printf(" eventSubURL = %s\n", d->eventsuburl);
  105. printf(" SCPDURL = %s\n", d->scpdurl);
  106. }