1 /*-------------------------------------------------------------------
2  *
3  * Name:	ax25_pad.h
4  *
5  * Purpose:	Header file for using ax25_pad.c
6  *
7  *------------------------------------------------------------------*/
8 
9 #ifndef AX25_PAD_H
10 #define AX25_PAD_H 1
11 
12 
13 #define AX25_MAX_REPEATERS 8
14 #define AX25_MIN_ADDRS 2	/* Destinatin & Source. */
15 #define AX25_MAX_ADDRS 10	/* Destination, Source, 8 digipeaters. */
16 
17 #define AX25_DESTINATION  0	/* Address positions in frame. */
18 #define AX25_SOURCE       1
19 #define AX25_REPEATER_1   2
20 #define AX25_REPEATER_2   3
21 #define AX25_REPEATER_3   4
22 #define AX25_REPEATER_4   5
23 #define AX25_REPEATER_5   6
24 #define AX25_REPEATER_6   7
25 #define AX25_REPEATER_7   8
26 #define AX25_REPEATER_8   9
27 
28 #define AX25_MAX_ADDR_LEN 12	/* In theory, you would expect the maximum length */
29 				/* to be 6 letters, dash, 2 digits, and nul for a */
30 				/* total of 10.  However, object labels can be 10 */
31 				/* characters so throw in a couple extra bytes */
32 				/* to be safe. */
33 
34 #define AX25_MIN_INFO_LEN 0	/* Previously 1 when considering only APRS. */
35 
36 #define AX25_MAX_INFO_LEN 2048	/* Maximum size for APRS. */
37 				/* AX.25 starts out with 256 as the default max */
38 				/* length but the end stations can negotiate */
39 				/* something different. */
40 				/* version 0.8:  Change from 256 to 2028 to */
41 				/* handle the larger paclen for Linux AX25. */
42 
43 				/* These don't include the 2 bytes for the */
44 				/* HDLC frame FCS. */
45 
46 /*
47  * Previously, for APRS only.
48  * #define AX25_MIN_PACKET_LEN ( 2 * 7 + 2 + AX25_MIN_INFO_LEN)
49  * #define AX25_MAX_PACKET_LEN ( AX25_MAX_ADDRS * 7 + 2 + AX25_MAX_INFO_LEN)
50  */
51 
52 /* The more general case. */
53 /* An AX.25 frame can have a control byte and no protocol. */
54 
55 #define AX25_MIN_PACKET_LEN ( 2 * 7 + 1 )
56 
57 #define AX25_MAX_PACKET_LEN ( AX25_MAX_ADDRS * 7 + 2 + 3 + AX25_MAX_INFO_LEN)
58 
59 
60 /*
61  * packet_t is a pointer to a packet object.
62  *
63  * The actual implementation is not visible outside ax25_pad.c.
64  */
65 
66 #define AX25_UI_FRAME 3		/* Control field value. */
67 
68 #define AX25_PID_NO_LAYER_3 0xf0		/* protocol ID used for APRS */
69 #define AX25_PID_SEGMENTATION_FRAGMENT 0x08
70 #define AX25_PID_ESCAPE_CHARACTER 0xff
71 
72 
73 #ifdef AX25_PAD_C	/* Keep this hidden - implementation could change. */
74 
75 struct packet_s {
76 
77 	int magic1;		/* for error checking. */
78 
79 	int seq;		/* unique sequence number for debugging. */
80 
81 	double release_time;	/* Time stamp in format returned by dtime_now(). */
82 				/* When to release from the SATgate mode delay queue. */
83 
84 #define MAGIC 0x41583235
85 
86 	struct packet_s *nextp;	/* Pointer to next in queue. */
87 
88 	int num_addr;		/* Number of addresses in frame. */
89 				/* Range of AX25_MIN_ADDRS .. AX25_MAX_ADDRS for AX.25. */
90 				/* It will be 0 if it doesn't look like AX.25. */
91 				/* -1 is used temporarily at allocation to mean */
92 				/* not determined yet. */
93 
94 
95 
96 				/*
97  				 * The 7th octet of each address contains:
98 			         *
99 				 * Bits:   H  R  R  SSID  0
100 				 *
101 				 *   H 		for digipeaters set to 0 intially.
102 				 *		Changed to 1 when position has been used.
103  				 *
104 				 *		for source & destination it is called
105 				 *		command/response.  Normally both 1 for APRS.
106 				 *		They should be opposites for connected mode.
107 				 *
108 				 *   R	R	Reserved.  Normally set to 1 1.
109 				 *
110 				 *   SSID	Substation ID.  Range of 0 - 15.
111 				 *
112 				 *   0		Usually 0 but 1 for last address.
113 				 */
114 
115 
116 #define SSID_H_MASK	0x80
117 #define SSID_H_SHIFT	7
118 
119 #define SSID_RR_MASK	0x60
120 #define SSID_RR_SHIFT	5
121 
122 #define SSID_SSID_MASK	0x1e
123 #define SSID_SSID_SHIFT	1
124 
125 #define SSID_LAST_MASK	0x01
126 
127 
128 	int frame_len;		/* Frame length without CRC. */
129 
130 	int modulo;		/* I & S frames have sequence numbers of either 3 bits (modulo 8) */
131 				/* or 7 bits (modulo 128).  This is conveyed by either 1 or 2 */
132 				/* control bytes.  Unfortunately, we can't determine this by looking */
133 				/* at an isolated frame.  We need to know about the context.  If we */
134 				/* are part of the conversation, we would know.  But if we are */
135 				/* just listening to others, this would be more difficult to determine. */
136 
137 				/* For U frames:   	set to 0 - not applicable */
138 				/* For I & S frames:	8 or 128 if known.  0 if unknown. */
139 
140 	unsigned char frame_data[AX25_MAX_PACKET_LEN+1];
141 				/* Raw frame contents, without the CRC. */
142 
143 
144 	int magic2;		/* Will get stomped on if above overflows. */
145 };
146 
147 
148 
149 
150 #else			/* Public view. */
151 
152 struct packet_s {
153 	int secret;
154 };
155 
156 #endif
157 
158 
159 typedef struct packet_s *packet_t;
160 
161 typedef enum cmdres_e { cr_00 = 2, cr_cmd = 1, cr_res = 0, cr_11 = 3 } cmdres_t;
162 
163 
164 extern packet_t ax25_new (void);
165 
166 
167 #ifdef AX25_PAD_C	/* Keep this hidden - implementation could change. */
168 
169 
170 /*
171  * APRS always has one control octet of 0x03 but the more
172  * general AX.25 case is one or two control bytes depending on
173  * whether "modulo 128 operation" is in effect.
174  */
175 
176 //#define DEBUGX 1
177 
ax25_get_control_offset(packet_t this_p)178 static inline int ax25_get_control_offset (packet_t this_p)
179 {
180 	return (this_p->num_addr*7);
181 }
182 
ax25_get_num_control(packet_t this_p)183 static inline int ax25_get_num_control (packet_t this_p)
184 {
185 	int c;
186 
187 	c = this_p->frame_data[ax25_get_control_offset(this_p)];
188 
189 	if ( (c & 0x01) == 0 ) {			/* I   xxxx xxx0 */
190 #if DEBUGX
191 	  dw_printf ("ax25_get_num_control, %02x is I frame, returns %d\n", c, (this_p->modulo == 128) ? 2 : 1);
192 #endif
193 	  return ((this_p->modulo == 128) ? 2 : 1);
194 	}
195 
196 	if ( (c & 0x03) == 1 ) {			/* S   xxxx xx01 */
197 #if DEBUGX
198 	  dw_printf ("ax25_get_num_control, %02x is S frame, returns %d\n", c, (this_p->modulo == 128) ? 2 : 1);
199 #endif
200 	  return ((this_p->modulo == 128) ? 2 : 1);
201 	}
202 
203 #if DEBUGX
204 	dw_printf ("ax25_get_num_control, %02x is U frame, always returns 1.\n", c);
205 #endif
206 
207 	return (1);					/* U   xxxx xx11 */
208 }
209 
210 
211 
212 /*
213  * APRS always has one protocol octet of 0xF0 meaning no level 3
214  * protocol but the more general case is 0, 1 or 2 protocol ID octets.
215  */
216 
ax25_get_pid_offset(packet_t this_p)217 static inline int ax25_get_pid_offset (packet_t this_p)
218 {
219 	return (ax25_get_control_offset (this_p) + ax25_get_num_control(this_p));
220 }
221 
ax25_get_num_pid(packet_t this_p)222 static int ax25_get_num_pid (packet_t this_p)
223 {
224 	int c;
225 	int pid;
226 
227 	c = this_p->frame_data[ax25_get_control_offset(this_p)];
228 
229 	if ( (c & 0x01) == 0 ||				/* I   xxxx xxx0 */
230 	     c == 0x03 || c == 0x13) {			/* UI  000x 0011 */
231 
232 	  pid = this_p->frame_data[ax25_get_pid_offset(this_p)];
233 #if DEBUGX
234 	  dw_printf ("ax25_get_num_pid, %02x is I or UI frame, pid = %02x, returns %d\n", c, pid, (pid==AX25_PID_ESCAPE_CHARACTER) ? 2 : 1);
235 #endif
236 	  if (pid == AX25_PID_ESCAPE_CHARACTER) {
237 	    return (2);			/* pid 1111 1111 means another follows. */
238 	  }
239 	  return (1);
240 	}
241 #if DEBUGX
242 	dw_printf ("ax25_get_num_pid, %02x is neither I nor UI frame, returns 0\n", c);
243 #endif
244 	return (0);
245 }
246 
247 
248 /*
249  * AX.25 has info field for 5 frame types depending on the control field.
250  *
251  *	xxxx xxx0	I
252  *	000x 0011	UI		(which includes APRS)
253  *	101x 1111	XID
254  *	111x 0011	TEST
255  *	100x 0111	FRMR
256  *
257  * APRS always has an Information field with at least one octet for the Data Type Indicator.
258  */
259 
ax25_get_info_offset(packet_t this_p)260 static inline int ax25_get_info_offset (packet_t this_p)
261 {
262 	int offset = ax25_get_control_offset (this_p) + ax25_get_num_control(this_p) + ax25_get_num_pid(this_p);
263 #if DEBUGX
264 	dw_printf ("ax25_get_info_offset, returns %d\n", offset);
265 #endif
266 	return (offset);
267 }
268 
ax25_get_num_info(packet_t this_p)269 static inline int ax25_get_num_info (packet_t this_p)
270 {
271 	int len;
272 
273 	/* assuming AX.25 frame. */
274 
275 	len = this_p->frame_len - this_p->num_addr * 7 - ax25_get_num_control(this_p) - ax25_get_num_pid(this_p);
276 	if (len < 0) {
277 	  len = 0;		/* print error? */
278 	}
279 
280 	return (len);
281 }
282 
283 #endif
284 
285 
286 typedef enum ax25_modulo_e { modulo_unknown = 0, modulo_8 = 8, modulo_128 = 128 } ax25_modulo_t;
287 
288 typedef enum ax25_frame_type_e {
289 
290 	frame_type_I = 0,	// Information
291 
292 	frame_type_S_RR,	// Receive Ready - System Ready To Receive
293 	frame_type_S_RNR,	// Receive Not Ready - TNC Buffer Full
294 	frame_type_S_REJ,	// Reject Frame - Out of Sequence or Duplicate
295 	frame_type_S_SREJ,	// Selective Reject - Request single frame repeat
296 
297 	frame_type_U_SABME,	// Set Async Balanced Mode, Extended
298 	frame_type_U_SABM,	// Set Async Balanced Mode
299 	frame_type_U_DISC,	// Disconnect
300 	frame_type_U_DM,	// Disconnect Mode
301 	frame_type_U_UA,	// Unnumbered Acknowledge
302 	frame_type_U_FRMR,	// Frame Reject
303 	frame_type_U_UI,	// Unnumbered Information
304 	frame_type_U_XID,	// Exchange Identification
305 	frame_type_U_TEST,	// Test
306 	frame_type_U,		// other Unnumbered, not used by AX.25.
307 
308 	frame_not_AX25		// Could not get control byte from frame.
309 				// This must be last because value plus 1 is
310 				// for the size of an array.
311 
312 } ax25_frame_type_t;
313 
314 
315 /*
316  * Originally this was a single number.
317  * Let's try something new in version 1.2.
318  * Also collect AGC values from the mark and space filters.
319  */
320 
321 typedef struct alevel_s {
322 
323 	int rec;
324 	int mark;
325 	int space;
326 	//float ms_ratio;	// TODO: take out after temporary investigation.
327 } alevel_t;
328 
329 
330 #ifndef AXTEST
331 // TODO: remove this?
332 #define AX25MEMDEBUG 1
333 #endif
334 
335 
336 #if AX25MEMDEBUG	// to investigate a memory leak problem
337 
338 
339 extern void ax25memdebug_set(void);
340 extern int ax25memdebug_get (void);
341 extern int ax25memdebug_seq (packet_t this_p);
342 
343 
344 extern packet_t ax25_from_text_debug (char *monitor, int strict, char *src_file, int src_line);
345 #define ax25_from_text(m,s) ax25_from_text_debug(m,s,__FILE__,__LINE__)
346 
347 extern packet_t ax25_from_frame_debug (unsigned char *data, int len, alevel_t alevel, char *src_file, int src_line);
348 #define ax25_from_frame(d,l,a) ax25_from_frame_debug(d,l,a,__FILE__,__LINE__);
349 
350 extern packet_t ax25_dup_debug (packet_t copy_from, char *src_file, int src_line);
351 #define ax25_dup(p) ax25_dup_debug(p,__FILE__,__LINE__);
352 
353 extern void ax25_delete_debug (packet_t pp, char *src_file, int src_line);
354 #define ax25_delete(p) ax25_delete_debug(p,__FILE__,__LINE__);
355 
356 #else
357 
358 extern packet_t ax25_from_text (char *monitor, int strict);
359 
360 extern packet_t ax25_from_frame (unsigned char *data, int len, alevel_t alevel);
361 
362 extern packet_t ax25_dup (packet_t copy_from);
363 
364 extern void ax25_delete (packet_t pp);
365 
366 #endif
367 
368 
369 
370 
371 extern int ax25_parse_addr (int position, char *in_addr, int strict, char *out_addr, int *out_ssid, int *out_heard);
372 extern int ax25_check_addresses (packet_t pp);
373 
374 extern packet_t ax25_unwrap_third_party (packet_t from_pp);
375 
376 extern void ax25_set_addr (packet_t pp, int, char *);
377 extern void ax25_insert_addr (packet_t this_p, int n, char *ad);
378 extern void ax25_remove_addr (packet_t this_p, int n);
379 
380 extern int ax25_get_num_addr (packet_t pp);
381 extern int ax25_get_num_repeaters (packet_t this_p);
382 
383 extern void ax25_get_addr_with_ssid (packet_t pp, int n, char *station);
384 extern void ax25_get_addr_no_ssid (packet_t pp, int n, char *station);
385 
386 extern int ax25_get_ssid (packet_t pp, int n);
387 extern void ax25_set_ssid (packet_t this_p, int n, int ssid);
388 
389 extern int ax25_get_h (packet_t pp, int n);
390 
391 extern void ax25_set_h (packet_t pp, int n);
392 
393 extern int ax25_get_heard(packet_t this_p);
394 
395 extern int ax25_get_first_not_repeated(packet_t pp);
396 
397 extern int ax25_get_rr (packet_t this_p, int n);
398 
399 extern int ax25_get_info (packet_t pp, unsigned char **paddr);
400 extern int ax25_cut_at_crlf (packet_t this_p);
401 
402 extern void ax25_set_nextp (packet_t this_p, packet_t next_p);
403 
404 extern int ax25_get_dti (packet_t this_p);
405 
406 extern packet_t ax25_get_nextp (packet_t this_p);
407 
408 extern void ax25_set_release_time (packet_t this_p, double release_time);
409 extern double ax25_get_release_time (packet_t this_p);
410 
411 extern void ax25_set_modulo (packet_t this_p, int modulo);
412 
413 extern void ax25_format_addrs (packet_t pp, char *);
414 extern void ax25_format_via_path (packet_t this_p, char *result, size_t result_size);
415 
416 extern int ax25_pack (packet_t pp, unsigned char result[AX25_MAX_PACKET_LEN]);
417 
418 extern ax25_frame_type_t ax25_frame_type (packet_t this_p, cmdres_t *cr, char *desc, int *pf, int *nr, int *ns);
419 
420 extern void ax25_hex_dump (packet_t this_p);
421 
422 extern int ax25_is_aprs (packet_t pp);
423 extern int ax25_is_null_frame (packet_t this_p);
424 
425 extern int ax25_get_control (packet_t this_p);
426 extern int ax25_get_c2 (packet_t this_p);
427 
428 extern int ax25_get_pid (packet_t this_p);
429 
430 extern int ax25_get_frame_len (packet_t this_p);
431 
432 extern unsigned short ax25_dedupe_crc (packet_t pp);
433 
434 extern unsigned short ax25_m_m_crc (packet_t pp);
435 
436 extern void ax25_safe_print (char *, int, int ascii_only);
437 
438 #define AX25_ALEVEL_TO_TEXT_SIZE 40	// overkill but safe.
439 extern int ax25_alevel_to_text (alevel_t alevel, char text[AX25_ALEVEL_TO_TEXT_SIZE]);
440 
441 
442 #endif /* AX25_PAD_H */
443 
444 /* end ax25_pad.h */
445 
446 
447