summaryrefslogtreecommitdiffstats
path: root/rose/rsdwnlnk.c
blob: decc06d4ed64605a28f977eaeedd068e4cc8dbbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>

#include <config.h>

#include <sys/time.h>
#include <sys/types.h>

#include <sys/socket.h>
#ifdef HAVE_NETAX25_AX25_H
#include <netax25/ax25.h>
#else
#include <netax25/kernel_ax25.h>
#endif
#ifdef HAVE_NETROSE_ROSE_H
#include <netrose/rose.h>
#else 
#include <netax25/kernel_rose.h>
#endif

#include <netax25/axlib.h>
#include <netax25/axconfig.h>

#define	AX25_HBIT	0x80

void alarm_handler(int sig)
{
}

int main(int argc, char **argv)
{
	unsigned char buffer[512], *addr;
	fd_set read_fd;
	int n = 0, s, yes = 1;
	struct full_sockaddr_ax25 axbind, axconnect;
	struct sockaddr_rose rosesock, rosepeer;
	socklen_t addrlen;

	openlog("rsdwnlnk", LOG_PID, LOG_DAEMON);

	/*
	 * Arguments should be "rsdwnlnk ax25port ax25call"
	 */
	if (argc != 3) {
		syslog(LOG_ERR, "invalid number of parameters\n");
		closelog();
		return 1;
	}

	if (ax25_config_load_ports() == 0) {
		syslog(LOG_ERR, "problem with axports file\n");
		closelog();
		return 1;
	}

	addrlen = sizeof(struct sockaddr_rose);

	if (getsockname(STDIN_FILENO, (struct sockaddr *)&rosesock, &addrlen) == -1) {
		syslog(LOG_ERR, "cannot getsockname - %s\n", strerror(errno));
		closelog();
		return 1;
	}

	addrlen = sizeof(struct sockaddr_rose);

	if (getpeername(STDIN_FILENO, (struct sockaddr *)&rosepeer, &addrlen) == -1) {
		syslog(LOG_ERR, "cannot getpeername - %s\n", strerror(errno));
		closelog();
		return 1;
	}

	if (setsockopt(STDIN_FILENO, SOL_ROSE, ROSE_QBITINCL, &yes, sizeof(yes)) == -1) {
		syslog(LOG_ERR, "cannot setsockopt(ROSE_QBITINCL) - %s\n", strerror(errno));
		closelog();
		return 1;
	}

	/*
	 * Parse the passed values for correctness.
	 */
	axbind.fsa_ax25.sax25_family = AF_AX25;
	axbind.fsa_ax25.sax25_ndigis = 1;
	axbind.fsa_ax25.sax25_call   = rosepeer.srose_call;

	if ((addr = ax25_config_get_addr(argv[1])) == NULL) {
		syslog(LOG_ERR, "invalid AX.25 port name - %s\n", argv[1]);
		closelog();
		return 1;
	}

	if (ax25_aton_entry(addr, axbind.fsa_digipeater[0].ax25_call) == -1) {
		syslog(LOG_ERR, "invalid AX.25 port callsign - %s\n", argv[1]);
		closelog();
		return 1;
	}

	axconnect.fsa_ax25.sax25_family = AF_AX25;
	axconnect.fsa_ax25.sax25_call   = rosesock.srose_call;

	/*
	 *	The path at the far end has a digi in it.
	 */
	if (rosepeer.srose_ndigis == 1) {
		axconnect.fsa_digipeater[n] = rosepeer.srose_digi;
		axconnect.fsa_digipeater[n].ax25_call[6] |= AX25_HBIT;
		n++;
	}

	/*
	 *	Incoming call has a different DNIC
	 */
	if (memcmp(rosepeer.srose_addr.rose_addr, rosesock.srose_addr.rose_addr, 2) != 0) {
		addr = rose_ntoa(&rosepeer.srose_addr);
		addr[4] = '\0';
		if (ax25_aton_entry(addr, axconnect.fsa_digipeater[n].ax25_call) == -1) {
			syslog(LOG_ERR, "invalid callsign - %s\n", addr);
			closelog();
			return 1;
		}
		axconnect.fsa_digipeater[n].ax25_call[6] |= AX25_HBIT;
		n++;		
	}

	/*
	 *	Put the remote address sans DNIC into the digi chain.
	 */
	addr = rose_ntoa(&rosepeer.srose_addr);
	if (ax25_aton_entry(addr + 4, axconnect.fsa_digipeater[n].ax25_call) == -1) {
		syslog(LOG_ERR, "invalid callsign - %s\n", addr + 4);
		closelog();
		return 1;
	}
	axconnect.fsa_digipeater[n].ax25_call[6] |= AX25_HBIT;
	n++;		

	/*
	 *	And my local ROSE callsign.
	 */
	if (ax25_aton_entry(argv[2], axconnect.fsa_digipeater[n].ax25_call) == -1) {
		syslog(LOG_ERR, "invalid callsign - %s\n", argv[2]);
		closelog();
		return 1;
	}
	axconnect.fsa_digipeater[n].ax25_call[6] |= AX25_HBIT;
	n++;

	/*
	 *	A digi has been specified for this end.
	 */
	if (rosesock.srose_ndigis == 1) {
		axconnect.fsa_digipeater[n] = rosesock.srose_digi;
		n++;
	}

	axconnect.fsa_ax25.sax25_ndigis = n;

	addrlen = sizeof(struct full_sockaddr_ax25);

	/*
	 * Open the socket into the kernel.
	 */
	if ((s = socket(AF_AX25, SOCK_SEQPACKET, 0)) < 0) {
		syslog(LOG_ERR, "cannot open AX.25 socket, %s\n", strerror(errno));
		closelog();
		return 1;
	}
#ifdef HAVE_AX25_IAMDIGI
	if (setsockopt(s, SOL_AX25, AX25_IAMDIGI, &yes, sizeof(yes)) == -1) {
		syslog(LOG_ERR, "cannot setsockopt(AX25_IAMDIGI), %s\n", strerror(errno));
		close(s);
		closelog();
		return 1;
	}
#endif /* HAVE_AX25_IAMDIGI */
#ifdef HAVE_AX25_PIDINCL
	if (setsockopt(s, SOL_AX25, AX25_PIDINCL, &yes, sizeof(yes)) == -1) {
		syslog(LOG_ERR, "cannot setsockopt(AX25_PIDINCL), %s\n", strerror(errno));
		close(s);
		closelog();
		return 1;
	}
#endif /* HAVE_AX25_PIDINCL */
	/*
	 * Set our AX.25 callsign and AX.25 port callsign accordingly.
	 */
	if (bind(s, (struct sockaddr *)&axbind, addrlen) != 0) {
		syslog(LOG_ERR, "cannot bind AX.25 socket, %s\n", strerror(errno));
		close(s);
		closelog();
		return 1;
	}

	/*
	 * If no response in 60 seconds, go away.
	 */
	alarm(60);

	signal(SIGALRM, alarm_handler);

	/*
	 * Lets try and connect to the far end.
	 */
	if (connect(s, (struct sockaddr *)&axconnect, addrlen) != 0) {
		switch (errno) {
			case ECONNREFUSED:
				strcpy(buffer, "*** Connection refused\r");
				break;
			case ENETUNREACH:
				strcpy(buffer, "*** No known route\r");
				break;
			case EINTR:
				strcpy(buffer, "*** Connection timed out\r");
				break;
			default:
				sprintf(buffer, "ERROR: cannot connect to AX.25 callsign, %s\r", strerror(errno));
				break;
		}

		close(s);

		write(STDOUT_FILENO, buffer, strlen(buffer));
		
		sleep(20);
		
		return 0;
	}

	/*
	 * We got there.
	 */
	alarm(0);

	strcpy(buffer, "*** Connected\r");
	write(STDOUT_FILENO, buffer, strlen(buffer));

	/*
	 * Loop until one end of the connection goes away.
	 */
	for (;;) {
		FD_ZERO(&read_fd);
		FD_SET(STDIN_FILENO, &read_fd);
		FD_SET(s, &read_fd);
		
		select(s + 1, &read_fd, NULL, NULL, NULL);

		if (FD_ISSET(s, &read_fd)) {
			if ((n = read(s, buffer + 2, sizeof(buffer)-2)) == -1)
				break;
			if (buffer[2] == 0xF0) {
				buffer[2] = 0;
				write(STDOUT_FILENO, buffer + 2, n);
			} else {
				buffer[0] = 1;		/* Set Q Bit on */
				buffer[1] = 0x7F;	/* Q Bit escape */
				write(STDOUT_FILENO, buffer, n + 2);
			}
		}

		if (FD_ISSET(STDIN_FILENO, &read_fd)) {
			if ((n = read(STDIN_FILENO, buffer, 512)) == -1) {
				close(s);
				break;
			}
			if (buffer[0] == 0) {		/* Q Bit not set */
				buffer[0] = 0xF0;
				write(s, buffer, n);
			} else {
				/* Lose the leading 0x7F */
				write(s, buffer + 2, n - 2);
			}
		}
	}

	closelog();

	return 0;
}