octopus port to ipv6

This commit is contained in:
Neale Pickett 2011-09-21 16:19:00 -06:00
parent 5c870b3c6f
commit 64b2316860
5 changed files with 32 additions and 53 deletions

View File

@ -5,8 +5,6 @@ octopus-install: octopus-build
$(call COPYTREE, packages/octopus/service, $(OCTOPUS_PKGDIR)/service) $(call COPYTREE, packages/octopus/service, $(OCTOPUS_PKGDIR)/service)
$(call COPYTREE, packages/octopus/tokens, $(OCTOPUS_PKGDIR)/tokens)
cp packages/octopus/src/octopus $(OCTOPUS_PKGDIR)/bin/ cp packages/octopus/src/octopus $(OCTOPUS_PKGDIR)/bin/
octopus-clean: octopus-clean:

View File

@ -1 +1 @@
10.0.0.8/24 fd84:b410:3441:1663::8888/48

View File

@ -3,4 +3,5 @@
exec 2>&1 exec 2>&1
IP=$(cat ip.txt) IP=$(cat ip.txt)
ip addr add $IP label eth0:octopus dev eth0 ip addr add $IP label eth0:octopus dev eth0
ip monitor | grep -q $IP
exec /opt/octopus/bin/octopus ${IP%/*} exec /opt/octopus/bin/octopus ${IP%/*}

View File

@ -1,16 +1,16 @@
#! /bin/sh #! /bin/sh
port=8888 port=8888
host=${1:-10.0.0.8} host=${1:-[::1]}
blooper=$(tempfile) blooper=$(tempfile)
trap "rm $blooper" 0 trap "rm $blooper" 0
echo foo | socat -t 0.01 STDIO UDP:$host:$port | tail -n +5 > $blooper echo foo | socat -t 0.01 STDIO UDP6:$host:$port | tail -n +5 > $blooper
for i in $(seq 8); do for i in $(seq 8); do
result=$(socat -t 0.01 STDIO UDP:$host:$port < $blooper | awk -F': ' '(NF > 1) {print $2; exit;}') result=$(socat -t 0.01 STDIO UDP6:$host:$port < $blooper | awk -F': ' '(NF > 1) {print $2; exit;}')
port=$(echo "ibase=8; $result" | bc) port=$(printf "%d" "0$result")
echo "next port: $port ($result)" echo "next port: $port ($result)"
done done
echo $result echo $result

View File

@ -19,13 +19,8 @@
#define max(a,b) (((a)>(b))?(a):(b)) #define max(a,b) (((a)>(b))?(a):(b))
#endif #endif
char token[80]; const char token[] = "octopus:xylep-radar-nanox";
size_t tokenlen; const size_t tokenlen = sizeof(token) - 1;
uint8_t const key[] = {0x99, 0x5f, 0xcb, 0xde,
0xf9, 0x6d, 0x02, 0xf3,
0x47, 0x60, 0x0a, 0xe0,
0x0a, 0x25, 0x4d, 0x16};
char const octopus[] = char const octopus[] =
(" ___\n" (" ___\n"
@ -197,17 +192,17 @@ struct bound_port {
} bound_ports[PORTS]; } bound_ports[PORTS];
int int
bind_port(struct in_addr *addr, int fd, uint16_t port) { bind_port(struct in6_addr *addr, int fd, uint16_t port) {
struct sockaddr_in saddr; struct sockaddr_in6 saddr = {0};
saddr.sin_family = AF_INET; saddr.sin6_family = AF_INET6;
saddr.sin_port = htons(port); saddr.sin6_port = htons(port);
memcpy(&saddr.sin_addr.s_addr, addr, sizeof(struct in_addr)); memcpy(&saddr.sin6_addr, addr, sizeof *addr);
return bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); return bind(fd, (struct sockaddr *)&saddr, sizeof saddr);
} }
int int
rebind(struct in_addr *addr) rebind(struct in6_addr *addr)
{ {
static int offset = 0; static int offset = 0;
int i; int i;
@ -226,7 +221,7 @@ rebind(struct in_addr *addr)
} }
/* Bind to a port */ /* Bind to a port */
bound_ports[i + offset].fd = socket(PF_INET, SOCK_DGRAM, 0); bound_ports[i + offset].fd = socket(AF_INET6, SOCK_DGRAM, 0);
do { do {
port = (random() % 56635) + 10000; port = (random() % 56635) + 10000;
ret = bind_port(addr, bound_ports[i + offset].fd, port); ret = bind_port(addr, bound_ports[i + offset].fd, port);
@ -266,14 +261,14 @@ rebind(struct in_addr *addr)
void void
do_io(int which) do_io(int which)
{ {
struct bound_port *bp = &bound_ports[which]; struct bound_port *bp = &bound_ports[which];
char input[INPUT_MAX]; char input[INPUT_MAX];
ssize_t inlen; ssize_t inlen;
struct sockaddr from; struct sockaddr_in6 from;
socklen_t fromlen = sizeof(from); socklen_t fromlen = sizeof(from);
inlen = recvfrom(bp->fd, input, INPUT_MAX, 0, inlen = recvfrom(bp->fd, input, INPUT_MAX, 0,
&from, &fromlen); (struct sockaddr *)&from, &fromlen);
if (-1 == inlen) { if (-1 == inlen) {
/* Well don't that just beat all. */ /* Well don't that just beat all. */
return; return;
@ -299,10 +294,6 @@ loop()
int i; int i;
int nfds = 0; int nfds = 0;
fd_set rfds; fd_set rfds;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
FD_ZERO(&rfds); FD_ZERO(&rfds);
for (i = 0; i < PORTS; i += 1) { for (i = 0; i < PORTS; i += 1) {
@ -310,7 +301,7 @@ loop()
FD_SET(bound_ports[i].fd, &rfds); FD_SET(bound_ports[i].fd, &rfds);
} }
while (-1 == select(nfds+1, &rfds, NULL, NULL, &timeout)) { while (-1 == select(nfds+1, &rfds, NULL, NULL, NULL)) {
if (EINTR == errno) { if (EINTR == errno) {
continue; continue;
} }
@ -329,25 +320,25 @@ loop()
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int ret; int ret;
int i; int i;
time_t last_bind = 0; time_t last_bind = 0;
time_t last_token = 0; time_t last_token = 0;
struct in_addr addr; struct in6_addr addr;
/* The random seed isn't super important here. */ /* The random seed isn't super important here. */
srand(time(NULL)); srand(time(NULL));
if (argc > 1) { if (argc > 1) {
if (-1 == inet_aton(argv[1], &addr)) { if (0 >= inet_pton(AF_INET6, argv[1], &addr)) {
fprintf(stderr, "invalid address: %s\n", argv[1]); fprintf(stderr, "invalid address: %s\n", argv[1]);
return EX_IOERR; return EX_IOERR;
} }
} else { } else {
addr.s_addr = INADDR_ANY; memcpy(&addr, &in6addr_any, sizeof addr);
} }
bound_ports[0].fd = socket(PF_INET, SOCK_DGRAM, 0); bound_ports[0].fd = socket(AF_INET6, SOCK_DGRAM, 0);
ret = bind_port(&addr, bound_ports[0].fd, 8888); ret = bind_port(&addr, bound_ports[0].fd, 8888);
if (-1 == ret) { if (-1 == ret) {
perror("bind port 8888"); perror("bind port 8888");
@ -361,17 +352,6 @@ main(int argc, char *argv[])
do { do {
time_t now = time(NULL); time_t now = time(NULL);
if (last_token + 60 < now) {
last_token = now;
while (NULL == fgets(token, sizeof(token), stdin)) {
if (-1 == fseek(stdin, 0, SEEK_SET)) {
/* Non-seekable stdin: we're done. */
return 0;
}
}
for (tokenlen = 0; token[tokenlen] && (token[tokenlen] != '\n'); tokenlen += 1);
}
if (last_bind + 4 < now) { if (last_bind + 4 < now) {
last_bind = now; last_bind = now;
if (-1 == rebind(&addr)) break; if (-1 == rebind(&addr)) break;