2011-12-21 09:55:05 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-06-15 05:34:36 +00:00
|
|
|
#include <assert.h>
|
2012-05-10 04:37:11 +00:00
|
|
|
#include <time.h>
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-06-28 08:04:21 +00:00
|
|
|
#include "strbuf.h"
|
2012-08-22 00:51:38 +00:00
|
|
|
#include "overlay_buffer.h"
|
2012-08-27 00:34:59 +00:00
|
|
|
#include "overlay_packet.h"
|
2011-08-08 06:41:05 +00:00
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-08 06:41:05 +00:00
|
|
|
int overlay_ready=0;
|
|
|
|
int overlay_interface_count=0;
|
|
|
|
overlay_interface overlay_interfaces[OVERLAY_MAX_INTERFACES];
|
2012-01-08 17:49:52 +00:00
|
|
|
int overlay_last_interface_number=-1;
|
2011-08-08 06:41:05 +00:00
|
|
|
|
2011-08-12 07:47:29 +00:00
|
|
|
struct interface_rules {
|
|
|
|
char *namespec;
|
|
|
|
unsigned long long speed_in_bits;
|
|
|
|
int port;
|
|
|
|
char type;
|
|
|
|
char excludeP;
|
|
|
|
struct interface_rules *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct interface_rules *interface_filter=NULL;
|
|
|
|
|
2012-07-02 06:34:00 +00:00
|
|
|
struct profile_total interface_poll_stats;
|
|
|
|
struct profile_total dummy_poll_stats;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
struct sched_ent sock_any;
|
2012-08-24 05:51:23 +00:00
|
|
|
struct sockaddr_in sock_any_addr;
|
2012-08-08 05:27:27 +00:00
|
|
|
struct profile_total sock_any_stats;
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
struct outgoing_packet{
|
|
|
|
overlay_interface *interface;
|
|
|
|
int i;
|
2012-09-10 01:25:12 +00:00
|
|
|
int unicast;
|
2012-08-27 00:34:59 +00:00
|
|
|
struct sockaddr_in dest;
|
2012-08-22 00:51:38 +00:00
|
|
|
struct overlay_buffer *buffer;
|
2012-07-12 01:06:41 +00:00
|
|
|
};
|
|
|
|
|
2012-07-12 01:11:47 +00:00
|
|
|
struct sched_ent next_packet;
|
|
|
|
struct profile_total send_packet;
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int overlay_tick_interface(int i, time_ms_t now);
|
|
|
|
static void overlay_interface_poll(struct sched_ent *alarm);
|
|
|
|
static void logServalPacket(int level, struct __sourceloc where, const char *message, const unsigned char *packet, size_t len);
|
|
|
|
static long long parse_quantity(char *q);
|
2012-07-06 00:28:54 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
unsigned char magic_header[]={/* Magic */ 'O',0x10,
|
|
|
|
/* Version */ 0x00,0x01};
|
|
|
|
|
2011-08-20 09:36:15 +00:00
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int overlay_interface_type(char *s)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
|
|
|
if (!strcasecmp(s,"ethernet")) return OVERLAY_INTERFACE_ETHERNET;
|
|
|
|
if (!strcasecmp(s,"wifi")) return OVERLAY_INTERFACE_WIFI;
|
|
|
|
if (!strcasecmp(s,"other")) return OVERLAY_INTERFACE_UNKNOWN;
|
|
|
|
if (!strcasecmp(s,"catear")) return OVERLAY_INTERFACE_PACKETRADIO;
|
|
|
|
return WHY("Invalid interface type -- consider using 'wifi','ethernet' or 'other'");
|
|
|
|
}
|
|
|
|
|
|
|
|
int overlay_interface_arg(char *arg)
|
|
|
|
{
|
|
|
|
/* Parse an interface argument, of the form:
|
|
|
|
|
2011-08-12 07:47:29 +00:00
|
|
|
<+|->[interfacename][=type]
|
|
|
|
|
|
|
|
+interface tells DNA to sit on that interface
|
|
|
|
-interface tells DNA to not sit on that interface
|
|
|
|
+/- without an interface tells DNA to sit on all interfaces.
|
|
|
|
|
|
|
|
The first match rules, so -en0+ tells DNA to use all interfaces, excepting en0
|
|
|
|
|
|
|
|
The optional =type specifier tells DNA how to handle the interface in terms of
|
|
|
|
bandwidth:distance relationship for calculating tick times etc.
|
|
|
|
|
|
|
|
The special type =custom allows full specification:
|
|
|
|
|
|
|
|
XXX - Settle the custom specification now that we have changed the interface
|
|
|
|
management.
|
2011-08-08 06:41:05 +00:00
|
|
|
*/
|
2011-08-12 07:47:29 +00:00
|
|
|
|
|
|
|
char sign[80]="+";
|
|
|
|
char interface_name[80]="";
|
|
|
|
char speed[80]="1m";
|
2011-08-08 06:41:05 +00:00
|
|
|
char typestring[80]="wifi";
|
|
|
|
int port=PORT_DNA;
|
|
|
|
int type=OVERLAY_INTERFACE_UNKNOWN;
|
|
|
|
int n=0;
|
|
|
|
|
|
|
|
/* Too long */
|
|
|
|
if (strlen(arg)>79) return WHY("interface specification was >79 characters");
|
|
|
|
|
2011-08-12 07:47:29 +00:00
|
|
|
struct interface_rules *r=calloc(sizeof(struct interface_rules),1);
|
|
|
|
if (!r) return WHY("calloc(struct interface rules),1) failed");
|
|
|
|
|
|
|
|
|
2012-06-21 04:28:10 +00:00
|
|
|
if (sscanf(arg,"%[+-]%n%[^=:,]%n=%[^:]%n:%d%n:%[^:]%n",
|
2011-08-12 07:47:29 +00:00
|
|
|
sign,&n,interface_name,&n,typestring,&n,&port,&n,speed,&n)>=1)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
2011-08-12 07:47:29 +00:00
|
|
|
if (n<strlen(arg)) { free(r); return WHY("Extra junk at end of interface specification"); }
|
|
|
|
|
|
|
|
if (strlen(sign)>1) { free(r); return WHY("Sign must be + or -"); }
|
|
|
|
switch(sign[0])
|
|
|
|
{
|
|
|
|
case '+': break;
|
|
|
|
case '-': r->excludeP=1; break;
|
|
|
|
default:
|
|
|
|
free(r);
|
|
|
|
return WHY("Invalid interface list item: Must begin with + or -");
|
|
|
|
}
|
|
|
|
|
|
|
|
long long speed_in_bits=parse_quantity(speed);
|
2011-08-08 06:41:05 +00:00
|
|
|
if (speed_in_bits<=1) {
|
2011-08-12 07:47:29 +00:00
|
|
|
free(r);
|
2011-08-08 06:41:05 +00:00
|
|
|
return WHY("Interfaces must be capable of at least 1 bit per second");
|
|
|
|
}
|
|
|
|
if (n<strlen(arg)) return WHY("Extra stuff at end of interface specification");
|
|
|
|
|
|
|
|
type=overlay_interface_type(typestring);
|
2011-08-12 07:47:29 +00:00
|
|
|
if (type<0) { free(r); return WHY("Invalid interface type in specification"); }
|
|
|
|
|
|
|
|
/* Okay, register the interface preference */
|
|
|
|
r->namespec=strdup(interface_name);
|
|
|
|
r->speed_in_bits=speed_in_bits;
|
|
|
|
r->port=port;
|
|
|
|
r->type=type;
|
|
|
|
|
|
|
|
r->next=interface_filter;
|
|
|
|
interface_filter=r;
|
|
|
|
|
|
|
|
return 0;
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
2011-08-12 07:47:29 +00:00
|
|
|
else { free(r); return WHY("Bad interface specification"); }
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 04:19:38 +00:00
|
|
|
int overlay_interface_args(const char *arg)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
|
|
|
/* Parse series of comma-separated interface definitions from a single argument
|
|
|
|
*/
|
|
|
|
int i=0;
|
|
|
|
char interface[80];
|
|
|
|
int len=0;
|
|
|
|
|
|
|
|
for(i=0;arg[i];i++)
|
|
|
|
{
|
2012-03-21 21:56:19 +00:00
|
|
|
if (arg[i]==','||arg[i]=='\n') {
|
2011-08-08 06:41:05 +00:00
|
|
|
interface[len]=0;
|
|
|
|
if (overlay_interface_arg(interface)) return WHY("Could not add interface");
|
|
|
|
len=0;
|
|
|
|
} else {
|
|
|
|
if (len<79) {
|
|
|
|
interface[len++]=arg[i];
|
|
|
|
interface[len]=0;
|
|
|
|
} else
|
|
|
|
return WHY("Interface definition is too long (each must be <80 characters)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len) if (overlay_interface_arg(interface)) return WHY("Could not add final interface");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static void
|
|
|
|
overlay_interface_close(overlay_interface *interface){
|
2012-08-08 05:27:27 +00:00
|
|
|
if (interface->fileP){
|
|
|
|
INFOF("Interface %s is down", interface->name);
|
|
|
|
}else{
|
|
|
|
INFOF("Interface %s addr %s is down", interface->name, inet_ntoa(interface->broadcast_address.sin_addr));
|
|
|
|
}
|
2012-07-25 07:23:44 +00:00
|
|
|
unschedule(&interface->alarm);
|
|
|
|
unwatch(&interface->alarm);
|
|
|
|
close(interface->alarm.poll.fd);
|
|
|
|
interface->alarm.poll.fd=-1;
|
|
|
|
interface->state=INTERFACE_STATE_DOWN;
|
|
|
|
}
|
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
// create a socket with options common to all our UDP sockets
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
|
|
|
overlay_bind_socket(const struct sockaddr *addr, size_t addr_size, char *interface_name){
|
2012-08-08 05:27:27 +00:00
|
|
|
int fd;
|
|
|
|
int reuseP = 1;
|
|
|
|
int broadcastP = 1;
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
fd = socket(PF_INET,SOCK_DGRAM,0);
|
|
|
|
if (fd < 0) {
|
|
|
|
WHY_perror("Error creating socket");
|
|
|
|
return -1;
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
2012-08-08 05:27:27 +00:00
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseP, sizeof(reuseP)) < 0) {
|
2012-07-25 07:23:44 +00:00
|
|
|
WHY_perror("setsockopt(SO_REUSEADR)");
|
|
|
|
goto error;
|
|
|
|
}
|
2012-08-08 05:27:27 +00:00
|
|
|
|
|
|
|
#ifdef SO_REUSEPORT
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuseP, sizeof(reuseP)) < 0) {
|
|
|
|
WHY_perror("setsockopt(SO_REUSEPORT)");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcastP, sizeof(broadcastP)) < 0) {
|
2012-07-25 07:23:44 +00:00
|
|
|
WHY_perror("setsockopt(SO_BROADCAST)");
|
2012-06-08 08:59:27 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2012-08-08 05:27:27 +00:00
|
|
|
|
2012-02-23 01:27:46 +00:00
|
|
|
/* Automatically close socket on calls to exec().
|
2012-08-08 05:27:27 +00:00
|
|
|
This makes life easier when we restart with an exec after receiving
|
|
|
|
a bad signal. */
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, NULL) | O_CLOEXEC);
|
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
#ifdef SO_BINDTODEVICE
|
|
|
|
/*
|
|
|
|
Limit incoming and outgoing packets to this interface, no matter what the routing table says.
|
|
|
|
This should allow for a device with multiple interfaces on the same subnet.
|
2012-08-08 05:27:27 +00:00
|
|
|
Don't abort if this fails, I believe it requires root, just log it.
|
2012-07-25 07:23:44 +00:00
|
|
|
*/
|
2012-08-08 05:27:27 +00:00
|
|
|
if (interface_name && setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, interface_name, strlen(interface_name)+1) < 0) {
|
2012-07-25 07:23:44 +00:00
|
|
|
WHY_perror("setsockopt(SO_BINDTODEVICE)");
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-08 05:27:27 +00:00
|
|
|
|
|
|
|
if (bind(fd, addr, addr_size)) {
|
|
|
|
WHY_perror("Bind failed");
|
2012-06-08 08:59:27 +00:00
|
|
|
goto error;
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
2012-08-08 05:27:27 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
error:
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
overlay_interface * overlay_interface_find(struct in_addr addr){
|
|
|
|
int i;
|
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state!=INTERFACE_STATE_UP)
|
|
|
|
continue;
|
|
|
|
if ((overlay_interfaces[i].netmask.s_addr & addr.s_addr) == (overlay_interfaces[i].netmask.s_addr & overlay_interfaces[i].address.sin_addr.s_addr)){
|
|
|
|
return &overlay_interfaces[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-14 02:20:45 +00:00
|
|
|
overlay_interface * overlay_interface_find_name(const char *name){
|
|
|
|
int i;
|
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state!=INTERFACE_STATE_UP)
|
|
|
|
continue;
|
|
|
|
if (strcasecmp(name, overlay_interfaces[i].name)==0){
|
|
|
|
return &overlay_interfaces[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
// OSX doesn't recieve broadcast packets on sockets bound to an interface's address
|
|
|
|
// So we have to bind a socket to INADDR_ANY to receive these packets.
|
2012-08-31 02:41:31 +00:00
|
|
|
static void
|
|
|
|
overlay_interface_read_any(struct sched_ent *alarm){
|
2012-08-22 05:20:14 +00:00
|
|
|
if (alarm->poll.revents & POLLIN) {
|
|
|
|
int plen=0;
|
|
|
|
int recvttl=1;
|
|
|
|
unsigned char packet[16384];
|
|
|
|
overlay_interface *interface=NULL;
|
|
|
|
struct sockaddr src_addr;
|
|
|
|
socklen_t addrlen = sizeof(src_addr);
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
/* Read only one UDP packet per call to share resources more fairly, and also
|
|
|
|
enable stats to accurately count packets received */
|
|
|
|
plen = recvwithttl(alarm->poll.fd, packet, sizeof(packet), &recvttl, &src_addr, &addrlen);
|
|
|
|
if (plen == -1) {
|
|
|
|
WHY_perror("recvwithttl(c)");
|
|
|
|
unwatch(alarm);
|
|
|
|
close(alarm->poll.fd);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
struct in_addr src = ((struct sockaddr_in *)&src_addr)->sin_addr;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
/* Try to identify the real interface that the packet arrived on */
|
2012-08-30 00:04:52 +00:00
|
|
|
interface = overlay_interface_find(src);
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
/* Drop the packet if we don't find a match */
|
2012-08-22 05:20:14 +00:00
|
|
|
if (!interface){
|
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES)
|
|
|
|
DEBUGF("Could not find matching interface for packet received from %s", inet_ntoa(src));
|
|
|
|
return;
|
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
/* We have a frame from this interface */
|
|
|
|
if (debug&DEBUG_PACKETRX)
|
|
|
|
DEBUG_packet_visualise("Read from real interface", packet,plen);
|
2012-09-10 01:25:12 +00:00
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES)
|
2012-09-10 01:01:01 +00:00
|
|
|
DEBUGF("Received %d bytes from %s on interface %s (ANY)",plen,
|
|
|
|
inet_ntoa(((struct sockaddr_in *)&src_addr)->sin_addr),
|
|
|
|
interface->name);
|
2012-08-22 05:20:14 +00:00
|
|
|
if (packetOk(interface,packet,plen,NULL,recvttl,&src_addr,addrlen,1)) {
|
|
|
|
WHY("Malformed packet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alarm->poll.revents & (POLLHUP | POLLERR)) {
|
|
|
|
INFO("Closing broadcast socket due to error");
|
|
|
|
unwatch(alarm);
|
|
|
|
close(alarm->poll.fd);
|
|
|
|
alarm->poll.fd=-1;
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-08-08 05:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// bind a socket to INADDR_ANY:port
|
|
|
|
// for now, we don't have a graceful close for this interface but it should go away when the process dies
|
2012-08-31 02:41:31 +00:00
|
|
|
static int overlay_interface_init_any(int port){
|
2012-08-08 05:27:27 +00:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
if (sock_any.poll.fd>0){
|
2012-08-24 05:51:23 +00:00
|
|
|
// Check the port number matches
|
2012-09-01 02:51:32 +00:00
|
|
|
if (sock_any_addr.sin_port != htons(port))
|
|
|
|
return WHYF("Unable to listen to broadcast packets for ports %d & %d", port, ntohs(sock_any_addr.sin_port));
|
2012-08-24 05:51:23 +00:00
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
2012-08-24 05:51:23 +00:00
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
sock_any.poll.fd = overlay_bind_socket((const struct sockaddr *)&addr, sizeof(addr), NULL);
|
|
|
|
if (sock_any.poll.fd<0)
|
|
|
|
return -1;
|
|
|
|
|
2012-08-24 05:51:23 +00:00
|
|
|
sock_any_addr = addr;
|
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
sock_any.poll.events=POLLIN;
|
|
|
|
sock_any.function = overlay_interface_read_any;
|
|
|
|
|
|
|
|
sock_any_stats.name="overlay_interface_read_any";
|
|
|
|
sock_any.stats=&sock_any_stats;
|
|
|
|
watch(&sock_any);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
2012-08-08 05:27:27 +00:00
|
|
|
overlay_interface_init_socket(int interface_index)
|
|
|
|
{
|
|
|
|
overlay_interface *const interface = &overlay_interfaces[interface_index];
|
|
|
|
interface->fileP = 0;
|
|
|
|
|
|
|
|
/*
|
2012-09-11 05:50:44 +00:00
|
|
|
On linux you can bind to the broadcast address to receive broadcast packets per interface [or subnet],
|
|
|
|
but then you can't receive unicast packets on the same socket.
|
|
|
|
|
|
|
|
On osx, you can only receive broadcast packets if you bind to INADDR_ANY.
|
|
|
|
|
|
|
|
So the most portable way to do this is to bind to each interface's IP address for sending broadcasts
|
|
|
|
and receiving unicasts, and bind a separate socket to INADDR_ANY just for receiving broadcast packets.
|
|
|
|
|
|
|
|
Sending packets from INADDR_ANY would probably work, but gives us less control over which interfaces are sending packets.
|
|
|
|
But there may be some platforms that need some other combination for everything to work.
|
2012-08-08 05:27:27 +00:00
|
|
|
*/
|
2012-09-11 05:50:44 +00:00
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
overlay_interface_init_any(interface->port);
|
2012-09-11 05:50:44 +00:00
|
|
|
|
|
|
|
const struct sockaddr *addr = (const struct sockaddr *)&interface->address;
|
2012-08-08 05:27:27 +00:00
|
|
|
|
|
|
|
interface->alarm.poll.fd = overlay_bind_socket(addr, sizeof(interface->broadcast_address), interface->name);
|
|
|
|
if (interface->alarm.poll.fd<0){
|
|
|
|
interface->state=INTERFACE_STATE_DOWN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug & (DEBUG_PACKETRX | DEBUG_IO)){
|
|
|
|
char srctxt[INET_ADDRSTRLEN];
|
|
|
|
if (inet_ntop(AF_INET, (const void *)&interface->broadcast_address.sin_addr, srctxt, INET_ADDRSTRLEN))
|
|
|
|
DEBUGF("Bound to %s:%d", srctxt, ntohs(interface->broadcast_address.sin_port));
|
2012-07-31 08:19:24 +00:00
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->alarm.poll.events=POLLIN;
|
|
|
|
interface->alarm.function = overlay_interface_poll;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
|
|
|
interface_poll_stats.name="overlay_interface_poll";
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->alarm.stats=&interface_poll_stats;
|
|
|
|
watch(&interface->alarm);
|
2012-07-06 00:28:54 +00:00
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
if (interface->tick_ms>0){
|
|
|
|
// run the first tick asap
|
|
|
|
interface->alarm.alarm=gettime_ms();
|
|
|
|
interface->alarm.deadline=interface->alarm.alarm+10;
|
|
|
|
schedule(&interface->alarm);
|
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->state=INTERFACE_STATE_UP;
|
2012-07-25 07:23:44 +00:00
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
INFOF("Interface %s addr %s, is up",interface->name, inet_ntoa(interface->broadcast_address.sin_addr));
|
2012-09-07 00:31:34 +00:00
|
|
|
|
|
|
|
// mark our sid to be sent in full
|
|
|
|
if (my_subscriber)
|
|
|
|
my_subscriber->send_full = 1;
|
2012-09-14 02:20:45 +00:00
|
|
|
|
|
|
|
// try to register ourselves with a directory service
|
|
|
|
directory_interface_up(interface);
|
|
|
|
|
2011-08-12 07:47:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
|
|
|
overlay_interface_init(char *name, struct in_addr src_addr, struct in_addr netmask,
|
|
|
|
struct in_addr broadcast,
|
|
|
|
int speed_in_bits, int port, int type)
|
2011-08-12 07:47:29 +00:00
|
|
|
{
|
|
|
|
/* Too many interfaces */
|
|
|
|
if (overlay_interface_count>=OVERLAY_MAX_INTERFACES) return WHY("Too many interfaces -- Increase OVERLAY_MAX_INTERFACES");
|
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
overlay_interface *const interface = &overlay_interfaces[overlay_interface_count];
|
2011-08-12 07:47:29 +00:00
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
strcpy(interface->name,name);
|
2011-08-12 07:47:29 +00:00
|
|
|
|
2011-08-09 04:45:24 +00:00
|
|
|
/* Pick a reasonable default MTU.
|
|
|
|
This will ultimately get tuned by the bandwidth and other properties of the interface */
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->mtu=1200;
|
|
|
|
interface->state=INTERFACE_STATE_DOWN;
|
|
|
|
interface->bits_per_second=speed_in_bits;
|
|
|
|
interface->port=port;
|
|
|
|
interface->type=type;
|
|
|
|
interface->last_tick_ms= -1; // not ticked yet
|
|
|
|
interface->alarm.poll.fd=0;
|
2012-08-30 00:04:52 +00:00
|
|
|
|
|
|
|
// how often do we announce ourselves on this interface?
|
2012-07-02 02:56:01 +00:00
|
|
|
switch (type) {
|
|
|
|
case OVERLAY_INTERFACE_PACKETRADIO:
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->tick_ms = confValueGetInt64Range("mdp.packetradio.tick_ms", 15000LL, 1LL, 3600000LL);
|
2012-07-02 02:56:01 +00:00
|
|
|
break;
|
|
|
|
case OVERLAY_INTERFACE_ETHERNET:
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->tick_ms = confValueGetInt64Range("mdp.ethernet.tick_ms", 500LL, 1LL, 3600000LL);
|
2012-07-02 02:56:01 +00:00
|
|
|
break;
|
|
|
|
case OVERLAY_INTERFACE_WIFI:
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->tick_ms = confValueGetInt64Range("mdp.wifi.tick_ms", 500LL, 1LL, 3600000LL);
|
2012-07-02 02:56:01 +00:00
|
|
|
break;
|
|
|
|
case OVERLAY_INTERFACE_UNKNOWN:
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->tick_ms = confValueGetInt64Range("mdp.unknown.tick_ms", 500LL, 1LL, 3600000LL);
|
2012-07-02 02:56:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return WHYF("Unsupported interface type %d", type);
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
|
2012-08-30 02:17:13 +00:00
|
|
|
// allow for a per interface override of tick interval
|
2012-08-30 00:04:52 +00:00
|
|
|
{
|
|
|
|
char option_name[64];
|
|
|
|
snprintf(option_name, sizeof(option_name), "mdp.%s.tick_ms", name);
|
|
|
|
interface->tick_ms = confValueGetInt64Range(option_name, interface->tick_ms, 1LL, 3600000LL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable announcements and other broadcasts if tick_ms=0.
|
|
|
|
if (interface->tick_ms>0)
|
|
|
|
interface->send_broadcasts=1;
|
|
|
|
else
|
|
|
|
interface->send_broadcasts=0;
|
|
|
|
|
2011-08-13 11:17:49 +00:00
|
|
|
if (name[0]=='>') {
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->fileP=1;
|
2012-03-19 05:36:34 +00:00
|
|
|
char dummyfile[1024];
|
2012-06-21 04:28:10 +00:00
|
|
|
if (name[1]=='/') {
|
|
|
|
/* Absolute path */
|
|
|
|
snprintf(dummyfile,1024,"%s",&name[1]);
|
|
|
|
} else
|
|
|
|
/* Relative to instance path */
|
2012-06-21 06:22:44 +00:00
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(dummyfile, &name[1]))
|
|
|
|
return WHY("could not form dummy interfance name");
|
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
if ((interface->alarm.poll.fd = open(dummyfile,O_APPEND|O_RDWR)) < 1) {
|
2012-06-21 06:22:44 +00:00
|
|
|
return WHY("could not open dummy interface file for append");
|
|
|
|
}
|
|
|
|
|
2011-08-13 11:17:49 +00:00
|
|
|
/* Seek to end of file as initial reading point */
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->recv_offset = lseek(interface->alarm.poll.fd,0,SEEK_END);
|
2011-08-13 11:17:49 +00:00
|
|
|
/* XXX later add pretend location information so that we can decide which "packets" to receive
|
|
|
|
based on closeness */
|
2012-07-02 03:49:54 +00:00
|
|
|
|
|
|
|
// schedule an alarm for this interface
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->alarm.function=overlay_dummy_poll;
|
|
|
|
interface->alarm.alarm=gettime_ms()+10;
|
|
|
|
interface->alarm.deadline=interface->alarm.alarm;
|
2012-07-02 05:50:30 +00:00
|
|
|
dummy_poll_stats.name="overlay_dummy_poll";
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->alarm.stats=&dummy_poll_stats;
|
|
|
|
schedule(&interface->alarm);
|
2012-07-25 07:23:44 +00:00
|
|
|
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->state=INTERFACE_STATE_UP;
|
|
|
|
INFOF("Dummy interface %s is up",interface->name);
|
2012-07-25 07:23:44 +00:00
|
|
|
|
2011-08-13 11:17:49 +00:00
|
|
|
} else {
|
2012-08-08 05:27:27 +00:00
|
|
|
|
|
|
|
interface->netmask = netmask;
|
|
|
|
|
|
|
|
interface->address.sin_addr = src_addr;
|
|
|
|
interface->address.sin_family = AF_INET;
|
|
|
|
interface->address.sin_port = htons(interface->port);
|
|
|
|
|
|
|
|
interface->broadcast_address.sin_addr = broadcast;
|
|
|
|
interface->broadcast_address.sin_family = AF_INET;
|
|
|
|
interface->broadcast_address.sin_port = htons(interface->port);
|
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
if (overlay_interface_init_socket(overlay_interface_count))
|
2011-08-13 11:17:49 +00:00
|
|
|
return WHY("overlay_interface_init_socket() failed");
|
|
|
|
}
|
2011-08-12 07:47:29 +00:00
|
|
|
|
2011-08-08 06:41:05 +00:00
|
|
|
overlay_interface_count++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static void overlay_interface_poll(struct sched_ent *alarm)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
2012-07-02 03:49:54 +00:00
|
|
|
struct overlay_interface *interface = (overlay_interface *)alarm;
|
2012-07-06 00:28:54 +00:00
|
|
|
|
|
|
|
if (alarm->poll.revents==0){
|
2012-07-25 07:23:44 +00:00
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
if (interface->state==INTERFACE_STATE_UP && interface->tick_ms>0){
|
2012-07-25 07:23:44 +00:00
|
|
|
// tick the interface
|
2012-08-09 02:44:32 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
2012-07-25 07:23:44 +00:00
|
|
|
int i = (interface - overlay_interfaces);
|
|
|
|
overlay_tick_interface(i, now);
|
|
|
|
alarm->alarm=now+interface->tick_ms;
|
|
|
|
alarm->deadline=alarm->alarm+interface->tick_ms/2;
|
|
|
|
schedule(alarm);
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:28:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
if (alarm->poll.revents & POLLIN) {
|
|
|
|
int plen=0;
|
|
|
|
unsigned char packet[16384];
|
|
|
|
|
|
|
|
struct sockaddr src_addr;
|
|
|
|
socklen_t addrlen = sizeof(src_addr);
|
|
|
|
|
|
|
|
|
|
|
|
/* Read only one UDP packet per call to share resources more fairly, and also
|
|
|
|
enable stats to accurately count packets received */
|
|
|
|
int recvttl=1;
|
|
|
|
plen = recvwithttl(alarm->poll.fd,packet, sizeof(packet), &recvttl, &src_addr, &addrlen);
|
|
|
|
if (plen == -1) {
|
|
|
|
WHY_perror("recvwithttl(c)");
|
|
|
|
overlay_interface_close(interface);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-27 00:34:59 +00:00
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
/* We have a frame from this interface */
|
|
|
|
if (debug&DEBUG_PACKETRX)
|
|
|
|
DEBUG_packet_visualise("Read from real interface", packet,plen);
|
2012-09-10 01:01:01 +00:00
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES)
|
|
|
|
DEBUGF("Received %d bytes from %s on interface %s",plen,
|
|
|
|
inet_ntoa(((struct sockaddr_in *)&src_addr)->sin_addr),
|
|
|
|
interface->name);
|
2012-08-22 05:20:14 +00:00
|
|
|
if (packetOk(interface,packet,plen,NULL,recvttl,&src_addr,addrlen,1)) {
|
|
|
|
WHY("Malformed packet");
|
|
|
|
// Do we really want to attempt to parse it again?
|
|
|
|
//DEBUG_packet_visualise("Malformed packet", packet,plen);
|
|
|
|
}
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 05:20:14 +00:00
|
|
|
if (alarm->poll.revents & (POLLHUP | POLLERR)) {
|
|
|
|
overlay_interface_close(interface);
|
2012-08-27 00:34:59 +00:00
|
|
|
}
|
2012-06-22 03:55:41 +00:00
|
|
|
}
|
2012-06-21 07:32:36 +00:00
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void overlay_dummy_poll(struct sched_ent *alarm)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
2012-07-02 03:49:54 +00:00
|
|
|
overlay_interface *interface = (overlay_interface *)alarm;
|
2011-08-08 06:41:05 +00:00
|
|
|
/* Grab packets, unpackage and dispatch frames to consumers */
|
|
|
|
/* XXX Okay, so how are we managing out-of-process consumers?
|
2012-06-21 07:32:36 +00:00
|
|
|
They need some way to register their interest in listening to a port.
|
|
|
|
*/
|
2012-07-31 08:19:24 +00:00
|
|
|
unsigned char packet[2048];
|
2011-08-12 19:05:11 +00:00
|
|
|
int plen=0;
|
2012-07-02 03:49:54 +00:00
|
|
|
struct sockaddr src_addr;
|
2012-07-03 06:06:51 +00:00
|
|
|
size_t addrlen = sizeof(src_addr);
|
2012-07-03 00:56:22 +00:00
|
|
|
unsigned char transaction_id[8];
|
2012-08-09 02:44:32 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
2012-06-22 03:55:41 +00:00
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
if (interface->tick_ms>0 && (interface->last_tick_ms == -1 || now >= interface->last_tick_ms + interface->tick_ms)) {
|
2012-07-06 00:28:54 +00:00
|
|
|
// tick the interface
|
|
|
|
int i = (interface - overlay_interfaces);
|
|
|
|
overlay_tick_interface(i, now);
|
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
/* Read from dummy interface file */
|
|
|
|
long long length=lseek(alarm->poll.fd,0,SEEK_END);
|
2012-07-31 08:19:24 +00:00
|
|
|
if (interface->recv_offset >= length) {
|
|
|
|
/* if there's no input, while we want to check for more soon,
|
|
|
|
we need to allow all other low priority alarms to fire first,
|
|
|
|
otherwise we'll dominate the scheduler without accomplishing anything */
|
|
|
|
alarm->alarm = gettime_ms() + 20;
|
|
|
|
if (interface->last_tick_ms != -1 && alarm->alarm > interface->last_tick_ms + interface->tick_ms)
|
|
|
|
alarm->alarm = interface->last_tick_ms + interface->tick_ms;
|
|
|
|
alarm->deadline = alarm->alarm + 10000;
|
|
|
|
} else {
|
|
|
|
if (lseek(alarm->poll.fd,interface->recv_offset,SEEK_SET) == -1)
|
|
|
|
WHY_perror("lseek");
|
|
|
|
else {
|
2012-07-03 00:56:22 +00:00
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES)
|
2012-07-31 08:19:24 +00:00
|
|
|
DEBUGF("Read interface %s (size=%lld) at offset=%d",interface->name, length, interface->recv_offset);
|
|
|
|
ssize_t nread = read(alarm->poll.fd, packet, sizeof packet);
|
2012-07-03 06:06:51 +00:00
|
|
|
if (nread == -1)
|
|
|
|
WHY_perror("read");
|
|
|
|
else {
|
2012-07-31 08:19:24 +00:00
|
|
|
interface->recv_offset += nread;
|
|
|
|
if (nread == sizeof packet) {
|
|
|
|
plen = packet[110] + (packet[111] << 8);
|
2012-07-03 06:06:51 +00:00
|
|
|
if (plen > nread - 128)
|
|
|
|
plen = -1;
|
|
|
|
if (debug&DEBUG_PACKETRX)
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUG_packet_visualise("Read from dummy interface", &packet[128], plen);
|
2012-07-02 03:49:54 +00:00
|
|
|
bzero(&transaction_id[0],8);
|
|
|
|
bzero(&src_addr,sizeof(src_addr));
|
2012-07-03 06:06:51 +00:00
|
|
|
if (plen >= 4) {
|
|
|
|
if (packet[0] == 0x01 && packet[1] == 0 && packet[2] == 0 && packet[3] == 0) {
|
|
|
|
if (packetOk(interface,&packet[128],plen,transaction_id, -1 /* fake TTL */, &src_addr,addrlen,1) == -1)
|
|
|
|
WARN("Unsupported packet from dummy interface");
|
|
|
|
} else {
|
|
|
|
WARNF("Unsupported packet version from dummy interface: %02x %02x %02x %02x", packet[0], packet[1], packet[2], packet[3]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WARNF("Invalid packet from dummy interface: plen=%lld", (long long) plen);
|
|
|
|
}
|
2012-06-20 05:59:46 +00:00
|
|
|
}
|
2012-07-03 06:06:51 +00:00
|
|
|
else
|
|
|
|
WARNF("Read %lld bytes from dummy interface", nread);
|
2012-07-02 03:49:54 +00:00
|
|
|
}
|
2012-01-10 03:35:26 +00:00
|
|
|
}
|
2012-07-31 08:19:24 +00:00
|
|
|
/* keep reading new packets as fast as possible,
|
|
|
|
but don't prevent other high priority alarms */
|
|
|
|
alarm->alarm = gettime_ms();
|
|
|
|
alarm->deadline = alarm->alarm + 200;
|
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
|
|
|
|
schedule(alarm);
|
2012-06-21 07:32:36 +00:00
|
|
|
|
2012-06-22 03:55:41 +00:00
|
|
|
return ;
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
|
|
|
overlay_broadcast_ensemble(int interface_number,
|
2012-08-30 02:17:13 +00:00
|
|
|
struct sockaddr_in *recipientaddr,
|
2012-08-31 02:41:31 +00:00
|
|
|
unsigned char *bytes,int len)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
2012-04-13 20:56:20 +00:00
|
|
|
if (debug&DEBUG_PACKETTX)
|
|
|
|
{
|
2012-06-28 08:04:21 +00:00
|
|
|
DEBUGF("Sending this packet via interface #%d",interface_number);
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUG_packet_visualise(NULL,bytes,len);
|
2012-04-13 20:56:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 06:06:51 +00:00
|
|
|
overlay_interface *interface = &overlay_interfaces[interface_number];
|
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
if (interface->state!=INTERFACE_STATE_UP){
|
|
|
|
return WHYF("Cannot send to interface %s as it is down", interface->name);
|
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
|
2012-07-03 06:06:51 +00:00
|
|
|
if (interface->fileP)
|
2011-08-12 07:47:29 +00:00
|
|
|
{
|
2011-08-14 15:58:27 +00:00
|
|
|
char buf[2048];
|
2012-06-21 06:22:44 +00:00
|
|
|
bzero(&buf[0],128);
|
2011-08-14 15:58:27 +00:00
|
|
|
/* Version information */
|
|
|
|
buf[0]=1; buf[1]=0;
|
|
|
|
buf[2]=0; buf[3]=0;
|
2012-06-21 06:22:44 +00:00
|
|
|
/* PID of creator */
|
|
|
|
buf[4]=getpid()&0xff; buf[5]=getpid()>>8;
|
|
|
|
|
|
|
|
/* TODO make a structure for all this stuff */
|
2011-08-14 15:58:27 +00:00
|
|
|
/* bytes 4-5 = half-power beam height (uint16) */
|
|
|
|
/* bytes 6-7 = half-power beam width (uint16) */
|
|
|
|
/* bytes 8-11 = range in metres, centre beam (uint32) */
|
|
|
|
/* bytes 16-47 = sender */
|
|
|
|
/* bytes 48-79 = next hop */
|
|
|
|
/* bytes 80-83 = latitude (uint32) */
|
|
|
|
/* bytes 84-87 = longitude (uint32) */
|
|
|
|
/* bytes 88-89 = X/Z direction (uint16) */
|
|
|
|
/* bytes 90-91 = Y direction (uint16) */
|
|
|
|
/* bytes 92-93 = speed in metres per second (uint16) */
|
|
|
|
/* bytes 94-97 = TX frequency in Hz, uncorrected for doppler (which must be done at the receiving end to take into account
|
|
|
|
relative motion) */
|
|
|
|
/* bytes 98-109 = coding method (use for doppler response etc) null terminated string */
|
2012-01-10 03:35:26 +00:00
|
|
|
/* bytes 110-111 = length of packet body in bytes */
|
|
|
|
/* bytes 112-127 reserved for future use */
|
2011-08-14 15:58:27 +00:00
|
|
|
|
|
|
|
if (len>2048-128) {
|
2012-06-28 08:04:21 +00:00
|
|
|
WARN("Truncating long packet to fit within 1920 byte limit for dummy interface");
|
2011-08-14 15:58:27 +00:00
|
|
|
len=2048-128;
|
|
|
|
}
|
|
|
|
|
2012-01-10 03:35:26 +00:00
|
|
|
/* Record length of packet */
|
|
|
|
buf[110]=len&0xff;
|
|
|
|
buf[111]=(len>>8)&0xff;
|
|
|
|
|
2011-08-14 15:58:27 +00:00
|
|
|
bzero(&buf[128+len],2048-(128+len));
|
|
|
|
bcopy(bytes,&buf[128],len);
|
2012-07-03 06:06:51 +00:00
|
|
|
/* This lseek() is unneccessary because the dummy file is opened in O_APPEND mode. It's
|
|
|
|
only purpose is to find out the offset to print in the DEBUG statement. It is vulnerable
|
|
|
|
to a race condition with other processes appending to the same file. */
|
|
|
|
off_t fsize = lseek(interface->alarm.poll.fd, (off_t) 0, SEEK_END);
|
|
|
|
if (fsize == -1)
|
|
|
|
return WHY_perror("lseek");
|
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES)
|
2012-07-31 08:19:24 +00:00
|
|
|
DEBUGF("Write to interface %s at offset=%d", interface->name, fsize);
|
2012-07-03 06:06:51 +00:00
|
|
|
ssize_t nwrite = write(interface->alarm.poll.fd, buf, 2048);
|
|
|
|
if (nwrite == -1)
|
|
|
|
return WHY_perror("write");
|
|
|
|
if (nwrite != 2048)
|
|
|
|
return WHYF("only wrote %lld of %lld bytes", nwrite, 2048);
|
|
|
|
return 0;
|
2011-08-12 07:47:29 +00:00
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
else
|
2011-08-13 11:17:49 +00:00
|
|
|
{
|
2012-09-11 05:50:44 +00:00
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES)
|
|
|
|
DEBUGF("Sending %d byte overlay frame on %s to %s",len,interface->name,inet_ntoa(recipientaddr->sin_addr));
|
2012-07-03 06:06:51 +00:00
|
|
|
if(sendto(interface->alarm.poll.fd,
|
2012-08-30 00:04:52 +00:00
|
|
|
bytes, len, 0, (struct sockaddr *)recipientaddr, sizeof(struct sockaddr_in)) != len){
|
2012-07-25 07:23:44 +00:00
|
|
|
WHY_perror("sendto(c)");
|
|
|
|
overlay_interface_close(interface);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-07-03 06:06:51 +00:00
|
|
|
return 0;
|
2011-08-13 11:17:49 +00:00
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-01-08 17:49:52 +00:00
|
|
|
/* This function is called to return old non-overlay requests back out the
|
|
|
|
interface they came in. */
|
|
|
|
int overlay_sendto(struct sockaddr_in *recipientaddr,unsigned char *bytes,int len)
|
|
|
|
{
|
2012-06-28 08:04:21 +00:00
|
|
|
if (debug&DEBUG_PACKETTX) DEBUGF("Sending %d bytes",len);
|
2012-07-03 06:06:51 +00:00
|
|
|
if (overlay_broadcast_ensemble(overlay_last_interface_number,recipientaddr,bytes,len) == -1)
|
2012-01-08 17:49:52 +00:00
|
|
|
return -1;
|
2012-07-03 06:06:51 +00:00
|
|
|
return len;
|
2012-01-08 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 08:59:27 +00:00
|
|
|
/* Register the interface, or update the existing interface registration */
|
|
|
|
int
|
|
|
|
overlay_interface_register(char *name,
|
2012-08-08 05:27:27 +00:00
|
|
|
struct in_addr addr,
|
|
|
|
struct in_addr mask) {
|
2012-06-08 08:59:27 +00:00
|
|
|
struct interface_rules *r, *me;
|
|
|
|
int i;
|
2012-08-08 05:27:27 +00:00
|
|
|
struct in_addr broadcast = {.s_addr = addr.s_addr | ~mask.s_addr};
|
|
|
|
|
|
|
|
if (debug & DEBUG_OVERLAYINTERFACES) {
|
|
|
|
// note, inet_ntop doesn't seem to behave on android
|
|
|
|
DEBUGF("%s address: %s", name, inet_ntoa(addr));
|
|
|
|
DEBUGF("%s broadcast address: %s", name, inet_ntoa(broadcast));
|
|
|
|
}
|
|
|
|
|
2012-06-08 08:59:27 +00:00
|
|
|
/* See if the interface is listed in the filter */
|
|
|
|
me = NULL;
|
|
|
|
r = interface_filter;
|
2012-04-28 02:55:19 +00:00
|
|
|
while(r) {
|
2012-06-08 08:59:27 +00:00
|
|
|
if (!strcasecmp(name, r->namespec))
|
|
|
|
me = r;
|
|
|
|
|
|
|
|
r = r->next;
|
2012-04-28 02:55:19 +00:00
|
|
|
}
|
2012-06-08 08:59:27 +00:00
|
|
|
|
|
|
|
if (me == NULL || me->excludeP) {
|
|
|
|
if (debug & DEBUG_OVERLAYINTERFACES)
|
2012-06-28 06:07:36 +00:00
|
|
|
DEBUGF("Interface %s is not interesting.",name);
|
2012-06-08 08:59:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
int found_interface= -1;
|
2012-06-08 08:59:27 +00:00
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
/* Search in the exist list of interfaces */
|
|
|
|
for(i = 0; i < overlay_interface_count; i++){
|
|
|
|
int broadcast_match = 0;
|
|
|
|
int name_match =0;
|
|
|
|
|
2012-06-08 08:59:27 +00:00
|
|
|
if ((overlay_interfaces[i].broadcast_address.sin_addr.s_addr & 0xffffffff)
|
2012-08-08 05:27:27 +00:00
|
|
|
== (broadcast.s_addr & 0xffffffff)){
|
2012-07-25 07:23:44 +00:00
|
|
|
broadcast_match = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
name_match = !strcasecmp(overlay_interfaces[i].name, name);
|
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
// if we find an exact match we can stop searching
|
2012-07-25 07:23:44 +00:00
|
|
|
if (name_match && broadcast_match){
|
|
|
|
// mark this interface as still alive
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_DETECTING)
|
|
|
|
overlay_interfaces[i].state=INTERFACE_STATE_UP;
|
|
|
|
|
2012-08-08 05:27:27 +00:00
|
|
|
// try to bring the interface back up again even if the address has changed
|
2012-07-25 07:23:44 +00:00
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_DOWN){
|
2012-08-08 05:27:27 +00:00
|
|
|
overlay_interfaces[i].address.sin_addr = addr;
|
2012-07-25 07:23:44 +00:00
|
|
|
overlay_interface_init_socket(i);
|
2012-06-08 08:59:27 +00:00
|
|
|
}
|
2012-07-25 07:23:44 +00:00
|
|
|
|
|
|
|
// we already know about this interface, and it's up so stop looking immediately
|
|
|
|
return 0;
|
2012-04-28 02:55:19 +00:00
|
|
|
}
|
2012-07-25 07:23:44 +00:00
|
|
|
|
|
|
|
// remember this slot to bring the interface back up again, even if the address has changed
|
|
|
|
if (name_match && overlay_interfaces[i].state==INTERFACE_STATE_DOWN)
|
|
|
|
found_interface=i;
|
2012-04-28 02:55:19 +00:00
|
|
|
}
|
2012-07-25 07:23:44 +00:00
|
|
|
|
|
|
|
if (found_interface>=0){
|
2012-08-08 05:27:27 +00:00
|
|
|
// try to reactivate the existing interface
|
2012-09-06 00:27:36 +00:00
|
|
|
overlay_interfaces[found_interface].address.sin_addr = addr;
|
|
|
|
overlay_interfaces[found_interface].broadcast_address.sin_addr = broadcast;
|
|
|
|
overlay_interfaces[found_interface].netmask = mask;
|
|
|
|
return overlay_interface_init_socket(found_interface);
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* New interface, so register it */
|
2012-08-08 05:27:27 +00:00
|
|
|
if (overlay_interface_init(name, addr, mask, broadcast, me->speed_in_bits, me->port, me->type))
|
2012-07-25 07:23:44 +00:00
|
|
|
return WHYF("Could not initialise newly seen interface %s", name);
|
|
|
|
else
|
|
|
|
if (debug & DEBUG_OVERLAYINTERFACES) DEBUGF("Registered interface %s", name);
|
2012-06-08 08:59:27 +00:00
|
|
|
|
2012-04-28 02:55:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void overlay_interface_discover(struct sched_ent *alarm){
|
2012-07-25 07:23:44 +00:00
|
|
|
int i;
|
2012-06-15 05:34:36 +00:00
|
|
|
struct interface_rules *r;
|
2012-08-08 05:27:27 +00:00
|
|
|
struct in_addr dummyaddr;
|
2012-07-25 07:23:44 +00:00
|
|
|
int detect_real_interfaces = 0;
|
|
|
|
|
|
|
|
/* Mark all UP interfaces as DETECTING, so we can tell which interfaces are new, and which are dead */
|
2012-07-30 09:05:28 +00:00
|
|
|
for (i = 0; i < overlay_interface_count; i++)
|
2012-07-25 07:23:44 +00:00
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP)
|
|
|
|
overlay_interfaces[i].state=INTERFACE_STATE_DETECTING;
|
2011-08-12 07:47:29 +00:00
|
|
|
|
2011-08-13 11:17:49 +00:00
|
|
|
/* Check through for any virtual dummy interfaces */
|
2012-06-15 05:34:36 +00:00
|
|
|
for (r = interface_filter; r != NULL; r = r->next) {
|
2012-07-25 07:23:44 +00:00
|
|
|
if (r->namespec[0] != '>'){
|
|
|
|
detect_real_interfaces = 1;
|
2012-06-15 05:34:36 +00:00
|
|
|
continue;
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
2012-06-15 05:34:36 +00:00
|
|
|
|
2012-07-30 09:05:28 +00:00
|
|
|
for (i = 0; i < overlay_interface_count; i++)
|
2012-07-25 07:23:44 +00:00
|
|
|
if (!strcasecmp(overlay_interfaces[i].name,r->namespec)){
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_DETECTING)
|
|
|
|
overlay_interfaces[i].state=INTERFACE_STATE_UP;
|
2012-06-15 05:34:36 +00:00
|
|
|
break;
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
2012-06-15 05:34:36 +00:00
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
if (i >= overlay_interface_count){
|
2012-06-22 04:26:37 +00:00
|
|
|
/* New interface, so register it */
|
2012-08-08 05:27:27 +00:00
|
|
|
overlay_interface_init(r->namespec,dummyaddr,dummyaddr,dummyaddr,1000000,PORT_DNA,OVERLAY_INTERFACE_WIFI);
|
2011-08-13 11:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-15 05:34:36 +00:00
|
|
|
|
|
|
|
/* Look for real interfaces */
|
2012-07-25 07:23:44 +00:00
|
|
|
if (detect_real_interfaces){
|
|
|
|
int no_route = 1;
|
|
|
|
|
2012-06-08 07:01:59 +00:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
2012-07-25 07:23:44 +00:00
|
|
|
if (no_route != 0)
|
|
|
|
no_route = doifaddrs();
|
2012-06-08 07:01:59 +00:00
|
|
|
#endif
|
2011-08-13 11:17:49 +00:00
|
|
|
|
2012-05-28 05:24:33 +00:00
|
|
|
#ifdef SIOCGIFCONF
|
2012-07-25 07:23:44 +00:00
|
|
|
if (no_route != 0)
|
|
|
|
no_route = lsif();
|
2012-05-03 13:16:00 +00:00
|
|
|
#endif
|
2012-05-28 05:24:33 +00:00
|
|
|
|
|
|
|
#ifdef linux
|
2012-07-25 07:23:44 +00:00
|
|
|
if (no_route != 0)
|
|
|
|
no_route = scrapeProcNetRoute();
|
2012-05-28 05:24:33 +00:00
|
|
|
#endif
|
2012-05-03 13:16:00 +00:00
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
if (no_route != 0) {
|
|
|
|
FATAL("Unable to get any interface information");
|
|
|
|
}
|
2012-04-28 02:55:19 +00:00
|
|
|
}
|
2012-06-08 07:01:59 +00:00
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
// detect if any interfaces have gone away and need to be closed
|
|
|
|
for(i = 0; i < overlay_interface_count; i++)
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_DETECTING)
|
|
|
|
overlay_interface_close(&overlay_interfaces[i]);
|
|
|
|
|
2012-07-30 07:52:38 +00:00
|
|
|
alarm->alarm = gettime_ms()+5000;
|
2012-07-12 00:45:16 +00:00
|
|
|
alarm->deadline = alarm->alarm + 10000;
|
2012-07-02 03:49:54 +00:00
|
|
|
schedule(alarm);
|
2012-06-22 03:55:41 +00:00
|
|
|
return;
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
/* remove and free a payload from the queue */
|
2012-08-27 00:34:59 +00:00
|
|
|
static struct overlay_frame *
|
|
|
|
overlay_queue_remove(overlay_txqueue *queue, struct overlay_frame *frame){
|
|
|
|
struct overlay_frame *prev = frame->prev;
|
|
|
|
struct overlay_frame *next = frame->next;
|
2012-07-12 01:06:41 +00:00
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else if(frame == queue->first)
|
|
|
|
queue->first = next;
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
else if(frame == queue->last)
|
|
|
|
queue->last = prev;
|
|
|
|
|
|
|
|
queue->length--;
|
|
|
|
|
|
|
|
op_free(frame);
|
|
|
|
|
|
|
|
return next;
|
2011-09-05 02:49:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
/* XXX: unused */
|
|
|
|
static int
|
|
|
|
overlay_queue_dump(overlay_txqueue *q)
|
2012-01-10 08:01:14 +00:00
|
|
|
{
|
2012-06-28 08:04:21 +00:00
|
|
|
strbuf b = strbuf_alloca(8192);
|
2012-01-10 08:01:14 +00:00
|
|
|
struct overlay_frame *f;
|
2012-06-28 08:04:21 +00:00
|
|
|
strbuf_sprintf(b,"overlay_txqueue @ 0x%p\n",q);
|
|
|
|
strbuf_sprintf(b," length=%d\n",q->length);
|
|
|
|
strbuf_sprintf(b," maxLenght=%d\n",q->maxLength);
|
|
|
|
strbuf_sprintf(b," latencyTarget=%d milli-seconds\n",q->latencyTarget);
|
|
|
|
strbuf_sprintf(b," first=%p\n",q->first);
|
2012-01-10 08:01:14 +00:00
|
|
|
f=q->first;
|
|
|
|
while(f) {
|
2012-07-12 01:06:41 +00:00
|
|
|
strbuf_sprintf(b," %p: ->next=%p, ->prev=%p\n",
|
|
|
|
f,f->next,f->prev);
|
2012-01-10 08:01:14 +00:00
|
|
|
if (f==f->next) {
|
2012-06-28 08:04:21 +00:00
|
|
|
strbuf_sprintf(b," LOOP!\n"); break;
|
2012-01-10 08:01:14 +00:00
|
|
|
}
|
|
|
|
f=f->next;
|
|
|
|
}
|
2012-06-28 08:04:21 +00:00
|
|
|
strbuf_sprintf(b," last=%p\n",q->last);
|
2012-01-10 08:01:14 +00:00
|
|
|
f=q->last;
|
|
|
|
while(f) {
|
2012-06-28 08:04:21 +00:00
|
|
|
strbuf_sprintf(b," %p: ->next=%p, ->prev=%p\n",
|
2012-01-10 08:01:14 +00:00
|
|
|
f,f->next,f->prev);
|
|
|
|
if (f==f->prev) {
|
2012-06-28 08:04:21 +00:00
|
|
|
strbuf_sprintf(b," LOOP!\n"); break;
|
2012-01-10 08:01:14 +00:00
|
|
|
}
|
|
|
|
f=f->prev;
|
|
|
|
}
|
2012-06-28 08:04:21 +00:00
|
|
|
DEBUG(strbuf_str(b));
|
2012-01-10 08:01:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static void
|
2012-09-10 01:25:12 +00:00
|
|
|
overlay_init_packet(struct outgoing_packet *packet, overlay_interface *interface){
|
2012-08-27 00:34:59 +00:00
|
|
|
packet->interface = interface;
|
|
|
|
packet->i = (interface - overlay_interfaces);
|
2012-09-10 01:25:12 +00:00
|
|
|
packet->dest=interface->broadcast_address;
|
2012-08-22 00:51:38 +00:00
|
|
|
packet->buffer=ob_new();
|
2012-07-12 01:06:41 +00:00
|
|
|
ob_limitsize(packet->buffer, packet->interface->mtu);
|
|
|
|
ob_append_bytes(packet->buffer,magic_header,4);
|
2012-07-17 06:00:50 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
overlay_address_clear();
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 01:11:47 +00:00
|
|
|
// update the alarm time and return 1 if changed
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
2012-08-27 00:34:59 +00:00
|
|
|
overlay_calc_queue_time(overlay_txqueue *queue, struct overlay_frame *frame){
|
2012-07-12 01:11:47 +00:00
|
|
|
int ret=0;
|
2012-08-09 02:44:32 +00:00
|
|
|
time_ms_t send_time;
|
2012-08-27 00:34:59 +00:00
|
|
|
|
|
|
|
// ignore packet if the destination is currently unreachable
|
2012-08-30 02:19:12 +00:00
|
|
|
if (frame->destination && subscriber_is_reachable(frame->destination)==REACHABLE_NONE)
|
2012-07-12 01:11:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// when is the next packet from this queue due?
|
|
|
|
send_time=queue->first->enqueued_at + queue->transmit_delay;
|
|
|
|
if (next_packet.alarm==0 || send_time < next_packet.alarm){
|
|
|
|
next_packet.alarm=send_time;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// how long can we wait if the server is busy?
|
|
|
|
send_time += queue->grace_period;
|
|
|
|
if (next_packet.deadline==0 || send_time < next_packet.deadline){
|
|
|
|
next_packet.deadline=send_time;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
if (!next_packet.function){
|
|
|
|
next_packet.function=overlay_send_packet;
|
|
|
|
send_packet.name="overlay_send_packet";
|
|
|
|
next_packet.stats=&send_packet;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static void
|
2012-08-27 00:34:59 +00:00
|
|
|
overlay_stuff_packet(struct outgoing_packet *packet, overlay_txqueue *queue, time_ms_t now){
|
|
|
|
struct overlay_frame *frame = queue->first;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
|
|
|
// TODO stop when the packet is nearly full?
|
|
|
|
|
|
|
|
while(frame){
|
2012-08-15 06:26:58 +00:00
|
|
|
if (frame->enqueued_at + queue->latencyTarget < now){
|
2012-08-27 00:34:59 +00:00
|
|
|
DEBUGF("Dropping frame type %x for %s due to expiry timeout",
|
|
|
|
frame->type, frame->destination?alloca_tohex_sid(frame->destination->sid):"All");
|
2012-07-12 01:06:41 +00:00
|
|
|
frame = overlay_queue_remove(queue, frame);
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-15 06:26:58 +00:00
|
|
|
/* Note, once we queue a broadcast packet we are committed to sending it out every interface,
|
|
|
|
even if we hear it from somewhere else in the mean time
|
|
|
|
*/
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
struct subscriber *next_hop = frame->destination;
|
|
|
|
|
|
|
|
if (next_hop){
|
2012-09-10 01:25:12 +00:00
|
|
|
switch(subscriber_is_reachable(next_hop)){
|
|
|
|
case REACHABLE_NONE:
|
|
|
|
goto skip;
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
case REACHABLE_INDIRECT:
|
|
|
|
next_hop=next_hop->next_hop;
|
|
|
|
|
|
|
|
// fall through
|
|
|
|
case REACHABLE_DIRECT:
|
2012-09-10 01:25:12 +00:00
|
|
|
case REACHABLE_UNICAST:
|
2012-08-27 00:34:59 +00:00
|
|
|
frame->sendBroadcast=0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REACHABLE_BROADCAST:
|
|
|
|
if (!frame->sendBroadcast){
|
|
|
|
frame->sendBroadcast=1;
|
|
|
|
overlay_broadcast_generate_address(&frame->broadcast_id);
|
|
|
|
int i;
|
|
|
|
for(i=0;i<OVERLAY_MAX_INTERFACES;i++)
|
|
|
|
frame->broadcast_sent_via[i]=0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
if (!packet->buffer){
|
|
|
|
// use the interface of the first payload we find
|
2012-08-27 00:34:59 +00:00
|
|
|
if (frame->sendBroadcast){
|
2012-07-12 01:06:41 +00:00
|
|
|
// find an interface that we haven't broadcast on yet
|
|
|
|
int i;
|
|
|
|
for(i=0;i<OVERLAY_MAX_INTERFACES;i++)
|
|
|
|
{
|
2012-08-30 00:04:52 +00:00
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP
|
|
|
|
&& !frame->broadcast_sent_via[i]){
|
2012-09-10 01:25:12 +00:00
|
|
|
overlay_init_packet(packet, &overlay_interfaces[i]);
|
2012-08-30 00:04:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!packet->buffer){
|
|
|
|
// oh dear, why is this broadcast still in the queue?
|
|
|
|
frame = overlay_queue_remove(queue, frame);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}else{
|
2012-09-10 01:25:12 +00:00
|
|
|
overlay_init_packet(packet, next_hop->interface);
|
|
|
|
if (next_hop->reachable==REACHABLE_UNICAST){
|
|
|
|
packet->dest = next_hop->address;
|
|
|
|
packet->unicast=1;
|
|
|
|
}
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// make sure this payload can be sent via this interface
|
2012-08-27 00:34:59 +00:00
|
|
|
if (frame->sendBroadcast){
|
2012-07-12 01:06:41 +00:00
|
|
|
if (frame->broadcast_sent_via[packet->i]){
|
|
|
|
goto skip;
|
|
|
|
}
|
2012-09-10 01:25:12 +00:00
|
|
|
}else{
|
|
|
|
if(packet->interface != next_hop->interface)
|
|
|
|
goto skip;
|
|
|
|
if (next_hop->reachable==REACHABLE_DIRECT && packet->unicast)
|
|
|
|
goto skip;
|
|
|
|
if (next_hop->reachable==REACHABLE_UNICAST &&
|
|
|
|
((!packet->unicast) ||
|
|
|
|
packet->dest.sin_addr.s_addr != next_hop->address.sin_addr.s_addr))
|
|
|
|
goto skip;
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
if (debug&DEBUG_OVERLAYFRAMES){
|
|
|
|
DEBUGF("Sending payload type %x len %d for %s via %s", frame->type, frame->payload->position,
|
|
|
|
frame->destination?alloca_tohex_sid(frame->destination->sid):"All",
|
|
|
|
frame->sendBroadcast?alloca_tohex(frame->broadcast_id.id, BROADCAST_LEN):alloca_tohex_sid(next_hop->sid));
|
|
|
|
}
|
|
|
|
|
2012-09-07 00:31:34 +00:00
|
|
|
if (overlay_frame_append_payload(packet->interface, frame, next_hop, packet->buffer))
|
2012-07-12 01:06:41 +00:00
|
|
|
// payload was not queued
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
// mark the payload as sent
|
|
|
|
int keep_payload = 0;
|
|
|
|
|
2012-08-27 00:34:59 +00:00
|
|
|
if (frame->sendBroadcast){
|
2012-07-12 01:06:41 +00:00
|
|
|
int i;
|
|
|
|
frame->broadcast_sent_via[packet->i]=1;
|
|
|
|
|
|
|
|
// check if there is still a broadcast to be sent
|
|
|
|
for(i=0;i<OVERLAY_MAX_INTERFACES;i++)
|
|
|
|
{
|
2012-07-25 07:23:44 +00:00
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP)
|
2012-07-12 01:06:41 +00:00
|
|
|
if (!frame->broadcast_sent_via[i]){
|
|
|
|
keep_payload=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keep_payload){
|
|
|
|
frame = overlay_queue_remove(queue, frame);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
skip:
|
2012-07-12 01:11:47 +00:00
|
|
|
// if we can't send the payload now, check when we should try
|
|
|
|
overlay_calc_queue_time(queue, frame);
|
2012-07-12 01:06:41 +00:00
|
|
|
frame = frame->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill a packet from our outgoing queues and send it
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
|
|
|
overlay_fill_send_packet(struct outgoing_packet *packet, time_ms_t now) {
|
2012-07-12 01:06:41 +00:00
|
|
|
int i;
|
|
|
|
IN();
|
2012-07-12 01:11:47 +00:00
|
|
|
// while we're looking at queues, work out when to schedule another packet
|
|
|
|
unschedule(&next_packet);
|
|
|
|
next_packet.alarm=0;
|
|
|
|
next_packet.deadline=0;
|
2012-07-12 01:06:41 +00:00
|
|
|
|
|
|
|
for (i=0;i<OQ_MAX;i++){
|
|
|
|
overlay_txqueue *queue=&overlay_tx[i];
|
|
|
|
|
|
|
|
overlay_stuff_packet(packet, queue, now);
|
|
|
|
}
|
|
|
|
|
2012-07-12 01:11:47 +00:00
|
|
|
if (next_packet.alarm)
|
|
|
|
schedule(&next_packet);
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
if(packet->buffer){
|
|
|
|
// send the packet
|
2012-08-22 00:51:38 +00:00
|
|
|
if (packet->buffer->position>=HEADERFIELDS_LEN){
|
2012-08-30 00:04:52 +00:00
|
|
|
// stuff rhizome announcements at the last moment
|
|
|
|
if (rhizome_enabled() && rhizome_http_server_running()){
|
|
|
|
overlay_rhizome_add_advertisements(packet->i,packet->buffer);
|
|
|
|
}
|
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
if (debug&DEBUG_PACKETCONSTRUCTION)
|
2012-08-22 00:51:38 +00:00
|
|
|
dump("assembled packet",&packet->buffer->bytes[0],packet->buffer->position);
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
overlay_broadcast_ensemble(packet->i, &packet->dest, packet->buffer->bytes, packet->buffer->position);
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
ob_free(packet->buffer);
|
2012-08-27 00:34:59 +00:00
|
|
|
overlay_address_clear();
|
2012-07-12 01:06:41 +00:00
|
|
|
RETURN(1);
|
|
|
|
}
|
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
2012-07-12 01:11:47 +00:00
|
|
|
// when the queue timer elapses, send a packet
|
|
|
|
void overlay_send_packet(struct sched_ent *alarm){
|
2012-07-12 01:06:41 +00:00
|
|
|
struct outgoing_packet packet;
|
|
|
|
bzero(&packet, sizeof(struct outgoing_packet));
|
|
|
|
|
2012-07-30 07:52:38 +00:00
|
|
|
overlay_fill_send_packet(&packet, gettime_ms());
|
2012-07-12 01:06:41 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 01:11:47 +00:00
|
|
|
// update time for next alarm and reschedule
|
2012-08-27 00:34:59 +00:00
|
|
|
void overlay_update_queue_schedule(overlay_txqueue *queue, struct overlay_frame *frame){
|
2012-07-12 01:11:47 +00:00
|
|
|
if (overlay_calc_queue_time(queue, frame)){
|
|
|
|
unschedule(&next_packet);
|
|
|
|
schedule(&next_packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
|
|
|
overlay_tick_interface(int i, time_ms_t now) {
|
2012-07-12 01:06:41 +00:00
|
|
|
struct outgoing_packet packet;
|
|
|
|
IN();
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
/* An interface with no speed budget is for listening only, so doesn't get ticked */
|
|
|
|
if (overlay_interfaces[i].bits_per_second<1
|
|
|
|
|| overlay_interfaces[i].state!=INTERFACE_STATE_UP) {
|
2012-07-12 01:06:41 +00:00
|
|
|
RETURN(0);
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
2012-07-03 00:56:22 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
// initialise the packet buffer
|
|
|
|
bzero(&packet, sizeof(struct outgoing_packet));
|
2012-09-10 01:25:12 +00:00
|
|
|
overlay_init_packet(&packet, &overlay_interfaces[i]);
|
2012-07-12 01:06:41 +00:00
|
|
|
|
2012-06-28 08:04:21 +00:00
|
|
|
if (debug&DEBUG_OVERLAYINTERFACES) DEBUGF("Ticking interface #%d",i);
|
2012-07-03 00:56:22 +00:00
|
|
|
|
2011-08-08 14:41:46 +00:00
|
|
|
/* 1. Send announcement about ourselves, including one SID that we host if we host more than one SID
|
|
|
|
(the first SID we host becomes our own identity, saving a little bit of data here).
|
|
|
|
*/
|
2012-07-31 06:51:29 +00:00
|
|
|
if (overlay_add_selfannouncement(i,packet.buffer) == -1)
|
|
|
|
return WHY("tick failed");
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
/* Add advertisements for ROUTES */
|
2012-09-07 00:31:34 +00:00
|
|
|
overlay_route_add_advertisements(packet.interface, packet.buffer);
|
2012-03-21 21:56:19 +00:00
|
|
|
|
2012-07-12 01:06:41 +00:00
|
|
|
/* Stuff more payloads from queues and send it */
|
|
|
|
overlay_fill_send_packet(&packet, now);
|
|
|
|
RETURN(0);
|
2011-08-08 14:41:46 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static long long
|
|
|
|
parse_quantity(char *q) {
|
2012-05-14 09:02:10 +00:00
|
|
|
int m;
|
|
|
|
char units[80];
|
|
|
|
|
|
|
|
if (strlen(q)>=80) return WHY("quantity string >=80 characters");
|
|
|
|
|
|
|
|
if (sscanf(q,"%d%s",&m,units)==2)
|
|
|
|
{
|
|
|
|
if (units[1]) return WHY("Units should be single character");
|
|
|
|
switch(units[0])
|
|
|
|
{
|
|
|
|
case 'k': return m*1000LL;
|
|
|
|
case 'K': return m*1024LL;
|
|
|
|
case 'm': return m*1000LL*1000LL;
|
|
|
|
case 'M': return m*1024LL*1024LL;
|
|
|
|
case 'g': return m*1000LL*1000LL*1000LL;
|
|
|
|
case 'G': return m*1024LL*1024LL*1024LL;
|
|
|
|
default:
|
|
|
|
return WHY("Illegal unit: should be k,K,m,M,g, or G.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sscanf(q,"%d",&m)==1)
|
|
|
|
{
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return WHY("Could not parse quantity");
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 08:24:02 +00:00
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static void
|
|
|
|
logServalPacket(int level, struct __sourceloc where, const char *message, const unsigned char *packet, size_t len) {
|
2012-08-03 07:14:05 +00:00
|
|
|
struct mallocbuf mb = STRUCT_MALLOCBUF_NULL;
|
|
|
|
if (serval_packetvisualise(XPRINTF_MALLOCBUF(&mb), message, packet, len) == -1)
|
2012-08-01 08:24:02 +00:00
|
|
|
WHY("serval_packetvisualise() failed");
|
2012-08-03 07:14:05 +00:00
|
|
|
else if (mb.buffer == NULL)
|
2012-08-03 09:38:44 +00:00
|
|
|
WHYF("serval_packetvisualise() output buffer missing, message=%s packet=%p len=%lu", alloca_toprint(-1, message, strlen(message)), packet, len);
|
2012-08-01 08:24:02 +00:00
|
|
|
else
|
2012-08-23 03:01:07 +00:00
|
|
|
logString(level, where, mb.buffer);
|
2012-08-03 07:14:05 +00:00
|
|
|
if (mb.buffer)
|
|
|
|
free(mb.buffer);
|
2012-08-01 08:24:02 +00:00
|
|
|
}
|