ax25_pad.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  1. //
  2. // This file is part of Dire Wolf, an amateur radio packet TNC.
  3. //
  4. // Copyright (C) 2011 , 2013, 2014, 2015 John Langner, WB2OSZ
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. //
  19. /*------------------------------------------------------------------
  20. *
  21. * Name: ax25_pad
  22. *
  23. * Purpose: Packet assembler and disasembler.
  24. *
  25. * This was written when I was only concerned about APRS which
  26. * uses only UI frames. ax25_pad2.c, added years later, has
  27. * functions for dealing with other types of frames.
  28. *
  29. * We can obtain AX.25 packets from different sources:
  30. *
  31. * (a) from an HDLC frame.
  32. * (b) from text representation.
  33. * (c) built up piece by piece.
  34. *
  35. * We also want to use a packet in different ways:
  36. *
  37. * (a) transmit as an HDLC frame.
  38. * (b) print in human-readable text.
  39. * (c) take it apart piece by piece.
  40. *
  41. * Looking at the more general case, we also want to modify
  42. * an existing packet. For instance an APRS repeater might
  43. * want to change "WIDE2-2" to "WIDE2-1" and retransmit it.
  44. *
  45. *
  46. * Description:
  47. *
  48. *
  49. * APRS uses only UI frames.
  50. * Each starts with 2-10 addressses (14-70 octets):
  51. *
  52. * * Destination Address (note: opposite order in printed format)
  53. *
  54. * * Source Address
  55. *
  56. * * 0-8 Digipeater Addresses (Could there ever be more as a result of
  57. * digipeaters inserting their own call for
  58. * the tracing feature?
  59. * NO. The limit is 8 when transmitting AX.25 over the
  60. * radio.
  61. * Communication with an IGate server could
  62. * have a longer VIA path but that is only in text form,
  63. * not as an AX.25 frame.)
  64. *
  65. * Each address is composed of:
  66. *
  67. * * 6 upper case letters or digits, blank padded.
  68. * These are shifted left one bit, leaving the LSB always 0.
  69. *
  70. * * a 7th octet containing the SSID and flags.
  71. * The LSB is always 0 except for the last octet of the address field.
  72. *
  73. * The final octet of the Destination has the form:
  74. *
  75. * C R R SSID 0, where,
  76. *
  77. * C = command/response = 1
  78. * R R = Reserved = 1 1
  79. * SSID = substation ID
  80. * 0 = zero
  81. *
  82. * The AX.25 spec states that the RR bits should be 11 if not used.
  83. * There are a couple documents talking about possible uses for APRS.
  84. * I'm ignoring them for now.
  85. * http://www.aprs.org/aprs12/preemptive-digipeating.txt
  86. * http://www.aprs.org/aprs12/RR-bits.txt
  87. *
  88. * I don't recall why I originally intended to set the source/destination C bits both to 1.
  89. * Reviewing this 5 years later, after spending more time delving into the
  90. * AX.25 spec, I think it should be 1 for destination and 0 for source.
  91. * In practice you see all four combinations being used by APRS stations
  92. * and no one really cares about these two bits.
  93. *
  94. * The final octet of the Source has the form:
  95. *
  96. * C R R SSID 0, where,
  97. *
  98. * C = command/response = 1 (originally, now I think it should be 0 for source.)
  99. * (Haven't gone back to check to see what code actually does.)
  100. * R R = Reserved = 1 1
  101. * SSID = substation ID
  102. * 0 = zero (or 1 if no repeaters)
  103. *
  104. * The final octet of each repeater has the form:
  105. *
  106. * H R R SSID 0, where,
  107. *
  108. * H = has-been-repeated = 0 initially.
  109. * Set to 1 after this address has been used.
  110. * R R = Reserved = 1 1
  111. * SSID = substation ID
  112. * 0 = zero (or 1 if last repeater in list)
  113. *
  114. * A digipeater would repeat this frame if it finds its address
  115. * with the "H" bit set to 0 and all earlier repeater addresses
  116. * have the "H" bit set to 1.
  117. * The "H" bit would be set to 1 in the repeated frame.
  118. *
  119. * In standard monitoring format, an asterisk is displayed after the last
  120. * digipeater with the "H" bit set. That indicates who you are hearing
  121. * over the radio.
  122. * (That is if digipeaters update the via path properly. Some don't so
  123. * we don't know who we are hearing. This is discussed in the User Guide.)
  124. * No asterisk means the source is being heard directly.
  125. *
  126. * Example, if we can hear all stations involved,
  127. *
  128. * SRC>DST,RPT1,RPT2,RPT3: -- we heard SRC
  129. * SRC>DST,RPT1*,RPT2,RPT3: -- we heard RPT1
  130. * SRC>DST,RPT1,RPT2*,RPT3: -- we heard RPT2
  131. * SRC>DST,RPT1,RPT2,RPT3*: -- we heard RPT3
  132. *
  133. *
  134. * Next we have:
  135. *
  136. * * One byte Control Field - APRS uses 3 for UI frame
  137. * The more general AX.25 frame can have two.
  138. *
  139. * * One byte Protocol ID - APRS uses 0xf0 for no layer 3
  140. *
  141. * Finally the Information Field of 1-256 bytes.
  142. *
  143. * And, of course, the 2 byte CRC.
  144. *
  145. * The descriptions above, for the C, H, and RR bits, are for APRS usage.
  146. * When operating as a KISS TNC we just pass everything along and don't
  147. * interpret or change them.
  148. *
  149. *
  150. * Constructors: ax25_init - Clear everything.
  151. * ax25_from_text - Tear apart a text string
  152. * ax25_from_frame - Tear apart an AX.25 frame.
  153. * Must be called before any other function.
  154. *
  155. * Get methods: .... - Extract destination, source, or digipeater
  156. * address from frame.
  157. *
  158. * Assumptions: CRC has already been verified to be correct.
  159. *
  160. *------------------------------------------------------------------*/
  161. #define AX25_PAD_C /* this will affect behavior of ax25_pad.h */
  162. #include "direwolf.h"
  163. #include <stdlib.h>
  164. #include <string.h>
  165. #include <assert.h>
  166. #include <stdio.h>
  167. #include <ctype.h>
  168. #include "regex.h"
  169. #if __WIN32__
  170. char *strtok_r(char *str, const char *delim, char **saveptr);
  171. #endif
  172. //#include "textcolor.h"
  173. #include "ax25_pad.h"
  174. #include "fcs_calc.h"
  175. /*
  176. * Accumulate statistics.
  177. * If new_count gets much larger than delete_count plus the size of
  178. * the transmit queue we have a memory leak.
  179. */
  180. static volatile int new_count = 0;
  181. static volatile int delete_count = 0;
  182. static volatile int last_seq_num = 0;
  183. #if AX25MEMDEBUG
  184. int ax25memdebug = 0;
  185. void ax25memdebug_set(void)
  186. {
  187. ax25memdebug = 1;
  188. }
  189. int ax25memdebug_get (void)
  190. {
  191. return (ax25memdebug);
  192. }
  193. int ax25memdebug_seq (packet_t this_p)
  194. {
  195. return (this_p->seq);
  196. }
  197. #endif
  198. #define CLEAR_LAST_ADDR_FLAG this_p->frame_data[this_p->num_addr*7-1] &= ~ SSID_LAST_MASK
  199. #define SET_LAST_ADDR_FLAG this_p->frame_data[this_p->num_addr*7-1] |= SSID_LAST_MASK
  200. /*------------------------------------------------------------------------------
  201. *
  202. * Name: ax25_new
  203. *
  204. * Purpose: Allocate memory for a new packet object.
  205. *
  206. * Returns: Identifier for a new packet object.
  207. * In the current implementation this happens to be a pointer.
  208. *
  209. *------------------------------------------------------------------------------*/
  210. packet_t ax25_new (void)
  211. {
  212. struct packet_s *this_p;
  213. #if DEBUG
  214. printf ("ax25_new(): before alloc, new=%d, delete=%d\n", new_count, delete_count);
  215. #endif
  216. last_seq_num++;
  217. new_count++;
  218. /*
  219. * check for memory leak.
  220. */
  221. // version 1.4 push up the threshold. We could have considerably more with connected mode.
  222. //if (new_count > delete_count + 100) {
  223. if (new_count > delete_count + 256) {
  224. printf ("Report to WB2OSZ - Memory leak for packet objects. new=%d, delete=%d\n", new_count, delete_count);
  225. #if AX25MEMDEBUG
  226. // Force on debug option to gather evidence.
  227. ax25memdebug_set();
  228. #endif
  229. }
  230. this_p = calloc(sizeof (struct packet_s), (size_t)1);
  231. if (this_p == NULL) {
  232. printf ("ERROR - can't allocate memory in ax25_new.\n");
  233. }
  234. assert (this_p != NULL);
  235. this_p->magic1 = MAGIC;
  236. this_p->seq = last_seq_num;
  237. this_p->magic2 = MAGIC;
  238. this_p->num_addr = (-1);
  239. return (this_p);
  240. }
  241. /*------------------------------------------------------------------------------
  242. *
  243. * Name: ax25_delete
  244. *
  245. * Purpose: Destroy a packet object, freeing up memory it was using.
  246. *
  247. *------------------------------------------------------------------------------*/
  248. #if AX25MEMDEBUG
  249. void ax25_delete_debug (packet_t this_p, char *src_file, int src_line)
  250. #else
  251. void ax25_delete (packet_t this_p)
  252. #endif
  253. {
  254. #if DEBUG
  255. printf ("ax25_delete(): before free, new=%d, delete=%d\n", new_count, delete_count);
  256. #endif
  257. if (this_p == NULL) {
  258. printf ("ERROR - NULL pointer passed to ax25_delete.\n");
  259. return;
  260. }
  261. delete_count++;
  262. #if AX25MEMDEBUG
  263. if (ax25memdebug) {
  264. printf ("ax25_delete, seq=%d, called from %s %d, new_count=%d, delete_count=%d\n", this_p->seq, src_file, src_line, new_count, delete_count);
  265. }
  266. #endif
  267. assert (this_p->magic1 == MAGIC);
  268. assert (this_p->magic2 == MAGIC);
  269. this_p->magic1 = 0;
  270. this_p->magic1 = 0;
  271. //memset (this_p, 0, sizeof (struct packet_s));
  272. free (this_p);
  273. }
  274. /*------------------------------------------------------------------------------
  275. *
  276. * Name: ax25_from_text
  277. *
  278. * Purpose: Parse a frame in human-readable monitoring format and change
  279. * to internal representation.
  280. *
  281. * Input: monitor - "TNC-2" monitor format for packet. i.e.
  282. * source>dest[,repeater1,repeater2,...]:information
  283. *
  284. * The information part can have non-printable characters
  285. * in the form of <0xff>. This will be converted to single
  286. * bytes. e.g. <0x0d> is carriage return.
  287. * In version 1.4H we will allow nul characters which means
  288. * we have to maintain a length rather than using strlen().
  289. * I maintain that it violates the spec but want to handle it
  290. * because it does happen and we want to preserve it when
  291. * acting as an IGate rather than corrupting it.
  292. *
  293. * strict - True to enforce rules for packets sent over the air.
  294. * False to be more lenient for packets from IGate server.
  295. *
  296. * Messages from an IGate server can have longer
  297. * addresses after qAC. Up to 9 observed so far.
  298. *
  299. * We can just truncate the name because we will only
  300. * end up discarding it. TODO: check on this.
  301. *
  302. * Returns: Pointer to new packet object in the current implementation.
  303. *
  304. * Outputs: Use the "get" functions to retrieve information in different ways.
  305. *
  306. *------------------------------------------------------------------------------*/
  307. #if AX25MEMDEBUG
  308. packet_t ax25_from_text_debug (char *monitor, int strict, char *src_file, int src_line)
  309. #else
  310. packet_t ax25_from_text (char *monitor, int strict)
  311. #endif
  312. {
  313. /*
  314. * Tearing it apart is destructive so make our own copy first.
  315. */
  316. char stuff[512];
  317. char *pinfo;
  318. char *pa;
  319. char *saveptr; /* Used with strtok_r because strtok is not thread safe. */
  320. int ssid_temp, heard_temp;
  321. char atemp[AX25_MAX_ADDR_LEN];
  322. char info_part[AX25_MAX_INFO_LEN+1];
  323. int info_len;
  324. packet_t this_p = ax25_new ();
  325. #if AX25MEMDEBUG
  326. if (ax25memdebug) {
  327. printf ("ax25_from_text, seq=%d, called from %s %d\n", this_p->seq, src_file, src_line);
  328. }
  329. #endif
  330. /* Is it possible to have a nul character (zero byte) in the */
  331. /* information field of an AX.25 frame? */
  332. /* At this point, we have a normal C string. */
  333. /* It is possible that will convert <0x00> to a nul character later. */
  334. /* There we need to maintain a separate length and not use normal C string functions. */
  335. strlcpy (stuff, monitor, sizeof(stuff));
  336. /*
  337. * Initialize the packet structure with two addresses and control/pid
  338. * for APRS.
  339. */
  340. memset (this_p->frame_data + AX25_DESTINATION*7, ' ' << 1, 6);
  341. this_p->frame_data[AX25_DESTINATION*7+6] = SSID_H_MASK | SSID_RR_MASK;
  342. memset (this_p->frame_data + AX25_SOURCE*7, ' ' << 1, 6);
  343. this_p->frame_data[AX25_SOURCE*7+6] = SSID_H_MASK | SSID_RR_MASK | SSID_LAST_MASK;
  344. this_p->frame_data[14] = AX25_UI_FRAME;
  345. this_p->frame_data[15] = AX25_PID_NO_LAYER_3;
  346. this_p->frame_len = 7 + 7 + 1 + 1;
  347. this_p->num_addr = (-1);
  348. assert (ax25_get_num_addr(this_p) == 2);
  349. /*
  350. * Separate the addresses from the rest.
  351. */
  352. pinfo = strchr (stuff, ':');
  353. if (pinfo == NULL) {
  354. ax25_delete (this_p);
  355. return (NULL);
  356. }
  357. *pinfo = '\0';
  358. pinfo++;
  359. /*
  360. * Separate the addresses.
  361. * Note that source and destination order is swappped.
  362. */
  363. /*
  364. * Source address.
  365. * Don't use traditional strtok because it is not thread safe.
  366. */
  367. pa = strtok_r (stuff, ">", &saveptr);
  368. if (pa == NULL) {
  369. printf ("Failed to create packet from text. No source address\n");
  370. ax25_delete (this_p);
  371. return (NULL);
  372. }
  373. if ( ! ax25_parse_addr (AX25_SOURCE, pa, strict, atemp, &ssid_temp, &heard_temp)) {
  374. printf ("Failed to create packet from text. Bad source address\n");
  375. ax25_delete (this_p);
  376. return (NULL);
  377. }
  378. ax25_set_addr (this_p, AX25_SOURCE, atemp);
  379. ax25_set_h (this_p, AX25_SOURCE); // c/r in this position
  380. ax25_set_ssid (this_p, AX25_SOURCE, ssid_temp);
  381. /*
  382. * Destination address.
  383. */
  384. pa = strtok_r (NULL, ",", &saveptr);
  385. if (pa == NULL) {
  386. printf ("Failed to create packet from text. No destination address\n");
  387. ax25_delete (this_p);
  388. return (NULL);
  389. }
  390. if ( ! ax25_parse_addr (AX25_DESTINATION, pa, strict, atemp, &ssid_temp, &heard_temp)) {
  391. printf ("Failed to create packet from text. Bad destination address\n");
  392. ax25_delete (this_p);
  393. return (NULL);
  394. }
  395. ax25_set_addr (this_p, AX25_DESTINATION, atemp);
  396. ax25_set_h (this_p, AX25_DESTINATION); // c/r in this position
  397. ax25_set_ssid (this_p, AX25_DESTINATION, ssid_temp);
  398. /*
  399. * VIA path.
  400. */
  401. while (( pa = strtok_r (NULL, ",", &saveptr)) != NULL && this_p->num_addr < AX25_MAX_ADDRS ) {
  402. int k;
  403. k = this_p->num_addr;
  404. if ( ! ax25_parse_addr (k, pa, strict, atemp, &ssid_temp, &heard_temp)) {
  405. printf ("Failed to create packet from text. Bad digipeater address\n");
  406. ax25_delete (this_p);
  407. return (NULL);
  408. }
  409. ax25_set_addr (this_p, k, atemp);
  410. ax25_set_ssid (this_p, k, ssid_temp);
  411. // Does it have an "*" at the end?
  412. // TODO: Complain if more than one "*".
  413. // Could also check for all has been repeated bits are adjacent.
  414. if (heard_temp) {
  415. for ( ; k >= AX25_REPEATER_1; k--) {
  416. ax25_set_h (this_p, k);
  417. }
  418. }
  419. }
  420. /*
  421. * Finally, process the information part.
  422. *
  423. * Translate hexadecimal values like <0xff> to single bytes.
  424. * MIC-E format uses 5 different non-printing characters.
  425. * We might want to manually generate UTF-8 characters such as degree.
  426. */
  427. //#define DEBUG14H 1
  428. #if DEBUG14H
  429. printf ("BEFORE: %s\nSAFE: ", pinfo);
  430. ax25_safe_print (pinfo, -1, 0);
  431. printf ("\n");
  432. #endif
  433. info_len = 0;
  434. while (*pinfo != '\0' && info_len < AX25_MAX_INFO_LEN) {
  435. if (strlen(pinfo) >= 6 &&
  436. pinfo[0] == '<' &&
  437. pinfo[1] == '0' &&
  438. pinfo[2] == 'x' &&
  439. isxdigit( (uint) pinfo[3]) &&//#include "strlcpy.c"
  440. isxdigit( (uint) pinfo[4]) &&
  441. pinfo[5] == '>') {
  442. char *p;
  443. info_part[info_len] = strtol (pinfo + 3, &p, 16);
  444. info_len++;
  445. pinfo += 6;
  446. }
  447. else {
  448. info_part[info_len] = *pinfo;
  449. info_len++;
  450. pinfo++;
  451. }
  452. }
  453. info_part[info_len] = '\0';
  454. #if DEBUG14H
  455. printf ("AFTER: %s\nSAFE: ", info_part);
  456. ax25_safe_print (info_part, info_len, 0);
  457. printf ("\n");
  458. #endif
  459. /*
  460. * Append the info part.
  461. */
  462. memcpy ((char*)(this_p->frame_data+this_p->frame_len), info_part, info_len);
  463. this_p->frame_len += info_len;
  464. return (this_p);
  465. }
  466. /*------------------------------------------------------------------------------
  467. *
  468. * Name: ax25_from_frame
  469. *
  470. * Purpose: Split apart an HDLC frame to components.
  471. *
  472. * Inputs: fbuf - Pointer to beginning of frame.
  473. *
  474. * flen - Length excluding the two FCS bytes.
  475. *
  476. * alevel - Audio level of received signal.
  477. * Maximum range 0 - 100.
  478. * -1 might be used when not applicable.
  479. *
  480. * Returns: Pointer to new packet object or NULL if error.
  481. *
  482. * Outputs: Use the "get" functions to retrieve information in different ways.
  483. *
  484. *------------------------------------------------------------------------------*/
  485. #if AX25MEMDEBUG
  486. packet_t ax25_from_frame_debug (unsigned char *fbuf, int flen, alevel_t alevel, char *src_file, int src_line)
  487. #else
  488. packet_t ax25_from_frame (unsigned char *fbuf, int flen, alevel_t alevel)
  489. #endif
  490. {
  491. packet_t this_p;
  492. /*
  493. * First make sure we have an acceptable length:
  494. *
  495. * We are not concerned with the FCS (CRC) because someone else checked it.
  496. *
  497. * Is is possible to have zero length for info?
  498. *
  499. * In the original version, assuming APRS, the answer was no.
  500. * We always had at least 3 octets after the address part:
  501. * control, protocol, and first byte of info part for data type.
  502. *
  503. * In later versions, this restriction was relaxed so other
  504. * variations of AX.25 could be used. Now the minimum length
  505. * is 7+7 for addresses plus 1 for control.
  506. *
  507. */
  508. if (flen < AX25_MIN_PACKET_LEN || flen > AX25_MAX_PACKET_LEN)
  509. {
  510. printf ("Frame length %d not in allowable range of %d to %d.\n", flen, AX25_MIN_PACKET_LEN, AX25_MAX_PACKET_LEN);
  511. return (NULL);
  512. }
  513. this_p = ax25_new ();
  514. #if AX25MEMDEBUG
  515. if (ax25memdebug) {
  516. printf ("ax25_from_frame, seq=%d, called from %s %d\n", this_p->seq, src_file, src_line);
  517. }
  518. #endif
  519. /* Copy the whole thing intact. */
  520. memcpy (this_p->frame_data, fbuf, flen);
  521. this_p->frame_data[flen] = 0;
  522. this_p->frame_len = flen;
  523. /* Find number of addresses. */
  524. this_p->num_addr = (-1);
  525. (void) ax25_get_num_addr (this_p);
  526. return (this_p);
  527. }
  528. /*------------------------------------------------------------------------------
  529. *
  530. * Name: ax25_dup
  531. *
  532. * Purpose: Make a copy of given packet object.
  533. *
  534. * Inputs: copy_from - Existing packet object.
  535. *
  536. * Returns: Pointer to new packet object or NULL if error.
  537. *
  538. *
  539. *------------------------------------------------------------------------------*/
  540. #if AX25MEMDEBUG
  541. packet_t ax25_dup_debug (packet_t copy_from, char *src_file, int src_line)
  542. #else
  543. packet_t ax25_dup (packet_t copy_from)
  544. #endif
  545. {
  546. int save_seq;
  547. packet_t this_p;
  548. this_p = ax25_new ();
  549. assert (this_p != NULL);
  550. save_seq = this_p->seq;
  551. memcpy (this_p, copy_from, sizeof (struct packet_s));
  552. this_p->seq = save_seq;
  553. #if AX25MEMDEBUG
  554. if (ax25memdebug) {
  555. printf ("ax25_dup, seq=%d, called from %s %d, clone of seq %d\n", this_p->seq, src_file, src_line, copy_from->seq);
  556. }
  557. #endif
  558. return (this_p);
  559. }
  560. /*------------------------------------------------------------------------------
  561. *
  562. * Name: ax25_parse_addr
  563. *
  564. * Purpose: Parse address with optional ssid.
  565. *
  566. * Inputs: position - AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER_1...
  567. * Used for more specific error message. -1 if not used.
  568. *
  569. * in_addr - Input such as "WB2OSZ-15*"
  570. *
  571. * strict - 1 (true) for strict checking (6 characters, no lower case,
  572. * SSID must be in range of 0 to 15).
  573. * Strict is appropriate for packets sent
  574. * over the radio. Communication with IGate
  575. * allows lower case (e.g. "qAR") and two
  576. * alphanumeric characters for the SSID.
  577. * We also get messages like this from a server.
  578. * KB1POR>APU25N,TCPIP*,qAC,T2NUENGLD:...
  579. *
  580. * 2 (extra true) will complain if * is found at end.
  581. *
  582. * Outputs: out_addr - Address without any SSID.
  583. * Must be at least AX25_MAX_ADDR_LEN bytes.
  584. *
  585. * out_ssid - Numeric value of SSID.
  586. *
  587. * out_heard - True if "*" found.
  588. *
  589. * Returns: True (1) if OK, false (0) if any error.
  590. * When 0, out_addr, out_ssid, and out_heard are unpredictable.
  591. *
  592. *
  593. *------------------------------------------------------------------------------*/
  594. static const char *position_name[1 + AX25_MAX_ADDRS] = {
  595. "", "Destination ", "Source ",
  596. "Digi1 ", "Digi2 ", "Digi3 ", "Digi4 ",
  597. "Digi5 ", "Digi6 ", "Digi7 ", "Digi8 " };
  598. int ax25_parse_addr (int position, char *in_addr, int strict, char *out_addr, int *out_ssid, int *out_heard)
  599. {
  600. char *p;
  601. char sstr[8]; /* Should be 1 or 2 digits for SSID. */
  602. int i, j, k;
  603. int maxlen;
  604. *out_addr = '\0';
  605. *out_ssid = 0;
  606. *out_heard = 0;
  607. if (position < -1) position = -1;
  608. if (position > AX25_REPEATER_8) position = AX25_REPEATER_8;
  609. position++; /* Adjust for position_name above. */
  610. if (strict && strlen(in_addr) >= 2 && strncmp(in_addr, "qA", 2) == 0) {
  611. printf ("%sAddress \"%s\" is a \"q-construct\" used for communicating with\n", position_name[position], in_addr);
  612. printf ("APRS Internet Servers. It should never appear when going over the radio.\n");
  613. }
  614. //printf ("ax25_parse_addr in: %s\n", in_addr);
  615. maxlen = strict ? 6 : (AX25_MAX_ADDR_LEN-1);
  616. p = in_addr;
  617. i = 0;
  618. for (p = in_addr; *p != '\0' && *p != '-' && *p != '*'; p++) {
  619. if (i >= maxlen) {
  620. printf ("%sAddress is too long. \"%s\" has more than %d characters.\n", position_name[position], in_addr, maxlen);
  621. return 0;
  622. }
  623. if ( ! isalnum((uint)*p)) {
  624. printf ("%sAddress, \"%s\" contains character other than letter or digit in character position %d.\n", position_name[position], in_addr, (int)(long)(p-in_addr)+1);
  625. return 0;
  626. }
  627. out_addr[i++] = *p;
  628. out_addr[i] = '\0';
  629. #if DECAMAIN // Hack when running in decode_aprs utility.
  630. // Exempt the "qA..." case because it was already mentioned.
  631. if (strict && islower(*p) && strncmp(in_addr, "qA", 2) != 0) {
  632. printf ("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
  633. }
  634. #else
  635. if (strict && islower((uint)*p)) {
  636. printf ("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
  637. return 0;
  638. }
  639. #endif
  640. }
  641. j = 0;
  642. sstr[j] = '\0';
  643. if (*p == '-') {
  644. for (p++; isalnum((int)*p); p++) {
  645. if (j >= 2) {
  646. printf ("%sSSID is too long. SSID part of \"%s\" has more than 2 characters.\n", position_name[position], in_addr);
  647. return 0;
  648. }
  649. sstr[j++] = *p;
  650. sstr[j] = '\0';
  651. if (strict && ! isdigit((uint)*p)) {
  652. printf ("%sSSID must be digits. \"%s\" has letters in SSID.\n", position_name[position], in_addr);
  653. return 0;
  654. }
  655. }
  656. k = atoi(sstr);
  657. if (k < 0 || k > 15) {
  658. printf ("%sSSID out of range. SSID of \"%s\" not in range of 0 to 15.\n", position_name[position], in_addr);
  659. return 0;
  660. }
  661. *out_ssid = k;
  662. }
  663. if (*p == '*') {
  664. *out_heard = 1;
  665. p++;
  666. if (strict == 2) {
  667. printf ("\"*\" is not allowed at end of address \"%s\" here.\n", in_addr);
  668. return 0;
  669. }
  670. }
  671. if (*p != '\0') {
  672. printf ("Invalid character \"%c\" found in %saddress \"%s\".\n", *p, position_name[position], in_addr);
  673. return 0;
  674. }
  675. //printf ("ax25_parse_addr out: %s %d %d\n", out_addr, *out_ssid, *out_heard);
  676. return (1);
  677. } /* end ax25_parse_addr */
  678. /*-------------------------------------------------------------------
  679. *
  680. * Name: ax25_check_addresses
  681. *
  682. * Purpose: Check addresses of given packet and print message if any issues.
  683. * We call this when receiving and transmitting.
  684. *
  685. * Inputs: pp - packet object pointer.
  686. *
  687. * Errors: Print error message.
  688. *
  689. * Returns: 1 for all valid. 0 if not.
  690. *
  691. * Examples: I was surprised to get this from an APRS-IS server with
  692. * a lower case source address.
  693. *
  694. * n1otx>APRS,TCPIP*,qAC,THIRD:@141335z4227.48N/07111.73W_348/005g014t044r000p000h60b10075.wview_5_20_2
  695. *
  696. * I haven't gotten to the bottom of this yet but it sounds
  697. * like "q constructs" are somehow getting on to the air when
  698. * they should only appear in conversations with IGate servers.
  699. *
  700. * https://groups.yahoo.com/neo/groups/direwolf_packet/conversations/topics/678
  701. *
  702. * WB0VGI-7>APDW12,W0YC-5*,qAR,AE0RF-10:}N0DZQ-10>APWW10,TCPIP,WB0VGI-7*:;145.230MN*080306z4607.62N/09230.58WrKE0ACL/R 145.230- T146.2 (Pine County ARES)
  703. *
  704. * Typical result:
  705. *
  706. * Digipeater WIDE2 (probably N3LEE-4) audio level = 28(10/6) [NONE] __|||||||
  707. * [0.5] VE2DJE-9>P_0_P?,VE2PCQ-3,K1DF-7,N3LEE-4,WIDE2*:'{S+l <0x1c>>/
  708. * Invalid character "_" in MIC-E destination/latitude.
  709. * Invalid character "_" in MIC-E destination/latitude.
  710. * Invalid character "?" in MIC-E destination/latitude.
  711. * Invalid MIC-E N/S encoding in 4th character of destination.
  712. * Invalid MIC-E E/W encoding in 6th character of destination.
  713. * MIC-E, normal car (side view), Unknown manufacturer, Returning
  714. * N 00 00.0000, E 005 55.1500, 0 MPH
  715. * Invalid character "_" found in Destination address "P_0_P?".
  716. *
  717. * *** The origin and journey of this packet should receive some scrutiny. ***
  718. *
  719. *--------------------------------------------------------------------*/
  720. int ax25_check_addresses (packet_t pp)
  721. {
  722. int n;
  723. char addr[AX25_MAX_ADDR_LEN];
  724. char ignore1[AX25_MAX_ADDR_LEN];
  725. int ignore2, ignore3;
  726. int all_ok = 1;
  727. for (n = 0; n < ax25_get_num_addr(pp); n++) {
  728. ax25_get_addr_with_ssid (pp, n, addr);
  729. all_ok &= ax25_parse_addr (n, addr, 1, ignore1, &ignore2, &ignore3);
  730. }
  731. if (! all_ok) {
  732. printf ("\n");
  733. printf ("*** The origin and journey of this packet should receive some scrutiny. ***\n");
  734. printf ("\n");
  735. }
  736. return (all_ok);
  737. } /* end ax25_check_addresses */
  738. /*------------------------------------------------------------------------------
  739. *
  740. * Name: ax25_unwrap_third_party
  741. *
  742. * Purpose: Unwrap a third party messge from the header.
  743. *
  744. * Inputs: copy_from - Existing packet object.
  745. *
  746. * Returns: Pointer to new packet object or NULL if error.
  747. *
  748. * Example: Input: A>B,C:}D>E,F:info
  749. * Output: D>E,F:info
  750. *
  751. *------------------------------------------------------------------------------*/
  752. packet_t ax25_unwrap_third_party (packet_t from_pp)
  753. {
  754. unsigned char *info_p;
  755. packet_t result_pp;
  756. if (ax25_get_dti(from_pp) != '}') {
  757. printf ("Internal error: ax25_unwrap_third_party: wrong data type.\n");
  758. return (NULL);
  759. }
  760. (void) ax25_get_info (from_pp, &info_p);
  761. // Want strict because addresses should conform to AX.25 here.
  762. // That's not the case for something from an Internet Server.
  763. result_pp = ax25_from_text((char *)info_p + 1, 1);
  764. return (result_pp);
  765. }
  766. /*------------------------------------------------------------------------------
  767. *
  768. * Name: ax25_set_addr
  769. *
  770. * Purpose: Add or change an address.
  771. *
  772. * Inputs: n - Index of address. Use the symbols
  773. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  774. *
  775. * Must be either an existing address or one greater
  776. * than the final which causes a new one to be added.
  777. *
  778. * ad - Address with optional dash and substation id.
  779. *
  780. * Assumption: ax25_from_text or ax25_from_frame was called first.
  781. *
  782. * TODO: ax25_from_text could use this.
  783. *
  784. * Returns: None.
  785. *
  786. *------------------------------------------------------------------------------*/
  787. void ax25_set_addr (packet_t this_p, int n, char *ad)
  788. {
  789. int ssid_temp, heard_temp;
  790. char atemp[AX25_MAX_ADDR_LEN];
  791. int i;
  792. assert (this_p->magic1 == MAGIC);
  793. assert (this_p->magic2 == MAGIC);
  794. assert (n >= 0 && n < AX25_MAX_ADDRS);
  795. //printf ("ax25_set_addr (%d, %s) num_addr=%d\n", n, ad, this_p->num_addr);
  796. if (n >= 0 && n < this_p->num_addr) {
  797. //printf ("ax25_set_addr , existing case\n");
  798. /*
  799. * Set existing address position.
  800. */
  801. // Why aren't we setting 'strict' here?
  802. // Messages from IGate have q-constructs.
  803. // We use this to parse it and later remove unwanted parts.
  804. ax25_parse_addr (n, ad, 0, atemp, &ssid_temp, &heard_temp);
  805. memset (this_p->frame_data + n*7, ' ' << 1, 6);
  806. for (i=0; i<6 && atemp[i] != '\0'; i++) {
  807. this_p->frame_data[n*7+i] = atemp[i] << 1;
  808. }
  809. ax25_set_ssid (this_p, n, ssid_temp);
  810. }
  811. else if (n == this_p->num_addr) {
  812. //printf ("ax25_set_addr , appending case\n");
  813. /*
  814. * One beyond last position, process as insert.
  815. */
  816. ax25_insert_addr (this_p, n, ad);
  817. }
  818. else {
  819. printf ("Internal error, ax25_set_addr, bad position %d for '%s'\n", n, ad);
  820. }
  821. //printf ("------\n");
  822. //printf ("dump after ax25_set_addr (%d, %s)\n", n, ad);
  823. //ax25_hex_dump (this_p);
  824. //printf ("------\n");
  825. }
  826. /*------------------------------------------------------------------------------
  827. *
  828. * Name: ax25_insert_addr
  829. *
  830. * Purpose: Insert address at specified position, shifting others up one
  831. * position.
  832. * This is used when a digipeater wants to insert its own call
  833. * for tracing purposes.
  834. * For example:
  835. * W1ABC>TEST,WIDE3-3
  836. * Would become:
  837. * W1ABC>TEST,WB2OSZ-1*,WIDE3-2
  838. *
  839. * Inputs: n - Index of address. Use the symbols
  840. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  841. *
  842. * ad - Address with optional dash and substation id.
  843. *
  844. * Bugs: Little validity or bounds checking is performed. Be careful.
  845. *
  846. * Assumption: ax25_from_text or ax25_from_frame was called first.
  847. *
  848. * Returns: None.
  849. *
  850. *
  851. *------------------------------------------------------------------------------*/
  852. void ax25_insert_addr (packet_t this_p, int n, char *ad)
  853. {
  854. int ssid_temp, heard_temp;
  855. char atemp[AX25_MAX_ADDR_LEN];
  856. int i;
  857. int expect;
  858. assert (this_p->magic1 == MAGIC);
  859. assert (this_p->magic2 == MAGIC);
  860. assert (n >= AX25_REPEATER_1 && n < AX25_MAX_ADDRS);
  861. //printf ("ax25_insert_addr (%d, %s)\n", n, ad);
  862. /* Don't do it if we already have the maximum number. */
  863. /* Should probably return success/fail code but currently the caller doesn't care. */
  864. if ( this_p->num_addr >= AX25_MAX_ADDRS) {
  865. return;
  866. }
  867. CLEAR_LAST_ADDR_FLAG;
  868. this_p->num_addr++;
  869. memmove (this_p->frame_data + (n+1)*7, this_p->frame_data + n*7, this_p->frame_len - (n*7));
  870. memset (this_p->frame_data + n*7, ' ' << 1, 6);
  871. this_p->frame_len += 7;
  872. this_p->frame_data[n*7+6] = SSID_RR_MASK;
  873. SET_LAST_ADDR_FLAG;
  874. // Why aren't we setting 'strict' here?
  875. // Messages from IGate have q-constructs.
  876. // We use this to parse it and later remove unwanted parts.
  877. ax25_parse_addr (n, ad, 0, atemp, &ssid_temp, &heard_temp);
  878. memset (this_p->frame_data + n*7, ' ' << 1, 6);
  879. for (i=0; i<6 && atemp[i] != '\0'; i++) {
  880. this_p->frame_data[n*7+i] = atemp[i] << 1;
  881. }
  882. ax25_set_ssid (this_p, n, ssid_temp);
  883. // Sanity check after messing with number of addresses.
  884. expect = this_p->num_addr;
  885. this_p->num_addr = (-1);
  886. if (expect != ax25_get_num_addr (this_p)) {
  887. printf ("Internal error ax25_remove_addr expect %d, actual %d\n", expect, this_p->num_addr);
  888. }
  889. }
  890. /*------------------------------------------------------------------------------
  891. *
  892. * Name: ax25_remove_addr
  893. *
  894. * Purpose: Remove address at specified position, shifting others down one position.
  895. * This is used when we want to remove something from the digipeater list.
  896. *
  897. * Inputs: n - Index of address. Use the symbols
  898. * AX25_REPEATER1, AX25_REPEATER2, etc.
  899. *
  900. * Bugs: Little validity or bounds checking is performed. Be careful.
  901. *
  902. * Assumption: ax25_from_text or ax25_from_frame was called first.
  903. *
  904. * Returns: None.
  905. *
  906. *
  907. *------------------------------------------------------------------------------*/
  908. void ax25_remove_addr (packet_t this_p, int n)
  909. {
  910. int expect;
  911. assert (this_p->magic1 == MAGIC);
  912. assert (this_p->magic2 == MAGIC);
  913. assert (n >= AX25_REPEATER_1 && n < AX25_MAX_ADDRS);
  914. /* Shift those beyond to fill this position. */
  915. CLEAR_LAST_ADDR_FLAG;
  916. this_p->num_addr--;
  917. memmove (this_p->frame_data + n*7, this_p->frame_data + (n+1)*7, this_p->frame_len - ((n+1)*7));
  918. this_p->frame_len -= 7;
  919. SET_LAST_ADDR_FLAG;
  920. // Sanity check after messing with number of addresses.
  921. expect = this_p->num_addr;
  922. this_p->num_addr = (-1);
  923. if (expect != ax25_get_num_addr (this_p)) {
  924. printf ("Internal error ax25_remove_addr expect %d, actual %d\n", expect, this_p->num_addr);
  925. }
  926. }
  927. /*------------------------------------------------------------------------------
  928. *
  929. * Name: ax25_get_num_addr
  930. *
  931. * Purpose: Return number of addresses in current packet.
  932. *
  933. * Assumption: ax25_from_text or ax25_from_frame was called first.
  934. *
  935. * Returns: Number of addresses in the current packet.
  936. * Should be in the range of 2 .. AX25_MAX_ADDRS.
  937. *
  938. * Version 0.9: Could be zero for a non AX.25 frame in KISS mode.
  939. *
  940. *------------------------------------------------------------------------------*/
  941. int ax25_get_num_addr (packet_t this_p)
  942. {
  943. //unsigned char *pf;
  944. int a;
  945. int addr_bytes;
  946. assert (this_p->magic1 == MAGIC);
  947. assert (this_p->magic2 == MAGIC);
  948. /* Use cached value if already set. */
  949. if (this_p->num_addr >= 0) {
  950. return (this_p->num_addr);
  951. }
  952. /* Otherwise, determine the number ofaddresses. */
  953. this_p->num_addr = 0; /* Number of addresses extracted. */
  954. addr_bytes = 0;
  955. for (a = 0; a < this_p->frame_len && addr_bytes == 0; a++) {
  956. if (this_p->frame_data[a] & SSID_LAST_MASK) {
  957. addr_bytes = a + 1;
  958. }
  959. }
  960. if (addr_bytes % 7 == 0) {
  961. int addrs = addr_bytes / 7;
  962. if (addrs >= AX25_MIN_ADDRS && addrs <= AX25_MAX_ADDRS) {
  963. this_p->num_addr = addrs;
  964. }
  965. }
  966. return (this_p->num_addr);
  967. }
  968. /*------------------------------------------------------------------------------
  969. *
  970. * Name: ax25_get_num_repeaters
  971. *
  972. * Purpose: Return number of repeater addresses in current packet.
  973. *
  974. * Assumption: ax25_from_text or ax25_from_frame was called first.
  975. *
  976. * Returns: Number of addresses in the current packet - 2.
  977. * Should be in the range of 0 .. AX25_MAX_ADDRS - 2.
  978. *
  979. *------------------------------------------------------------------------------*/
  980. int ax25_get_num_repeaters (packet_t this_p)
  981. {
  982. assert (this_p->magic1 == MAGIC);
  983. assert (this_p->magic2 == MAGIC);
  984. if (this_p->num_addr >= 2) {
  985. return (this_p->num_addr - 2);
  986. }
  987. return (0);
  988. }
  989. /*------------------------------------------------------------------------------
  990. *
  991. * Name: ax25_get_addr_with_ssid
  992. *
  993. * Purpose: Return specified address with any SSID in current packet.
  994. *
  995. * Inputs: n - Index of address. Use the symbols
  996. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  997. *
  998. * Outputs: station - String representation of the station, including the SSID.
  999. * e.g. "WB2OSZ-15"
  1000. * Usually variables will be AX25_MAX_ADDR_LEN bytes
  1001. * but 10 would be adequate.
  1002. *
  1003. * Bugs: No bounds checking is performed. Be careful.
  1004. *
  1005. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1006. *
  1007. * Returns: Character string in usual human readable format,
  1008. *
  1009. *
  1010. *------------------------------------------------------------------------------*/
  1011. void ax25_get_addr_with_ssid (packet_t this_p, int n, char *station)
  1012. {
  1013. int ssid;
  1014. char sstr[8]; /* Should be 1 or 2 digits for SSID. */
  1015. int i;
  1016. assert (this_p->magic1 == MAGIC);
  1017. assert (this_p->magic2 == MAGIC);
  1018. if (n < 0) {
  1019. printf ("Internal error detected in ax25_get_addr_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
  1020. printf ("Address index, %d, is less than zero.\n", n);
  1021. strlcpy (station, "??????", 10);
  1022. return;
  1023. }
  1024. if (n >= this_p->num_addr) {
  1025. printf ("Internal error detected in ax25_get_addr_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
  1026. printf ("Address index, %d, is too large for number of addresses, %d.\n", n, this_p->num_addr);
  1027. strlcpy (station, "??????", 10);
  1028. return;
  1029. }
  1030. // At one time this would stop at the first space, on the assumption we would have only trailing spaces.
  1031. // Then there was a forum discussion where someone encountered the address " WIDE2" with a leading space.
  1032. // In that case, we would have returned a zero length string here.
  1033. // Now we return exactly what is in the address field and trim trailing spaces.
  1034. // This will provide better information for troubleshooting.
  1035. for (i=0; i<6; i++) {
  1036. station[i] = (this_p->frame_data[n*7+i] >> 1) & 0x7f;
  1037. }
  1038. station[6] = '\0';
  1039. for (i=5; i>=0; i--) {
  1040. if (station[i] == ' ')
  1041. station[i] = '\0';
  1042. else
  1043. break;
  1044. }
  1045. ssid = ax25_get_ssid (this_p, n);
  1046. if (ssid != 0) {
  1047. snprintf (sstr, sizeof(sstr), "-%d", ssid);
  1048. strlcat (station, sstr, 10);
  1049. }
  1050. } /* end ax25_get_addr_with_ssid */
  1051. /*------------------------------------------------------------------------------
  1052. *
  1053. * Name: ax25_get_addr_no_ssid
  1054. *
  1055. * Purpose: Return specified address WITHOUT any SSID.
  1056. *
  1057. * Inputs: n - Index of address. Use the symbols
  1058. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  1059. *
  1060. * Outputs: station - String representation of the station, WITHOUT the SSID.
  1061. * e.g. "WB2OSZ"
  1062. * Usually variables will be AX25_MAX_ADDR_LEN bytes
  1063. * but 7 would be adequate.
  1064. *
  1065. * Bugs: No bounds checking is performed. Be careful.
  1066. *
  1067. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1068. *
  1069. * Returns: Character string in usual human readable format,
  1070. *
  1071. *
  1072. *------------------------------------------------------------------------------*/
  1073. void ax25_get_addr_no_ssid (packet_t this_p, int n, char *station)
  1074. {
  1075. int i;
  1076. assert (this_p->magic1 == MAGIC);
  1077. assert (this_p->magic2 == MAGIC);
  1078. if (n < 0) {
  1079. printf ("Internal error detected in ax25_get_addr_no_ssid, %s, line %d.\n", __FILE__, __LINE__);
  1080. printf ("Address index, %d, is less than zero.\n", n);
  1081. strlcpy (station, "??????", 7);
  1082. return;
  1083. }
  1084. if (n >= this_p->num_addr) {
  1085. printf ("Internal error detected in ax25_get_no_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
  1086. printf ("Address index, %d, is too large for number of addresses, %d.\n", n, this_p->num_addr);
  1087. strlcpy (station, "??????", 7);
  1088. return;
  1089. }
  1090. // At one time this would stop at the first space, on the assumption we would have only trailing spaces.
  1091. // Then there was a forum discussion where someone encountered the address " WIDE2" with a leading space.
  1092. // In that case, we would have returned a zero length string here.
  1093. // Now we return exactly what is in the address field and trim trailing spaces.
  1094. // This will provide better information for troubleshooting.
  1095. for (i=0; i<6; i++) {
  1096. station[i] = (this_p->frame_data[n*7+i] >> 1) & 0x7f;
  1097. }
  1098. station[6] = '\0';
  1099. for (i=5; i>=0; i--) {
  1100. if (station[i] == ' ')
  1101. station[i] = '\0';
  1102. else
  1103. break;
  1104. }
  1105. } /* end ax25_get_addr_no_ssid */
  1106. /*------------------------------------------------------------------------------
  1107. *
  1108. * Name: ax25_get_ssid
  1109. *
  1110. * Purpose: Return SSID of specified address in current packet.
  1111. *
  1112. * Inputs: n - Index of address. Use the symbols
  1113. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  1114. *
  1115. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1116. *
  1117. * Returns: Substation id, as integer 0 .. 15.
  1118. *
  1119. *------------------------------------------------------------------------------*/
  1120. int ax25_get_ssid (packet_t this_p, int n)
  1121. {
  1122. assert (this_p->magic1 == MAGIC);
  1123. assert (this_p->magic2 == MAGIC);
  1124. if (n >= 0 && n < this_p->num_addr) {
  1125. return ((this_p->frame_data[n*7+6] & SSID_SSID_MASK) >> SSID_SSID_SHIFT);
  1126. }
  1127. else {
  1128. printf ("Internal error: ax25_get_ssid(%d), num_addr=%d\n", n, this_p->num_addr);
  1129. return (0);
  1130. }
  1131. }
  1132. /*------------------------------------------------------------------------------
  1133. *
  1134. * Name: ax25_set_ssid
  1135. *
  1136. * Purpose: Set the SSID of specified address in current packet.
  1137. *
  1138. * Inputs: n - Index of address. Use the symbols
  1139. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  1140. *
  1141. * ssid - New SSID. Must be in range of 0 to 15.
  1142. *
  1143. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1144. *
  1145. * Bugs: Rewrite to keep call and SSID separate internally.
  1146. *
  1147. *------------------------------------------------------------------------------*/
  1148. void ax25_set_ssid (packet_t this_p, int n, int ssid)
  1149. {
  1150. assert (this_p->magic1 == MAGIC);
  1151. assert (this_p->magic2 == MAGIC);
  1152. if (n >= 0 && n < this_p->num_addr) {
  1153. this_p->frame_data[n*7+6] = (this_p->frame_data[n*7+6] & ~ SSID_SSID_MASK) |
  1154. ((ssid << SSID_SSID_SHIFT) & SSID_SSID_MASK) ;
  1155. }
  1156. else {
  1157. printf ("Internal error: ax25_set_ssid(%d,%d), num_addr=%d\n", n, ssid, this_p->num_addr);
  1158. }
  1159. }
  1160. /*------------------------------------------------------------------------------
  1161. *
  1162. * Name: ax25_get_h
  1163. *
  1164. * Purpose: Return "has been repeated" flag of specified address in current packet.
  1165. *
  1166. * Inputs: n - Index of address. Use the symbols
  1167. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  1168. *
  1169. * Bugs: No bounds checking is performed. Be careful.
  1170. *
  1171. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1172. *
  1173. * Returns: True or false.
  1174. *
  1175. *------------------------------------------------------------------------------*/
  1176. int ax25_get_h (packet_t this_p, int n)
  1177. {
  1178. assert (this_p->magic1 == MAGIC);
  1179. assert (this_p->magic2 == MAGIC);
  1180. assert (n >= 0 && n < this_p->num_addr);
  1181. if (n >= 0 && n < this_p->num_addr) {
  1182. return ((this_p->frame_data[n*7+6] & SSID_H_MASK) >> SSID_H_SHIFT);
  1183. }
  1184. else {
  1185. printf ("Internal error: ax25_get_h(%d), num_addr=%d\n", n, this_p->num_addr);
  1186. return (0);
  1187. }
  1188. }
  1189. /*------------------------------------------------------------------------------
  1190. *
  1191. * Name: ax25_set_h
  1192. *
  1193. * Purpose: Set the "has been repeated" flag of specified address in current packet.
  1194. *
  1195. * Inputs: n - Index of address. Use the symbols
  1196. * Should be in range of AX25_REPEATER_1 .. AX25_REPEATER_8.
  1197. *
  1198. * Bugs: No bounds checking is performed. Be careful.
  1199. *
  1200. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1201. *
  1202. * Returns: None
  1203. *
  1204. *------------------------------------------------------------------------------*/
  1205. void ax25_set_h (packet_t this_p, int n)
  1206. {
  1207. assert (this_p->magic1 == MAGIC);
  1208. assert (this_p->magic2 == MAGIC);
  1209. if (n >= 0 && n < this_p->num_addr) {
  1210. this_p->frame_data[n*7+6] |= SSID_H_MASK;
  1211. }
  1212. else {
  1213. printf ("Internal error: ax25_set_hd(%d), num_addr=%d\n", n, this_p->num_addr);
  1214. }
  1215. }
  1216. /*------------------------------------------------------------------------------
  1217. *
  1218. * Name: ax25_get_heard
  1219. *
  1220. * Purpose: Return index of the station that we heard.
  1221. *
  1222. * Inputs: none
  1223. *
  1224. *
  1225. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1226. *
  1227. * Returns: If any of the digipeaters have the has-been-repeated bit set,
  1228. * return the index of the last one. Otherwise return index for source.
  1229. *
  1230. *------------------------------------------------------------------------------*/
  1231. int ax25_get_heard(packet_t this_p)
  1232. {
  1233. int i;
  1234. int result;
  1235. assert (this_p->magic1 == MAGIC);
  1236. assert (this_p->magic2 == MAGIC);
  1237. result = AX25_SOURCE;
  1238. for (i = AX25_REPEATER_1; i < ax25_get_num_addr(this_p); i++) {
  1239. if (ax25_get_h(this_p,i)) {
  1240. result = i;
  1241. }
  1242. }
  1243. return (result);
  1244. }
  1245. /*------------------------------------------------------------------------------
  1246. *
  1247. * Name: ax25_get_first_not_repeated
  1248. *
  1249. * Purpose: Return index of the first repeater that does NOT have the
  1250. * "has been repeated" flag set or -1 if none.
  1251. *
  1252. * Inputs: none
  1253. *
  1254. *
  1255. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1256. *
  1257. * Returns: In range of X25_REPEATER_1 .. X25_REPEATER_8 or -1 if none.
  1258. *
  1259. *------------------------------------------------------------------------------*/
  1260. int ax25_get_first_not_repeated(packet_t this_p)
  1261. {
  1262. int i;
  1263. assert (this_p->magic1 == MAGIC);
  1264. assert (this_p->magic2 == MAGIC);
  1265. for (i = AX25_REPEATER_1; i < ax25_get_num_addr(this_p); i++) {
  1266. if ( ! ax25_get_h(this_p,i)) {
  1267. return (i);
  1268. }
  1269. }
  1270. return (-1);
  1271. }
  1272. /*------------------------------------------------------------------------------
  1273. *
  1274. * Name: ax25_get_rr
  1275. *
  1276. * Purpose: Return the two reserved "RR" bits in the specified address field.
  1277. *
  1278. * Inputs: pp - Packet object.
  1279. *
  1280. * n - Index of address. Use the symbols
  1281. * AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
  1282. *
  1283. * Returns: 0, 1, 2, or 3.
  1284. *
  1285. *------------------------------------------------------------------------------*/
  1286. int ax25_get_rr (packet_t this_p, int n)
  1287. {
  1288. assert (this_p->magic1 == MAGIC);
  1289. assert (this_p->magic2 == MAGIC);
  1290. assert (n >= 0 && n < this_p->num_addr);
  1291. if (n >= 0 && n < this_p->num_addr) {
  1292. return ((this_p->frame_data[n*7+6] & SSID_RR_MASK) >> SSID_RR_SHIFT);
  1293. }
  1294. else {
  1295. printf ("Internal error: ax25_get_rr(%d), num_addr=%d\n", n, this_p->num_addr);
  1296. return (0);
  1297. }
  1298. }
  1299. /*------------------------------------------------------------------------------
  1300. *
  1301. * Name: ax25_get_info
  1302. *
  1303. * Purpose: Obtain Information part of current packet.
  1304. *
  1305. * Inputs: this_p - Packet object pointer.
  1306. *
  1307. * Outputs: paddr - Starting address of information part is returned here.
  1308. *
  1309. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1310. *
  1311. * Returns: Number of octets in the Information part.
  1312. * Should be in the range of AX25_MIN_INFO_LEN .. AX25_MAX_INFO_LEN.
  1313. *
  1314. *------------------------------------------------------------------------------*/
  1315. int ax25_get_info (packet_t this_p, unsigned char **paddr)
  1316. {
  1317. unsigned char *info_ptr;
  1318. int info_len;
  1319. assert (this_p->magic1 == MAGIC);
  1320. assert (this_p->magic2 == MAGIC);
  1321. if (this_p->num_addr >= 2) {
  1322. /* AX.25 */
  1323. info_ptr = this_p->frame_data + ax25_get_info_offset(this_p);
  1324. info_len = ax25_get_num_info(this_p);
  1325. }
  1326. else {
  1327. /* Not AX.25. Treat Whole packet as info. */
  1328. info_ptr = this_p->frame_data;
  1329. info_len = this_p->frame_len;
  1330. }
  1331. /* Add nul character in case caller treats as printable string. */
  1332. assert (info_len >= 0);
  1333. info_ptr[info_len] = '\0';
  1334. *paddr = info_ptr;
  1335. return (info_len);
  1336. } /* end ax25_get_info */
  1337. /*------------------------------------------------------------------------------
  1338. *
  1339. * Name: ax25_cut_at_crlf
  1340. *
  1341. * Purpose: Truncate the information part at the first CR or LF.
  1342. * This is used for the RF>IS IGate function.
  1343. * CR/LF is used as record separator so we must remove it
  1344. * before packaging up packet to sending to server.
  1345. *
  1346. * Inputs: this_p - Packet object pointer.
  1347. *
  1348. * Outputs: Packet is modified in place.
  1349. *
  1350. * Returns: Number of characters removed from the end.
  1351. * 0 if not changed.
  1352. *
  1353. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1354. *
  1355. *------------------------------------------------------------------------------*/
  1356. int ax25_cut_at_crlf (packet_t this_p)
  1357. {
  1358. unsigned char *info_ptr;
  1359. int info_len;
  1360. int j;
  1361. assert (this_p->magic1 == MAGIC);
  1362. assert (this_p->magic2 == MAGIC);
  1363. info_len = ax25_get_info (this_p, &info_ptr);
  1364. // Can't use strchr because there is potential of nul character.
  1365. for (j = 0; j < info_len; j++) {
  1366. if (info_ptr[j] == '\r' || info_ptr[j] == '\n') {
  1367. int chop = info_len - j;
  1368. this_p->frame_len -= chop;
  1369. return (chop);
  1370. }
  1371. }
  1372. return (0);
  1373. }
  1374. /*------------------------------------------------------------------------------
  1375. *
  1376. * Name: ax25_get_dti
  1377. *
  1378. * Purpose: Get Data Type Identifier from Information part.
  1379. *
  1380. * Inputs: None.
  1381. *
  1382. * Assumption: ax25_from_text or ax25_from_frame was called first.
  1383. *
  1384. * Returns: First byte from the information part.
  1385. *
  1386. *------------------------------------------------------------------------------*/
  1387. int ax25_get_dti (packet_t this_p)
  1388. {
  1389. assert (this_p->magic1 == MAGIC);
  1390. assert (this_p->magic2 == MAGIC);
  1391. if (this_p->num_addr >= 2) {
  1392. return (this_p->frame_data[ax25_get_info_offset(this_p)]);
  1393. }
  1394. return (' ');
  1395. }
  1396. /*------------------------------------------------------------------------------
  1397. *
  1398. * Name: ax25_set_nextp
  1399. *
  1400. * Purpose: Set next packet object in queue.
  1401. *
  1402. * Inputs: this_p - Current packet object.
  1403. *
  1404. * next_p - pointer to next one
  1405. *
  1406. * Description: This is used to build a linked list for a queue.
  1407. *
  1408. *------------------------------------------------------------------------------*/
  1409. void ax25_set_nextp (packet_t this_p, packet_t next_p)
  1410. {
  1411. assert (this_p->magic1 == MAGIC);
  1412. assert (this_p->magic2 == MAGIC);
  1413. this_p->nextp = next_p;
  1414. }
  1415. /*------------------------------------------------------------------------------
  1416. *
  1417. * Name: ax25_get_nextp
  1418. *
  1419. * Purpose: Obtain next packet object in queue.
  1420. *
  1421. * Inputs: Packet object.
  1422. *
  1423. * Returns: Following object in queue or NULL.
  1424. *
  1425. *------------------------------------------------------------------------------*/
  1426. packet_t ax25_get_nextp (packet_t this_p)
  1427. {
  1428. assert (this_p->magic1 == MAGIC);
  1429. assert (this_p->magic2 == MAGIC);
  1430. return (this_p->nextp);
  1431. }
  1432. /*------------------------------------------------------------------------------
  1433. *
  1434. * Name: ax25_set_release_time
  1435. *
  1436. * Purpose: Set release time
  1437. *
  1438. * Inputs: this_p - Current packet object.
  1439. *
  1440. * release_time - Time as returned by dtime_now().
  1441. *
  1442. *------------------------------------------------------------------------------*/
  1443. void ax25_set_release_time (packet_t this_p, double release_time)
  1444. {
  1445. assert (this_p->magic1 == MAGIC);
  1446. assert (this_p->magic2 == MAGIC);
  1447. this_p->release_time = release_time;
  1448. }
  1449. /*------------------------------------------------------------------------------
  1450. *
  1451. * Name: ax25_get_release_time
  1452. *
  1453. * Purpose: Get release time.
  1454. *
  1455. *------------------------------------------------------------------------------*/
  1456. double ax25_get_release_time (packet_t this_p)
  1457. {
  1458. assert (this_p->magic1 == MAGIC);
  1459. assert (this_p->magic2 == MAGIC);
  1460. return (this_p->release_time);
  1461. }
  1462. /*------------------------------------------------------------------------------
  1463. *
  1464. * Name: ax25_set_modulo
  1465. *
  1466. * Purpose: Set modulo value for I and S frame sequence numbers.
  1467. *
  1468. *------------------------------------------------------------------------------*/
  1469. void ax25_set_modulo (packet_t this_p, int modulo)
  1470. {
  1471. assert (this_p->magic1 == MAGIC);
  1472. assert (this_p->magic2 == MAGIC);
  1473. this_p->modulo = modulo;
  1474. }
  1475. /*------------------------------------------------------------------
  1476. *
  1477. * Function: ax25_format_addrs
  1478. *
  1479. * Purpose: Format all the addresses suitable for printing.
  1480. *
  1481. * The AX.25 spec refers to this as "Source Path Header" - "TNC-2" Format
  1482. *
  1483. * Inputs: Current packet.
  1484. *
  1485. * Outputs: result - All addresses combined into a single string of the form:
  1486. *
  1487. * "Source > Destination [ , repeater ... ] :"
  1488. *
  1489. * An asterisk is displayed after the last digipeater
  1490. * with the "H" bit set. e.g. If we hear RPT2,
  1491. *
  1492. * SRC>DST,RPT1,RPT2*,RPT3:
  1493. *
  1494. * No asterisk means the source is being heard directly.
  1495. * Needs to be 101 characters to avoid overflowing.
  1496. * (Up to 100 characters + \0)
  1497. *
  1498. * Errors: No error checking so caller needs to be careful.
  1499. *
  1500. *
  1501. *------------------------------------------------------------------*/
  1502. // TODO: max len for result. buffer overflow?
  1503. void ax25_format_addrs (packet_t this_p, char *result)
  1504. {
  1505. int i;
  1506. int heard;
  1507. char stemp[AX25_MAX_ADDR_LEN];
  1508. assert (this_p->magic1 == MAGIC);
  1509. assert (this_p->magic2 == MAGIC);
  1510. *result = '\0';
  1511. /* New in 0.9. */
  1512. /* Don't get upset if no addresses. */
  1513. /* This will allow packets that do not comply to AX.25 format. */
  1514. if (this_p->num_addr == 0) {
  1515. return;
  1516. }
  1517. ax25_get_addr_with_ssid (this_p, AX25_SOURCE, stemp);
  1518. strcat (result, stemp);
  1519. strcat (result, ">");
  1520. ax25_get_addr_with_ssid (this_p, AX25_DESTINATION, stemp);
  1521. strcat (result, stemp);
  1522. heard = ax25_get_heard(this_p);
  1523. for (i=(int)AX25_REPEATER_1; i<this_p->num_addr; i++) {
  1524. ax25_get_addr_with_ssid (this_p, i, stemp);
  1525. strcat (result, ",");
  1526. strcat (result, stemp);
  1527. if (i == heard) {
  1528. strcat (result, "*");
  1529. }
  1530. }
  1531. strcat (result, ":");
  1532. }
  1533. /*------------------------------------------------------------------
  1534. *
  1535. * Function: ax25_format_via_path
  1536. *
  1537. * Purpose: Format via path addresses suitable for printing.
  1538. *
  1539. * Inputs: Current packet.
  1540. *
  1541. * result_size - Number of bytes available for result.
  1542. * We can have up to 8 addresses x 9 characters
  1543. * plus 7 commas, possible *, and nul = 81 minimum.
  1544. *
  1545. * Outputs: result - Digipeater field addresses combined into a single string of the form:
  1546. *
  1547. * "repeater, repeater ..."
  1548. *
  1549. * An asterisk is displayed after the last digipeater
  1550. * with the "H" bit set. e.g. If we hear RPT2,
  1551. *
  1552. * RPT1,RPT2*,RPT3
  1553. *
  1554. * No asterisk means the source is being heard directly.
  1555. *
  1556. *------------------------------------------------------------------*/
  1557. void ax25_format_via_path (packet_t this_p, char *result, size_t result_size)
  1558. {
  1559. int i;
  1560. int heard;
  1561. char stemp[AX25_MAX_ADDR_LEN];
  1562. assert (this_p->magic1 == MAGIC);
  1563. assert (this_p->magic2 == MAGIC);
  1564. *result = '\0';
  1565. /* Don't get upset if no addresses. */
  1566. /* This will allow packets that do not comply to AX.25 format. */
  1567. if (this_p->num_addr == 0) {
  1568. return;
  1569. }
  1570. heard = ax25_get_heard(this_p);
  1571. for (i=(int)AX25_REPEATER_1; i<this_p->num_addr; i++) {
  1572. if (i > (int)AX25_REPEATER_1) {
  1573. strlcat (result, ",", result_size);
  1574. }
  1575. ax25_get_addr_with_ssid (this_p, i, stemp);
  1576. strlcat (result, stemp, result_size);
  1577. if (i == heard) {
  1578. strlcat (result, "*", result_size);
  1579. }
  1580. }
  1581. } /* end ax25_format_via_path */
  1582. /*------------------------------------------------------------------
  1583. *
  1584. * Function: ax25_pack
  1585. *
  1586. * Purpose: Put all the pieces into format ready for transmission.
  1587. *
  1588. * Inputs: this_p - pointer to packet object.
  1589. *
  1590. * Outputs: result - Frame buffer, AX25_MAX_PACKET_LEN bytes.
  1591. * Should also have two extra for FCS to be
  1592. * added later.
  1593. *
  1594. * Returns: Number of octets in the frame buffer.
  1595. * Does NOT include the extra 2 for FCS.
  1596. *
  1597. * Errors: Returns -1.
  1598. *
  1599. *------------------------------------------------------------------*/
  1600. int ax25_pack (packet_t this_p, unsigned char result[AX25_MAX_PACKET_LEN])
  1601. {
  1602. assert (this_p->magic1 == MAGIC);
  1603. assert (this_p->magic2 == MAGIC);
  1604. assert (this_p->frame_len >= 0 && this_p->frame_len <= AX25_MAX_PACKET_LEN);
  1605. memcpy (result, this_p->frame_data, this_p->frame_len);
  1606. return (this_p->frame_len);
  1607. }
  1608. /*------------------------------------------------------------------
  1609. *
  1610. * Function: ax25_frame_type
  1611. *
  1612. * Purpose: Extract the type of frame.
  1613. * This is derived from the control byte(s) but
  1614. * is an enumerated type for easier handling.
  1615. *
  1616. * Inputs: this_p - pointer to packet object.
  1617. *
  1618. * Outputs: desc - Text description such as "I frame" or
  1619. * "U frame SABME".
  1620. * Supply 40 bytes to be safe.
  1621. *
  1622. * cr - Command or response?
  1623. *
  1624. * pf - P/F - Poll/Final or -1 if not applicable
  1625. *
  1626. * nr - N(R) - receive sequence or -1 if not applicable.
  1627. *
  1628. * ns - N(S) - send sequence or -1 if not applicable.
  1629. *
  1630. * Returns: Frame type from enum ax25_frame_type_e.
  1631. *
  1632. *------------------------------------------------------------------*/
  1633. // TODO: need someway to ensure caller allocated enough space.
  1634. // Should pass in as parameter.
  1635. #define DESC_SIZ 40
  1636. ax25_frame_type_t ax25_frame_type (packet_t this_p, cmdres_t *cr, char *desc, int *pf, int *nr, int *ns)
  1637. {
  1638. int c; // U frames are always one control byte.
  1639. int c2 = 0; // I & S frames can have second Control byte.
  1640. assert (this_p->magic1 == MAGIC);
  1641. assert (this_p->magic2 == MAGIC);
  1642. strlcpy (desc, "????", DESC_SIZ);
  1643. *cr = cr_11;
  1644. *pf = -1;
  1645. *nr = -1;
  1646. *ns = -1;
  1647. c = ax25_get_control(this_p);
  1648. if (c < 0) {
  1649. strlcpy (desc, "Not AX.25", DESC_SIZ);
  1650. return (frame_not_AX25);
  1651. }
  1652. /*
  1653. * TERRIBLE HACK :-( for display purposes.
  1654. *
  1655. * I and S frames can have 1 or 2 control bytes but there is
  1656. * no good way to determine this without dipping into the data
  1657. * link state machine. Can we guess?
  1658. *
  1659. * S frames have no protocol id or information so if there is one
  1660. * more byte beyond the control field, we could assume there are
  1661. * two control bytes.
  1662. *
  1663. * For I frames, the protocol id will usually be 0xf0. If we find
  1664. * that as the first byte of the information field, it is probably
  1665. * the pid and not part of the information. Ditto for segments 0x08.
  1666. * Not fool proof but good enough for troubleshooting text out.
  1667. *
  1668. * If we have a link to the peer station, this will be set properly
  1669. * before it needs to be used for other reasons.
  1670. *
  1671. * Setting one of the RR bits (find reference!) is sounding better and better.
  1672. * It's in common usage so I should lobby to get that in the official protocol spec.
  1673. */
  1674. if (this_p->modulo == 0 && (c & 3) == 1 && ax25_get_c2(this_p) != -1) {
  1675. this_p->modulo = modulo_128;
  1676. }
  1677. else if (this_p->modulo == 0 && (c & 1) == 0 && this_p->frame_data[ax25_get_info_offset(this_p)] == 0xF0) {
  1678. this_p->modulo = modulo_128;
  1679. }
  1680. else if (this_p->modulo == 0 && (c & 1) == 0 && this_p->frame_data[ax25_get_info_offset(this_p)] == 0x08) { // same for segments
  1681. this_p->modulo = modulo_128;
  1682. }
  1683. if (this_p->modulo == modulo_128) {
  1684. c2 = ax25_get_c2 (this_p);
  1685. }
  1686. int dst_c = this_p->frame_data[AX25_DESTINATION * 7 + 6] & SSID_H_MASK;
  1687. int src_c = this_p->frame_data[AX25_SOURCE * 7 + 6] & SSID_H_MASK;
  1688. char cr_text[8];
  1689. char pf_text[8];
  1690. if (dst_c) {
  1691. if (src_c) { *cr = cr_11; strcpy(cr_text,"cc=11"); strcpy(pf_text,"p/f"); }
  1692. else { *cr = cr_cmd; strcpy(cr_text,"cmd"); strcpy(pf_text,"p"); }
  1693. }
  1694. else {
  1695. if (src_c) { *cr = cr_res; strcpy(cr_text,"res"); strcpy(pf_text,"f"); }
  1696. else { *cr = cr_00; strcpy(cr_text,"cc=00"); strcpy(pf_text,"p/f"); }
  1697. }
  1698. if ((c & 1) == 0) {
  1699. // Information rrr p sss 0 or sssssss 0 rrrrrrr p
  1700. if (this_p->modulo == modulo_128) {
  1701. *ns = (c >> 1) & 0x7f;
  1702. *pf = c2 & 1;
  1703. *nr = (c2 >> 1) & 0x7f;
  1704. }
  1705. else {
  1706. *ns = (c >> 1) & 7;
  1707. *pf = (c >> 4) & 1;
  1708. *nr = (c >> 5) & 7;
  1709. }
  1710. //snprintf (desc, DESC_SIZ, "I %s, n(s)=%d, n(r)=%d, %s=%d", cr_text, *ns, *nr, pf_text, *pf);
  1711. snprintf (desc, DESC_SIZ, "I %s, n(s)=%d, n(r)=%d, %s=%d, pid=0x%02x", cr_text, *ns, *nr, pf_text, *pf, ax25_get_pid(this_p));
  1712. return (frame_type_I);
  1713. }
  1714. else if ((c & 2) == 0) {
  1715. // Supervisory rrr p/f ss 0 1 or 0000 ss 0 1 rrrrrrr p/f
  1716. if (this_p->modulo == modulo_128) {
  1717. *pf = c2 & 1;
  1718. *nr = (c2 >> 1) & 0x7f;
  1719. }
  1720. else {
  1721. *pf = (c >> 4) & 1;
  1722. *nr = (c >> 5) & 7;
  1723. }
  1724. switch ((c >> 2) & 3) {
  1725. case 0: snprintf (desc, DESC_SIZ, "RR %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_RR); break;
  1726. case 1: snprintf (desc, DESC_SIZ, "RNR %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_RNR); break;
  1727. case 2: snprintf (desc, DESC_SIZ, "REJ %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_REJ); break;
  1728. case 3: snprintf (desc, DESC_SIZ, "SREJ %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_SREJ); break;
  1729. }
  1730. }
  1731. else {
  1732. // Unnumbered mmm p/f mm 1 1
  1733. *pf = (c >> 4) & 1;
  1734. switch (c & 0xef) {
  1735. case 0x6f: snprintf (desc, DESC_SIZ, "SABME %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_SABME); break;
  1736. case 0x2f: snprintf (desc, DESC_SIZ, "SABM %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_SABM); break;
  1737. case 0x43: snprintf (desc, DESC_SIZ, "DISC %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_DISC); break;
  1738. case 0x0f: snprintf (desc, DESC_SIZ, "DM %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_DM); break;
  1739. case 0x63: snprintf (desc, DESC_SIZ, "UA %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_UA); break;
  1740. case 0x87: snprintf (desc, DESC_SIZ, "FRMR %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_FRMR); break;
  1741. case 0x03: snprintf (desc, DESC_SIZ, "UI %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_UI); break;
  1742. case 0xaf: snprintf (desc, DESC_SIZ, "XID %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_XID); break;
  1743. case 0xe3: snprintf (desc, DESC_SIZ, "TEST %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_TEST); break;
  1744. default: snprintf (desc, DESC_SIZ, "U other???"); return (frame_type_U); break;
  1745. }
  1746. }
  1747. // Should be unreachable but compiler doesn't realize that.
  1748. // Here only to suppress "warning: control reaches end of non-void function"
  1749. return (frame_not_AX25);
  1750. } /* end ax25_frame_type */
  1751. /*------------------------------------------------------------------
  1752. *
  1753. * Function: ax25_hex_dump
  1754. *
  1755. * Purpose: Print out packet in hexadecimal for debugging.
  1756. *
  1757. * Inputs: fptr - Pointer to frame data.
  1758. *
  1759. * flen - Frame length, bytes. Does not include CRC.
  1760. *
  1761. *------------------------------------------------------------------*/
  1762. static void hex_dump (unsigned char *p, int len)
  1763. {
  1764. int n, i, offset;
  1765. offset = 0;
  1766. while (len > 0) {
  1767. n = len < 16 ? len : 16;
  1768. printf (" %03x: ", offset);
  1769. for (i=0; i<n; i++) {
  1770. printf (" %02x", p[i]);
  1771. }
  1772. for (i=n; i<16; i++) {
  1773. printf (" ");
  1774. }
  1775. printf (" ");
  1776. for (i=0; i<n; i++) {
  1777. printf ("%c", isprint(p[i]) ? p[i] : '.');
  1778. }
  1779. printf ("\n");
  1780. p += 16;
  1781. offset += 16;
  1782. len -= 16;
  1783. }
  1784. }
  1785. /* Text description of control octet. */
  1786. // FIXME: this is wrong. It doesn't handle modulo 128.
  1787. // TODO: use ax25_frame_type() instead.
  1788. static void ctrl_to_text (int c, char *out, size_t outsiz)
  1789. {
  1790. if ((c & 1) == 0) { snprintf (out, outsiz, "I frame: n(r)=%d, p=%d, n(s)=%d", (c>>5)&7, (c>>4)&1, (c>>1)&7); }
  1791. else if ((c & 0xf) == 0x01) { snprintf (out, outsiz, "S frame RR: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
  1792. else if ((c & 0xf) == 0x05) { snprintf (out, outsiz, "S frame RNR: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
  1793. else if ((c & 0xf) == 0x09) { snprintf (out, outsiz, "S frame REJ: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
  1794. else if ((c & 0xf) == 0x0D) { snprintf (out, outsiz, "S frame sREJ: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
  1795. else if ((c & 0xef) == 0x6f) { snprintf (out, outsiz, "U frame SABME: p=%d", (c>>4)&1); }
  1796. else if ((c & 0xef) == 0x2f) { snprintf (out, outsiz, "U frame SABM: p=%d", (c>>4)&1); }
  1797. else if ((c & 0xef) == 0x43) { snprintf (out, outsiz, "U frame DISC: p=%d", (c>>4)&1); }
  1798. else if ((c & 0xef) == 0x0f) { snprintf (out, outsiz, "U frame DM: f=%d", (c>>4)&1); }
  1799. else if ((c & 0xef) == 0x63) { snprintf (out, outsiz, "U frame UA: f=%d", (c>>4)&1); }
  1800. else if ((c & 0xef) == 0x87) { snprintf (out, outsiz, "U frame FRMR: f=%d", (c>>4)&1); }
  1801. else if ((c & 0xef) == 0x03) { snprintf (out, outsiz, "U frame UI: p/f=%d", (c>>4)&1); }
  1802. else if ((c & 0xef) == 0xAF) { snprintf (out, outsiz, "U frame XID: p/f=%d", (c>>4)&1); }
  1803. else if ((c & 0xef) == 0xe3) { snprintf (out, outsiz, "U frame TEST: p/f=%d", (c>>4)&1); }
  1804. else { snprintf (out, outsiz, "Unknown frame type for control = 0x%02x", c); }
  1805. }
  1806. /* Text description of protocol id octet. */
  1807. #define PID_TEXT_SIZE 80
  1808. static void pid_to_text (int p, char out[PID_TEXT_SIZE])
  1809. {
  1810. if ((p & 0x30) == 0x10) { snprintf (out, PID_TEXT_SIZE, "AX.25 layer 3 implemented."); }
  1811. else if ((p & 0x30) == 0x20) { snprintf (out, PID_TEXT_SIZE, "AX.25 layer 3 implemented."); }
  1812. else if (p == 0x01) { snprintf (out, PID_TEXT_SIZE, "ISO 8208/CCITT X.25 PLP"); }
  1813. else if (p == 0x06) { snprintf (out, PID_TEXT_SIZE, "Compressed TCP/IP packet. Van Jacobson (RFC 1144)"); }
  1814. else if (p == 0x07) { snprintf (out, PID_TEXT_SIZE, "Uncompressed TCP/IP packet. Van Jacobson (RFC 1144)"); }
  1815. else if (p == 0x08) { snprintf (out, PID_TEXT_SIZE, "Segmentation fragment"); }
  1816. else if (p == 0xC3) { snprintf (out, PID_TEXT_SIZE, "TEXNET datagram protocol"); }
  1817. else if (p == 0xC4) { snprintf (out, PID_TEXT_SIZE, "Link Quality Protocol"); }
  1818. else if (p == 0xCA) { snprintf (out, PID_TEXT_SIZE, "Appletalk"); }
  1819. else if (p == 0xCB) { snprintf (out, PID_TEXT_SIZE, "Appletalk ARP"); }
  1820. else if (p == 0xCC) { snprintf (out, PID_TEXT_SIZE, "ARPA Internet Protocol"); }
  1821. else if (p == 0xCD) { snprintf (out, PID_TEXT_SIZE, "ARPA Address resolution"); }
  1822. else if (p == 0xCE) { snprintf (out, PID_TEXT_SIZE, "FlexNet"); }
  1823. else if (p == 0xCF) { snprintf (out, PID_TEXT_SIZE, "NET/ROM"); }
  1824. else if (p == 0xF0) { snprintf (out, PID_TEXT_SIZE, "No layer 3 protocol implemented."); }
  1825. else if (p == 0xFF) { snprintf (out, PID_TEXT_SIZE, "Escape character. Next octet contains more Level 3 protocol information."); }
  1826. else { snprintf (out, PID_TEXT_SIZE, "Unknown protocol id = 0x%02x", p); }
  1827. }
  1828. void ax25_hex_dump (packet_t this_p)
  1829. {
  1830. int n;
  1831. unsigned char *fptr = this_p->frame_data;
  1832. int flen = this_p->frame_len;
  1833. if (this_p->num_addr >= AX25_MIN_ADDRS && this_p->num_addr <= AX25_MAX_ADDRS) {
  1834. int c, p;
  1835. char cp_text[120];
  1836. char l_text[20];
  1837. c = fptr[this_p->num_addr*7];
  1838. p = fptr[this_p->num_addr*7+1];
  1839. ctrl_to_text (c, cp_text, sizeof(cp_text)); // TODO: use ax25_frame_type() instead.
  1840. if ( (c & 0x01) == 0 || /* I xxxx xxx0 */
  1841. c == 0x03 || c == 0x13) { /* UI 000x 0011 */
  1842. char pid_text[PID_TEXT_SIZE];
  1843. pid_to_text (p, pid_text);
  1844. strlcat (cp_text, ", ", sizeof(cp_text));
  1845. strlcat (cp_text, pid_text, sizeof(cp_text));
  1846. }
  1847. snprintf (l_text, sizeof(l_text), ", length = %d", flen);
  1848. strlcat (cp_text, l_text, sizeof(cp_text));
  1849. printf ("%s\n", cp_text);
  1850. }
  1851. printf (" dest %c%c%c%c%c%c %2d c/r=%d res=%d last=%d\n",
  1852. fptr[0]>>1, fptr[1]>>1, fptr[2]>>1, fptr[3]>>1, fptr[4]>>1, fptr[5]>>1,
  1853. (fptr[6]&SSID_SSID_MASK)>>SSID_SSID_SHIFT,
  1854. (fptr[6]&SSID_H_MASK)>>SSID_H_SHIFT,
  1855. (fptr[6]&SSID_RR_MASK)>>SSID_RR_SHIFT,
  1856. fptr[6]&SSID_LAST_MASK);
  1857. printf (" source %c%c%c%c%c%c %2d c/r=%d res=%d last=%d\n",
  1858. fptr[7]>>1, fptr[8]>>1, fptr[9]>>1, fptr[10]>>1, fptr[11]>>1, fptr[12]>>1,
  1859. (fptr[13]&SSID_SSID_MASK)>>SSID_SSID_SHIFT,
  1860. (fptr[13]&SSID_H_MASK)>>SSID_H_SHIFT,
  1861. (fptr[13]&SSID_RR_MASK)>>SSID_RR_SHIFT,
  1862. fptr[13]&SSID_LAST_MASK);
  1863. for (n=2; n<this_p->num_addr; n++) {
  1864. printf (" digi %d %c%c%c%c%c%c %2d h=%d res=%d last=%d\n",
  1865. n - 1,
  1866. fptr[n*7+0]>>1, fptr[n*7+1]>>1, fptr[n*7+2]>>1, fptr[n*7+3]>>1, fptr[n*7+4]>>1, fptr[n*7+5]>>1,
  1867. (fptr[n*7+6]&SSID_SSID_MASK)>>SSID_SSID_SHIFT,
  1868. (fptr[n*7+6]&SSID_H_MASK)>>SSID_H_SHIFT,
  1869. (fptr[n*7+6]&SSID_RR_MASK)>>SSID_RR_SHIFT,
  1870. fptr[n*7+6]&SSID_LAST_MASK);
  1871. }
  1872. hex_dump (fptr, flen);
  1873. } /* end ax25_hex_dump */
  1874. /*------------------------------------------------------------------
  1875. *
  1876. * Function: ax25_is_aprs
  1877. *
  1878. * Purpose: Is this packet APRS format?
  1879. *
  1880. * Inputs: this_p - pointer to packet object.
  1881. *
  1882. * Returns: True if this frame has the proper control
  1883. * octets for an APRS packet.
  1884. * control 3 for UI frame
  1885. * protocol id 0xf0 for no layer 3
  1886. *
  1887. *
  1888. * Description: Dire Wolf should be able to act as a KISS TNC for
  1889. * any type of AX.25 activity. However, there are other
  1890. * places where we want to process only APRS.
  1891. * (e.g. digipeating and IGate.)
  1892. *
  1893. *------------------------------------------------------------------*/
  1894. int ax25_is_aprs (packet_t this_p)
  1895. {
  1896. int ctrl, pid, is_aprs;
  1897. assert (this_p->magic1 == MAGIC);
  1898. assert (this_p->magic2 == MAGIC);
  1899. if (this_p->frame_len == 0) return(0);
  1900. ctrl = ax25_get_control(this_p);
  1901. pid = ax25_get_pid(this_p);
  1902. is_aprs = this_p->num_addr >= 2 && ctrl == AX25_UI_FRAME && pid == AX25_PID_NO_LAYER_3;
  1903. #if 0
  1904. printf ("ax25_is_aprs(): ctrl=%02x, pid=%02x, is_aprs=%d\n", ctrl, pid, is_aprs);
  1905. #endif
  1906. return (is_aprs);
  1907. }
  1908. /*------------------------------------------------------------------
  1909. *
  1910. * Function: ax25_is_null_frame
  1911. *
  1912. * Purpose: Is this packet structure empty?
  1913. *
  1914. * Inputs: this_p - pointer to packet object.
  1915. *
  1916. * Returns: True if frame data length is 0.
  1917. *
  1918. * Description: This is used when we want to wake up the
  1919. * transmit queue processing thread but don't
  1920. * want to transmit a frame.
  1921. *
  1922. *------------------------------------------------------------------*/
  1923. int ax25_is_null_frame (packet_t this_p)
  1924. {
  1925. int is_null;
  1926. assert (this_p->magic1 == MAGIC);
  1927. assert (this_p->magic2 == MAGIC);
  1928. is_null = this_p->frame_len == 0;
  1929. #if 0
  1930. printf ("ax25_is_null_frame(): is_null=%d\n", is_null);
  1931. #endif
  1932. return (is_null);
  1933. }
  1934. /*------------------------------------------------------------------
  1935. *
  1936. * Function: ax25_get_control
  1937. ax25_get_c2
  1938. *
  1939. * Purpose: Get Control field from packet.
  1940. *
  1941. * Inputs: this_p - pointer to packet object.
  1942. *
  1943. * Returns: APRS uses AX25_UI_FRAME.
  1944. * This could also be used in other situations.
  1945. *
  1946. *------------------------------------------------------------------*/
  1947. int ax25_get_control (packet_t this_p)
  1948. {
  1949. assert (this_p->magic1 == MAGIC);
  1950. assert (this_p->magic2 == MAGIC);
  1951. if (this_p->frame_len == 0) return(-1);
  1952. if (this_p->num_addr >= 2) {
  1953. return (this_p->frame_data[ax25_get_control_offset(this_p)]);
  1954. }
  1955. return (-1);
  1956. }
  1957. int ax25_get_c2 (packet_t this_p)
  1958. {
  1959. assert (this_p->magic1 == MAGIC);
  1960. assert (this_p->magic2 == MAGIC);
  1961. if (this_p->frame_len == 0) return(-1);
  1962. if (this_p->num_addr >= 2) {
  1963. int offset2 = ax25_get_control_offset(this_p)+1;
  1964. if (offset2 < this_p->frame_len) {
  1965. return (this_p->frame_data[offset2]);
  1966. }
  1967. else {
  1968. return (-1); /* attempt to go beyond the end of frame. */
  1969. }
  1970. }
  1971. return (-1); /* not AX.25 */
  1972. }
  1973. /*------------------------------------------------------------------
  1974. *
  1975. * Function: ax25_get_pid
  1976. *
  1977. * Purpose: Get protocol ID from packet.
  1978. *
  1979. * Inputs: this_p - pointer to packet object.
  1980. *
  1981. * Returns: APRS uses 0xf0 for no layer 3.
  1982. * This could also be used in other situations.
  1983. *
  1984. * AX.25: "The Protocol Identifier (PID) field appears in information
  1985. * frames (I and UI) only. It identifies which kind of
  1986. * Layer 3 protocol, if any, is in use."
  1987. *
  1988. *------------------------------------------------------------------*/
  1989. int ax25_get_pid (packet_t this_p)
  1990. {
  1991. assert (this_p->magic1 == MAGIC);
  1992. assert (this_p->magic2 == MAGIC);
  1993. // TODO: handle 2 control byte case.
  1994. // TODO: sanity check: is it I or UI frame?
  1995. if (this_p->frame_len == 0) return(-1);
  1996. if (this_p->num_addr >= 2) {
  1997. return (this_p->frame_data[ax25_get_pid_offset(this_p)]);
  1998. }
  1999. return (-1);
  2000. }
  2001. /*------------------------------------------------------------------
  2002. *
  2003. * Function: ax25_get_frame_len
  2004. *
  2005. * Purpose: Get length of frame.
  2006. *
  2007. * Inputs: this_p - pointer to packet object.
  2008. *
  2009. * Returns: Number of octets in the frame buffer.
  2010. * Does NOT include the extra 2 for FCS.
  2011. *
  2012. *------------------------------------------------------------------*/
  2013. int ax25_get_frame_len (packet_t this_p)
  2014. {
  2015. assert (this_p->magic1 == MAGIC);
  2016. assert (this_p->magic2 == MAGIC);
  2017. assert (this_p->frame_len >= 0 && this_p->frame_len <= AX25_MAX_PACKET_LEN);
  2018. return (this_p->frame_len);
  2019. } /* end ax25_get_frame_len */
  2020. /*------------------------------------------------------------------------------
  2021. *
  2022. * Name: ax25_dedupe_crc
  2023. *
  2024. * Purpose: Calculate a checksum for the packet source, destination, and
  2025. * information but NOT the digipeaters.
  2026. * This is used for duplicate detection in the digipeater
  2027. * and IGate algorithms.
  2028. *
  2029. * Input: pp - Pointer to packet object.
  2030. *
  2031. * Returns: Value which will be the same for a duplicate but very unlikely
  2032. * to match a non-duplicate packet.
  2033. *
  2034. * Description: For detecting duplicates, we need to look
  2035. * + source station
  2036. * + destination
  2037. * + information field
  2038. * but NOT the changing list of digipeaters.
  2039. *
  2040. * Typically, only a checksum is kept to reduce memory
  2041. * requirements and amount of compution for comparisons.
  2042. * There is a very very small probability that two unrelated
  2043. * packets will result in the same checksum, and the
  2044. * undesired dropping of the packet.
  2045. *
  2046. * There is a 1 / 65536 chance of getting a false positive match
  2047. * which is good enough for this application.
  2048. * We could reduce that with a 32 bit CRC instead of reusing
  2049. * code from the AX.25 frame CRC calculation.
  2050. *
  2051. * Version 1.3: We exclude any trailing CR/LF at the end of the info part
  2052. * so we can detect duplicates that are received only over the
  2053. * air and those which have gone thru an IGate where the process
  2054. * removes any trailing CR/LF. Example:
  2055. *
  2056. * Original via RF only:
  2057. * W1TG-1>APU25N,N3LEE-10*,WIDE2-1:<IGATE,MSG_CNT=30,LOC_CNT=61<0x0d>
  2058. *
  2059. * When we get the same thing via APRS-IS:
  2060. * W1TG-1>APU25N,K1FFK,WIDE2*,qAR,WB2ZII-15:<IGATE,MSG_CNT=30,LOC_CNT=61
  2061. *
  2062. * (Actually there is a trailing space. Maybe some systems
  2063. * change control characters to space???)
  2064. * Hmmmm. I guess we should ignore trailing space as well for
  2065. * duplicate detection and suppression.
  2066. *
  2067. *------------------------------------------------------------------------------*/
  2068. unsigned short ax25_dedupe_crc (packet_t pp)
  2069. {
  2070. unsigned short crc;
  2071. char src[AX25_MAX_ADDR_LEN];
  2072. char dest[AX25_MAX_ADDR_LEN];
  2073. unsigned char *pinfo;
  2074. int info_len;
  2075. ax25_get_addr_with_ssid(pp, AX25_SOURCE, src);
  2076. ax25_get_addr_with_ssid(pp, AX25_DESTINATION, dest);
  2077. info_len = ax25_get_info (pp, &pinfo);
  2078. while (info_len >= 1 && (pinfo[info_len-1] == '\r' ||
  2079. pinfo[info_len-1] == '\n' ||
  2080. pinfo[info_len-1] == ' ')) {
  2081. // Temporary for debugging!
  2082. // if (pinfo[info_len-1] == ' ') {
  2083. // printf ("DEBUG: ax25_dedupe_crc ignoring trailing space.\n");
  2084. // }
  2085. info_len--;
  2086. }
  2087. crc = 0xffff;
  2088. crc = crc16((unsigned char *)src, strlen(src), crc);
  2089. crc = crc16((unsigned char *)dest, strlen(dest), crc);
  2090. crc = crc16(pinfo, info_len, crc);
  2091. return (crc);
  2092. }
  2093. /*------------------------------------------------------------------------------
  2094. *
  2095. * Name: ax25_m_m_crc
  2096. *
  2097. * Purpose: Calculate a checksum for the packet.
  2098. * This is used for the multimodem duplicate detection.
  2099. *
  2100. * Input: pp - Pointer to packet object.
  2101. *
  2102. * Returns: Value which will be the same for a duplicate but very unlikely
  2103. * to match a non-duplicate packet.
  2104. *
  2105. * Description: For detecting duplicates, we need to look the entire packet.
  2106. *
  2107. * Typically, only a checksum is kept to reduce memory
  2108. * requirements and amount of compution for comparisons.
  2109. * There is a very very small probability that two unrelated
  2110. * packets will result in the same checksum, and the
  2111. * undesired dropping of the packet.
  2112. *------------------------------------------------------------------------------*/
  2113. unsigned short ax25_m_m_crc (packet_t pp)
  2114. {
  2115. unsigned short crc;
  2116. unsigned char fbuf[AX25_MAX_PACKET_LEN];
  2117. int flen;
  2118. flen = ax25_pack (pp, fbuf);
  2119. crc = 0xffff;
  2120. crc = crc16(fbuf, flen, crc);
  2121. return (crc);
  2122. }
  2123. /*------------------------------------------------------------------
  2124. *
  2125. * Function: ax25_safe_print
  2126. *
  2127. * Purpose: Print given string, changing non printable characters to
  2128. * hexadecimal notation. Note that character values
  2129. * <DEL>, 28, 29, 30, and 31 can appear in MIC-E message.
  2130. *
  2131. * Inputs: pstr - Pointer to string.
  2132. *
  2133. * len - Number of bytes. If < 0 we use strlen().
  2134. *
  2135. * ascii_only - Restrict output to only ASCII.
  2136. * Normally we allow UTF-8.
  2137. *
  2138. * Stops after non-zero len characters or at nul.
  2139. *
  2140. * Returns: none
  2141. *
  2142. * Description: Print a string in a "safe" manner.
  2143. * Anything that is not a printable character
  2144. * will be converted to a hexadecimal representation.
  2145. * For example, a Line Feed character will appear as <0x0a>
  2146. * rather than dropping down to the next line on the screen.
  2147. *
  2148. * ax25_from_text can accept this format.
  2149. *
  2150. *
  2151. * Example: W1MED-1>T2QP0S,N1OHZ,N8VIM*,WIDE1-1:'cQBl <0x1c>-/]<0x0d>
  2152. * ------ ------
  2153. *
  2154. * Questions: What should we do about UTF-8? Should that be displayed
  2155. * as hexadecimal for troubleshooting? Maybe an option so the
  2156. * packet raw data is in hexadecimal but an extracted
  2157. * comment displays UTF-8? Or a command line option for only ASCII?
  2158. *
  2159. * Trailing space:
  2160. * I recently noticed a case where a packet has space character
  2161. * at the end. If the last character of the line is a space,
  2162. * this will be displayed in hexadecimal to make it obvious.
  2163. *
  2164. *------------------------------------------------------------------*/
  2165. #define MAXSAFE 500
  2166. void ax25_safe_print (char *pstr, int len, int ascii_only)
  2167. {
  2168. int ch;
  2169. char safe_str[MAXSAFE*6+1];
  2170. int safe_len;
  2171. safe_len = 0;
  2172. safe_str[safe_len] = '\0';
  2173. if (len < 0)
  2174. len = strlen(pstr);
  2175. if (len > MAXSAFE)
  2176. len = MAXSAFE;
  2177. while (len > 0)
  2178. {
  2179. ch = *((unsigned char *)pstr);
  2180. if (ch == ' ' && (len == 1 || pstr[1] == '\0')) {
  2181. snprintf (safe_str + safe_len, sizeof(safe_str)-safe_len, "<0x%02x>", ch);
  2182. safe_len += 6;
  2183. }
  2184. else if (ch < ' ' || ch == 0x7f || ch == 0xfe || ch == 0xff ||
  2185. (ascii_only && ch >= 0x80) ) {
  2186. /* Control codes and delete. */
  2187. /* UTF-8 does not use fe and ff except in a possible */
  2188. /* "Byte Order Mark" (BOM) at the beginning. */
  2189. snprintf (safe_str + safe_len, sizeof(safe_str)-safe_len, "<0x%02x>", ch);
  2190. safe_len += 6;
  2191. }
  2192. else {
  2193. /* Let everything else thru so we can handle UTF-8 */
  2194. /* Maybe we should have an option to display 0x80 */
  2195. /* and above as hexadecimal. */
  2196. safe_str[safe_len++] = ch;
  2197. safe_str[safe_len] = '\0';
  2198. }
  2199. pstr++;
  2200. len--;
  2201. }
  2202. // TODO1.2: should return string rather printing to remove a race condition.
  2203. printf ("%s", safe_str);
  2204. } /* end ax25_safe_print */
  2205. /* end ax25_pad.c */