SAMV71 Xplained Ultra Software Package 1.3

tapdev.c

00001 /*
00002  * Copyright (c) 2001, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  *
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *
00016  * 3. Neither the name of the Institute nor the names of its contributors
00017  *    may be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00024  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00025  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00026  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00029  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * Author: Adam Dunkels <adam@sics.se>
00033  *
00034  * $Id: tapdev.c,v 1.8 2006/06/07 08:39:58 adam Exp $
00035  */
00036 
00037 #define UIP_DRIPADDR0   192
00038 #define UIP_DRIPADDR1   168
00039 #define UIP_DRIPADDR2   0
00040 #define UIP_DRIPADDR3   1
00041 
00042 #include <fcntl.h>
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <unistd.h>
00046 #include <string.h>
00047 #include <sys/ioctl.h>
00048 #include <sys/socket.h>
00049 #include <sys/types.h>
00050 #include <sys/time.h>
00051 #include <sys/uio.h>
00052 #include <sys/socket.h>
00053 
00054 #ifdef linux
00055 #include <sys/ioctl.h>
00056 #include <linux/if.h>
00057 #include <linux/if_tun.h>
00058 #define DEVTAP "/dev/net/tun"
00059 #else  /* linux */
00060 #define DEVTAP "/dev/tap0"
00061 #endif /* linux */
00062 
00063 #include "uip.h"
00064 
00065 static int drop = 0;
00066 static int fd;
00067 
00068 
00069 /*---------------------------------------------------------------------------*/
00070 void
00071 tapdev_init(void)
00072 {
00073   char buf[1024];
00074   
00075   fd = open(DEVTAP, O_RDWR);
00076   if(fd == -1) {
00077     perror("tapdev: tapdev_init: open");
00078     exit(1);
00079   }
00080 
00081 #ifdef linux
00082   {
00083     struct ifreq ifr;
00084     memset(&ifr, 0, sizeof(ifr));
00085     ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
00086     if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
00087       perror(buf);
00088       exit(1);
00089     }
00090   }
00091 #endif /* Linux */
00092 
00093   snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d",
00094        UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3);
00095   system(buf);
00096 
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 unsigned int
00100 tapdev_read(void)
00101 {
00102   fd_set fdset;
00103   struct timeval tv, now;
00104   int ret;
00105   
00106   tv.tv_sec = 0;
00107   tv.tv_usec = 1000;
00108 
00109 
00110   FD_ZERO(&fdset);
00111   FD_SET(fd, &fdset);
00112 
00113   ret = select(fd + 1, &fdset, NULL, NULL, &tv);
00114   if(ret == 0) {
00115     return 0;
00116   }
00117   ret = read(fd, uip_buf, UIP_BUFSIZE);
00118   if(ret == -1) {
00119     perror("tap_dev: tapdev_read: read");
00120   }
00121 
00122   /*  printf("--- tap_dev: tapdev_read: read %d bytes\n", ret);*/
00123   /*  {
00124     int i;
00125     for(i = 0; i < 20; i++) {
00126       printf("%x ", uip_buf[i]);
00127     }
00128     printf("\n");
00129     }*/
00130   /*  check_checksum(uip_buf, ret);*/
00131   return ret;
00132 }
00133 /*---------------------------------------------------------------------------*/
00134 void
00135 tapdev_send(void)
00136 {
00137   int ret;
00138   /*  printf("tapdev_send: sending %d bytes\n", size);*/
00139   /*  check_checksum(uip_buf, size);*/
00140 
00141   /*  drop++;
00142   if(drop % 8 == 7) {
00143     printf("Dropped a packet!\n");
00144     return;
00145     }*/
00146   ret = write(fd, uip_buf, uip_len);
00147   if(ret == -1) {
00148     perror("tap_dev: tapdev_send: writev");
00149     exit(1);
00150   }
00151 }
00152 /*---------------------------------------------------------------------------*/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines