| 1 |
#include <config.h> |
| 2 |
#include <unistd.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <syslog.h> |
| 6 |
#include <signal.h> |
| 7 |
#include <string.h> |
| 8 |
#include <time.h> |
| 9 |
|
| 10 |
#include <sys/types.h> |
| 11 |
#include <sys/socket.h> |
| 12 |
|
| 13 |
#include <netax25/ax25.h> |
| 14 |
#include <netrose/rose.h> |
| 15 |
|
| 16 |
#include <netax25/axlib.h> |
| 17 |
#include <netax25/axconfig.h> |
| 18 |
#include <netax25/daemon.h> |
| 19 |
|
| 20 |
#include <scm-version.h> |
| 21 |
|
| 22 |
static int logging = FALSE; |
| 23 |
static int mail = FALSE; |
| 24 |
static int single = FALSE; |
| 25 |
|
| 26 |
#define STR_USAGE "usage: beacon [-c <src_call>] [-d <dest_call>] [-f] [-H] [-l] [-m] [-s] [-t interval] [-v] <port> <message>\n" |
| 27 |
|
| 28 |
static void terminate(int sig) |
| 29 |
{ |
| 30 |
if (logging) { |
| 31 |
syslog(LOG_INFO, "terminating on SIGTERM\n"); |
| 32 |
closelog(); |
| 33 |
} |
| 34 |
|
| 35 |
exit(0); |
| 36 |
} |
| 37 |
|
| 38 |
int main(int argc, char *argv[]) |
| 39 |
{ |
| 40 |
struct full_sockaddr_ax25 dest; |
| 41 |
struct full_sockaddr_ax25 src; |
| 42 |
int s, n, dlen, len; |
| 43 |
char *addr, *port, *message, *portcall; |
| 44 |
char *srccall = NULL, *destcall = NULL; |
| 45 |
int dofork = 1; |
| 46 |
time_t interval = 30; |
| 47 |
int interval_relative = 0; |
| 48 |
|
| 49 |
while ((n = getopt(argc, argv, "c:d:fHlmst:v")) != -1) { |
| 50 |
switch (n) { |
| 51 |
case 'c': |
| 52 |
srccall = optarg; |
| 53 |
break; |
| 54 |
case 'd': |
| 55 |
destcall = optarg; |
| 56 |
break; |
| 57 |
case 'f': |
| 58 |
dofork = 0; |
| 59 |
break; |
| 60 |
case 'H': |
| 61 |
interval_relative = 1; |
| 62 |
break; |
| 63 |
case 'l': |
| 64 |
logging = TRUE; |
| 65 |
break; |
| 66 |
case 'm': |
| 67 |
mail = TRUE; |
| 68 |
/* falls through */ |
| 69 |
case 's': |
| 70 |
single = TRUE; |
| 71 |
break; |
| 72 |
case 't': |
| 73 |
interval = (time_t ) atoi(optarg); |
| 74 |
if (interval < 1) { |
| 75 |
fprintf(stderr, "beacon: interval must be greater than on minute\n"); |
| 76 |
return 1; |
| 77 |
} |
| 78 |
break; |
| 79 |
case 'v': |
| 80 |
printf("beacon: %s\n", FULL_VER); |
| 81 |
return 0; |
| 82 |
case '?': |
| 83 |
case ':': |
| 84 |
fprintf(stderr, STR_USAGE); |
| 85 |
return 1; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (interval_relative && interval > 60L) { |
| 90 |
fprintf(stderr, "beacon: can't align interval > 60min to an hour\n"); |
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
signal(SIGTERM, terminate); |
| 95 |
|
| 96 |
if (optind == argc || optind == argc - 1) { |
| 97 |
fprintf(stderr, STR_USAGE); |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
port = argv[optind]; |
| 102 |
message = argv[optind + 1]; |
| 103 |
|
| 104 |
if (ax25_config_load_ports() == 0) { |
| 105 |
fprintf(stderr, "beacon: no AX.25 ports defined\n"); |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
portcall = ax25_config_get_addr(port); |
| 110 |
if (portcall == NULL) { |
| 111 |
fprintf(stderr, "beacon: invalid AX.25 port setting - %s\n", port); |
| 112 |
return 1; |
| 113 |
} |
| 114 |
|
| 115 |
addr = NULL; |
| 116 |
if (mail) |
| 117 |
addr = strdup("MAIL"); |
| 118 |
else if (destcall != NULL) |
| 119 |
addr = strdup(destcall); |
| 120 |
else |
| 121 |
addr = strdup("IDENT"); |
| 122 |
if (addr == NULL) |
| 123 |
return 1; |
| 124 |
|
| 125 |
dlen = ax25_aton(addr, &dest); |
| 126 |
if (dlen == -1) { |
| 127 |
fprintf(stderr, "beacon: unable to convert callsign '%s'\n", addr); |
| 128 |
return 1; |
| 129 |
} |
| 130 |
if (addr != NULL) { |
| 131 |
free(addr); |
| 132 |
addr = NULL; |
| 133 |
} |
| 134 |
|
| 135 |
if (srccall != NULL && strcmp(srccall, portcall) != 0) { |
| 136 |
addr = malloc(strlen(srccall) + 1 + strlen(portcall) + 1); |
| 137 |
if (addr == NULL) |
| 138 |
return 1; |
| 139 |
sprintf(addr, "%s %s", srccall, portcall); |
| 140 |
} else { |
| 141 |
addr = strdup(portcall); |
| 142 |
if (addr == NULL) |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
len = ax25_aton(addr, &src); |
| 147 |
if (len == -1) { |
| 148 |
fprintf(stderr, "beacon: unable to convert callsign '%s'\n", addr); |
| 149 |
return 1; |
| 150 |
} |
| 151 |
if (addr != NULL) { |
| 152 |
free(addr); |
| 153 |
addr = NULL; |
| 154 |
} |
| 155 |
|
| 156 |
if (!single && dofork) { |
| 157 |
if (!daemon_start(FALSE)) { |
| 158 |
fprintf(stderr, "beacon: cannot become a daemon\n"); |
| 159 |
return 1; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (logging) { |
| 164 |
openlog("beacon", LOG_PID, LOG_DAEMON); |
| 165 |
syslog(LOG_INFO, "starting"); |
| 166 |
} |
| 167 |
|
| 168 |
/* interval has a one minute resolution */ |
| 169 |
interval *= 60L; |
| 170 |
|
| 171 |
for (;;) { |
| 172 |
|
| 173 |
if (interval_relative) { |
| 174 |
time_t t_sleep = interval - (time(NULL) % interval); |
| 175 |
if (t_sleep > 0) |
| 176 |
sleep(t_sleep); |
| 177 |
} |
| 178 |
|
| 179 |
s = socket(AF_AX25, SOCK_DGRAM, 0); |
| 180 |
if (s == -1) { |
| 181 |
if (logging) { |
| 182 |
syslog(LOG_ERR, "socket: %m"); |
| 183 |
closelog(); |
| 184 |
} |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
if (bind(s, (struct sockaddr *)&src, len) == -1) { |
| 189 |
if (logging) { |
| 190 |
syslog(LOG_ERR, "bind: %m"); |
| 191 |
closelog(); |
| 192 |
} |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
if (sendto(s, message, strlen(message), 0, (struct sockaddr *)&dest, dlen) == -1) { |
| 197 |
if (logging) { |
| 198 |
syslog(LOG_ERR, "sendto: %m"); |
| 199 |
closelog(); |
| 200 |
} |
| 201 |
return 1; |
| 202 |
} |
| 203 |
|
| 204 |
close(s); |
| 205 |
|
| 206 |
if (single) |
| 207 |
break; |
| 208 |
|
| 209 |
if (!interval_relative) |
| 210 |
sleep(interval); |
| 211 |
} |
| 212 |
|
| 213 |
return 0; |
| 214 |
} |