docsis_encode.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_encode.h"
  23. #include "docsis_globals.h"
  24. #include "ethermac.h"
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #ifdef WIN32
  30. #include <io.h>
  31. #include <winsock.h>
  32. #include "inet_aton.h"
  33. #else
  34. #include <unistd.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. #endif
  38. #include <fcntl.h>
  39. #include <math.h>
  40. extern unsigned int line; /* defined in docsis_lex.l */
  41. int get_uint ( unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  42. {
  43. unsigned int int_value;
  44. union t_val *helper; /* We only use this to cast the void* we receive to what we think it should be */
  45. if ( buf == NULL ) {
  46. printf ("get_uint called w/NULL buffer!\n");
  47. exit (-1);
  48. }
  49. if ( tval == NULL ) {
  50. printf ("get_uint called w/NULL value struct !\n");
  51. exit (-1);
  52. }
  53. helper = (union t_val *) tval;
  54. if ( sym_ptr->low_limit || sym_ptr->high_limit ) {
  55. if ( helper->uintval < sym_ptr->low_limit || helper->uintval > sym_ptr->high_limit ) {
  56. printf ("%s: at line %d, %s value %d out of range %hd-%hd\n ", prog_name,line,sym_ptr->sym_ident,helper->uintval,sym_ptr->low_limit, sym_ptr->high_limit);
  57. exit(-15);
  58. }
  59. }
  60. int_value = htonl( helper->uintval );
  61. #ifdef DEBUG
  62. printf ("get_uint: found %s value %d\n",sym_ptr->sym_ident, helper->uintval);
  63. #endif /* DEBUG */
  64. memcpy ( buf,&int_value, sizeof(unsigned int));
  65. return ( sizeof(unsigned int));
  66. }
  67. int get_ushort ( unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  68. {
  69. unsigned short sint;
  70. union t_val *helper; /* We only use this to cast the void* we receive to what we think it should be */
  71. if ( buf == NULL ) {
  72. printf ("get_ushort called w/NULL buffer!\n");
  73. exit (-1);
  74. }
  75. if ( tval == NULL ) {
  76. printf ("get_ushort called w/NULL value struct !\n");
  77. exit (-1);
  78. }
  79. helper = (union t_val *) tval;
  80. if ( sym_ptr->low_limit || sym_ptr->high_limit ) {
  81. if ( helper->uintval < sym_ptr->low_limit || helper->uintval > sym_ptr->high_limit ) {
  82. printf ("%s: at line %d, %s value %d out of range %hd-%hd\n ", prog_name,line,sym_ptr->sym_ident,helper->uintval,sym_ptr->low_limit, sym_ptr->high_limit);
  83. exit(-15);
  84. }
  85. }
  86. sint = htons( (unsigned short) helper->uintval );
  87. #ifdef DEBUG
  88. printf ("get_ushort: found %s value %hd\n",sym_ptr->sym_ident, helper->uintval);
  89. #endif /* DEBUG */
  90. memcpy ( buf,&sint,sizeof(unsigned short));
  91. return ( sizeof(unsigned short));
  92. }
  93. int get_uchar ( unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  94. {
  95. unsigned int int_value;
  96. char *cp;
  97. union t_val *helper; /* We only use this to cast the void* we receive to what we think it should be */
  98. if ( buf == NULL ) {
  99. printf ("get_uchar called w/NULL buffer!\n");
  100. exit (-1);
  101. }
  102. if ( tval == NULL ) {
  103. printf ("get_uchar called w/NULL value struct !\n");
  104. exit (-1);
  105. }
  106. helper = (union t_val *) tval;
  107. int_value = htonl( helper->uintval );
  108. if ( sym_ptr->low_limit || sym_ptr->high_limit ) {
  109. if ( helper->uintval < sym_ptr->low_limit || helper->uintval > sym_ptr->high_limit ) {
  110. printf ("%s: at line %d, %s value %d out of range %hd-%hd\n ", prog_name,line,sym_ptr->sym_ident,helper->uintval,sym_ptr->low_limit, sym_ptr->high_limit);
  111. exit(-15);
  112. }
  113. }
  114. cp = (char *)&int_value;
  115. buf[0]=(unsigned char)cp[3];
  116. #ifdef DEBUG
  117. printf ("get_uchar: found %s value %hd\n",sym_ptr->sym_ident, helper->uintval);
  118. #endif
  119. return ( sizeof(unsigned char));
  120. }
  121. int get_ip( unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  122. {
  123. struct in_addr in;
  124. int retval; /* return value of inet_aton */
  125. union t_val *helper; /* We only use this to cast the void* we receive to what we think it should be */
  126. if ( buf == NULL ) {
  127. printf ("get_ip called w/NULL buffer!\n");
  128. exit (-1);
  129. }
  130. if ( tval == NULL ) {
  131. printf ("get_ip called w/NULL value struct !\n");
  132. exit (-1);
  133. }
  134. helper = (union t_val *) tval;
  135. if (!(retval = inet_aton ( helper->strval, &in)) ) {
  136. printf ( "Invalid IP address %s at line %d", helper->strval, line );
  137. exit (-1);
  138. }
  139. #ifdef DEBUG
  140. printf ("get_ip: found %s at line %d\n",inet_ntoa(in), line);
  141. #endif /* DEBUG */
  142. memcpy ( buf, &in, sizeof(struct in_addr));
  143. return ( sizeof(struct in_addr));
  144. }
  145. int get_ether ( unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  146. {
  147. int retval; /* return value of inet_aton */
  148. union t_val *helper; /* We only use this to cast the void* we receive to what we think it should be */
  149. if ( buf == NULL ) {
  150. printf ("get_ether called w/NULL buffer!\n");
  151. exit (-1);
  152. }
  153. if ( tval == NULL ) {
  154. printf ("get_ether called w/NULL value struct !\n");
  155. exit (-1);
  156. }
  157. helper = (union t_val *) tval;
  158. if (!(retval = ether_aton ( helper->strval, buf)) ) {
  159. printf ( "Invalid MAC address %s at line %d", helper->strval, line );
  160. exit (-1);
  161. }
  162. #ifdef DEBUG
  163. printf ("get_ether: found %s at line %d\n", ether_ntoa(buf), line);
  164. #endif /* DEBUG */
  165. return retval; /* hopefully this equals 6 :) */
  166. }
  167. int get_ethermask ( unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  168. {
  169. int reta, retb; /* return value of ether_aton */
  170. char *ether,*mask;
  171. union t_val *helper; /* We only use this to cast the void* we receive to what we think it should be */
  172. if ( buf == NULL ) {
  173. printf ("get_ethermask called w/NULL buffer!\n");
  174. exit (-1);
  175. }
  176. if ( tval == NULL ) {
  177. printf ("get_ethermask called w/NULL value struct !\n");
  178. exit (-1);
  179. }
  180. helper = (union t_val *) tval;
  181. ether = helper -> strval;
  182. mask = strchr ( ether, (int) '/');
  183. if (mask == NULL) {
  184. printf ("get_ethermask: at line %d, format should be <mac_address>/<mac_mask>\n", line);
  185. exit (-1);
  186. }
  187. mask[0]=0x0; mask++; /* cut the string in two */
  188. if (!(reta = ether_aton ( ether, buf)) ) {
  189. printf ( "Invalid MAC address %s at line %d\n", ether, line );
  190. exit (-1);
  191. }
  192. if (!(retb = ether_aton ( mask, buf+reta*(sizeof(char))) ) ) {
  193. printf ( "Invalid MAC address %s at line %d\n", mask, line );
  194. exit (-1);
  195. }
  196. #ifdef DEBUG
  197. printf ("get_ethermask: found %s/%s at line %d\n", ether_ntoa(buf), ether_ntoa(buf+reta*sizeof(char)), line);
  198. #endif /* DEBUG */
  199. return (reta+retb); /* hopefully this equals 12 :) */
  200. }
  201. int get_string(unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  202. {
  203. unsigned int string_size;
  204. /* We only use this to cast the void* we receive to what we think it should be */
  205. union t_val *helper;
  206. if ( buf == NULL ) {
  207. printf ("get_string called w/NULL buffer!\n");
  208. exit (-1);
  209. }
  210. if ( tval == NULL ) {
  211. printf ("get_string called w/NULL value struct !\n");
  212. exit (-1);
  213. }
  214. helper = (union t_val *) tval;
  215. string_size = strlen ( helper->strval );
  216. if (sym_ptr->low_limit || sym_ptr->high_limit) {
  217. if ( string_size < sym_ptr->low_limit ) {
  218. printf("get_string: String too short, must be min %d chars\n",
  219. sym_ptr->low_limit);
  220. exit(-1);
  221. }
  222. if ( sym_ptr->high_limit < string_size ) {
  223. printf("get_string: String too long, must be max %d chars\n",
  224. sym_ptr->high_limit);
  225. exit(-1);
  226. }
  227. }
  228. #ifdef DEBUG
  229. printf ("get_string: found '%s' on line %d\n", helper->strval, line );
  230. #endif /* DEBUG */
  231. memset(buf,0,string_size+1);
  232. memcpy ( buf, helper->strval, string_size);
  233. return ( string_size );
  234. }
  235. int get_hexstr (unsigned char *buf, void *tval, struct symbol_entry *sym_ptr)
  236. {
  237. unsigned int fragval;
  238. int i,rval;
  239. char *p;
  240. unsigned int string_size;
  241. /* We only use this to cast the void * we receive and extract the data from the union */
  242. union t_val *helper;
  243. if ( buf == NULL ) {
  244. printf ("get_hexstr called w/NULL buffer!\n");
  245. exit (-1);
  246. }
  247. if ( tval == NULL ) {
  248. printf ("get_hexstr called w/NULL value struct !\n");
  249. exit (-1);
  250. }
  251. helper = (union t_val *) tval;
  252. string_size = strlen ( helper->strval );
  253. if ( string_size != 2*floor(string_size/2) ) {
  254. printf ("get_hexstr: invalid hex string !\n");
  255. exit (-1);
  256. }
  257. p = helper->strval;
  258. if (p[0] != '0' || ( p[1] != 'x' && p[1] != 'X' )) {
  259. printf("get_hexstr: invalid hex string %s\n", p);
  260. exit (-1);
  261. }
  262. p += 2*sizeof(char);
  263. i=0;
  264. while (*p) {
  265. if ( (rval=sscanf(p, "%02x", &fragval)) == 0) {
  266. printf ("Invalid Hex Value %s\n", helper->strval);
  267. exit (-1);
  268. }
  269. buf[i] = (char) fragval; i++;
  270. p += 2*sizeof(char);
  271. }
  272. if (sym_ptr->low_limit || sym_ptr->high_limit) {
  273. if ( i < sym_ptr->low_limit ) {
  274. printf("get_hexstr: Hex value too short, must be min %d octets\n",
  275. sym_ptr->low_limit);
  276. exit(-1);
  277. }
  278. if ( sym_ptr->high_limit < i ) {
  279. printf("get_hexstr: Hex value too long, must be max %d octets\n",
  280. sym_ptr->high_limit);
  281. exit(-1);
  282. }
  283. }
  284. #ifdef DEBUG
  285. printf ("get_hexstr: found '%s' on line %d\n", helper->strval, line );
  286. #endif /* DEBUG */
  287. return ( i );
  288. }
  289. int get_nothing(unsigned char *buf, void *tval, struct symbol_entry *sym_ptr )
  290. {
  291. return 0;
  292. }