linux.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. int getindx(int, char *);
  27. int getea(int, char *, uchar *);
  28. int
  29. dial(char *eth) // get us a raw connection to an interface
  30. {
  31. int i;
  32. int n, s;
  33. struct sockaddr_ll sa;
  34. enum { aoe_type = 0x88a2 };
  35. memset(&sa, 0, sizeof sa);
  36. s = socket(PF_PACKET, SOCK_RAW, htons(aoe_type));
  37. if (s == -1) {
  38. perror("got bad socket");
  39. return -1;
  40. }
  41. i = getindx(s, eth);
  42. sa.sll_family = AF_PACKET;
  43. sa.sll_protocol = htons(0x88a2);
  44. sa.sll_ifindex = i;
  45. n = bind(s, (struct sockaddr *)&sa, sizeof sa);
  46. if (n == -1) {
  47. perror("bind funky");
  48. return -1;
  49. }
  50. return s;
  51. }
  52. int
  53. getindx(int s, char *name) // return the index of device 'name'
  54. {
  55. struct ifreq xx;
  56. int n;
  57. strcpy(xx.ifr_name, name);
  58. n = ioctl(s, SIOCGIFINDEX, &xx);
  59. if (n == -1)
  60. return -1;
  61. return xx.ifr_ifindex;
  62. }
  63. int
  64. getea(int s, char *name, uchar *ea)
  65. {
  66. struct ifreq xx;
  67. int n;
  68. strcpy(xx.ifr_name, name);
  69. n = ioctl(s, SIOCGIFHWADDR, &xx);
  70. if (n == -1) {
  71. perror("Can't get hw addr");
  72. return 0;
  73. }
  74. memmove(ea, xx.ifr_hwaddr.sa_data, 6);
  75. return 1;
  76. }
  77. int
  78. getsec(int fd, uchar *place, vlong lba, int nsec)
  79. {
  80. lseek(fd, lba * 512, 0);
  81. return read(fd, place, nsec * 512);
  82. }
  83. int
  84. putsec(int fd, uchar *place, vlong lba, int nsec)
  85. {
  86. lseek(fd, lba * 512, 0);
  87. return write(fd, place, nsec * 512);
  88. }
  89. int
  90. getpkt(int fd, uchar *buf, int sz)
  91. {
  92. return read(fd, buf, sz);
  93. }
  94. int
  95. putpkt(int fd, uchar *buf, int sz)
  96. {
  97. return write(fd, buf, sz);
  98. }
  99. vlong
  100. getsize(int fd)
  101. {
  102. vlong size;
  103. struct stat s;
  104. int n;
  105. n = ioctl(fd, BLKGETSIZE64, &size);
  106. if (n == -1) { // must not be a block special
  107. n = fstat(fd, &s);
  108. if (n == -1) {
  109. perror("getsize");
  110. exit(1);
  111. }
  112. size = s.st_size;
  113. }
  114. printf("ioctl returned %d\n", n);
  115. printf("%lld bytes\n", size);
  116. return size;
  117. }