access_descriptor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * ESG parser
  3. *
  4. * Copyright (C) 2006 Stephane Este-Gracias (sestegra@free.fr)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <libesg/bootstrap/access_descriptor.h>
  23. struct esg_access_descriptor *esg_access_descriptor_decode(uint8_t *buffer, uint32_t size) {
  24. uint32_t pos;
  25. struct esg_access_descriptor *access_descriptor;
  26. struct esg_entry *entry;
  27. struct esg_entry *last_entry;
  28. uint32_t entry_length;
  29. uint16_t entry_index;
  30. uint8_t ip_index;
  31. if ((buffer == NULL) || (size <= 2)) {
  32. return NULL;
  33. }
  34. pos = 0;
  35. access_descriptor = (struct esg_access_descriptor *) malloc(sizeof(struct esg_access_descriptor));
  36. memset(access_descriptor, 0, sizeof(struct esg_access_descriptor));
  37. access_descriptor->entry_list = NULL;
  38. access_descriptor->n_o_entries = (buffer[pos] << 8) | buffer[pos+1];
  39. pos += 2;
  40. last_entry = NULL;
  41. for (entry_index = 0; entry_index < access_descriptor->n_o_entries; entry_index++) {
  42. entry = (struct esg_entry *) malloc(sizeof(struct esg_entry));
  43. memset(entry, 0, sizeof(struct esg_entry));
  44. entry->_next = NULL;
  45. if (last_entry == NULL) {
  46. access_descriptor->entry_list = entry;
  47. } else {
  48. last_entry->_next = entry;
  49. }
  50. last_entry = entry;
  51. entry->version = buffer[pos];
  52. pos += 1;
  53. pos += vluimsbf8(buffer + pos, size - pos, &entry_length);
  54. if (size < pos + entry_length) {
  55. esg_access_descriptor_free(access_descriptor);
  56. return NULL;
  57. }
  58. entry->multiple_stream_transport = (buffer[pos] & 0x80) ? 1 : 0;
  59. entry->ip_version_6 = (buffer[pos] & 0x40) ? 1 : 0;
  60. pos += 1;
  61. entry->provider_id = (buffer[pos] << 8) | buffer[pos+1];
  62. pos += 2;
  63. if (entry->ip_version_6) {
  64. for (ip_index = 0; ip_index < 16; ip_index++) {
  65. entry->source_ip.ipv6[ip_index] = buffer[pos+ip_index];
  66. entry->destination_ip.ipv6[ip_index] = buffer[pos+16+ip_index];
  67. }
  68. pos += 32;
  69. } else {
  70. for (ip_index = 0; ip_index < 4; ip_index++) {
  71. entry->source_ip.ipv4[ip_index] = buffer[pos+ip_index];
  72. entry->destination_ip.ipv4[ip_index] = buffer[pos+4+ip_index];
  73. }
  74. pos += 8;
  75. }
  76. entry->port = (buffer[pos] << 8) | buffer[pos+1];
  77. pos += 2;
  78. entry->tsi = (buffer[pos] << 8) | buffer[pos+1];
  79. pos += 2;
  80. }
  81. return access_descriptor;
  82. }
  83. void esg_access_descriptor_free(struct esg_access_descriptor *access_descriptor) {
  84. struct esg_entry *entry;
  85. struct esg_entry *next_entry;
  86. if (access_descriptor == NULL) {
  87. return;
  88. }
  89. for(entry = access_descriptor->entry_list; entry; entry = next_entry) {
  90. next_entry = entry->_next;
  91. free(entry);
  92. }
  93. free(access_descriptor);
  94. }