43 #include <sys/types.h> 50 #include <sys/ioctl.h> 51 #include <sys/socket.h> 56 #define LOG_MODULE "BR" 57 #define LOG_LEVEL LOG_LEVEL_NONE 61 #include <linux/if_tun.h> 70 extern const char *slip_config_ipaddr;
71 extern char slip_config_tundev[32];
72 extern uint16_t slip_config_basedelay;
77 static int set_fd(fd_set *rset, fd_set *wset);
78 static void handle_fd(fd_set *rset, fd_set *wset);
79 static const struct select_callback tun_select_callback = {
85 int ssystem(
const char *fmt, ...)
86 __attribute__((__format__ (__printf__, 1, 2)));
88 ssystem(const
char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
91 ssystem(const
char *fmt, ...)
96 vsnprintf(cmd,
sizeof(cmd), fmt, ap);
106 ssystem(
"ifconfig %s down", slip_config_tundev);
108 ssystem(
"sysctl -w net.ipv6.conf.all.forwarding=1");
110 ssystem(
"netstat -nr" 111 " | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'" 117 sigcleanup(
int signo)
119 fprintf(stderr,
"signal %d\n", signo);
124 ifconf(
const char *tundev,
const char *
ipaddr)
127 ssystem(
"ifconfig %s inet `hostname` up", tundev);
128 ssystem(
"ifconfig %s add %s", tundev, ipaddr);
129 #elif defined(__APPLE__) 130 ssystem(
"ifconfig %s inet6 %s up", tundev, ipaddr);
131 ssystem(
"sysctl -w net.inet.ip.forwarding=1");
133 ssystem(
"ifconfig %s inet `hostname` %s up", tundev, ipaddr);
134 ssystem(
"sysctl -w net.inet.ip.forwarding=1");
138 ssystem(
"ifconfig %s\n", tundev);
142 devopen(
const char *dev,
int flags)
146 strncat(t, dev,
sizeof(t) - 5);
147 return open(t, flags);
157 if((fd = open(
"/dev/net/tun", O_RDWR)) < 0) {
161 memset(&ifr, 0,
sizeof(ifr));
166 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
168 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
171 if((err = ioctl(fd, TUNSETIFF, (
void *)&ifr)) < 0) {
175 strcpy(dev, ifr.ifr_name);
182 return devopen(dev, O_RDWR);
191 setvbuf(stdout, NULL, _IOLBF, 0);
197 static uint16_t delaymsec = 0;
198 static uint32_t delaystartsec, delaystartmsec;
204 setvbuf(stdout, NULL, _IOLBF, 0);
208 LOG_INFO(
"Opening tun interface:%s\n", slip_config_tundev);
210 tunfd = tun_alloc(slip_config_tundev);
213 err(1,
"tun_init: open");
216 select_set_callback(tunfd, &tun_select_callback);
218 fprintf(stderr,
"opened %s device ``/dev/%s''\n",
219 "tun", slip_config_tundev);
222 signal(SIGHUP, sigcleanup);
223 signal(SIGTERM, sigcleanup);
224 signal(SIGINT, sigcleanup);
225 ifconf(slip_config_tundev, slip_config_ipaddr);
229 tun_output(uint8_t *data,
int len)
232 if(write(tunfd, data, len) != len) {
233 err(1,
"serial_to_tun: write");
240 tun_input(
unsigned char *data,
int maxlen)
243 if((size = read(tunfd, data, maxlen)) == -1) {
244 err(1,
"tun_input: read");
263 const struct uip_fallback_interface rpl_interface = {
271 set_fd(fd_set *rset, fd_set *wset)
279 handle_fd(fd_set *rset, fd_set *wset)
287 gettimeofday(&tv, NULL);
288 dmsec = (tv.tv_sec - delaystartsec) * 1000 + tv.tv_usec / 1000 - delaystartmsec;
292 if(dmsec > delaymsec) {
300 if(FD_ISSET(tunfd, rset)) {
306 if(slip_config_basedelay) {
308 gettimeofday(&tv, NULL);
309 delaymsec = slip_config_basedelay;
310 delaystartsec = tv.tv_sec;
311 delaystartmsec = tv.tv_usec / 1000;
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Border router header file
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Header file for IPv6-related data structures.
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Header file for the uIP TCP/IP stack.
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the logging system
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.