ethermac.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * DOCSIS is a registered trademark of Cablelabs, http://www.cablelabs.com
  21. */
  22. /*
  23. change history
  24. 2002-10-10 fixed overflow causing last two bytes of the MAC address to not be converted
  25. correctly when the format was "xx:xx:xx:xx:xx:x"
  26. */
  27. #include "ethermac.h"
  28. int ether_aton ( const char *macstr, unsigned char *outbuf )
  29. {
  30. int fragval;
  31. const char *ptr; char *p;
  32. char fragptr[3];
  33. unsigned char themac[6]; /* MAC address in binary form */
  34. int i=0,rval=0;
  35. ptr=macstr;
  36. p = strchr ( ptr, (int) ':' );
  37. while (p && i<5) {
  38. if ( p-ptr > 2 || p == NULL ) {
  39. fprintf(stderr, "\nInvalid MAC Address %s\n", macstr);
  40. return 0;
  41. }
  42. memset(fragptr,0,3);
  43. memcpy ( fragptr, ptr, (size_t)(p-ptr) );
  44. if ( (rval= sscanf ( fragptr, "%x", &fragval)) == 0 ) {
  45. fprintf(stderr, "\nInvalid MAC Address %s\n", macstr );
  46. return 0;
  47. }
  48. if ( (unsigned int) fragval > 255 ) {
  49. fprintf(stderr, "\nInvalid MAC Address %s\n", macstr );
  50. return 0;
  51. }
  52. themac[i]=(unsigned char) fragval;
  53. ptr=++p;
  54. p = strchr ( ptr, (int) ':' );
  55. i++;
  56. }
  57. if ( i != 5 || strlen(ptr) > 2 ) {
  58. fprintf(stderr, "Invalid MAC Address %s\n", macstr);
  59. return 0;
  60. }
  61. memset(fragptr,0,3);
  62. memcpy ( fragptr, ptr, strlen(ptr) );
  63. if ( (rval=sscanf ( fragptr, "%x", &fragval)) == 0 ) {
  64. fprintf(stderr, "\nInvalid MAC Address %s\n", macstr );
  65. return 0;
  66. }
  67. if ( (unsigned int) fragval > 255 ) {
  68. fprintf(stderr, "\nInvalid MAC Address %s\n", macstr );
  69. return 0;
  70. }
  71. themac[i] = (unsigned char) fragval;
  72. memcpy ( outbuf, themac, 6);
  73. return 6;
  74. }
  75. /*
  76. * Tranforms the binary-form Ethernet MAC address received as argument into
  77. * string-form human readable Ethernet MAC address
  78. * themac is a static char which WILL be overwritten by subsequent calls
  79. * to this function
  80. */
  81. char *ether_ntoa ( const unsigned char *mac)
  82. {
  83. int i;
  84. static char themac[18];
  85. memset (themac, 0, 18);
  86. for (i=0;i<5;i++) sprintf ( themac+(3*i), "%02x:", mac[i]);
  87. sprintf ( themac+(3*i), "%02x", mac[i]);
  88. return themac;
  89. }