linux.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // linux.c: low level access routines for Linux
  2. #include "config.h"
  3. #include <sys/socket.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/time.h>
  9. #include <features.h> /* for the glibc version number */
  10. #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
  11. #include <netpacket/packet.h>
  12. #include <net/ethernet.h> /* the L2 protocols */
  13. #else
  14. #include <asm/types.h>
  15. #include <linux/if_packet.h>
  16. #include <linux/if_ether.h> /* The L2 protocols */
  17. #endif
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include <net/if.h>
  21. #include <netinet/in.h>
  22. #include <linux/fs.h>
  23. #include <sys/stat.h>
  24. #include "dat.h"
  25. #include "fns.h"
  26. static int
  27. getindx(int s, char *name) // return the index of device 'name'
  28. {
  29. struct ifreq xx;
  30. int n;
  31. strcpy(xx.ifr_name, name);
  32. n = ioctl(s, SIOCGIFINDEX, &xx);
  33. if (n == -1)
  34. return -1;
  35. return xx.ifr_ifindex;
  36. }
  37. int
  38. dial(char *eth) // get us a raw connection to an interface
  39. {
  40. int i;
  41. int n, s;
  42. struct sockaddr_ll sa;
  43. enum { aoe_type = 0x88a2 };
  44. memset(&sa, 0, sizeof sa);
  45. s = socket(PF_PACKET, SOCK_RAW, htons(aoe_type));
  46. if (s == -1) {
  47. perror("got bad socket");
  48. return -1;
  49. }
  50. i = getindx(s, eth);
  51. sa.sll_family = AF_PACKET;
  52. sa.sll_protocol = htons(0x88a2);
  53. sa.sll_ifindex = i;
  54. n = bind(s, (struct sockaddr *)&sa, sizeof sa);
  55. if (n == -1) {
  56. perror("bind funky");
  57. return -1;
  58. }
  59. return s;
  60. }
  61. int
  62. getea(int s, char *name, uchar *ea)
  63. {
  64. struct ifreq xx;
  65. int n;
  66. strcpy(xx.ifr_name, name);
  67. n = ioctl(s, SIOCGIFHWADDR, &xx);
  68. if (n == -1) {
  69. perror("Can't get hw addr");
  70. return 0;
  71. }
  72. memmove(ea, xx.ifr_hwaddr.sa_data, 6);
  73. return 1;
  74. }