docsis_decode.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * DOCSIS configuration file encoder.
  3. * Copyright (c) 2001 Cornel Ciocirlan, ctrl@users.sourceforge.net.
  4. * Copyright (c) 2002 Evvolve Media SRL,office@evvolve.com
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * DOCSIS is a registered trademark of Cablelabs, http://www.cablelabs.com
  21. */
  22. #include "docsis.h"
  23. #include "ethermac.h"
  24. #include <math.h>
  25. extern symbol_type *global_symtable;
  26. struct symbol_entry *find_symbol_by_code_and_pid (unsigned char code, unsigned int pid)
  27. {
  28. int i;
  29. for ( i=0; i<NUM_IDENTIFIERS; i++) {
  30. if (global_symtable[i].docsis_code == code && global_symtable[i].parent_id == pid) {
  31. return &global_symtable[i];
  32. }
  33. }
  34. return NULL;
  35. }
  36. void decode_uint (unsigned char *tlvbuf, struct symbol_entry *sym)
  37. {
  38. static unsigned int helper;
  39. memset( &helper, 0,sizeof(unsigned int));
  40. memcpy( &helper, tlvbuf+2, sizeof(unsigned int));
  41. printf ( "%s %u;\n", sym->sym_ident, ntohl(helper));
  42. }
  43. void decode_ushort (unsigned char *tlvbuf, symbol_type *sym)
  44. {
  45. static unsigned short helper;
  46. memset( &helper, 0,sizeof(unsigned short));
  47. memcpy( &helper, tlvbuf+2, sizeof(unsigned short));
  48. printf ( "%s %hu;\n", sym->sym_ident, ntohs(helper));
  49. }
  50. void decode_uchar (unsigned char *tlvbuf, symbol_type *sym)
  51. {
  52. printf ( "%s %hhu;\n", sym->sym_ident, (unsigned char) *(tlvbuf+2));
  53. }
  54. void decode_ip (unsigned char *tlvbuf, symbol_type *sym)
  55. {
  56. printf ( "%s %s;\n",
  57. sym->sym_ident, inet_ntoa(*((struct in_addr *)(tlvbuf+2))) );
  58. }
  59. void decode_ether (unsigned char *tlvbuf, symbol_type *sym)
  60. {
  61. printf ( "%s %s;\n",
  62. sym->sym_ident, ether_ntoa(tlvbuf+2));
  63. }
  64. void decode_ethermask (unsigned char *tlvbuf, symbol_type *sym)
  65. {
  66. /* the return value of ether_ntoa is a pointer to a static string
  67. * in the ether_ntoa function, so we have to print the values in two
  68. * "passees" to avoid the 2nd call overwriting the 1st.
  69. */
  70. printf ( "%s %s/", sym->sym_ident, ether_ntoa(tlvbuf+2));
  71. printf( "%s;\n", ether_ntoa(tlvbuf+8));
  72. }
  73. void decode_md5 (unsigned char *tlvbuf, symbol_type *sym)
  74. {
  75. int j=0;
  76. printf ("%s ", sym->sym_ident);
  77. for (j=0;j<16;j++) printf ("%02x", tlvbuf[j+2]);
  78. printf(";\n");
  79. }
  80. void decode_snmp_wd (unsigned char *tlvbuf, symbol_type *sym)
  81. {
  82. printf ( "%s ", sym->sym_ident);
  83. decode_wd (tlvbuf+2,(unsigned int) tlvbuf[1]);
  84. printf(";\n");
  85. }
  86. void decode_snmp_object (unsigned char *tlvbuf, symbol_type *sym)
  87. {
  88. printf ( "%s ", sym->sym_ident);
  89. decode_vbind (tlvbuf+2, (unsigned int) tlvbuf[1]);
  90. printf(";\n");
  91. }
  92. void decode_string (unsigned char *tlvbuf, symbol_type *sym)
  93. {
  94. char *helper;
  95. helper = (char *) malloc ( ((unsigned int) tlvbuf[1])+1 );
  96. memset ( helper, 0, tlvbuf[1]+1);
  97. strncpy ( helper, (char *) tlvbuf+2, (unsigned int) tlvbuf[1] );
  98. printf ( "%s \"%s\";\n", sym->sym_ident, helper );
  99. }
  100. void decode_hexstr (unsigned char *tlvbuf, symbol_type *sym)
  101. {
  102. char *helper;
  103. int i;
  104. /* TODO */
  105. helper = (char *) malloc ( ((unsigned int) tlvbuf[1])+1 );
  106. memset ( helper, 0, tlvbuf[1]+1);
  107. memcpy ( helper, (char *) tlvbuf+2, (unsigned int) tlvbuf[1] );
  108. printf ( "%s 0x", sym->sym_ident);
  109. for(i=0; i<(int) tlvbuf[1]; i++) {
  110. printf("%02x", (unsigned char) helper[i]);
  111. }
  112. printf(";\n");
  113. }
  114. void decode_unknown (unsigned char *tlvbuf, symbol_type *sym)
  115. {
  116. int j=0,len=0;
  117. len = (int) tlvbuf[1];
  118. printf ("/* GenericUnknownTLV code %d len %d value 0x ",
  119. (unsigned int) tlvbuf[0],(unsigned int) tlvbuf[1]);
  120. for (j=0;j<len;j++) printf ("%02x", tlvbuf[j+2]);
  121. printf("; */\n");
  122. }
  123. void decode_special (unsigned char *tlvbuf, symbol_type *sym)
  124. {
  125. printf ( "%s\n", sym->sym_ident);
  126. }
  127. void decode_aggregate (unsigned char *tlvbuf, symbol_type *sym)
  128. {
  129. register unsigned char *cp;
  130. symbol_type *current_symbol;
  131. cp = tlvbuf+2*sizeof(unsigned char); /* skip type,len of parent TLV */
  132. printf( "%s {\n", sym->sym_ident);
  133. while ( (unsigned int) (cp - tlvbuf) < (unsigned int) tlvbuf[1] ) {
  134. current_symbol = find_symbol_by_code_and_pid (cp[0], sym->id);
  135. if (current_symbol == NULL) {
  136. decode_unknown(cp, NULL);
  137. } else {
  138. current_symbol->decode_func (cp, current_symbol);
  139. }
  140. cp = (unsigned char*) cp + (((unsigned char)*(cp+1))+2)*sizeof(unsigned char);
  141. }
  142. printf("}\n");
  143. }
  144. /* This function is needed because we don't have a symbol to call it.
  145. * We can't put a "Main" symbol in the symtable because docsis_code is
  146. * unsigned char (in struct symbol_entry) and we reserve the values for
  147. * DOCSIS use.
  148. * It's also a bif different from docsis_aggregate in that docsis_aggregate
  149. * takes an aggregate tlvbuf as argument that INCLUDES the "parent" code and
  150. * length. On the main aggregate we don't have a "parent" code / length, the
  151. * first code/length is the first configuration setting.
  152. */
  153. void decode_main_aggregate (unsigned char *tlvbuf, unsigned int buflen)
  154. {
  155. register unsigned char *cp;
  156. symbol_type *current_symbol;
  157. cp = tlvbuf;
  158. printf( "Main {\n");
  159. while ( (unsigned int) (cp - tlvbuf) < buflen ) {
  160. current_symbol = find_symbol_by_code_and_pid (cp[0],0);
  161. if (current_symbol == NULL) {
  162. decode_unknown(cp, NULL);
  163. } else {
  164. current_symbol->decode_func (cp, current_symbol);
  165. }
  166. cp = (unsigned char*) cp + (((unsigned char)*(cp+1))+2)*sizeof(unsigned char);
  167. }
  168. printf("}\n");
  169. }