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-09-05 09:23:22 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2012-06-15 05:34:36 +00:00
|
|
|
#include <assert.h>
|
2012-05-10 04:37:11 +00:00
|
|
|
#include <time.h>
|
2012-12-04 03:42:28 +00:00
|
|
|
#include <fnmatch.h>
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2012-06-28 08:04:21 +00:00
|
|
|
#include "strbuf.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "strbuf_helpers.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"
|
2012-10-31 07:46:05 +00:00
|
|
|
#include "str.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
|
|
|
|
2012-07-02 06:34:00 +00:00
|
|
|
struct profile_total interface_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-08-31 02:41:31 +00:00
|
|
|
static void overlay_interface_poll(struct sched_ent *alarm);
|
2012-10-16 06:16:52 +00:00
|
|
|
static void logServalPacket(int level, struct __sourceloc __whence, const char *message, const unsigned char *packet, size_t len);
|
|
|
|
|
|
|
|
#define DEBUG_packet_visualise(M,P,N) logServalPacket(LOG_LEVEL_DEBUG, __WHENCE__, (M), (P), (N))
|
2012-07-06 00:28:54 +00:00
|
|
|
|
2012-12-11 06:18:02 +00:00
|
|
|
static int mark_subscriber_down(struct subscriber *subscriber, void *context)
|
|
|
|
{
|
|
|
|
overlay_interface *interface=context;
|
|
|
|
if ((subscriber->reachable & REACHABLE_DIRECT) && subscriber->interface == interface)
|
|
|
|
set_reachable(subscriber, REACHABLE_NONE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static void
|
|
|
|
overlay_interface_close(overlay_interface *interface){
|
2012-12-11 06:18:02 +00:00
|
|
|
enum_subscribers(NULL, mark_subscriber_down, interface);
|
|
|
|
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. */
|
2012-09-05 09:23:22 +00:00
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, NULL) |
|
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
FD_CLOEXEC
|
|
|
|
#else
|
|
|
|
O_CLOEXEC
|
|
|
|
#endif
|
|
|
|
);
|
2012-08-08 05:27:27 +00:00
|
|
|
|
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-12-07 05:34:40 +00:00
|
|
|
overlay_interface * overlay_interface_get_default(){
|
|
|
|
int i;
|
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP && overlay_interfaces[i].default_route)
|
|
|
|
return &overlay_interfaces[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
overlay_interface * overlay_interface_find(struct in_addr addr, int return_default){
|
2012-08-30 00:04:52 +00:00
|
|
|
int i;
|
2012-12-04 04:17:57 +00:00
|
|
|
overlay_interface *ret = NULL;
|
2012-08-30 00:04:52 +00:00
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state!=INTERFACE_STATE_UP)
|
|
|
|
continue;
|
2012-12-04 04:17:57 +00:00
|
|
|
|
2012-08-30 00:04:52 +00:00
|
|
|
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];
|
|
|
|
}
|
2012-12-04 04:17:57 +00:00
|
|
|
|
|
|
|
// check if this is a default interface
|
2012-12-07 05:34:40 +00:00
|
|
|
if (return_default && overlay_interfaces[i].default_route)
|
2012-12-04 04:17:57 +00:00
|
|
|
ret=&overlay_interfaces[i];
|
2012-08-30 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 04:17:57 +00:00
|
|
|
return ret;
|
2012-08-30 00:04:52 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2012-12-05 05:17:14 +00:00
|
|
|
if (strcasecmp(name, overlay_interfaces[i].name) == 0)
|
2012-09-14 02:20:45 +00:00
|
|
|
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-12-07 05:34:40 +00:00
|
|
|
interface = overlay_interface_find(src, 0);
|
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){
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces)
|
2012-08-22 05:20:14 +00:00
|
|
|
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 */
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetrx)
|
2012-08-22 05:20:14 +00:00
|
|
|
DEBUG_packet_visualise("Read from real interface", packet,plen);
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces)
|
2012-09-10 01:01:01 +00:00
|
|
|
DEBUGF("Received %d bytes from %s on interface %s (ANY)",plen,
|
2012-11-28 03:43:25 +00:00
|
|
|
inet_ntoa(src),
|
2012-09-10 01:01:01 +00:00
|
|
|
interface->name);
|
2012-11-21 05:00:20 +00:00
|
|
|
|
|
|
|
if (packetOkOverlay(interface, packet, plen, recvttl, &src_addr, addrlen)) {
|
2013-02-14 20:42:01 +00:00
|
|
|
WHYF("Malformed packet (length = %d)",plen);
|
|
|
|
if (config.debug.slip) dump("the malformed packet",packet,plen);
|
2012-08-22 05:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-12-04 03:42:28 +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];
|
|
|
|
|
|
|
|
/*
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetrx || config.debug.io) {
|
2012-08-08 05:27:27 +00:00
|
|
|
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;
|
|
|
|
watch(&interface->alarm);
|
2012-09-14 02:20:45 +00:00
|
|
|
|
2011-08-12 07:47:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 02:41:31 +00:00
|
|
|
static int
|
2012-12-04 03:42:28 +00:00
|
|
|
overlay_interface_init(const char *name, struct in_addr src_addr, struct in_addr netmask, struct in_addr broadcast,
|
|
|
|
const struct config_network_interface *ifconfig)
|
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-11-09 03:10:55 +00:00
|
|
|
strncpy(interface->name, name, sizeof interface->name);
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
// copy ifconfig values
|
|
|
|
interface->drop_broadcasts = ifconfig->drop_broadcasts;
|
|
|
|
interface->drop_unicasts = ifconfig->drop_unicasts;
|
|
|
|
interface->port = ifconfig->port;
|
|
|
|
interface->type = ifconfig->type;
|
|
|
|
interface->send_broadcasts = ifconfig->send_broadcasts;
|
|
|
|
interface->prefer_unicast = ifconfig->prefer_unicast;
|
|
|
|
interface->default_route = ifconfig->default_route;
|
|
|
|
interface->socket_type = ifconfig->socket_type;
|
|
|
|
interface->encapsulation = ifconfig->encapsulation;
|
|
|
|
|
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->last_tick_ms= -1; // not ticked yet
|
|
|
|
interface->alarm.poll.fd=0;
|
2013-02-14 03:48:56 +00:00
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
// How often do we announce ourselves on this interface?
|
2013-02-14 03:48:56 +00:00
|
|
|
int tick_ms=-1;
|
2012-12-13 07:20:09 +00:00
|
|
|
int packet_interval=-1;
|
|
|
|
|
|
|
|
// hard coded defaults:
|
|
|
|
switch (ifconfig->type) {
|
2012-12-04 03:42:28 +00:00
|
|
|
case OVERLAY_INTERFACE_PACKETRADIO:
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms = 15000;
|
2012-12-13 07:20:09 +00:00
|
|
|
packet_interval = 1000;
|
2012-12-04 03:42:28 +00:00
|
|
|
break;
|
|
|
|
case OVERLAY_INTERFACE_ETHERNET:
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms = 500;
|
2012-12-13 07:20:09 +00:00
|
|
|
packet_interval = 100;
|
2012-12-04 03:42:28 +00:00
|
|
|
break;
|
|
|
|
case OVERLAY_INTERFACE_WIFI:
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms = 500;
|
2012-12-13 07:20:09 +00:00
|
|
|
packet_interval = 400;
|
2012-12-04 03:42:28 +00:00
|
|
|
break;
|
|
|
|
case OVERLAY_INTERFACE_UNKNOWN:
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms = 500;
|
2012-12-13 07:20:09 +00:00
|
|
|
packet_interval = 100;
|
2012-12-04 03:42:28 +00:00
|
|
|
break;
|
2012-12-13 07:20:09 +00:00
|
|
|
}
|
|
|
|
// configurable defaults per interface
|
|
|
|
{
|
|
|
|
int i = config_mdp_iftypelist__get(&config.mdp.iftype, &ifconfig->type);
|
|
|
|
if (i != -1){
|
|
|
|
if (config.mdp.iftype.av[i].value.tick_ms>=0)
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms = config.mdp.iftype.av[i].value.tick_ms;
|
2012-12-13 07:20:09 +00:00
|
|
|
if (config.mdp.iftype.av[i].value.packet_interval>=0)
|
|
|
|
packet_interval=config.mdp.iftype.av[i].value.packet_interval;
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-13 07:20:09 +00:00
|
|
|
// specific value for this interface
|
|
|
|
if (ifconfig->mdp_tick_ms>=0)
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms = ifconfig->mdp_tick_ms;
|
2012-12-13 07:20:09 +00:00
|
|
|
if (ifconfig->packet_interval>=0)
|
|
|
|
packet_interval=ifconfig->packet_interval;
|
|
|
|
|
|
|
|
if (packet_interval<0)
|
|
|
|
return WHYF("Invalid packet interval %d specified for interface %s", packet_interval, name);
|
|
|
|
if (packet_interval==0){
|
|
|
|
INFOF("Interface %s is not sending any traffic!", name);
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms=0;
|
2012-12-13 07:20:09 +00:00
|
|
|
}else if (!interface->send_broadcasts){
|
|
|
|
INFOF("Interface %s is not sending any broadcast traffic!", name);
|
|
|
|
// no broadcast traffic implies no ticks
|
2013-02-14 03:48:56 +00:00
|
|
|
tick_ms=0;
|
|
|
|
}else if (tick_ms==0)
|
2012-09-19 00:22:14 +00:00
|
|
|
INFOF("Interface %s is running tickless", name);
|
2012-12-13 07:20:09 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if (tick_ms<0)
|
2012-12-13 07:20:09 +00:00
|
|
|
return WHYF("Invalid tick interval %d specified for interface %s", interface->tick_ms, name);
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
interface->tick_ms = tick_ms;
|
|
|
|
|
2012-12-13 07:20:09 +00:00
|
|
|
limit_init(&interface->transfer_limit, packet_interval);
|
2013-01-29 00:57:13 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
interface->address.sin_family=AF_INET;
|
|
|
|
interface->address.sin_port = htons(ifconfig->port);
|
|
|
|
interface->address.sin_addr = ifconfig->dummy_address;
|
2012-12-13 07:20:09 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
interface->netmask=ifconfig->dummy_netmask;
|
|
|
|
|
|
|
|
interface->broadcast_address.sin_family=AF_INET;
|
|
|
|
interface->broadcast_address.sin_port = htons(ifconfig->port);
|
|
|
|
interface->broadcast_address.sin_addr.s_addr = interface->address.sin_addr.s_addr | ~interface->netmask.s_addr;
|
|
|
|
|
|
|
|
interface->alarm.function = overlay_interface_poll;
|
|
|
|
interface_poll_stats.name="overlay_interface_poll";
|
|
|
|
interface->alarm.stats=&interface_poll_stats;
|
|
|
|
|
|
|
|
if (ifconfig->socket_type==SOCK_DGRAM){
|
|
|
|
interface->address.sin_addr = src_addr;
|
|
|
|
interface->broadcast_address.sin_addr = broadcast;
|
|
|
|
interface->netmask = netmask;
|
|
|
|
|
|
|
|
if (overlay_interface_init_socket(overlay_interface_count))
|
|
|
|
return WHY("overlay_interface_init_socket() failed");
|
|
|
|
}else{
|
|
|
|
char read_file[1024];
|
|
|
|
|
|
|
|
strbuf d = strbuf_local(read_file, sizeof read_file);
|
|
|
|
strbuf_path_join(d, serval_instancepath(), config.server.interface_path, ifconfig->file, NULL);
|
|
|
|
if (strbuf_overrun(d)){
|
|
|
|
WHYF("interface file name overrun: %s", alloca_str_toprint(strbuf_str(d)));
|
|
|
|
goto cleanup;
|
2013-02-04 19:36:44 +00:00
|
|
|
}
|
2012-12-04 04:17:57 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if ((interface->alarm.poll.fd = open(read_file,O_APPEND|O_RDWR)) < 1) {
|
|
|
|
WHYF_perror("could not open interface file %s", read_file);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-12-04 04:17:57 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if (ifconfig->type==OVERLAY_INTERFACE_PACKETRADIO)
|
|
|
|
overlay_packetradio_setup_port(interface);
|
2012-12-04 04:17:57 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if (ifconfig->socket_type==SOCK_STREAM){
|
|
|
|
interface->slip_decode_state.dst_offset=0;
|
2013-02-14 19:37:51 +00:00
|
|
|
// The encapsulation type should be configurable,
|
|
|
|
// but for now default to the one that should be safe on the RFD900
|
|
|
|
// radios, and that also allows us to receive RSSI reports inline
|
|
|
|
interface->slip_decode_state.encapsulator=SLIP_FORMAT_UPPER7;
|
2013-02-04 20:03:52 +00:00
|
|
|
interface->alarm.poll.events=POLLIN;
|
|
|
|
watch(&interface->alarm);
|
2013-02-14 03:48:56 +00:00
|
|
|
}else if(ifconfig->socket_type==SOCK_FILE){
|
|
|
|
/* Seek to end of file as initial reading point */
|
|
|
|
interface->recv_offset = lseek(interface->alarm.poll.fd,0,SEEK_END);
|
2013-02-04 11:36:49 +00:00
|
|
|
}
|
2011-08-13 11:17:49 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +00:00
|
|
|
|
|
|
|
// schedule the first tick asap
|
|
|
|
interface->alarm.alarm=gettime_ms();
|
|
|
|
interface->alarm.deadline=interface->alarm.alarm;
|
|
|
|
schedule(&interface->alarm);
|
|
|
|
interface->state=INTERFACE_STATE_UP;
|
|
|
|
INFOF("Interface %s addr %s:%d, is up",interface->name,
|
|
|
|
inet_ntoa(interface->address.sin_addr), ntohs(interface->address.sin_port));
|
|
|
|
|
|
|
|
directory_registration();
|
|
|
|
|
2012-12-13 07:20:09 +00:00
|
|
|
INFOF("Allowing a maximum of %d packets every %lldms", interface->transfer_limit.burst_size, interface->transfer_limit.burst_length);
|
2011-08-12 07:47:29 +00:00
|
|
|
|
2011-08-08 06:41:05 +00:00
|
|
|
overlay_interface_count++;
|
|
|
|
return 0;
|
2013-02-14 03:48:56 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (interface->alarm.poll.fd>=0){
|
|
|
|
unwatch(&interface->alarm);
|
|
|
|
close(interface->alarm.poll.fd);
|
|
|
|
interface->alarm.poll.fd=-1;
|
|
|
|
}
|
|
|
|
interface->state=INTERFACE_STATE_DOWN;
|
|
|
|
return -1;
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
static void interface_read_dgram(struct overlay_interface *interface){
|
|
|
|
int plen=0;
|
|
|
|
unsigned char packet[8096];
|
|
|
|
|
|
|
|
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(interface->alarm.poll.fd,packet, sizeof(packet), &recvttl, &src_addr, &addrlen);
|
|
|
|
if (plen == -1) {
|
|
|
|
WHY_perror("recvwithttl(c)");
|
|
|
|
overlay_interface_close(interface);
|
2012-07-06 00:28:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
/* We have a frame from this interface */
|
|
|
|
if (config.debug.packetrx)
|
|
|
|
DEBUG_packet_visualise("Read from real interface", packet,plen);
|
|
|
|
if (config.debug.overlayinterfaces) {
|
|
|
|
struct in_addr src = ((struct sockaddr_in *)&src_addr)->sin_addr; // avoid strict-alias warning on Solaris (gcc 4.4)
|
|
|
|
DEBUGF("Received %d bytes from %s on interface %s",plen,
|
|
|
|
inet_ntoa(src),
|
|
|
|
interface->name);
|
|
|
|
}
|
|
|
|
if (packetOkOverlay(interface, packet, plen, recvttl, &src_addr, addrlen)) {
|
|
|
|
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-06-22 03:55:41 +00:00
|
|
|
}
|
2012-06-21 07:32:36 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
struct file_packet{
|
2012-12-08 04:39:41 +00:00
|
|
|
struct sockaddr_in src_addr;
|
|
|
|
struct sockaddr_in dst_addr;
|
|
|
|
int pid;
|
|
|
|
int payload_length;
|
|
|
|
|
|
|
|
/* TODO ? ;
|
2013-02-14 03:48:56 +00:00
|
|
|
half-power beam height (uint16)
|
|
|
|
half-power beam width (uint16)
|
|
|
|
range in metres, centre beam (uint32)
|
|
|
|
latitude (uint32)
|
|
|
|
longitude (uint32)
|
|
|
|
X/Z direction (uint16)
|
|
|
|
Y direction (uint16)
|
|
|
|
speed in metres per second (uint16)
|
|
|
|
TX frequency in Hz, uncorrected for doppler (which must be done at the receiving end to take into account
|
|
|
|
relative motion)
|
|
|
|
coding method (use for doppler response etc) null terminated string
|
|
|
|
*/
|
2012-12-08 04:39:41 +00:00
|
|
|
|
|
|
|
unsigned char payload[1400];
|
|
|
|
};
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
static void interface_read_file(struct overlay_interface *interface)
|
2011-08-08 06:41:05 +00:00
|
|
|
{
|
|
|
|
/* Grab packets, unpackage and dispatch frames to consumers */
|
2013-02-14 03:48:56 +00:00
|
|
|
struct file_packet packet;
|
2012-08-09 02:44:32 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
2013-02-14 03:48:56 +00:00
|
|
|
|
|
|
|
/* Read from interface file */
|
|
|
|
long long length=lseek(interface->alarm.poll.fd,0,SEEK_END);
|
2012-09-19 23:48:41 +00:00
|
|
|
|
|
|
|
int new_packets = (length - interface->recv_offset) / sizeof packet;
|
|
|
|
if (new_packets > 20)
|
|
|
|
WARNF("Getting behind, there are %d unread packets", new_packets);
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if (interface->recv_offset<length){
|
|
|
|
if (lseek(interface->alarm.poll.fd,interface->recv_offset,SEEK_SET) == -1){
|
2012-07-31 08:19:24 +00:00
|
|
|
WHY_perror("lseek");
|
2012-12-08 04:39:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces)
|
2012-12-08 04:39:41 +00:00
|
|
|
DEBUGF("Read interface %s (size=%lld) at offset=%d",interface->name, length, interface->recv_offset);
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
ssize_t nread = read(interface->alarm.poll.fd, &packet, sizeof packet);
|
2012-12-08 04:39:41 +00:00
|
|
|
if (nread == -1){
|
|
|
|
WHY_perror("read");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread == sizeof packet) {
|
|
|
|
interface->recv_offset += nread;
|
2013-02-14 03:48:56 +00:00
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packetrx)
|
2012-12-08 04:39:41 +00:00
|
|
|
DEBUG_packet_visualise("Read from dummy interface", packet.payload, packet.payload_length);
|
|
|
|
|
2013-01-29 00:57:13 +00:00
|
|
|
if (((!interface->drop_unicasts) && memcmp(&packet.dst_addr, &interface->address, sizeof(packet.dst_addr))==0) ||
|
2012-12-08 04:39:41 +00:00
|
|
|
((!interface->drop_broadcasts) &&
|
2013-02-04 19:12:50 +00:00
|
|
|
memcmp(&packet.dst_addr, &interface->broadcast_address, sizeof(packet.dst_addr))==0)){
|
2013-02-14 03:48:56 +00:00
|
|
|
|
2013-02-14 06:47:45 +00:00
|
|
|
if (packetOkOverlay(interface, packet.payload, packet.payload_length, -1,
|
|
|
|
(struct sockaddr*)&packet.src_addr, sizeof(packet.src_addr))<0) {
|
|
|
|
WARN("Unsupported packet from dummy interface");
|
|
|
|
}
|
|
|
|
}else if (config.debug.packetrx)
|
|
|
|
DEBUGF("Ignoring packet addressed to %s:%d", inet_ntoa(packet.dst_addr.sin_addr), ntohs(packet.dst_addr.sin_port));
|
2012-01-10 03:35:26 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
if (interface->recv_offset>=length){
|
|
|
|
if (interface->alarm.alarm == -1 || now + 5 < interface->alarm.alarm){
|
|
|
|
interface->alarm.alarm = now + 5;
|
|
|
|
interface->alarm.deadline = interface->alarm.alarm + 10000;
|
|
|
|
}
|
|
|
|
}else{
|
2012-07-31 08:19:24 +00:00
|
|
|
/* keep reading new packets as fast as possible,
|
2013-02-14 03:48:56 +00:00
|
|
|
but don't completely prevent other high priority alarms */
|
|
|
|
if (interface->alarm.alarm == -1 || now < interface->alarm.alarm){
|
|
|
|
interface->alarm.alarm = now;
|
|
|
|
interface->alarm.deadline = interface->alarm.alarm + 10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void interface_read_stream(struct overlay_interface *interface){
|
|
|
|
unsigned char buffer[OVERLAY_INTERFACE_RX_BUFFER_SIZE];
|
|
|
|
ssize_t nread = read(interface->alarm.poll.fd, buffer, OVERLAY_INTERFACE_RX_BUFFER_SIZE);
|
|
|
|
if (nread == -1){
|
2013-02-14 06:47:45 +00:00
|
|
|
WHY_perror("read");
|
2013-02-14 03:48:56 +00:00
|
|
|
return;
|
2012-07-31 08:19:24 +00:00
|
|
|
}
|
2013-02-14 22:41:08 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
struct slip_decode_state *state=&interface->slip_decode_state;
|
|
|
|
|
2013-02-14 22:41:08 +00:00
|
|
|
if (config.debug.slip)
|
|
|
|
dump("RX bytes",&state->src[state->src_offset],
|
|
|
|
state->src_size-state->src_offset);
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
state->src=buffer;
|
|
|
|
state->src_size=nread;
|
|
|
|
state->src_offset=0;
|
|
|
|
|
|
|
|
while (state->src_offset < state->src_size) {
|
|
|
|
int ret = slip_decode(state);
|
|
|
|
if (ret==1){
|
2013-02-14 22:41:08 +00:00
|
|
|
packetOkOverlay(interface, state->dst, state->packet_length, -1, NULL, -1);
|
2013-02-14 03:48:56 +00:00
|
|
|
state->dst_offset=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_stream_buffer(overlay_interface *interface){
|
|
|
|
if (interface->tx_bytes_pending>0) {
|
|
|
|
int written=write(interface->alarm.poll.fd,interface->txbuffer,
|
|
|
|
interface->tx_bytes_pending);
|
|
|
|
if (config.debug.packetradio) DEBUGF("Trying to write %d bytes",
|
|
|
|
interface->tx_bytes_pending);
|
|
|
|
if (written>0) {
|
|
|
|
interface->tx_bytes_pending-=written;
|
|
|
|
bcopy(&interface->txbuffer[written],&interface->txbuffer[0],
|
|
|
|
interface->tx_bytes_pending);
|
|
|
|
if (config.debug.packetradio) DEBUGF("Wrote %d bytes (%d left pending)",
|
|
|
|
written,interface->tx_bytes_pending);
|
|
|
|
} else {
|
|
|
|
if (config.debug.packetradio) DEBUGF("Failed to write any data");
|
|
|
|
}
|
2012-09-19 23:48:41 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
if (interface->tx_bytes_pending>0) {
|
|
|
|
// more to write, so keep POLLOUT flag
|
|
|
|
interface->alarm.poll.events|=POLLOUT;
|
|
|
|
} else {
|
|
|
|
// nothing more to write, so clear POLLOUT flag
|
|
|
|
interface->alarm.poll.events&=~POLLOUT;
|
|
|
|
// try to empty another packet from the queue ASAP
|
|
|
|
overlay_queue_schedule_next(gettime_ms());
|
|
|
|
}
|
|
|
|
watch(&interface->alarm);
|
|
|
|
}
|
|
|
|
|
2012-06-21 07:32:36 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
static void overlay_interface_poll(struct sched_ent *alarm)
|
|
|
|
{
|
|
|
|
struct overlay_interface *interface = (overlay_interface *)alarm;
|
|
|
|
|
|
|
|
if (alarm->poll.revents==0){
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
|
|
|
|
if (interface->state==INTERFACE_STATE_UP && interface->tick_ms>0 && now >= interface->last_tick_ms+interface->tick_ms){
|
|
|
|
// tick the interface
|
|
|
|
overlay_route_queue_advertisements(interface);
|
|
|
|
interface->last_tick_ms=now;
|
|
|
|
alarm->alarm=interface->last_tick_ms+interface->tick_ms;
|
|
|
|
}else{
|
|
|
|
alarm->alarm=-1;
|
|
|
|
}
|
|
|
|
alarm->deadline=alarm->alarm+interface->tick_ms/2;
|
|
|
|
|
|
|
|
switch(interface->socket_type){
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
case SOCK_STREAM:
|
|
|
|
break;
|
|
|
|
case SOCK_FILE:
|
|
|
|
interface_read_file(interface);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (alarm->alarm!=-1)
|
|
|
|
schedule(alarm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alarm->poll.revents & POLLOUT){
|
|
|
|
switch(interface->socket_type){
|
|
|
|
case SOCK_STREAM:
|
|
|
|
write_stream_buffer(interface);
|
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
case SOCK_FILE:
|
|
|
|
//XXX error? fatal?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alarm->poll.revents & POLLIN) {
|
|
|
|
switch(interface->socket_type){
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
interface_read_dgram(interface);
|
|
|
|
break;
|
|
|
|
case SOCK_STREAM:
|
|
|
|
interface_read_stream(interface);
|
|
|
|
break;
|
|
|
|
case SOCK_FILE:
|
|
|
|
interface_read_file(interface);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alarm->poll.revents & (POLLHUP | POLLERR)) {
|
|
|
|
overlay_interface_close(interface);
|
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-20 06:11:06 +00:00
|
|
|
int
|
2013-02-14 03:48:56 +00:00
|
|
|
overlay_broadcast_ensemble(overlay_interface *interface,
|
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-12-11 05:29:46 +00:00
|
|
|
if (config.debug.packettx)
|
2012-04-13 20:56:20 +00:00
|
|
|
{
|
2013-02-14 03:48:56 +00:00
|
|
|
DEBUGF("Sending this packet via interface %s (len=%d)",interface->name,len);
|
2012-08-01 08:24:02 +00:00
|
|
|
DEBUG_packet_visualise(NULL,bytes,len);
|
2012-04-13 20:56:20 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
switch(interface->socket_type){
|
|
|
|
case SOCK_STREAM:
|
2011-08-12 07:47:29 +00:00
|
|
|
{
|
2013-02-14 03:48:56 +00:00
|
|
|
if (interface->tx_bytes_pending>0)
|
|
|
|
return WHYF("Cannot send two packets to a stream at the same time");
|
|
|
|
|
|
|
|
/* Encode packet with SLIP escaping.
|
|
|
|
XXX - Add error correction here also */
|
|
|
|
unsigned char *buffer = interface->txbuffer;
|
|
|
|
int out_len=0;
|
|
|
|
|
2013-02-14 19:37:51 +00:00
|
|
|
int encoded = slip_encode(SLIP_FORMAT_UPPER7,
|
|
|
|
bytes, len, buffer+out_len, sizeof(interface->txbuffer) - out_len);
|
2013-02-14 03:48:56 +00:00
|
|
|
if (encoded < 0)
|
|
|
|
return WHY("Buffer overflow");
|
2013-02-14 20:11:40 +00:00
|
|
|
|
|
|
|
if (config.debug.slip)
|
|
|
|
{
|
|
|
|
// Test decoding of the packet we send
|
|
|
|
struct slip_decode_state state;
|
|
|
|
state.encapsulator=SLIP_FORMAT_UPPER7;
|
|
|
|
state.src_size=encoded;
|
|
|
|
state.src_offset=0;
|
|
|
|
state.src=buffer+out_len;
|
|
|
|
slip_decode(&state);
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
out_len+=encoded;
|
2012-12-08 04:39:41 +00:00
|
|
|
|
2013-02-14 03:48:56 +00:00
|
|
|
interface->tx_bytes_pending=out_len;
|
|
|
|
write_stream_buffer(interface);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SOCK_FILE:
|
|
|
|
{
|
|
|
|
struct file_packet packet={
|
2012-12-08 04:39:41 +00:00
|
|
|
.src_addr = interface->address,
|
|
|
|
.dst_addr = *recipientaddr,
|
|
|
|
.pid = getpid(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (len > sizeof(packet.payload)){
|
|
|
|
WARN("Truncating long packet to fit within MTU byte limit for dummy interface");
|
|
|
|
len = sizeof(packet.payload);
|
2011-08-14 15:58:27 +00:00
|
|
|
}
|
2012-12-08 04:39:41 +00:00
|
|
|
packet.payload_length=len;
|
|
|
|
bcopy(bytes, packet.payload, 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
|
2013-02-14 03:48:56 +00:00
|
|
|
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. */
|
2012-07-03 06:06:51 +00:00
|
|
|
off_t fsize = lseek(interface->alarm.poll.fd, (off_t) 0, SEEK_END);
|
2013-02-14 17:32:19 +00:00
|
|
|
/* Don't complain if the seek fails because we are writing to a pipe or device that does
|
|
|
|
not support seeking. */
|
|
|
|
if (errno!=ESPIPE) {
|
|
|
|
if (fsize == -1)
|
|
|
|
return WHY_perror("lseek");
|
|
|
|
if (config.debug.overlayinterfaces)
|
|
|
|
DEBUGF("Write to interface %s at offset=%d", interface->name, fsize);
|
|
|
|
}
|
2012-12-08 04:39:41 +00:00
|
|
|
ssize_t nwrite = write(interface->alarm.poll.fd, &packet, sizeof(packet));
|
2012-07-03 06:06:51 +00:00
|
|
|
if (nwrite == -1)
|
|
|
|
return WHY_perror("write");
|
2012-12-08 04:39:41 +00:00
|
|
|
if (nwrite != sizeof(packet))
|
|
|
|
return WHYF("only wrote %lld of %lld bytes", nwrite, sizeof(packet));
|
2012-07-03 06:06:51 +00:00
|
|
|
return 0;
|
2011-08-12 07:47:29 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +00:00
|
|
|
|
|
|
|
case SOCK_DGRAM:
|
2011-08-13 11:17:49 +00:00
|
|
|
{
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces)
|
2012-09-11 05:50:44 +00:00
|
|
|
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-11-12 04:11:14 +00:00
|
|
|
int e=errno;
|
2012-07-25 07:23:44 +00:00
|
|
|
WHY_perror("sendto(c)");
|
2012-11-12 04:11:14 +00:00
|
|
|
// only close the interface on some kinds of errors
|
|
|
|
if (e==ENETDOWN || e==EINVAL)
|
|
|
|
overlay_interface_close(interface);
|
2012-07-25 07:23:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-07-03 06:06:51 +00:00
|
|
|
return 0;
|
2011-08-13 11:17:49 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return WHY("Unsupported socket type");
|
|
|
|
}
|
2011-08-08 06:41:05 +00:00
|
|
|
}
|
2011-08-08 14:41:46 +00:00
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
/* Register the real interface, or update the existing interface registration. */
|
2012-06-08 08:59:27 +00:00
|
|
|
int
|
|
|
|
overlay_interface_register(char *name,
|
2012-08-08 05:27:27 +00:00
|
|
|
struct in_addr addr,
|
2012-12-04 03:42:28 +00:00
|
|
|
struct in_addr mask)
|
|
|
|
{
|
2012-08-08 05:27:27 +00:00
|
|
|
struct in_addr broadcast = {.s_addr = addr.s_addr | ~mask.s_addr};
|
2012-12-04 03:42:28 +00:00
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces) {
|
2012-08-08 05:27:27 +00:00
|
|
|
// 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-12-04 03:42:28 +00:00
|
|
|
|
|
|
|
// Find the matching non-dummy interface rule.
|
|
|
|
const struct config_network_interface *ifconfig = NULL;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < config.interfaces.ac; ++i, ifconfig = NULL) {
|
|
|
|
ifconfig = &config.interfaces.av[i].value;
|
2013-02-14 03:48:56 +00:00
|
|
|
if (ifconfig->socket_type==SOCK_DGRAM) {
|
2012-12-04 03:42:28 +00:00
|
|
|
int j;
|
2012-12-10 04:48:22 +00:00
|
|
|
for (j = 0; j < ifconfig->match.patc; ++j){
|
2012-12-04 03:42:28 +00:00
|
|
|
if (fnmatch(ifconfig->match.patv[j], name, 0) == 0)
|
|
|
|
break;
|
2012-12-10 04:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (j < ifconfig->match.patc)
|
|
|
|
break;
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ifconfig == NULL) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces)
|
2012-12-04 03:42:28 +00:00
|
|
|
DEBUGF("Interface %s does not match any rule", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ifconfig->exclude) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.overlayinterfaces)
|
2012-12-04 03:42:28 +00:00
|
|
|
DEBUGF("Interface %s is explicitly excluded", name);
|
2012-06-08 08:59:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-25 07:23:44 +00:00
|
|
|
/* Search in the exist list of interfaces */
|
2012-12-04 03:42:28 +00:00
|
|
|
int found_interface= -1;
|
2012-07-25 07:23:44 +00:00
|
|
|
for(i = 0; i < overlay_interface_count; i++){
|
|
|
|
int broadcast_match = 0;
|
|
|
|
int name_match =0;
|
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
if (overlay_interfaces[i].broadcast_address.sin_addr.s_addr == broadcast.s_addr)
|
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-12-04 03:42:28 +00:00
|
|
|
if (overlay_interface_init(name, addr, mask, broadcast, ifconfig))
|
2012-07-25 07:23:44 +00:00
|
|
|
return WHYF("Could not initialise newly seen interface %s", name);
|
|
|
|
else
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.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-12-04 03:42:28 +00:00
|
|
|
void overlay_interface_discover(struct sched_ent *alarm)
|
|
|
|
{
|
2012-07-25 07:23:44 +00:00
|
|
|
/* Mark all UP interfaces as DETECTING, so we can tell which interfaces are new, and which are dead */
|
2012-12-04 03:42:28 +00:00
|
|
|
int i;
|
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
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
/* Register new dummy interfaces */
|
|
|
|
int detect_real_interfaces = 0;
|
|
|
|
const struct config_network_interface *ifconfig = NULL;
|
|
|
|
for (i = 0; i < config.interfaces.ac; ++i, ifconfig = NULL) {
|
|
|
|
ifconfig = &config.interfaces.av[i].value;
|
2013-02-14 03:48:56 +00:00
|
|
|
if (ifconfig->socket_type==SOCK_DGRAM) {
|
2012-07-25 07:23:44 +00:00
|
|
|
detect_real_interfaces = 1;
|
2012-06-15 05:34:36 +00:00
|
|
|
continue;
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
2013-02-04 11:36:49 +00:00
|
|
|
int j;
|
2013-02-14 03:48:56 +00:00
|
|
|
for (j = 0; j < overlay_interface_count; j++){
|
|
|
|
if (overlay_interfaces[i].socket_type == ifconfig->socket_type &&
|
|
|
|
strcasecmp(overlay_interfaces[j].name, ifconfig->file) == 0 &&
|
|
|
|
overlay_interfaces[j].state==INTERFACE_STATE_DETECTING){
|
|
|
|
overlay_interfaces[j].state=INTERFACE_STATE_UP;
|
2012-06-15 05:34:36 +00:00
|
|
|
break;
|
2012-07-25 07:23:44 +00:00
|
|
|
}
|
2013-02-14 03:48:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 11:36:49 +00:00
|
|
|
if (j >= overlay_interface_count) {
|
2012-12-04 03:42:28 +00:00
|
|
|
// New dummy interface, so register it.
|
2013-02-05 06:26:37 +00:00
|
|
|
struct in_addr dummyaddr = hton_in_addr(INADDR_NONE);
|
2013-02-14 03:48:56 +00:00
|
|
|
overlay_interface_init(ifconfig->file, dummyaddr, dummyaddr, dummyaddr, ifconfig);
|
2011-08-13 11:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-15 05:34:36 +00:00
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
// Register new real interfaces
|
|
|
|
if (detect_real_interfaces) {
|
2012-07-25 07:23:44 +00:00
|
|
|
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
|
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-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-12-04 03:42:28 +00:00
|
|
|
|
|
|
|
// Close any interfaces that have gone away.
|
2012-07-25 07:23:44 +00:00
|
|
|
for(i = 0; i < overlay_interface_count; i++)
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_DETECTING)
|
|
|
|
overlay_interface_close(&overlay_interfaces[i]);
|
2012-12-04 03:42:28 +00:00
|
|
|
|
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-08-31 02:41:31 +00:00
|
|
|
static void
|
2012-10-16 06:16:52 +00:00
|
|
|
logServalPacket(int level, struct __sourceloc __whence, const char *message, const unsigned char *packet, size_t len) {
|
2012-08-03 07:14:05 +00:00
|
|
|
struct mallocbuf mb = STRUCT_MALLOCBUF_NULL;
|
2013-02-04 20:20:06 +00:00
|
|
|
if (!message) message="<no message>";
|
2012-08-03 07:14:05 +00:00
|
|
|
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-10-16 06:16:52 +00:00
|
|
|
logString(level, __whence, mb.buffer);
|
2012-08-03 07:14:05 +00:00
|
|
|
if (mb.buffer)
|
|
|
|
free(mb.buffer);
|
2012-08-01 08:24:02 +00:00
|
|
|
}
|