uronode/axdigi.c

307 lines
7.1 KiB
C
Raw Normal View History

2015-09-03 20:01:45 +01:00
/*
* axdigi: Cross and straight port digipeater program
* Copyright (C) 1995 Craig Small VK2XLZ
2015-11-22 14:56:30 +00:00
* modificatioins 2012-present Brian N1URO
2015-09-03 20:01:45 +01:00
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* JSN - Small tweaks to ensure compilation and execution under Linux 2.1.x.
* 12th June 1997.
2022-02-05 11:58:00 +00:00
*
* Thanks to Helmut Grohne for a minor patch
*
2015-09-03 20:01:45 +01:00
*/
2022-02-05 11:58:00 +00:00
#include <net/if.h>
2015-09-03 20:01:45 +01:00
#include <linux/if_ether.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
/* below added by N1URO */
#include <string.h>
#include <unistd.h>
2016-04-01 16:06:32 +01:00
#include <fcntl.h>
#include <error.h>
2015-11-22 14:56:30 +00:00
#include <signal.h>
2015-09-03 20:01:45 +01:00
#include <linux/ax25.h>
2015-11-22 14:56:30 +00:00
/* added by N1URO */
#include <netax25/daemon.h>
2017-01-21 14:34:12 +00:00
#include "node.h"
2015-09-03 20:01:45 +01:00
int recv_packet(unsigned char *buf, int size, unsigned char *port);
void print_call(unsigned char *buf);
unsigned char *find_call(char *port);
void add_port(char *call, char *port);
void get_interfaces(int skt);
/*
* The defines we use
*/
#define AXALEN 7
#define E_BIT 0x01 /* Address extension bit */
#define REPEATED 0x80 /* Has-been-repeated bit */
#define MAX_PORTS 16
2017-01-21 14:34:12 +00:00
// #define VERSION "0.3"
2015-11-22 14:56:30 +00:00
#define AXDIGI_PID_FILE "/var/run/axdigi.pid"
2015-09-03 20:01:45 +01:00
int port_count = 0;
unsigned char portname[MAX_PORTS][20];
unsigned char portcall[MAX_PORTS][8];
2015-11-22 14:56:30 +00:00
void(*sigterm_defhnd)(int);
2016-04-01 16:06:32 +01:00
void quit_handler(int sig)
2015-11-22 14:56:30 +00:00
{
unlink(AXDIGI_PID_FILE);
2016-04-01 16:06:32 +01:00
signal(SIGTERM, SIG_IGN);
fprintf(stderr, "axDigi quitting.\n\r");
signal(SIGTERM, sigterm_defhnd);
2015-11-22 14:56:30 +00:00
raise(SIGTERM);
return;
}
2015-09-03 20:01:45 +01:00
2016-04-01 16:06:32 +01:00
void hup_handler(int sig)
{
signal(SIGHUP, SIG_IGN);
fprintf(stderr, "SIGHUP caught by axDigi.\n\r");
signal(SIGHUP, hup_handler); /* Restore hangup handler */
}
2015-09-03 20:01:45 +01:00
int main(int argc, char *argv[])
{
int skt;
int size, rt;
unsigned char buf[4096];
struct sockaddr sa;
int asize;
2015-11-22 14:56:30 +00:00
FILE *pidfile;
2015-09-03 20:01:45 +01:00
/* Check our huge range of flags */
if (argc > 1)
{
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "-h") ==0)
{
2017-01-21 14:34:12 +00:00
printf("axdigi: version %s.\n\r", VERSION);
printf("Copywrite (c) 1995 Craig Small - VK2XLZ\n\r");
2016-04-01 16:06:32 +01:00
printf("modificatiions Copyright (C) 2012-present by Brian N1URO\n\n");
2017-01-21 14:34:12 +00:00
printf("axdigi comes with ABSOLUTELY NO WARRANTY.\n\r");
printf("This is free software, and you are welcome to redistribute it\n\r");
printf("under the terms of GNU General Public Licence as published\n\r");
printf("by Free Software Foundation; either version 2 of the License, or\n\r");
printf("(at your option) any later version.\n\r");
2015-09-03 20:01:45 +01:00
return 0;
}
2015-11-22 14:56:30 +00:00
}
/* Routine to daemonize - added by N1URO */
2016-04-01 16:06:32 +01:00
2015-11-22 14:56:30 +00:00
if (!daemon_start(TRUE)) {
fprintf(stderr, "Sorry, axdigi cannot become a daemon\n");
return 1;
}
2015-09-03 20:01:45 +01:00
/* Change to keep code more modern - N1URO */
if ((skt = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_AX25))) == -1)
{
perror("socket");
return(1);
}
2015-11-22 14:56:30 +00:00
pidfile = fopen(AXDIGI_PID_FILE, "w");
fprintf(pidfile, "%d\n", (int)getpid());
2017-01-21 14:34:12 +00:00
fprintf(stderr, "axDigi started. \n");
2015-11-22 14:56:30 +00:00
fclose(pidfile);
2016-04-01 16:06:32 +01:00
signal(SIGHUP, hup_handler);
sigterm_defhnd = signal(SIGTERM, quit_handler);
2015-09-03 20:01:45 +01:00
get_interfaces(skt);
while(1)
{
asize = sizeof(sa);
if ((size = recvfrom(skt, buf, sizeof(buf), 0, &sa, &asize)) == -1)
{
perror("recv");
exit(1);
}
if ((rt = recv_packet(buf, size, sa.sa_data)) >= 0)
{
if (rt < port_count)
{
asize = sizeof(sa);
strcpy(sa.sa_data, portname[rt]);
if (sendto(skt, buf, size, 0, &sa, asize) == -1)
perror("sendto");
continue;
}
/* printf("Unknown port %s\n", sa.sa_data);*/
} /* recv_packet true */
} /* while(1) */
2015-11-22 14:56:30 +00:00
2015-09-03 20:01:45 +01:00
close(skt);
}
int recv_packet(unsigned char *buf, int size, unsigned char *port)
{
unsigned char *bptr;
int count, i;
unsigned char *call;
/* printf("Got packet size %d\n", size);*/
/*
* Decode the AX.25 Packet
*/
/* Find packet, skip over flag */
bptr = buf+1;
/* Now at destination address */
/* print_call(bptr);
printf("<-");*/
bptr += AXALEN;
/* Now at source address */
/* print_call(bptr);*/
if (bptr[6] & E_BIT)
{
/* printf("\n");*/
return -1; /* No digis, we're not interested */
}
/* printf(" ");*/
bptr += AXALEN;
/* Now at digipeaters */
count = 0;
while( count < AX25_MAX_DIGIS && ( (bptr - buf) < size))
{
/* print_call(bptr);
printf(", ");*/
if (bptr[6] & REPEATED)
{
/* This one has been repeated, move to next one */
bptr += AXALEN;
count++;
continue;
}
/* Check to see if callsign is one of ours */
for (i = 0; i < port_count; i++)
{
/* printf("compare ");
print_call(bptr);
printf(" ");
print_call(portcall[i]);
printf("\n");*/
if ( (bcmp(bptr, portcall[i], AXALEN-1) == 0) && ((bptr[6] & 0x1e) == portcall[i][6]))
{
/* Copy new address over and turn on repeated bit*/
call = find_call(port);
if (call == NULL)
return -1;
bcopy(call, bptr, AXALEN-1);
bptr[6] = (bptr[6] & ~0x1e) | call[6];
bptr[6] |= REPEATED;
return i;
}
} /* for */
return -1;
}
return -1;
}
void print_call(unsigned char *bptr)
{
printf("%c%c%c%c%c%c-%d", bptr[0] >> 1, bptr[1] >> 1,
bptr[2] >> 1, bptr[3] >> 1, bptr[4] >> 1, bptr[5] >> 1,
(bptr[6] >> 1) & 0xf);
}
void add_port(char *call, char *port)
{
unsigned char *s;
int n;
if (port_count == MAX_PORTS)
return;
s = portcall[port_count];
while( (*call != '-') && ( (int)(s - portcall[port_count])< 6))
*s++ = (*call++) << 1;
call++; /* skip over dash */
n = atoi(call);
*s = n << 1;
strcpy(portname[port_count], port);
port_count++;
}
unsigned char *find_call(char *port)
{
static unsigned char callsign[8];
int i;
for(i = 0; i < port_count; i++)
{
if (strcmp(port, portname[i]) == 0)
{
bcopy(portcall[i], callsign, 7);
return callsign;
}
}
return (char*)NULL;
}
void get_interfaces(int skt)
{
char buf[1024];
struct ifconf ifc;
struct ifreq *ifr;
int i;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(skt, SIOCGIFCONF, &ifc) < 0)
{
perror("ioctl");
exit(1);
}
ifr = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
{
if (ioctl(skt, SIOCGIFHWADDR, ifr) < 0)
continue;
if (ifr->ifr_hwaddr.sa_family == AF_AX25)
{
/* AX25 port, add to list */
if (port_count < MAX_PORTS)
{
bcopy(ifr->ifr_hwaddr.sa_data, portcall[port_count], 7);
strcpy(portname[port_count], ifr->ifr_name);
port_count++;
}
}
} /* for */
}
2016-04-01 16:06:32 +01:00
void(*sigterm_defhnd)(int);