?? tun.c
字號:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/lib/tun.c,v 1.2 2001/11/08 15:56:30 tneale Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: tun.c,v $ * Revision 1.2 2001/11/08 15:56:30 tneale * Updated for newest file layout * * Revision 1.1.1.1 2001/11/05 17:48:44 tneale * Tornado shuffle * * Revision 1.7 2001/01/19 22:23:56 paul * Update copyright. * * Revision 1.6 2000/10/16 19:21:55 paul * Restore sockets and mempool code. * * Revision 1.5 2000/03/17 00:12:48 meister * Update copyright message * * Revision 1.4 2000/03/13 21:22:13 paul * Removed some code that we are no longer working on. * * Revision 1.3 1999/11/05 22:29:07 paul * Updated driver structs to reflect conditional backwards-compatibility * fields and new fields. * * Revision 1.2 1998/11/23 20:52:59 wes * Make MEDIA_CTL_CHECK_ADDRESS succeed rather than die. * Die using BUG instead of abort() for unknown media_ctl ops. * * Revision 1.1 1998/06/11 20:01:58 wes * Snark driver to talk to BSD tunnel driver.. *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*//* * Use the *BSD tun0 / /dev/tun interface to glue snark into the real world. * This is a work in progress; ifconfig'ing the host side of the interface * and adding appropriate routes on the other side is currently left as * a exercise for the user of this driver. */#include <wrn/wm/attache/config.h>#include <wrn/wm/attache/mib.h>#include <wrn/wm/attache/timer.h>#include <wrn/wm/attache/packet.h>#include <wrn/wm/attache/net.h>#include <wrn/wm/attache/route.h>#include <wrn/wm/attache/ip.h>#include <wrn/wm/attache/slowtime.h>#include <wrn/wm/attache/glue.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <dirent.h>#include <errno.h>#include <fcntl.h>#include <sys/file.h>#include <unistd.h>#include <sys/stat.h>#include <sys/socket.h>#include <sys/un.h>#include <assert.h>#include <sys/time.h>#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/read_ini.h>#define TEN_MEGABITS 10000000#include <wrn/wm/demo/bsdif.h>static void tun_driver_read (int fd, void *cookie, unsigned flags){ char buf[2048]; struct net *net = cookie; struct packet *p; if ((net->flags & NF_DRIVER_DOWN) == 0 && (flags & BSDIF_READ) != 0) { int i; do { i = read(fd, buf, sizeof(buf)); } while ((i<0) && (errno == EINTR)); if (i <= 0) return; p = pkt_alloc(i+MaxLnh); if (p == NULL) return; p->pkt_data += MaxLnh; p->pkt_datalen -= MaxLnh; p->pkt_n = net; MEMCPY(p->pkt_data, buf, i); ip_rcv(p); }}static void tun_driver_init(struct net *net){ struct bsdif *bif = net->specific; char *name = net->s_name; char namebuf[256]; net->flags |= NF_DRIVER_DOWN; net->speed = TEN_MEGABITS; sprintf(namebuf, "/dev/%s", name); if ((bif->fd = open(namebuf, O_RDWR, 0)) < 0) return; bif->handler = tun_driver_read; bif->flags = BSDIF_READ; net->flags &= ~NF_DRIVER_DOWN;}static void tun_driver_send (struct packet *p){ struct bsdif *bif = p->pkt_n->specific; if (write(bif->fd, p->pkt_data, p->pkt_datalen) < 0) { fputs(p->pkt_n->s_name, stderr); perror(": write() failure"); } pkt_free(p);}static void tun_ip_send (struct net *net, packet *p, inaddr_t host){ p->pkt_n = net; tun_driver_send(p);}static void tun_driver_close (struct net *net){ struct bsdif *bif = net->specific; if ((net->flags & NF_DRIVER_DOWN) == 0) { close(bif->fd); net->flags |= NF_DRIVER_DOWN; }}static int tun_media_ctl (struct net *net, int code, void *cookie){ switch (code) { case MEDIA_CTL_INIT: tun_driver_init(net); return MEDIA_CTL_GOOD; case MEDIA_CTL_CLOSE: tun_driver_close(net); return MEDIA_CTL_GOOD; case MEDIA_CTL_IP_UP: net->flags &= ~NF_DOWN; net->status_tstamp = GLUE_NOW();#if INSTALL_ATTACHE_MIB net->ifLastChange = centiseconds_since_attache_boot();#endif ip_advise_if_up(net); return MEDIA_CTL_GOOD; case MEDIA_CTL_IP_DOWN: net->flags |= NF_DOWN; net->status_tstamp = GLUE_NOW();#if INSTALL_ATTACHE_MIB net->ifLastChange = centiseconds_since_attache_boot();#endif ip_advise_if_down(net); return MEDIA_CTL_GOOD; case MEDIA_CTL_ADD_IPV4_ADDRESS: return MEDIA_CTL_GOOD; case MEDIA_CTL_DELETE_IPV4_ADDRESS: return MEDIA_CTL_GOOD; case MEDIA_CTL_CHECK_ADDRESS: return MEDIA_CTL_GOOD; default: BUG (BUG_SNARK_TUN_ERROR, BUG_FATAL, net, (BUG_OUT, "%s: unknown media_ctl opcode %d", net->s_name, code)); return MEDIA_CTL_ERROR; }}static struct driver tun_driver = { tun_driver_init, /* Interface initialization routine */ tun_driver_send, /* Raw packet send routine */ tun_ip_send, /* IP packet send back compat routine */ 0, /* ARP packet send encapsulation routine */ 0, /* Test routine (unused) */ tun_driver_close, /* Interface close routine */ "tun", /* Driver short name */ "Berkeley Packet Filter Ethernet", /* Driver long name */ 14, /* Local net header length */ 0, /* Local net trailer length */ 1500, /* Real Ethernet frame size is this... */ IF_PROP_SERIAL, /* MIB type of interfaces using this driver */ 0, /* arp hardware type (none) */ tun_media_ctl, /* Media control routine */ 0, /* ipv6 send.. */ 0 /* ipv4 send.. */};void tun_driver_find (void (*config)(char *, struct driver *, int, bits16_t, unsigned, bits32_t)){ int instance; char name[10]; for (instance = 0; instance < 2; instance++) { sprintf(name, "%d", instance); config (name, &tun_driver, instance, 0, tun_driver.maxlen - tun_driver.lnh - tun_driver.lnt, TEN_MEGABITS); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -