ethers.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * libc/inet/ethers.c
  3. *
  4. * Programmatic interface for the /etc/ethers file
  5. *
  6. * Copyright 2007 by Matthew Wilcox <matthew@wil.cx>
  7. *
  8. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  9. */
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <netinet/ether.h>
  14. #define ETHER_LINE_LEN 256
  15. /*
  16. * Internal function which returns a pointer to the part of the line
  17. * with the start of the hostname, or NULL if we couldn't parse the line.
  18. * Note that this line may have a comment symbol on it somewhere; if so
  19. * it will return NULL if the # is before or within the ether_addr, and
  20. * succeed if the # is before or within the host. It's up to the callers
  21. * to be aware of this.
  22. *
  23. * I would have preferred to write a NUL to the location of the comment
  24. * character, but ether_line takes a const argument. See __ether_line_w.
  25. */
  26. static const char *__ether_line(const char *line, struct ether_addr *addr)
  27. {
  28. struct ether_addr *res = ether_aton_r(line, addr);
  29. if (!res)
  30. return NULL;
  31. while (*line && (*line != '\n') && (*line != ' ') && (*line != '\t'))
  32. line++;
  33. while (*line && (*line != '\n') && ((*line == ' ') || (*line == '\t')))
  34. line++;
  35. return (*line && (*line != '\n')) ? line : NULL;
  36. }
  37. /*
  38. * Strips out the comment before calling __ether_line. We can do this,
  39. * since we know the buffer is writable.
  40. */
  41. static const char *__ether_line_w(char *line, struct ether_addr *addr)
  42. {
  43. char *end = strpbrk(line, "#\n");
  44. if (end)
  45. *end = '\0';
  46. return __ether_line(line, addr);
  47. }
  48. int ether_line(const char *line, struct ether_addr *addr, char *hostname)
  49. {
  50. const char *name = __ether_line(line, addr);
  51. if (!name)
  52. return -1;
  53. while (*name) {
  54. if ((*name == '#') || isspace(*name))
  55. break;
  56. *hostname++ = *name++;
  57. }
  58. *hostname = '\0';
  59. return 0;
  60. }
  61. int ether_ntohost(char *hostname, const struct ether_addr *addr)
  62. {
  63. int res = -1;
  64. FILE *fp;
  65. char buf[ETHER_LINE_LEN];
  66. fp = fopen(ETHER_FILE_NAME, "r");
  67. if (!fp)
  68. return -1;
  69. while (fgets(buf, sizeof(buf), fp)) {
  70. struct ether_addr tmp_addr;
  71. const char *cp = __ether_line_w(buf, &tmp_addr);
  72. if (!cp)
  73. continue;
  74. if (memcmp(addr, &tmp_addr, sizeof(tmp_addr)))
  75. continue;
  76. strcpy(hostname, cp);
  77. res = 0;
  78. break;
  79. }
  80. fclose(fp);
  81. return res;
  82. }
  83. int ether_hostton(const char *hostname, struct ether_addr *addr)
  84. {
  85. int res = -1;
  86. FILE *fp;
  87. char buf[ETHER_LINE_LEN];
  88. fp = fopen(ETHER_FILE_NAME, "r");
  89. if (!fp)
  90. return -1;
  91. while (fgets(buf, sizeof(buf), fp)) {
  92. const char *cp = __ether_line_w(buf, addr);
  93. if (!cp)
  94. continue;
  95. if (strcasecmp(hostname, cp))
  96. continue;
  97. res = 0;
  98. break;
  99. }
  100. fclose(fp);
  101. return res;
  102. }