51#include <sys/socket.h>
56#define LOG_MODULE "BR"
57#define LOG_LEVEL LOG_LEVEL_NONE
61#include <linux/if_tun.h>
70extern const char *slip_config_ipaddr;
71extern char slip_config_tundev[32];
72extern uint16_t slip_config_basedelay;
77static int set_fd(fd_set *rset, fd_set *wset);
78static void handle_fd(fd_set *rset, fd_set *wset);
79static const struct select_callback tun_select_callback = {
85int ssystem(
const char *fmt, ...)
86 __attribute__((__format__ (__printf__, 1, 2)));
88ssystem(const
char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
91ssystem(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; }'"
119 fprintf(stderr,
"signal %d\n", signo);
124ifconf(
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);
142devopen(
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 - 1);
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);
197static uint16_t delaymsec = 0;
198static 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);
229tun_output(uint8_t *data,
int len)
232 if(write(tunfd, data, len) != len) {
233 err(1,
"serial_to_tun: write");
240tun_input(
unsigned char *data,
int maxlen)
243 if((size = read(tunfd, data, maxlen)) == -1) {
244 err(1,
"tun_input: read");
263const struct uip_fallback_interface rpl_interface = {
271set_fd(fd_set *rset, fd_set *wset)
279handle_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;
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.
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
Header file for IPv6-related data structures.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Header file for the uIP TCP/IP stack.