ax25_pad.h 12 KB

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