Merge branch 'master' of ssh://contiki.git.sourceforge.net/gitroot/contiki/contiki

This commit is contained in:
Joakim Eriksson 2012-02-21 13:21:19 +01:00
commit d44396f883
2 changed files with 98 additions and 10 deletions

View file

@ -52,6 +52,8 @@ const char *slip_config_ipaddr;
int slip_config_flowcontrol = 0;
int slip_config_timestamp = 0;
const char *slip_config_siodev = NULL;
const char *slip_config_host = NULL;
const char *slip_config_port = NULL;
char slip_config_tundev[32] = { "" };
uint16_t slip_config_basedelay = 0;
@ -101,6 +103,14 @@ slip_config_handle_arguments(int argc, char **argv)
}
break;
case 'a':
slip_config_host = optarg;
break;
case 'p':
slip_config_port = optarg;
break;
case 'd':
slip_config_basedelay = 10;
if(optarg) slip_config_basedelay = atoi(optarg);
@ -121,6 +131,8 @@ fprintf(stderr," -B baudrate 9600,19200,38400,57600,115200,921600 (default 11
fprintf(stderr," -H Hardware CTS/RTS flow control (default disabled)\n");
fprintf(stderr," -L Log output format (adds time stamps)\n");
fprintf(stderr," -s siodev Serial device (default /dev/ttyUSB0)\n");
fprintf(stderr," -a host Connect via TCP to server at <host>\n");
fprintf(stderr," -p port Connect via TCP to server at <host>:<port>\n");
fprintf(stderr," -t tundev Name of interface (default tun0)\n");
fprintf(stderr," -v[level] Verbosity level\n");
fprintf(stderr," -v0 No messages\n");

View file

@ -43,22 +43,24 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <err.h>
#include "net/netstack.h"
#include "net/packetbuf.h"
#include "cmd.h"
extern int slip_config_verbose;
extern const char *slip_config_ipaddr;
extern int slip_config_flowcontrol;
extern const char *slip_config_siodev;
extern const char *slip_config_host;
extern const char *slip_config_port;
extern uint16_t slip_config_basedelay;
extern speed_t slip_config_b_rate;
@ -82,6 +84,62 @@ void slip_send(int fd, unsigned char c);
#define SLIP_ESC_END 0334
#define SLIP_ESC_ESC 0335
/*---------------------------------------------------------------------------*/
static void *
get_in_addr(struct sockaddr *sa)
{
if(sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
/*---------------------------------------------------------------------------*/
static int
connect_to_server(const char *host, const char *port)
{
/* Setup TCP connection */
struct addrinfo hints, *servinfo, *p;
char s[INET6_ADDRSTRLEN];
int rv, fd;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
err(1, "getaddrinfo: %s", gai_strerror(rv));
return -1;
}
/* loop through all the results and connect to the first we can */
for(p = servinfo; p != NULL; p = p->ai_next) {
if((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if(connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
close(fd);
perror("client: connect");
continue;
}
break;
}
if(p == NULL) {
err(1, "can't connect to ``%s:%s''", host, port);
return -1;
}
fcntl(fd, F_SETFL, O_NONBLOCK);
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof(s));
/* all done with this structure */
freeaddrinfo(servinfo);
return fd;
}
/*---------------------------------------------------------------------------*/
int
is_sensible_string(const unsigned char *s, int len)
@ -398,11 +456,21 @@ slip_init(void)
{
setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
if(slip_config_siodev != NULL) {
if(slip_config_host != NULL) {
if(slip_config_port == NULL) {
slip_config_port = "60001";
}
slipfd = connect_to_server(slip_config_siodev, slip_config_port);
if(slipfd == -1) {
err(1, "can't connect to ``%s:%s''", slip_config_host, slip_config_port);
}
} else if(slip_config_siodev != NULL) {
slipfd = devopen(slip_config_siodev, O_RDWR | O_NONBLOCK);
if(slipfd == -1) {
err(1, "can't open siodev ``/dev/%s''", slip_config_siodev);
}
} else {
static const char *siodevs[] = {
"ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */
@ -422,10 +490,18 @@ slip_init(void)
select_set_callback(slipfd, &slip_callback);
if(slip_config_host != NULL) {
fprintf(stderr, "********SLIP opened to ``%s:%s''\n", slip_config_host,
slip_config_port);
} else {
fprintf(stderr, "********SLIP started on ``/dev/%s''\n", slip_config_siodev);
stty_telos(slipfd);
}
slip_send(slipfd, SLIP_END);
inslip = fdopen(slipfd, "r");
if(inslip == NULL) err(1, "main: fdopen");
if(inslip == NULL) {
err(1, "main: fdopen");
}
}
/*---------------------------------------------------------------------------*/