tapdev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2001, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * 3. Neither the name of the Institute nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * Author: Adam Dunkels <adam@sics.se>
  33. *
  34. * $Id: tapdev.c,v 1.8 2006/06/07 08:39:58 adam Exp $
  35. */
  36. #define UIP_DRIPADDR0 192
  37. #define UIP_DRIPADDR1 168
  38. #define UIP_DRIPADDR2 0
  39. #define UIP_DRIPADDR3 1
  40. #include <fcntl.h>
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/socket.h>
  47. #include <sys/types.h>
  48. #include <sys/time.h>
  49. #include <sys/uio.h>
  50. #include <sys/socket.h>
  51. #ifdef linux
  52. #include <sys/ioctl.h>
  53. #include <linux/if.h>
  54. #include <linux/if_tun.h>
  55. #define DEVTAP "/dev/net/tun"
  56. #else /* linux */
  57. #define DEVTAP "/dev/tap0"
  58. #endif /* linux */
  59. #include "uip.h"
  60. static int drop = 0;
  61. static int fd;
  62. /*---------------------------------------------------------------------------*/
  63. void
  64. tapdev_init(void)
  65. {
  66. char buf[1024];
  67. fd = open(DEVTAP, O_RDWR);
  68. if(fd == -1) {
  69. perror("tapdev: tapdev_init: open");
  70. exit(1);
  71. }
  72. #ifdef linux
  73. {
  74. struct ifreq ifr;
  75. memset(&ifr, 0, sizeof(ifr));
  76. ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
  77. if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
  78. perror(buf);
  79. exit(1);
  80. }
  81. }
  82. #endif /* Linux */
  83. snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d",
  84. UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3);
  85. system(buf);
  86. }
  87. /*---------------------------------------------------------------------------*/
  88. unsigned int
  89. tapdev_read(void)
  90. {
  91. fd_set fdset;
  92. struct timeval tv, now;
  93. int ret;
  94. tv.tv_sec = 0;
  95. tv.tv_usec = 1000;
  96. FD_ZERO(&fdset);
  97. FD_SET(fd, &fdset);
  98. ret = select(fd + 1, &fdset, NULL, NULL, &tv);
  99. if(ret == 0) {
  100. return 0;
  101. }
  102. ret = read(fd, uip_buf, UIP_BUFSIZE);
  103. if(ret == -1) {
  104. perror("tap_dev: tapdev_read: read");
  105. }
  106. /* printf("--- tap_dev: tapdev_read: read %d bytes\n", ret);*/
  107. /* {
  108. int i;
  109. for(i = 0; i < 20; i++) {
  110. printf("%x ", uip_buf[i]);
  111. }
  112. printf("\n");
  113. }*/
  114. /* check_checksum(uip_buf, ret);*/
  115. return ret;
  116. }
  117. /*---------------------------------------------------------------------------*/
  118. void
  119. tapdev_send(void)
  120. {
  121. int ret;
  122. /* printf("tapdev_send: sending %d bytes\n", size);*/
  123. /* check_checksum(uip_buf, size);*/
  124. /* drop++;
  125. if(drop % 8 == 7) {
  126. printf("Dropped a packet!\n");
  127. return;
  128. }*/
  129. ret = write(fd, uip_buf, uip_len);
  130. if(ret == -1) {
  131. perror("tap_dev: tapdev_send: writev");
  132. exit(1);
  133. }
  134. }
  135. /*---------------------------------------------------------------------------*/