igd_desc_parse.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* $Id: igd_desc_parse.c,v 1.17 2015/09/15 13:30:04 nanard Exp $ */
  2. /* Project : miniupnp
  3. * http://miniupnp.free.fr/
  4. * Author : Thomas Bernard
  5. * Copyright (c) 2005-2015 Thomas Bernard
  6. * This software is subject to the conditions detailed in the
  7. * LICENCE file provided in this distribution. */
  8. #include "igd_desc_parse.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. /* Start element handler :
  12. * update nesting level counter and copy element name */
  13. void IGDstartelt(void * d, const char * name, int l)
  14. {
  15. struct IGDdatas * datas = (struct IGDdatas *)d;
  16. if(l >= MINIUPNPC_URL_MAXSIZE)
  17. l = MINIUPNPC_URL_MAXSIZE-1;
  18. memcpy(datas->cureltname, name, l);
  19. datas->cureltname[l] = '\0';
  20. datas->level++;
  21. if( (l==7) && !memcmp(name, "service", l) ) {
  22. datas->tmp.controlurl[0] = '\0';
  23. datas->tmp.eventsuburl[0] = '\0';
  24. datas->tmp.scpdurl[0] = '\0';
  25. datas->tmp.servicetype[0] = '\0';
  26. }
  27. }
  28. #define COMPARE(str, cstr) (0==memcmp(str, cstr, sizeof(cstr) - 1))
  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. if(COMPARE(datas->tmp.servicetype,
  40. "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:")) {
  41. memcpy(&datas->CIF, &datas->tmp, sizeof(struct IGDdatas_service));
  42. } else if(COMPARE(datas->tmp.servicetype,
  43. "urn:schemas-upnp-org:service:WANIPv6FirewallControl:")) {
  44. memcpy(&datas->IPv6FC, &datas->tmp, sizeof(struct IGDdatas_service));
  45. } else if(COMPARE(datas->tmp.servicetype,
  46. "urn:schemas-upnp-org:service:WANIPConnection:")
  47. || COMPARE(datas->tmp.servicetype,
  48. "urn:schemas-upnp-org:service:WANPPPConnection:") ) {
  49. if(datas->first.servicetype[0] == '\0') {
  50. memcpy(&datas->first, &datas->tmp, sizeof(struct IGDdatas_service));
  51. } else {
  52. memcpy(&datas->second, &datas->tmp, sizeof(struct IGDdatas_service));
  53. }
  54. }
  55. }
  56. }
  57. /* Data handler :
  58. * copy data depending on the current element name and state */
  59. void IGDdata(void * d, const char * data, int l)
  60. {
  61. struct IGDdatas * datas = (struct IGDdatas *)d;
  62. char * dstmember = 0;
  63. /*printf("%2d %s : %.*s\n",
  64. datas->level, datas->cureltname, l, data); */
  65. if( !strcmp(datas->cureltname, "URLBase") )
  66. dstmember = datas->urlbase;
  67. else if( !strcmp(datas->cureltname, "presentationURL") )
  68. dstmember = datas->presentationurl;
  69. else if( !strcmp(datas->cureltname, "serviceType") )
  70. dstmember = datas->tmp.servicetype;
  71. else if( !strcmp(datas->cureltname, "controlURL") )
  72. dstmember = datas->tmp.controlurl;
  73. else if( !strcmp(datas->cureltname, "eventSubURL") )
  74. dstmember = datas->tmp.eventsuburl;
  75. else if( !strcmp(datas->cureltname, "SCPDURL") )
  76. dstmember = datas->tmp.scpdurl;
  77. /* else if( !strcmp(datas->cureltname, "deviceType") )
  78. dstmember = datas->devicetype_tmp;*/
  79. if(dstmember)
  80. {
  81. if(l>=MINIUPNPC_URL_MAXSIZE)
  82. l = MINIUPNPC_URL_MAXSIZE-1;
  83. memcpy(dstmember, data, l);
  84. dstmember[l] = '\0';
  85. }
  86. }
  87. #ifdef DEBUG
  88. void printIGD(struct IGDdatas * d)
  89. {
  90. printf("urlbase = '%s'\n", d->urlbase);
  91. printf("WAN Device (Common interface config) :\n");
  92. /*printf(" deviceType = '%s'\n", d->CIF.devicetype);*/
  93. printf(" serviceType = '%s'\n", d->CIF.servicetype);
  94. printf(" controlURL = '%s'\n", d->CIF.controlurl);
  95. printf(" eventSubURL = '%s'\n", d->CIF.eventsuburl);
  96. printf(" SCPDURL = '%s'\n", d->CIF.scpdurl);
  97. printf("primary WAN Connection Device (IP or PPP Connection):\n");
  98. /*printf(" deviceType = '%s'\n", d->first.devicetype);*/
  99. printf(" servicetype = '%s'\n", d->first.servicetype);
  100. printf(" controlURL = '%s'\n", d->first.controlurl);
  101. printf(" eventSubURL = '%s'\n", d->first.eventsuburl);
  102. printf(" SCPDURL = '%s'\n", d->first.scpdurl);
  103. printf("secondary WAN Connection Device (IP or PPP Connection):\n");
  104. /*printf(" deviceType = '%s'\n", d->second.devicetype);*/
  105. printf(" servicetype = '%s'\n", d->second.servicetype);
  106. printf(" controlURL = '%s'\n", d->second.controlurl);
  107. printf(" eventSubURL = '%s'\n", d->second.eventsuburl);
  108. printf(" SCPDURL = '%s'\n", d->second.scpdurl);
  109. printf("WAN IPv6 Firewall Control :\n");
  110. /*printf(" deviceType = '%s'\n", d->IPv6FC.devicetype);*/
  111. printf(" servicetype = '%s'\n", d->IPv6FC.servicetype);
  112. printf(" controlURL = '%s'\n", d->IPv6FC.controlurl);
  113. printf(" eventSubURL = '%s'\n", d->IPv6FC.eventsuburl);
  114. printf(" SCPDURL = '%s'\n", d->IPv6FC.scpdurl);
  115. }
  116. #endif /* DEBUG */