[AX.25]: Constify ax25 utility functions
[deliverable/linux.git] / net / ax25 / ax25_addr.c
CommitLineData
1da177e4
LT
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 */
9#include <linux/errno.h>
10#include <linux/types.h>
11#include <linux/socket.h>
12#include <linux/in.h>
13#include <linux/kernel.h>
70868eac 14#include <linux/module.h>
1da177e4
LT
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <asm/uaccess.h>
26#include <asm/system.h>
27#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30
31/*
32 * The null address is defined as a callsign of all spaces with an
33 * SSID of zero.
34 */
35ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
36
70868eac
RB
37EXPORT_SYMBOL(null_ax25_address);
38
1da177e4
LT
39/*
40 * ax25 -> ascii conversion
41 */
e8cc49bb 42char *ax2asc(char *buf, const ax25_address *a)
1da177e4 43{
1da177e4
LT
44 char c, *s;
45 int n;
46
47 for (n = 0, s = buf; n < 6; n++) {
48 c = (a->ax25_call[n] >> 1) & 0x7F;
49
50 if (c != ' ') *s++ = c;
51 }
52
53 *s++ = '-';
54
55 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
56 *s++ = '1';
57 n -= 10;
58 }
59
60 *s++ = n + '0';
61 *s++ = '\0';
62
63 if (*buf == '\0' || *buf == '-')
64 return "*";
65
66 return buf;
67
68}
69
70868eac
RB
70EXPORT_SYMBOL(ax2asc);
71
1da177e4
LT
72/*
73 * ascii -> ax25 conversion
74 */
e8cc49bb 75void asc2ax(ax25_address *addr, const char *callsign)
1da177e4 76{
1da177e4
LT
77 char *s;
78 int n;
79
80 for (s = callsign, n = 0; n < 6; n++) {
81 if (*s != '\0' && *s != '-')
baed16a7 82 addr->ax25_call[n] = *s++;
1da177e4 83 else
baed16a7
RB
84 addr->ax25_call[n] = ' ';
85 addr->ax25_call[n] <<= 1;
86 addr->ax25_call[n] &= 0xFE;
1da177e4
LT
87 }
88
89 if (*s++ == '\0') {
baed16a7
RB
90 addr->ax25_call[6] = 0x00;
91 return;
1da177e4
LT
92 }
93
baed16a7 94 addr->ax25_call[6] = *s++ - '0';
1da177e4
LT
95
96 if (*s != '\0') {
baed16a7
RB
97 addr->ax25_call[6] *= 10;
98 addr->ax25_call[6] += *s++ - '0';
1da177e4
LT
99 }
100
baed16a7
RB
101 addr->ax25_call[6] <<= 1;
102 addr->ax25_call[6] &= 0x1E;
1da177e4
LT
103}
104
70868eac
RB
105EXPORT_SYMBOL(asc2ax);
106
1da177e4
LT
107/*
108 * Compare two ax.25 addresses
109 */
e8cc49bb 110int ax25cmp(const ax25_address *a, const ax25_address *b)
1da177e4
LT
111{
112 int ct = 0;
113
114 while (ct < 6) {
115 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
116 return 1;
117 ct++;
118 }
119
120 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
121 return 0;
122
123 return 2; /* Partial match */
124}
125
70868eac
RB
126EXPORT_SYMBOL(ax25cmp);
127
1da177e4
LT
128/*
129 * Compare two AX.25 digipeater paths.
130 */
e8cc49bb 131int ax25digicmp(const ax25_digi *digi1, const ax25_digi *digi2)
1da177e4
LT
132{
133 int i;
134
135 if (digi1->ndigi != digi2->ndigi)
136 return 1;
137
138 if (digi1->lastrepeat != digi2->lastrepeat)
139 return 1;
140
141 for (i = 0; i < digi1->ndigi; i++)
142 if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
143 return 1;
144
145 return 0;
146}
147
148/*
149 * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
150 *
151 */
e8cc49bb
RB
152const unsigned char *ax25_addr_parse(const unsigned char *buf, int len,
153 ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags,
154 int *dama)
1da177e4
LT
155{
156 int d = 0;
157
158 if (len < 14) return NULL;
159
160 if (flags != NULL) {
161 *flags = 0;
162
163 if (buf[6] & AX25_CBIT)
164 *flags = AX25_COMMAND;
165 if (buf[13] & AX25_CBIT)
166 *flags = AX25_RESPONSE;
167 }
168
169 if (dama != NULL)
170 *dama = ~buf[13] & AX25_DAMA_FLAG;
171
172 /* Copy to, from */
173 if (dest != NULL)
174 memcpy(dest, buf + 0, AX25_ADDR_LEN);
175 if (src != NULL)
176 memcpy(src, buf + 7, AX25_ADDR_LEN);
177
178 buf += 2 * AX25_ADDR_LEN;
179 len -= 2 * AX25_ADDR_LEN;
180
181 digi->lastrepeat = -1;
182 digi->ndigi = 0;
183
184 while (!(buf[-1] & AX25_EBIT)) {
185 if (d >= AX25_MAX_DIGIS) return NULL; /* Max of 6 digis */
186 if (len < 7) return NULL; /* Short packet */
187
188 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
189 digi->ndigi = d + 1;
190
191 if (buf[6] & AX25_HBIT) {
192 digi->repeated[d] = 1;
193 digi->lastrepeat = d;
194 } else {
195 digi->repeated[d] = 0;
196 }
197
198 buf += AX25_ADDR_LEN;
199 len -= AX25_ADDR_LEN;
200 d++;
201 }
202
203 return buf;
204}
205
206/*
207 * Assemble an AX.25 header from the bits
208 */
e8cc49bb
RB
209int ax25_addr_build(unsigned char *buf, const ax25_address *src,
210 const ax25_address *dest, const ax25_digi *d, int flag, int modulus)
1da177e4
LT
211{
212 int len = 0;
213 int ct = 0;
214
215 memcpy(buf, dest, AX25_ADDR_LEN);
216 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
217 buf[6] |= AX25_SSSID_SPARE;
218
219 if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
220
221 buf += AX25_ADDR_LEN;
222 len += AX25_ADDR_LEN;
223
224 memcpy(buf, src, AX25_ADDR_LEN);
225 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
226 buf[6] &= ~AX25_SSSID_SPARE;
227
228 if (modulus == AX25_MODULUS)
229 buf[6] |= AX25_SSSID_SPARE;
230 else
231 buf[6] |= AX25_ESSID_SPARE;
232
233 if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
234
235 /*
236 * Fast path the normal digiless path
237 */
238 if (d == NULL || d->ndigi == 0) {
239 buf[6] |= AX25_EBIT;
240 return 2 * AX25_ADDR_LEN;
241 }
242
243 buf += AX25_ADDR_LEN;
244 len += AX25_ADDR_LEN;
245
246 while (ct < d->ndigi) {
247 memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
248
249 if (d->repeated[ct])
250 buf[6] |= AX25_HBIT;
251 else
252 buf[6] &= ~AX25_HBIT;
253
254 buf[6] &= ~AX25_EBIT;
255 buf[6] |= AX25_SSSID_SPARE;
256
257 buf += AX25_ADDR_LEN;
258 len += AX25_ADDR_LEN;
259 ct++;
260 }
261
262 buf[-1] |= AX25_EBIT;
263
264 return len;
265}
266
e8cc49bb 267int ax25_addr_size(const ax25_digi *dp)
1da177e4
LT
268{
269 if (dp == NULL)
270 return 2 * AX25_ADDR_LEN;
271
272 return AX25_ADDR_LEN * (2 + dp->ndigi);
273}
274
275/*
276 * Reverse Digipeat List. May not pass both parameters as same struct
277 */
e8cc49bb 278void ax25_digi_invert(const ax25_digi *in, ax25_digi *out)
1da177e4
LT
279{
280 int ct;
281
282 out->ndigi = in->ndigi;
283 out->lastrepeat = in->ndigi - in->lastrepeat - 2;
284
285 /* Invert the digipeaters */
286 for (ct = 0; ct < in->ndigi; ct++) {
287 out->calls[ct] = in->calls[in->ndigi - ct - 1];
288
289 if (ct <= out->lastrepeat) {
290 out->calls[ct].ax25_call[6] |= AX25_HBIT;
291 out->repeated[ct] = 1;
292 } else {
293 out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
294 out->repeated[ct] = 0;
295 }
296 }
297}
298
This page took 0.289864 seconds and 5 git commands to generate.