Copied tapdev from minimal-net to gtk.

This commit is contained in:
oliverschmidt 2007-03-27 20:49:09 +00:00
parent 1ca96b614a
commit df02b4667a
4 changed files with 151 additions and 97 deletions

View file

@ -1,34 +1,73 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: tapdev-service.c,v 1.2 2007/03/27 20:49:09 oliverschmidt Exp $
*/
#include "net/packet-service.h" #include "contiki-net.h"
#include "tapdev.h" #include "tapdev.h"
#include "net/uip-neighbor.h"
#include "net/uip_arp.h" #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
static struct uip_eth_addr addr = static u8_t output(void);
{{0x08, 0x12, 0x23, 0x89, 0xa3, 0x94}};
SERVICE(tapdev_service, packet_service, { tapdev_send }); SERVICE(tapdev_service, packet_service, { output });
PROCESS(tapdev_process, "TAP driver"); PROCESS(tapdev_process, "TAP driver");
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define BUF ((struct uip_eth_hdr *)&uip_buf[0]) static u8_t
output(void)
{
uip_arp_out();
tapdev_send();
return 0;
}
/*---------------------------------------------------------------------------*/
static void static void
pollhandler(void) pollhandler(void)
{ {
/* tapdev_service_request_poll();*/
process_poll(&tapdev_process); process_poll(&tapdev_process);
uip_len = tapdev_poll(); uip_len = tapdev_poll();
if(uip_len > 0) { if(uip_len > 0) {
if(BUF->type == htons(UIP_ETHTYPE_IP)) { #if UIP_CONF_IPV6
uip_arp_ipin(); if(BUF->type == htons(UIP_ETHTYPE_IPV6)) {
uip_len -= sizeof(struct uip_eth_hdr); uip_neighbor_add(&IPBUF->srcipaddr, &BUF->src);
/* uip_input();*/ tcpip_input();
} else
#endif /* UIP_CONF_IPV6 */
if(BUF->type == htons(UIP_ETHTYPE_IP)) {
tcpip_input(); tcpip_input();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
} else if(BUF->type == htons(UIP_ETHTYPE_ARP)) { } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
uip_arp_arpin(); uip_arp_arpin();
/* If the above function invocation resulted in data that /* If the above function invocation resulted in data that
@ -41,7 +80,6 @@ pollhandler(void)
} }
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
PROCESS_THREAD(tapdev_process, ev, data) PROCESS_THREAD(tapdev_process, ev, data)
{ {
PROCESS_BEGIN(); PROCESS_BEGIN();
@ -61,3 +99,4 @@ PROCESS_THREAD(tapdev_process, ev, data)
PROCESS_END(); PROCESS_END();
} }
/*---------------------------------------------------------------------------*/

View file

@ -1,3 +1,36 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: tapdev-service.h,v 1.2 2007/03/27 20:49:09 oliverschmidt Exp $
*/
#ifndef __TAPDEV_SERVICE_H__ #ifndef __TAPDEV_SERVICE_H__
#define __TAPDEV_SERVICE_H__ #define __TAPDEV_SERVICE_H__

View file

@ -1,40 +1,39 @@
/* /*
* Copyright (c) 2001, Swedish Institute of Computer Science. * Copyright (c) 2001, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* 3. Neither the name of the Institute nor the names of its contributors * 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* Author: Adam Dunkels <adam@sics.se> * Author: Adam Dunkels <adam@sics.se>
* *
* $Id: tapdev.c,v 1.1 2006/06/17 22:41:29 adamdunkels Exp $ * $Id: tapdev.c,v 1.2 2007/03/27 20:49:09 oliverschmidt Exp $
*/ */
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -47,7 +46,6 @@
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/socket.h> #include <sys/socket.h>
#ifdef linux #ifdef linux
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/if.h> #include <linux/if.h>
@ -58,46 +56,20 @@
#endif /* linux */ #endif /* linux */
#include "contiki-net.h" #include "contiki-net.h"
#include "tapdev.h"
#define DROP 0 #define DROP 0
#if DROP
static int drop = 0; static int drop = 0;
#endif
static int fd; static int fd;
static unsigned long lasttime; static unsigned long lasttime;
static struct timezone tz;
#define BUF ((struct uip_eth_hdr *)&uip_buf[0]) #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
static void do_send(void);
u8_t tapdev_send(void);
u16_t
tapdev_poll(void)
{
fd_set fdset;
struct timeval tv;
int ret;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fdset);
if(fd > 0) {
FD_SET(fd, &fdset);
}
ret = select(fd + 1, &fdset, NULL, NULL, &tv);
if(ret == 0) {
return 0;
}
ret = read(fd, uip_buf, UIP_BUFSIZE);
if(ret == -1) {
perror("tapdev_poll: read");
}
return ret;
}
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
tapdev_init(void) tapdev_init(void)
@ -124,27 +96,51 @@ tapdev_init(void)
snprintf(buf, sizeof(buf), "ifconfig tap0 inet 192.168.2.1"); snprintf(buf, sizeof(buf), "ifconfig tap0 inet 192.168.2.1");
system(buf); system(buf);
printf("%s\n", buf); printf("%s\n", buf);
lasttime = 0; lasttime = 0;
/* gdk_input_add(fd, GDK_INPUT_READ,
read_callback, NULL);*/
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void u16_t
do_send(void) tapdev_poll(void)
{
fd_set fdset;
struct timeval tv;
int ret;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fdset);
if(fd > 0) {
FD_SET(fd, &fdset);
}
ret = select(fd + 1, &fdset, NULL, NULL, &tv);
if(ret == 0) {
return 0;
}
ret = read(fd, uip_buf, UIP_BUFSIZE);
if(ret == -1) {
perror("tapdev_poll: read");
}
return ret;
}
/*---------------------------------------------------------------------------*/
void
tapdev_send(void)
{ {
int ret; int ret;
if(fd <= 0) { if(fd <= 0) {
return; return;
} }
/* printf("tapdev_send: sending %d bytes\n", size);*/ /* printf("tapdev_send: sending %d bytes\n", size);*/
/* check_checksum(uip_buf, size);*/ /* check_checksum(uip_buf, size);*/
#if DROP #if DROP
drop++; drop++;
if(drop % 8 == 7) { if(drop % 8 == 7) {
@ -159,20 +155,5 @@ do_send(void)
perror("tap_dev: tapdev_send: writev"); perror("tap_dev: tapdev_send: writev");
exit(1); exit(1);
} }
}
/*---------------------------------------------------------------------------*/
u8_t
tapdev_send(void)
{
uip_arp_out();
do_send();
return 0;
}
/*---------------------------------------------------------------------------*/
void
tapdev_do_send(void)
{
do_send();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View file

@ -31,13 +31,14 @@
* *
* This file is part of the uIP TCP/IP stack. * This file is part of the uIP TCP/IP stack.
* *
* $Id: tapdev.h,v 1.1 2006/06/17 22:41:30 adamdunkels Exp $ * $Id: tapdev.h,v 1.2 2007/03/27 20:49:09 oliverschmidt Exp $
*
*/ */
#ifndef __TAPDEV_H__ #ifndef __TAPDEV_H__
#define __TAPDEV_H__ #define __TAPDEV_H__
void tapdev_init(void); void tapdev_init(void);
u16_t tapdev_poll(void);
void tapdev_send(void);
u8_t tapdev_send(void);
#endif /* __TAPDEV_H__ */ #endif /* __TAPDEV_H__ */