-
Notifications
You must be signed in to change notification settings - Fork 9
/
axsock.c
519 lines (492 loc) · 10.8 KB
/
axsock.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <errno.h>
#include "global.h"
#include "mbuf.h"
#include "lapb.h"
#include "ax25.h"
#include "socket.h"
#include "usock.h"
char Ax25_eol[] = "\r";
static void autobind(struct usock *up);
/* The following two variables are needed because there can be only one
* socket listening on each of the AX.25 modes (I and UI)
*/
int Axi_sock = -1; /* Socket number listening for AX25 connections */
static int Axui_sock = -1; /* Socket number listening for AX25 UI frames */
static struct mbuf *Bcq; /* Queue of incoming UI frames */
/* Function that handles incoming UI frames from lapb.c */
void
beac_input(
struct iface *iface,
uint8 *src,
struct mbuf **bpp
){
struct mbuf *hdr;
struct sockaddr_ax *sax;
if(Axui_sock == -1){
/* Nobody there to read it */
free_p(bpp);
} else {
pushdown(&hdr,NULL,sizeof(struct sockaddr_ax));
sax = (struct sockaddr_ax *)hdr->data;
sax->sax_family = AF_AX25;
memcpy(sax->ax25_addr,src,AXALEN);
strncpy(sax->iface,iface->name,ILEN);
hdr->next = (*bpp);
*bpp = NULL;
enqueue(&Bcq,&hdr);
}
}
int
so_ax_sock(
struct usock *up,
int protocol
){
return 0;
}
int
so_axui_sock(
struct usock *up,
int protocol
){
return 0;
}
int
so_axui_bind(
struct usock *up
){
if(Axui_sock != -1){
errno = EADDRINUSE;
return -1;
}
Axui_sock = up->index;
return 0;
}
int
so_ax_listen(
struct usock *up,
int backlog
){
struct sockaddr_ax *local;
if(up->name == NULL)
autobind(up);
if(up != itop(Axi_sock)){
errno = EOPNOTSUPP;
return -1;
}
local = (struct sockaddr_ax *)up->name;
up->cb.ax25 = open_ax25(NULL,local->ax25_addr,NULL,
backlog ? AX_SERVER:AX_PASSIVE,0,
s_arcall,s_atcall,s_ascall,Axi_sock);
return 0;
}
int
so_ax_conn(
struct usock *up
){
struct sockaddr_ax *local,*remote,localtmp;
struct ax25_cb *ax25;
struct iface *iface;
int s;
s = up->index;
remote = (struct sockaddr_ax *)up->peername;
if((iface = if_lookup(remote->iface)) == NULL){
errno = EINVAL;
return -1;
}
local = (struct sockaddr_ax *)up->name;
if(local == NULL){
/* The local address was unspecified; set it from
* the interface we'll use
*/
localtmp.sax_family = AF_AX25;
memcpy(localtmp.ax25_addr,iface->hwaddr,AXALEN);
memcpy(localtmp.iface,remote->iface,ILEN);
bind(s,(struct sockaddr *)&localtmp,sizeof(localtmp));
local = (struct sockaddr_ax *)up->name;
}
/* If we already have an AX25 link we can use it */
if((up->cb.ax25 = find_ax25(remote->ax25_addr)) != NULL
&& up->cb.ax25->state != LAPB_DISCONNECTED &&
up->cb.ax25->user == -1) {
up->cb.ax25->user = s;
up->cb.ax25->r_upcall = s_arcall;
up->cb.ax25->t_upcall = s_atcall;
up->cb.ax25->s_upcall = s_ascall;
if(up->cb.ax25->state == LAPB_CONNECTED
|| up->cb.ax25->state == LAPB_RECOVERY)
return 0;
} else {
up->cb.ax25 = open_ax25(iface,local->ax25_addr,
remote->ax25_addr,AX_ACTIVE,
Axwindow,s_arcall,s_atcall,s_ascall,s);
}
/* Wait for the connection to complete */
while((ax25 = up->cb.ax25) != NULL && ax25->state != LAPB_CONNECTED){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(ax25 == NULL){
/* Connection probably already exists */
free(up->peername);
up->peername = NULL;
errno = ECONNREFUSED;
return -1;
}
return 0;
}
int
so_axui_conn(
struct usock *up
){
if(up->name == NULL)
autobind(up);
return 0;
}
int
so_ax_recv(
struct usock *up,
struct mbuf **bpp,
struct sockaddr *from,
int *fromlen
){
struct ax25_cb *ax25;
int cnt;
while((ax25 = up->cb.ax25) != NULL
&& (*bpp = recv_ax25(ax25,(uint16)0)) == NULL){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(ax25 == NULL){
/* Connection went away */
errno = ENOTCONN;
return -1;
}
cnt = (*bpp)->cnt;
return cnt;
}
int
so_axui_recv(
struct usock *up,
struct mbuf **bpp,
struct sockaddr *from,
int *fromlen
){
int s;
s = up->index;
while(s == Axui_sock && Bcq == NULL){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(&Bcq)) != 0){
return -1;
}
}
if(s != Axui_sock){
errno = ENOTCONN;
return -1;
}
*bpp = dequeue(&Bcq);
if(from != NULL && fromlen != NULL
&& *fromlen >= sizeof(struct sockaddr_ax)){
pullup(bpp,from,sizeof(struct sockaddr_ax));
*fromlen = sizeof(struct sockaddr_ax);
} else {
pullup(bpp,NULL,sizeof(struct sockaddr_ax));
}
return len_p(*bpp);
}
int
so_ax_send(
struct usock *up,
struct mbuf **bpp,
struct sockaddr *to
){
struct ax25_cb *ax25;
if((ax25 = up->cb.ax25) == NULL){
free_p(bpp);
errno = ENOTCONN;
return -1;
}
send_ax25(ax25,bpp,PID_NO_L3);
while((ax25 = up->cb.ax25) != NULL &&
len_q(ax25->txq) * ax25->paclen > ax25->window){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(ax25 == NULL){
errno = EBADF;
return -1;
}
return 0;
}
int
so_axui_send(
struct usock *up,
struct mbuf **bpp,
struct sockaddr *to
){
struct sockaddr_ax *local,*remote;
local = (struct sockaddr_ax *)up->name;
if(to != NULL)
remote = (struct sockaddr_ax *)to;
else if(up->peername != NULL){
remote = (struct sockaddr_ax *)up->peername;
} else {
free_p(bpp);
errno = ENOTCONN;
return -1;
}
ax_output(if_lookup(remote->iface),remote->ax25_addr,
local->ax25_addr,PID_NO_L3,bpp);
return 0;
}
int
so_ax_qlen(
struct usock *up,
int rtx
){
int len;
if(up->cb.ax25 == NULL){
errno = ENOTCONN;
return -1;
}
switch(rtx){
case 0:
len = len_p(up->cb.ax25->rxq);
break;
case 1: /* Number of packets, not bytes */
len = len_q(up->cb.ax25->txq);
}
return len;
}
int
so_axui_qlen(
struct usock *up,
int rtx
){
int len;
switch(rtx){
case 0:
len = len_q(Bcq);
break;
case 1:
len = 0;
break;
}
return len;
}
int
so_ax_kick(
struct usock *up
){
if(up->cb.ax25 != NULL)
kick_ax25(up->cb.ax25);
return 0;
}
int
so_ax_shut(
struct usock *up,
int how
){
if(up->cb.ax25 == NULL)
return 0;
switch(how){
case 0:
case 1: /* Attempt regular disconnect */
disc_ax25(up->cb.ax25);
break;
case 2: /* Blow it away */
reset_ax25(up->cb.ax25);
up->cb.ax25 = NULL;
break;
}
return 0;
}
int
so_ax_close(
struct usock *up
){
if(up->cb.ax25 != NULL){
/* Tell the CLOSED upcall there's no more socket */
up->cb.ax25->user = -1;
disc_ax25(up->cb.ax25);
}
return 0;
}
int
so_axui_close(
struct usock *up
){
Axui_sock = -1;
free_q(&Bcq);
ksignal(&Bcq,0); /* Unblock any reads */
return 0;
}
/* AX.25 receive upcall */
void
s_arcall(
struct ax25_cb *axp,
int cnt
){
int ns;
struct usock *up,*nup,*oup;
union sp sp;
up = itop(axp->user);
/* When AX.25 data arrives for the first time the AX.25 listener
is notified, if active. If the AX.25 listener is a server its
socket is duplicated in the same manner as in s_tscall().
*/
if (Axi_sock != -1 && axp->user == -1) {
oup = up = itop(Axi_sock);
/* From now on, use the same upcalls as the listener */
axp->t_upcall = up->cb.ax25->t_upcall;
axp->r_upcall = up->cb.ax25->r_upcall;
axp->s_upcall = up->cb.ax25->s_upcall;
if (up->cb.ax25->flags.clone) {
/* Clone the socket */
ns = socket(AF_AX25,SOCK_STREAM,0);
nup = itop(ns);
ASSIGN(*nup,*up);
axp->user = ns;
nup->cb.ax25 = axp;
/* Allocate new memory for the name areas */
nup->name = mallocw(sizeof(struct sockaddr_ax));
nup->peername = mallocw(sizeof(struct sockaddr_ax));
/* Store the new socket # in the old one */
up->rdysock = ns;
up = nup;
} else {
axp->user = Axi_sock;
del_ax25(up->cb.ax25);
up->cb.ax25 = axp;
/* Allocate space for the peer's name */
up->peername = mallocw(sizeof(struct sockaddr_ax));
/* Store the old socket # in the old socket */
up->rdysock = Axi_sock;
}
/* Load the addresses. Memory for the name has already
* been allocated, either above or in the original bind.
*/
sp.ax = (struct sockaddr_ax *)up->name;
sp.ax->sax_family = AF_AX25;
memcpy(sp.ax->ax25_addr,axp->local,AXALEN);
memcpy(sp.ax->iface,axp->iface->name,ILEN);
up->namelen = sizeof(struct sockaddr_ax);
sp.ax = (struct sockaddr_ax *)up->peername;
sp.ax->sax_family = AF_AX25;
memcpy(sp.ax->ax25_addr,axp->remote,AXALEN);
memcpy(sp.ax->iface,axp->iface->name,ILEN);
up->peernamelen = sizeof(struct sockaddr_ax);
/* Wake up the guy accepting it, and let him run */
ksignal(oup,1);
kwait(NULL);
return;
}
/* Wake up anyone waiting, and let them run */
ksignal(up,1);
kwait(NULL);
}
/* AX.25 transmit upcall */
void
s_atcall(
struct ax25_cb *axp,
int cnt
){
/* Wake up anyone waiting, and let them run */
ksignal(itop(axp->user),1);
kwait(NULL);
}
/* AX25 state change upcall routine */
void
s_ascall(
register struct ax25_cb *axp,
int old,
int new
){
int s;
struct usock *up;
s = axp->user;
up = itop(s);
switch(new){
case LAPB_DISCONNECTED:
/* Clean up. If the user has already closed the socket,
* then up will be null (s was set to -1 by the close routine).
* If not, then this is an abnormal close (e.g., a reset)
* and clearing out the pointer in the socket structure will
* prevent any further operations on what will be a freed
* control block. Also wake up anybody waiting on events
* related to this block so they will notice it disappearing.
*/
if(up != NULL){
up->errcodes[0] = axp->reason;
up->cb.ax25 = NULL;
}
del_ax25(axp);
break;
default: /* Other transitions are ignored */
break;
}
ksignal(up,0); /* In case anybody's waiting */
}
/* Issue an automatic bind of a local AX25 address */
static void
autobind(
struct usock *up
){
struct sockaddr_ax local;
int s;
s = up->index;
local.sax_family = AF_AX25;
memcpy(local.ax25_addr,Mycall,AXALEN);
bind(s,(struct sockaddr *)&local,sizeof(struct sockaddr_ax));
}
int
checkaxaddr(
struct sockaddr *name,
int namelen
){
struct sockaddr_ax *sock;
sock = (struct sockaddr_ax *)name;
if(sock->sax_family != AF_AX25 || namelen != sizeof(struct sockaddr_ax))
return -1;
return 0;
}
char *
axpsocket(
struct sockaddr *p
){
struct sockaddr_ax *axp;
static char buf[30];
char tmp[11];
axp = (struct sockaddr_ax *)p;
pax25(tmp,axp->ax25_addr);
if(strlen(axp->iface) != 0)
sprintf(buf,"%s on %s",tmp,axp->iface);
else
strcpy(buf,tmp);
return buf;
}
char *
axstate(
struct usock *up
){
return Ax25states[up->cb.ax25->state];
}
so_ax_stat(
struct usock *up
){
st_ax25(up->cb.ax25);
return 0;
}