示例#1
0
文件: senslip.c 项目: EDAyele/wsn430
void cleanup(void) {
	ssystem("ifconfig %s down", tundev);
#ifndef linux
	ssystem("sysctl -w net.inet.ip.forwarding=0");
#endif
	/* ssystem("arp -d %s", ipaddr); */
	ssystem("netstat -nr"
		" | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
		" | sh", tundev);
}
示例#2
0
/*---------------------------------------------------------------------------*/
void
cleanup(void)
{
  ssystem("ifconfig %s down", slip_config_tundev);
#ifndef linux
  ssystem("sysctl -w net.ipv6.conf.all.forwarding=1");
#endif
  ssystem("netstat -nr"
	  " | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
	  " | sh",
	  slip_config_tundev);
}
示例#3
0
文件: tunslip6.c 项目: EDAyele/wsn430
	void
cleanup(void)
{
	if (timestamp) stamptime();
	ssystem("ifconfig %s down", tundev);
#ifndef linux
	ssystem("sysctl -w net.ipv6.conf.all.forwarding=1");
#endif
	/* ssystem("arp -d %s", ipaddr); */
	if (timestamp) stamptime();
	ssystem("netstat -nr"
			" | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
			" | sh",
			tundev);
}
示例#4
0
文件: tunslip6.c 项目: EDAyele/wsn430
	void
ifconf(const char *tundev, const char *ipaddr)
{
#ifdef linux
	if (timestamp) stamptime();
	ssystem("ifconfig %s inet `hostname` up", tundev);
	if (timestamp) stamptime();
	ssystem("ifconfig %s add %s", tundev, ipaddr);
#else
	if (timestamp) stamptime();
	ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);
	if (timestamp) stamptime();
	ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !linux */

	if (timestamp) stamptime();
	ssystem("ifconfig %s\n", tundev);
}
示例#5
0
文件: tunslip.c 项目: 4dahalibut/RIOT
void
ifconf(const char *tundev, const char *ipaddr, const char *netmask)
{
    struct in_addr netname;
    netname.s_addr = inet_addr(ipaddr) & inet_addr(netmask);

#ifdef linux
    ssystem("ifconfig %s inet `hostname` up", tundev);

    if (strcmp(ipaddr, "0.0.0.0") != 0) {
        ssystem("route add -net %s netmask %s dev %s",
                inet_ntoa(netname), netmask, tundev);
    }

#else
    ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);

    if (strcmp(ipaddr, "0.0.0.0") != 0) {
        ssystem("route add -net %s -netmask %s -interface %s",
                inet_ntoa(netname), netmask, tundev);
    }

    ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !linux */

    ssystem("ifconfig %s\n", tundev);
}
示例#6
0
void
cleanup(void)
{
#ifndef __APPLE__
  if (timestamp) stamptime();
  ssystem("ifconfig %s down", tundev);
#ifndef linux
  ssystem("sysctl -w net.ipv6.conf.all.forwarding=1");
#endif
  /* ssystem("arp -d %s", ipaddr); */
  if (timestamp) stamptime();
  ssystem("netstat -nr"
	  " | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
	  " | sh",
	  tundev);
#else
  {
    char *  itfaddr = strdup(ipaddr);
    char *  prefix = index(itfaddr, '/');
    if (timestamp) stamptime();
    ssystem("ifconfig %s inet6 %s remove", tundev, ipaddr);
    if (timestamp) stamptime();
    ssystem("ifconfig %s down", tundev);
    if ( prefix != NULL ) *prefix = '\0';
    ssystem("route delete -inet6 %s", itfaddr);
    free(itfaddr);
  }
#endif
}
示例#7
0
/*---------------------------------------------------------------------------*/
void
ifconf(const char *tundev)
{
  if(slip_config_ifup_script != NULL) {
    if(access(slip_config_ifup_script, R_OK | X_OK) == 0) {
      ssystem("%s %s %s", slip_config_ifup_script,
              use_raw_ethernet ? "raw" : "tap", slip_config_tundev);
    } else {
      fprintf(stderr, "Could not access %s : %s\n", slip_config_ifup_script,
              strerror(errno));
    }
  }
}
示例#8
0
/*---------------------------------------------------------------------------*/
void
ifconf(const char *tundev)
{
  if(slip_config_ifup_script != NULL) {
    if(access(slip_config_ifup_script, R_OK | X_OK) == 0) {
      LOG6LBR_INFO("Running 6lbr-ifdown script '%s'\n", slip_config_ifup_script);
      ssystem("%s %s %s 2>&1", slip_config_ifup_script,
              use_raw_ethernet ? "raw" : "tap", slip_config_tundev);
    } else {
      LOG6LBR_ERROR("Could not access %s : %s\n", slip_config_ifup_script,
              strerror(errno));
    }
  } else {
    LOG6LBR_INFO("No 6lbr-down script specified\n");
  }
}
示例#9
0
/*---------------------------------------------------------------------------*/
void
ifconf(const char *tundev, const char *ipaddr)
{
#ifdef linux
  ssystem("ifconfig %s inet `hostname` up", tundev);
  ssystem("ifconfig %s add %s", tundev, ipaddr);
#elif defined(__APPLE__)
  ssystem("ifconfig %s inet6 %s up", tundev, ipaddr);
  ssystem("sysctl -w net.inet.ip.forwarding=1");
#else
  ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);
  ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !linux */

  /* Print the configuration to the console. */
  ssystem("ifconfig %s\n", tundev);
}
示例#10
0
/*---------------------------------------------------------------------------*/
static void
ifconf(const char *eth_dev)
{
  if(sixlbr_config_ifup_script != NULL) {
    if(access(sixlbr_config_ifup_script, R_OK | X_OK) == 0) {
      LOG6LBR_INFO("Running 6lbr-ifup script '%s'\n", sixlbr_config_ifup_script);
      int status = ssystem("%s %s %s 2>&1", sixlbr_config_ifup_script,
              sixlbr_config_use_raw_ethernet ? "raw" : "tap", sixlbr_config_eth_device);
      if(status != 0) {
        LOG6LBR_FATAL("6lbr-ifup script returned an error, aborting...\n");
        exit(1);
      }
    } else {
      LOG6LBR_ERROR("Could not access %s : %s\n", sixlbr_config_ifup_script,
              strerror(errno));
    }
  } else {
    LOG6LBR_INFO("No 6lbr-up script specified\n");
  }
}
示例#11
0
/*---------------------------------------------------------------------------*/
void
cleanup(void)
{
  if(slip_config_ifdown_script != NULL) {
    if(access(slip_config_ifdown_script, R_OK | X_OK) == 0) {
      LOG6LBR_INFO("Running 6lbr-ifup script '%s'\n", slip_config_ifdown_script);
      ssystem("%s %s %s 2>&1", slip_config_ifdown_script,
              use_raw_ethernet ? "raw" : "tap", slip_config_tundev);
    } else {
      LOG6LBR_ERROR("Could not access %s : %s\n", slip_config_ifdown_script,
              strerror(errno));
    }
  } else {
    LOG6LBR_INFO("No 6lbr-ifup script specified\n");
  }
#if !CETIC_6LBR_ONE_ITF
  slip_set_mac(&linkaddr_null);
  slip_flushbuf(slipfd);
#endif
}
示例#12
0
/*---------------------------------------------------------------------------*/
static void
cleanup(void)
{
  if(sixlbr_config_ifdown_script != NULL) {
    if(access(sixlbr_config_ifdown_script, R_OK | X_OK) == 0) {
      LOG6LBR_INFO("Running 6lbr-ifdown script '%s'\n", sixlbr_config_ifdown_script);
      int status = ssystem("%s %s %s 2>&1", sixlbr_config_ifdown_script,
              sixlbr_config_use_raw_ethernet ? "raw" : "tap", sixlbr_config_eth_device);
      if(status != 0) {
        LOG6LBR_ERROR("6lbr-ifdown script returned an error\n");
      }
    } else {
      LOG6LBR_ERROR("Could not access %s : %s\n", sixlbr_config_ifdown_script,
              strerror(errno));
    }
  } else {
    LOG6LBR_INFO("No 6lbr-ifdown script specified\n");
  }
  cetic_6lbr_clear_ip();
#if !CETIC_6LBR_ONE_ITF
  slip_set_mac(&linkaddr_null);
  slip_flushbuf(slipfd);
#endif
}
示例#13
0
文件: tunslip6.c 项目: EDAyele/wsn430
/*
 * Read from serial, when we have a packet write it to tun. No output
 * buffering, input buffered by stdio.
 */
	void
serial_to_tun(FILE *inslip, int outfd)
{
	static union {
		unsigned char inbuf[2000];
	} uip;
	static unsigned int inbufptr = 0;
	int ret;
	unsigned int i;
	unsigned char c;

#ifdef linux
	ret = fread(&c, 1, 1, inslip);
	if(ret == -1 || ret == 0) err(1, "serial_to_tun: read");
	goto after_fread;
#endif

read_more:
	if(inbufptr >= sizeof(uip.inbuf)) {
		inbufptr = 0;
		if(timestamp) stamptime();
		fprintf(stderr, "*** dropping too large packet\n");
	}
	ret = fread(&c, 1, 1, inslip);
#ifdef linux
after_fread:
#endif
	if(ret == -1) {
		err(1, "serial_to_tun: read");
	}
	if(ret == 0) {
		clearerr(inslip);
		return;
	}
	/*  fprintf(stderr, ".");*/
	switch(c) {
		case SLIP_END:
			if(inbufptr > 0) {
				if(uip.inbuf[0] == '!') {
					if (uip.inbuf[1] == 'M') {
						/* Read gateway MAC address and autoconfigure tap0 interface */
						char macs[24];
						int i, pos;
						for(i = 0, pos = 0; i < 16; i++) {
							macs[pos++] = uip.inbuf[2 + i];
							if ((i & 1) == 1 && i < 14) {
								macs[pos++] = ':';
							}
						}
						if(timestamp) stamptime();
						macs[pos] = '\0';
						//	  printf("*** Gateway's MAC address: %s\n", macs);
						fprintf(stderr,"*** Gateway's MAC address: %s\n", macs);
						if (timestamp) stamptime();
						ssystem("ifconfig %s down", tundev);
						if (timestamp) stamptime();
						ssystem("ifconfig %s hw ether %s", tundev, &macs[6]);
						if (timestamp) stamptime();
						ssystem("ifconfig %s up", tundev);
					} else if(uip.inbuf[1] == 'P') {
						/* Prefix info requested */
						struct in6_addr addr;
						int i;
						char *s = strchr(ipaddr, '/');
						if(s != NULL) {
							*s = '\0';
						}
						inet_pton(AF_INET6, ipaddr, &addr);
						if(timestamp)
							stamptime();
						fprintf(stderr,"*** Address:%s => %02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
								ipaddr,
								addr.s6_addr[0], addr.s6_addr[1],
								addr.s6_addr[2], addr.s6_addr[3],
								addr.s6_addr[4], addr.s6_addr[5],
								addr.s6_addr[6], addr.s6_addr[7]);
						slip_send(slipfd, '!');
						slip_send(slipfd, 'P');
						for (i = 0; i < 8; i++) {
							//slip_send(slipfd, addr.s6_addr[i]);

							switch (addr.s6_addr[i]) {
								case SLIP_END:
									slip_send(outfd, SLIP_ESC);
									slip_send(outfd, SLIP_ESC_END);
									break;
								case SLIP_ESC:
									slip_send(outfd, SLIP_ESC);
									slip_send(outfd, SLIP_ESC_ESC);
									break;
								default:
									slip_send(outfd, addr.s6_addr[i]);
									break;
							}
						}
						slip_send(slipfd, SLIP_END);

						slip_flushbuf(slipfd); // DEBUG
					}
#define DEBUG_LINE_MARKER '\r'
				} else if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
					fwrite(uip.inbuf + 1, inbufptr - 1, 1, stdout);
				} else if(is_sensible_string(uip.inbuf, inbufptr)) {
					if(verbose==1) {   /* strings already echoed below for verbose>1 */
						if (timestamp) stamptime();
						fwrite(uip.inbuf, inbufptr, 1, stdout);
					}
				} else {
					if(verbose>2) {
						if (timestamp) stamptime();
						printf("Packet from SLIP of length %d - write TUN\n", inbufptr);
						if (verbose>4) {
							printf("         ");
							for(i = 0; i < inbufptr; i++) {
								printf("%02x", uip.inbuf[i]);
								if((i & 3) == 3) {
									printf(" ");
								}
								if((i & 15) == 15)
									printf("\n         ");
							}
							printf("\n");
						}
					}
					if(write(outfd, uip.inbuf, inbufptr) != inbufptr) {
						err(1, "serial_to_tun: write");
					}
				}
				inbufptr = 0;
			}
			break;

		case SLIP_ESC:
			if(fread(&c, 1, 1, inslip) != 1) {
				clearerr(inslip);
				/* Put ESC back and give up! */
				ungetc(SLIP_ESC, inslip);
				return;
			}

			switch(c) {
				case SLIP_ESC_END:
					c = SLIP_END;
					break;
				case SLIP_ESC_ESC:
					c = SLIP_ESC;
					break;
			}
			/* FALLTHROUGH */
		default:
			uip.inbuf[inbufptr++] = c;

			/* Echo lines as they are received for verbose=2,3,5+ */
			/* Echo all printable characters for verbose==4 */
			if((verbose==2) || (verbose==3) || (verbose>4)) {
				if(c=='\n') {
					if(is_sensible_string(uip.inbuf, inbufptr)) {
						if (timestamp) stamptime();
						fwrite(uip.inbuf, inbufptr, 1, stdout);
						inbufptr=0;
					}
				}
			} else if(verbose==4) {
				if(c == 0 || c == '\r' || c == '\n' || c == '\t' || (c >= ' ' && c <= '~')) {
					fwrite(&c, 1, 1, stdout);
					if(c=='\n') if(timestamp) stamptime();
				}
			}

			break;
	}

	goto read_more;
}
示例#14
0
文件: senslip.c 项目: EDAyele/wsn430
/*
 * Read from serial, when we have a packet write it to tun. No output
 * buffering, input buffered by stdio.
 */
void serial_to_tun(int sockfd, int outfd) {
	static union {
		unsigned char inbuf[2000];
		struct ip iphdr;
	} uip;
	static unsigned int inbufptr = 0;

	int ret;
	unsigned char c;
	static unsigned char old_c = 0;

#ifdef linux
	if (old_c)
	{
		c = old_c;
		old_c = 0;
		ret = 1;
	}
	else
	{
		ret = recv(sockfd, &c, 1, 0);
		if(ret == -1 || ret == 0)
		{
			return;
			err(1, "serial_to_tun: read");
		}
	}
	goto after_fread;
#endif

	read_more: if (inbufptr >= sizeof(uip.inbuf)) {
		inbufptr = 0;
	}
	ret = recv(sockfd, &c, 1, 0);
#ifdef linux
	after_fread:
#endif
	if (ret == -1) {
		return;
		err(1, "serial_to_tun: read");
	}
	if (ret == 0) {
		//~ clearerr(inslip);
		return;
		fprintf(stderr, "serial_to_tun: EOF\n");
		exit(1);
	}
	//~ fprintf(stderr, "|%2x",c);
	switch (c) {
	case SLIP_END:
		if (inbufptr > 0) {
			/*
			 * Sanity checks.
			 */
#define DEBUG_LINE_MARKER '\r'
			int ecode;
			ecode = check_ip(&uip.iphdr, inbufptr);
			if (ecode < 0 && inbufptr == 8 && strncmp((char *)uip.inbuf, "=IPA", 4)
					== 0) {
				static struct in_addr ipa;

				inbufptr = 0;
				if (memcmp(&ipa, &uip.inbuf[4], sizeof(ipa)) == 0) {
					break;
				}

				/* New address. */
				if (ipa.s_addr != 0) {
#ifdef linux
					ssystem("route delete -net %s netmask %s dev %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#else
					ssystem("route delete -net %s -netmask %s -interface %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#endif
				}

				memcpy(&ipa, &uip.inbuf[4], sizeof(ipa));
				if (ipa.s_addr != 0) {
#ifdef linux
					ssystem("route add -net %s netmask %s dev %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#else
					ssystem("route add -net %s -netmask %s -interface %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#endif
				}
				break;
			} else if (ecode < 0) {
				/*
				 * If sensible ASCII string, print it as debug info!
				 */
				if (uip.inbuf[0] == DEBUG_LINE_MARKER) {
					fwrite(uip.inbuf + 1, inbufptr - 1, 1, stderr);
				} else if (is_sensible_string(uip.inbuf, inbufptr)) {
					fwrite(uip.inbuf, inbufptr, 1, stderr);
				} else {
					fprintf(stderr,
							"serial_to_tun: drop packet len=%d ecode=%d\n",
							inbufptr, ecode);
				}
				inbufptr = 0;
				break;
			}
			PROGRESS("s");

			if (dhsock != -1) {
				struct ip *ip = (void *) uip.inbuf;
				if (ip->ip_p == 17 && ip->ip_dst == 0xffffffff /* UDP and broadcast */
				&& ip->uh_sport == ntohs(BOOTPC) && ip->uh_dport == ntohs(
						BOOTPS)) {
					relay_dhcp_to_server(ip, inbufptr);
					inbufptr = 0;
				}
			}
			if (write(outfd, uip.inbuf, inbufptr) != inbufptr) {
				err(1, "serial_to_tun: write");
			}
			inbufptr = 0;
		}
		break;

	case SLIP_ESC:
		ret = recv(sockfd, &c, 1, 0);
		//~ if(fread(&c, 1, 1, inslip) != 1) {
		if (ret != 1) {
			old_c = SLIP_ESC;
			//~ clearerr(inslip);
			/* Put ESC back and give up! */
			//~ ungetc(SLIP_ESC, inslip);
			return;
		}

		switch (c) {
		case SLIP_ESC_END:
			c = SLIP_END;
			break;
		case SLIP_ESC_ESC:
			c = SLIP_ESC;
			break;
		}
		/* FALLTHROUGH */
	default:
		uip.inbuf[inbufptr++] = c;
		break;
	}

	goto read_more;
}
示例#15
0
文件: tunslip6.c 项目: ant9000/RIOT
void
ifconf(const char *tundev, const char *ipaddr)
{
#ifdef __linux__

    if (timestamp) {
        stamptime();
    }

    ssystem("ifconfig %s inet `hostname` up", tundev);

    if (timestamp) {
        stamptime();
    }

    ssystem("ifconfig %s add %s", tundev, ipaddr);

    /* radvd needs a link local address for routing */
#if 0
    /* fe80::1/64 is good enough */
    ssystem("ifconfig %s add fe80::1/64", tundev);
#elif 1
    /* Generate a link local address a la sixxs/aiccu */
    /* First a full parse, stripping off the prefix length */
    {
        char lladdr[40];
        char c, *ptr = (char *)ipaddr;
        uint16_t digit, ai, a[8], cc, scc, i;

        for (ai = 0; ai < 8; ai++) {
            a[ai] = 0;
        }

        ai = 0;
        cc = scc = 0;

        while ((c = *(ptr++))) {
            if (c == '/') {
                break;
            }

            if (c == ':') {
                if (cc) {
                    scc = ai;
                }

                cc = 1;

                if (++ai > 7) {
                    break;
                }
            }
            else {
                cc = 0;
                digit = c - '0';

                if (digit > 9) {
                    digit = 10 + (c & 0xdf) - 'A';
                }

                a[ai] = (a[ai] << 4) + digit;
            }
        }

        /* Get # elided and shift what's after to the end */
        cc = 8 - ai;

        for (i = 0; i < cc; i++) {
            if ((8 - i - cc) <= scc) {
                a[7 - i] = 0;
            }
            else {
                a[7 - i] = a[8 - i - cc];
                a[8 - i - cc] = 0;
            }
        }

        sprintf(lladdr, "fe80::%x:%x:%x:%x", a[1] & 0xfefd, a[2], a[3], a[7]);

        if (timestamp) {
            stamptime();
        }

        ssystem("ifconfig %s add %s/64", tundev, lladdr);
    }
#endif /* link local */
#elif defined(__APPLE__)
    {
        char *itfaddr = strdup(ipaddr);
        char *prefix = index(itfaddr, '/');

        if (prefix != NULL) {
            *prefix = '\0';
            prefix++;
        }
        else {
            prefix = "64";
        }

        if (timestamp) {
            stamptime();
        }

        ssystem("ifconfig %s inet6 up", tundev);

        if (timestamp) {
            stamptime();
        }

        ssystem("ifconfig %s inet6 %s add", tundev, ipaddr);

        if (timestamp) {
            stamptime();
        }

        ssystem("sysctl -w net.inet6.ip6.forwarding=1");
        free(itfaddr);
    }
#else

    if (timestamp) {
        stamptime();
    }

    ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);

    if (timestamp) {
        stamptime();
    }

    ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !__linux__ */

    if (timestamp) {
        stamptime();
    }

    ssystem("ifconfig %s\n", tundev);
}
示例#16
0
/*
 * Read from serial, when we have a packet write it to tun. No output
 * buffering, input buffered by stdio.
 */
void
serial_to_tun(FILE *inslip, int outfd)
{
  static union {
    unsigned char inbuf[2000];
  } uip;
  static int inbufptr = 0;

  int ret;
  unsigned char c;

#ifdef linux
  ret = fread(&c, 1, 1, inslip);
  if(ret == -1 || ret == 0) err(1, "serial_to_tun: read");
  goto after_fread;
#endif

 read_more:
  if(inbufptr >= sizeof(uip.inbuf)) {
     inbufptr = 0;
  }
  ret = fread(&c, 1, 1, inslip);
#ifdef linux
 after_fread:
#endif
  if(ret == -1) {
    err(1, "serial_to_tun: read");
  }
  if(ret == 0) {
    clearerr(inslip);
    return;
    fprintf(stderr, "serial_to_tun: EOF\n");
    exit(1);
  }
  /*  fprintf(stderr, ".");*/
  switch(c) {
  case SLIP_END:
    if(inbufptr > 0) {
      if(uip.inbuf[0] == '!') {
	if (uip.inbuf[1] == 'M') {
	  /* Read gateway MAC address and autoconfigure tap0 interface */
	  char macs[24];
	  int i, pos;
	  for(i = 0, pos = 0; i < 16; i++) {
	    macs[pos++] = uip.inbuf[2 + i];
	    if ((i & 1) == 1 && i < 14) {
	      macs[pos++] = ':';
	    }
	  }
	  macs[pos] = '\0';
	  printf("*** Gateway's MAC address: %s\n", macs);

	  ssystem("ifconfig %s down", tundev);
	  ssystem("ifconfig %s hw ether %s", tundev, &macs[6]);
	  ssystem("ifconfig %s up", tundev);
	}
#define DEBUG_LINE_MARKER '\r'
      } else if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
	fwrite(uip.inbuf + 1, inbufptr - 1, 1, stdout);
      } else if(is_sensible_string(uip.inbuf, inbufptr)) {
	fwrite(uip.inbuf, inbufptr, 1, stdout);
      } else {
	printf("Writing to tun  len: %d\n", inbufptr);
	/*	print_packet(uip.inbuf, inbufptr);*/
	if(write(outfd, uip.inbuf, inbufptr) != inbufptr) {
	  err(1, "serial_to_tun: write");
	}
      }
      inbufptr = 0;
    }
    break;

  case SLIP_ESC:
    if(fread(&c, 1, 1, inslip) != 1) {
      clearerr(inslip);
      /* Put ESC back and give up! */
      ungetc(SLIP_ESC, inslip);
      return;
    }

    switch(c) {
    case SLIP_ESC_END:
      c = SLIP_END;
      break;
    case SLIP_ESC_ESC:
      c = SLIP_ESC;
      break;
    }
    /* FALLTHROUGH */
  default:
    uip.inbuf[inbufptr++] = c;
    break;
  }

  goto read_more;
}
示例#17
0
int main(int argc, char **argv)
{
	atexit(cleanup);
	signal(SIGHUP, sigcleanup);
	signal(SIGTERM, sigcleanup);
	signal(SIGINT, sigcleanup);
	
	char* prog = argv[0];
	int c;
	while((c = getopt(argc, argv, "W::R::B::C::s::t::")) != -1) {
		switch(c) {
		case 'W':
		working_mode = WISEBED_MODE;
		break;

		case 'R':
		reservation_key = optarg;
		break;
	
		case 'B':
		border_router_node = optarg;
		break;
		
		case 'C':        
		config_path = optarg;    
		break;
		
		case 's':
		to_wisebed_max_size = atoi(optarg);
		break;

		case 't':
		if(strncmp("/dev/", optarg, 5) == 0) {
		strncpy(tundev, optarg + 5, sizeof(tundev));
		} else {
		strncpy(tundev, optarg, sizeof(tundev));
		}
		break;

		/*case 'v':
		verbose = 2;
		if (optarg) verbose = atoi(optarg);
		break;*/
	
		case '?':
		case 'h':
		default:
		fprintf(stderr,"usage: sudo %s -W -R[RESERVATION-KEY] -B[BORDER-ROUTER-URN] [TUN-IP]\n", prog);
		fprintf(stderr,"Options are:\n");
		fprintf(stderr," -W                     Enable usage with WISEBED\n");
		fprintf(stderr," -R  reservation_code   WISEBED: Reservation code\n");
		fprintf(stderr," -B  border_router_URN  WISEBED: URN of the border router node (eg:urn:wisebed:uzl1:0x2100)\n");
		fprintf(stderr," -C  config_file        WISEBED: Place of the config file (default: wisebed/live.properties)\n");
		fprintf(stderr," -s  size               WISEBED: max fragment length to WISEBED (default 499)\n");
		fprintf(stderr," -t  tundev             Name of the IPv6 interface (default tun0)\n");
		/*fprintf(stderr," -v[level]      Verbosity level\n");
		fprintf(stderr,"    -v0         No messages\n");
		fprintf(stderr,"    -v1         Encapsulated SLIP debug messages (default)\n");
		fprintf(stderr,"    -v2         Printable strings after they are received\n");
		fprintf(stderr,"    -v3         Printable strings and SLIP packet notifications\n");
		fprintf(stderr,"    -v4         All printable characters as they are received\n");
		fprintf(stderr,"    -v5         All SLIP packets in hex\n");*/
		exit(1);
		break;
		}
	}
	argc -= (optind - 1);
	argv += (optind - 1);
	
	//Parameter check
	if(working_mode == 0)
	{
		perror( "No working mode selected!" );
		exit(EXIT_FAILURE);
	}
	if(working_mode == WISEBED_MODE)
	{
		if( reservation_key == NULL )
		{
			perror( "No reservation key!" );
			exit(EXIT_FAILURE);
		}
		
		if( border_router_node == NULL )
		{
			perror( "No border router!" );
			exit(EXIT_FAILURE);
		}
	}
	else
	{
		perror( "Only the WISEBED mode is supported at the moment" );
		exit(EXIT_FAILURE);
	}
	
	if(argv[1] == NULL)
	{
		perror( "No IPv6 address!" );
		exit(EXIT_FAILURE);
	}
	
	//copy the IPv6 address
	ipaddr = argv[1];
	
	//Default values
	if( config_path == NULL )
	{
		config_path = (char*) malloc( 24 );
		strcpy(config_path, "wisebed/live.properties");
	}
	
	if( tundev == NULL )
	{
		strcpy(tundev, "tun0");
	}
	
// 	    printf("things: %s %s %s %s %i", reservation_key, border_router_node, config_path, ipaddr, to_wisebed_max_size );

	//For IO handling
	fd_set rset;
	//struct timeval timeout;
	int wisebed_listening_fd = 0;
	
	printf( "WISEBED Tunslip6 program\n" );
	
	//---------------------------- Create and open WISEBED pipe -----------------------------
	
	//****** LISTEN ********
	//Create the listening pipe WISEBED java --> pipe
	if( access(wisebed_listening_pipe, F_OK ) != 0 )
		if( mkfifo(wisebed_listening_pipe, 0666) < 0 )
		{
			perror( "L Pipe creation error (/tmp/wisebed_listening_pipe)" );
			exit(EXIT_FAILURE);
		}
		
	//Open the pipe
	wisebed_listening_fd = open( wisebed_listening_pipe, O_RDONLY | O_NONBLOCK );        
	if( wisebed_listening_fd < 1 )
	{
		perror( "L Pipe opening error" );
		exit(EXIT_FAILURE);
	}
	
	//Convert filedescriptor to FILE because of fread
	wisebed_listening_file = fdopen(wisebed_listening_fd, "r");
	if( wisebed_listening_file == NULL )
	{
		perror( "L Pipe opening error" );
		exit(EXIT_FAILURE);
	}
	
	//******* SEND **********
	//Create the sending pipe
	if( access(wisebed_sending_pipe, F_OK ) != 0 )
		if( mkfifo(wisebed_sending_pipe, 0666) < 0 )
		{
			perror( "S Pipe creation error (/tmp/wisebed_sending_pipe)" );
			exit(EXIT_FAILURE);
		}
	
	//NOTE: The file is opened only when there is something to write
	
	//Start the Wisebed send script
	ssystem( "java -Dtestbed.secretreservationkeys=%s -Dtestbed.listtype=%s -Dtestbed.nodeurns=%s -Dtestbed.max_size=%i -jar wisebed/lib/tr.scripting-client-0.8-onejar.jar -p %s -f %s &", reservation_key, list_type, border_router_node, to_wisebed_max_size, config_path, send_java_location );
	
	//Wait 3 seconds to establish the connection
	sleep(3);    
	
	//Start the Wisebed listen script
	ssystem( "java -Dtestbed.secretreservationkeys=%s -Dtestbed.listtype=%s -Dtestbed.nodeurns=%s -jar wisebed/lib/tr.scripting-client-0.8-onejar.jar -p %s -f %s &", reservation_key, list_type, border_router_node, config_path, listen_java_location );
	
	//Wait 3 seconds to establish the connection
	sleep(3);

	//---------------------------- Create and open TUN interface -----------------------------
		
	//Create and open the tunnel file
	tunnel_fd = tun_alloc( tundev );
	if(tunnel_fd == -1) 
	{
		perror(  "Tunnel opening error" );
		exit(EXIT_FAILURE);
	}
	
	printf( "Opened tunnel device ''/dev/%s''\n", tundev );
	
	//Configure the interface
	ifconf_tun( tundev, ipaddr );
	
	//---------------------------- Loop I/O handling with FD_* macros -----------------------------
	while(1)
	{
		//This is for the select command
		int maxfd = 0;
		
		//Reset the sets
		FD_ZERO(&rset);
		
		//Set the rset for the listening pipe
		FD_SET(wisebed_listening_fd, &rset);
		if(wisebed_listening_fd > maxfd) maxfd = wisebed_listening_fd;
		//Set the rset for the tunnel
		FD_SET(tunnel_fd, &rset);
		if(tunnel_fd > maxfd) maxfd = tunnel_fd;
		
		/* Initialize the timeout data structure. */
		struct timeval timeout;
		timeout.tv_sec = 120;
		timeout.tv_usec = 0;
		
		//Wait here, until one of the files are ready or the timer expires
		int ret = select(maxfd + 1, &rset, NULL, NULL, &timeout);
		
		if( ret < 0 )
		{
			perror( "I/O handling (select) failed" );
			exit(EXIT_FAILURE);
		}
		
		//Timeout
		else if( ret == 0 )
		{
			//Send a ping to the border router to keep the connection alive
			//after 2 minutes if there wasn't any activity
			if( working_mode == WISEBED_MODE )
			{
				time_t actual_time;
				time (&actual_time);
				if(  difftime(actual_time,last_listen_time) > 119 || difftime(actual_time,last_send_time) > 119 )
				{
					printf("Keepalive ping: ");
					
					char strR[INET6_ADDRSTRLEN];
					if( inet_ntop(AF_INET6, global_address_buffer, strR, INET6_ADDRSTRLEN) != NULL )
						ssystem( "ping6 -c 1 -s 0 %s > /dev/null &", strR );
					else
						printf(" address parsing failed, is there a border router?\n" );
				}
			}
		}
		else
		{
			//Test wisebed listening pipe
			if(FD_ISSET(wisebed_listening_fd, &rset)) 
			{
				//Read from the pipe, write to the tun interface
				pipe_to_tun( wisebed_listening_file, tunnel_fd );
			}
			
			//Tunnel
			if(FD_ISSET(tunnel_fd, &rset)) 
			{
				//Read from the ipv6 tunnel and call the java send
				tun_to_buffer();
			}
		}
	}
}