1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713 |
- #define AX25_PAD_C
- #include "direwolf.h"
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <stdio.h>
- #include <ctype.h>
- #include "regex.h"
- #if __WIN32__
- char *strtok_r(char *str, const char *delim, char **saveptr);
- #endif
- #include "ax25_pad.h"
- #include "fcs_calc.h"
- static volatile int new_count = 0;
- static volatile int delete_count = 0;
- static volatile int last_seq_num = 0;
- #if AX25MEMDEBUG
- int ax25memdebug = 0;
- void ax25memdebug_set(void)
- {
- ax25memdebug = 1;
- }
- int ax25memdebug_get (void)
- {
- return (ax25memdebug);
- }
- int ax25memdebug_seq (packet_t this_p)
- {
- return (this_p->seq);
- }
- #endif
- #define CLEAR_LAST_ADDR_FLAG this_p->frame_data[this_p->num_addr*7-1] &= ~ SSID_LAST_MASK
- #define SET_LAST_ADDR_FLAG this_p->frame_data[this_p->num_addr*7-1] |= SSID_LAST_MASK
- packet_t ax25_new (void)
- {
- struct packet_s *this_p;
- #if DEBUG
- printf ("ax25_new(): before alloc, new=%d, delete=%d\n", new_count, delete_count);
- #endif
- last_seq_num++;
- new_count++;
-
- if (new_count > delete_count + 256) {
- printf ("Report to WB2OSZ - Memory leak for packet objects. new=%d, delete=%d\n", new_count, delete_count);
- #if AX25MEMDEBUG
-
- ax25memdebug_set();
- #endif
- }
- this_p = calloc(sizeof (struct packet_s), (size_t)1);
- if (this_p == NULL) {
- printf ("ERROR - can't allocate memory in ax25_new.\n");
- }
- assert (this_p != NULL);
- this_p->magic1 = MAGIC;
- this_p->seq = last_seq_num;
- this_p->magic2 = MAGIC;
- this_p->num_addr = (-1);
- return (this_p);
- }
- #if AX25MEMDEBUG
- void ax25_delete_debug (packet_t this_p, char *src_file, int src_line)
- #else
- void ax25_delete (packet_t this_p)
- #endif
- {
- #if DEBUG
- printf ("ax25_delete(): before free, new=%d, delete=%d\n", new_count, delete_count);
- #endif
- if (this_p == NULL) {
- printf ("ERROR - NULL pointer passed to ax25_delete.\n");
- return;
- }
- delete_count++;
- #if AX25MEMDEBUG
- if (ax25memdebug) {
- 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);
- }
- #endif
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
-
- this_p->magic1 = 0;
- this_p->magic1 = 0;
-
- free (this_p);
- }
-
- #if AX25MEMDEBUG
- packet_t ax25_from_text_debug (char *monitor, int strict, char *src_file, int src_line)
- #else
- packet_t ax25_from_text (char *monitor, int strict)
- #endif
- {
- char stuff[512];
- char *pinfo;
- char *pa;
- char *saveptr;
- int ssid_temp, heard_temp;
- char atemp[AX25_MAX_ADDR_LEN];
- char info_part[AX25_MAX_INFO_LEN+1];
- int info_len;
- packet_t this_p = ax25_new ();
- #if AX25MEMDEBUG
- if (ax25memdebug) {
- printf ("ax25_from_text, seq=%d, called from %s %d\n", this_p->seq, src_file, src_line);
- }
- #endif
-
-
-
-
-
- strlcpy (stuff, monitor, sizeof(stuff));
- memset (this_p->frame_data + AX25_DESTINATION*7, ' ' << 1, 6);
- this_p->frame_data[AX25_DESTINATION*7+6] = SSID_H_MASK | SSID_RR_MASK;
-
- memset (this_p->frame_data + AX25_SOURCE*7, ' ' << 1, 6);
- this_p->frame_data[AX25_SOURCE*7+6] = SSID_H_MASK | SSID_RR_MASK | SSID_LAST_MASK;
- this_p->frame_data[14] = AX25_UI_FRAME;
- this_p->frame_data[15] = AX25_PID_NO_LAYER_3;
- this_p->frame_len = 7 + 7 + 1 + 1;
- this_p->num_addr = (-1);
- assert (ax25_get_num_addr(this_p) == 2);
- pinfo = strchr (stuff, ':');
- if (pinfo == NULL) {
- ax25_delete (this_p);
- return (NULL);
- }
- *pinfo = '\0';
- pinfo++;
- pa = strtok_r (stuff, ">", &saveptr);
- if (pa == NULL) {
- printf ("Failed to create packet from text. No source address\n");
- ax25_delete (this_p);
- return (NULL);
- }
- if ( ! ax25_parse_addr (AX25_SOURCE, pa, strict, atemp, &ssid_temp, &heard_temp)) {
- printf ("Failed to create packet from text. Bad source address\n");
- ax25_delete (this_p);
- return (NULL);
- }
- ax25_set_addr (this_p, AX25_SOURCE, atemp);
- ax25_set_h (this_p, AX25_SOURCE);
- ax25_set_ssid (this_p, AX25_SOURCE, ssid_temp);
-
- pa = strtok_r (NULL, ",", &saveptr);
- if (pa == NULL) {
- printf ("Failed to create packet from text. No destination address\n");
- ax25_delete (this_p);
- return (NULL);
- }
- if ( ! ax25_parse_addr (AX25_DESTINATION, pa, strict, atemp, &ssid_temp, &heard_temp)) {
- printf ("Failed to create packet from text. Bad destination address\n");
- ax25_delete (this_p);
- return (NULL);
- }
- ax25_set_addr (this_p, AX25_DESTINATION, atemp);
- ax25_set_h (this_p, AX25_DESTINATION);
- ax25_set_ssid (this_p, AX25_DESTINATION, ssid_temp);
- while (( pa = strtok_r (NULL, ",", &saveptr)) != NULL && this_p->num_addr < AX25_MAX_ADDRS ) {
- int k;
- k = this_p->num_addr;
- if ( ! ax25_parse_addr (k, pa, strict, atemp, &ssid_temp, &heard_temp)) {
- printf ("Failed to create packet from text. Bad digipeater address\n");
- ax25_delete (this_p);
- return (NULL);
- }
- ax25_set_addr (this_p, k, atemp);
- ax25_set_ssid (this_p, k, ssid_temp);
-
-
-
-
- if (heard_temp) {
- for ( ; k >= AX25_REPEATER_1; k--) {
- ax25_set_h (this_p, k);
- }
- }
- }
- #if DEBUG14H
- printf ("BEFORE: %s\nSAFE: ", pinfo);
- ax25_safe_print (pinfo, -1, 0);
- printf ("\n");
- #endif
- info_len = 0;
- while (*pinfo != '\0' && info_len < AX25_MAX_INFO_LEN) {
- if (strlen(pinfo) >= 6 &&
- pinfo[0] == '<' &&
- pinfo[1] == '0' &&
- pinfo[2] == 'x' &&
- isxdigit( (uint) pinfo[3]) &&
- isxdigit( (uint) pinfo[4]) &&
- pinfo[5] == '>') {
- char *p;
- info_part[info_len] = strtol (pinfo + 3, &p, 16);
- info_len++;
- pinfo += 6;
- }
- else {
- info_part[info_len] = *pinfo;
- info_len++;
- pinfo++;
- }
- }
- info_part[info_len] = '\0';
- #if DEBUG14H
- printf ("AFTER: %s\nSAFE: ", info_part);
- ax25_safe_print (info_part, info_len, 0);
- printf ("\n");
- #endif
- memcpy ((char*)(this_p->frame_data+this_p->frame_len), info_part, info_len);
- this_p->frame_len += info_len;
- return (this_p);
- }
- #if AX25MEMDEBUG
- packet_t ax25_from_frame_debug (unsigned char *fbuf, int flen, alevel_t alevel, char *src_file, int src_line)
- #else
- packet_t ax25_from_frame (unsigned char *fbuf, int flen, alevel_t alevel)
- #endif
- {
- packet_t this_p;
- if (flen < AX25_MIN_PACKET_LEN || flen > AX25_MAX_PACKET_LEN)
- {
- printf ("Frame length %d not in allowable range of %d to %d.\n", flen, AX25_MIN_PACKET_LEN, AX25_MAX_PACKET_LEN);
- return (NULL);
- }
- this_p = ax25_new ();
- #if AX25MEMDEBUG
- if (ax25memdebug) {
- printf ("ax25_from_frame, seq=%d, called from %s %d\n", this_p->seq, src_file, src_line);
- }
- #endif
- memcpy (this_p->frame_data, fbuf, flen);
- this_p->frame_data[flen] = 0;
- this_p->frame_len = flen;
-
- this_p->num_addr = (-1);
- (void) ax25_get_num_addr (this_p);
- return (this_p);
- }
- #if AX25MEMDEBUG
- packet_t ax25_dup_debug (packet_t copy_from, char *src_file, int src_line)
- #else
- packet_t ax25_dup (packet_t copy_from)
- #endif
- {
- int save_seq;
- packet_t this_p;
-
- this_p = ax25_new ();
- assert (this_p != NULL);
- save_seq = this_p->seq;
- memcpy (this_p, copy_from, sizeof (struct packet_s));
- this_p->seq = save_seq;
- #if AX25MEMDEBUG
- if (ax25memdebug) {
- printf ("ax25_dup, seq=%d, called from %s %d, clone of seq %d\n", this_p->seq, src_file, src_line, copy_from->seq);
- }
- #endif
- return (this_p);
- }
- static const char *position_name[1 + AX25_MAX_ADDRS] = {
- "", "Destination ", "Source ",
- "Digi1 ", "Digi2 ", "Digi3 ", "Digi4 ",
- "Digi5 ", "Digi6 ", "Digi7 ", "Digi8 " };
- int ax25_parse_addr (int position, char *in_addr, int strict, char *out_addr, int *out_ssid, int *out_heard)
- {
- char *p;
- char sstr[8];
- int i, j, k;
- int maxlen;
- *out_addr = '\0';
- *out_ssid = 0;
- *out_heard = 0;
- if (position < -1) position = -1;
- if (position > AX25_REPEATER_8) position = AX25_REPEATER_8;
- position++;
- if (strict && strlen(in_addr) >= 2 && strncmp(in_addr, "qA", 2) == 0) {
- printf ("%sAddress \"%s\" is a \"q-construct\" used for communicating with\n", position_name[position], in_addr);
- printf ("APRS Internet Servers. It should never appear when going over the radio.\n");
- }
-
- maxlen = strict ? 6 : (AX25_MAX_ADDR_LEN-1);
- p = in_addr;
- i = 0;
- for (p = in_addr; *p != '\0' && *p != '-' && *p != '*'; p++) {
- if (i >= maxlen) {
- printf ("%sAddress is too long. \"%s\" has more than %d characters.\n", position_name[position], in_addr, maxlen);
- return 0;
- }
- if ( ! isalnum((uint)*p)) {
- 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);
- return 0;
- }
- out_addr[i++] = *p;
- out_addr[i] = '\0';
- #if DECAMAIN
-
- if (strict && islower(*p) && strncmp(in_addr, "qA", 2) != 0) {
- printf ("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
- }
- #else
- if (strict && islower((uint)*p)) {
- printf ("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
- return 0;
- }
- #endif
- }
-
- j = 0;
- sstr[j] = '\0';
- if (*p == '-') {
- for (p++; isalnum((int)*p); p++) {
- if (j >= 2) {
- printf ("%sSSID is too long. SSID part of \"%s\" has more than 2 characters.\n", position_name[position], in_addr);
- return 0;
- }
- sstr[j++] = *p;
- sstr[j] = '\0';
- if (strict && ! isdigit((uint)*p)) {
- printf ("%sSSID must be digits. \"%s\" has letters in SSID.\n", position_name[position], in_addr);
- return 0;
- }
- }
- k = atoi(sstr);
- if (k < 0 || k > 15) {
- printf ("%sSSID out of range. SSID of \"%s\" not in range of 0 to 15.\n", position_name[position], in_addr);
- return 0;
- }
- *out_ssid = k;
- }
- if (*p == '*') {
- *out_heard = 1;
- p++;
- if (strict == 2) {
- printf ("\"*\" is not allowed at end of address \"%s\" here.\n", in_addr);
- return 0;
- }
- }
- if (*p != '\0') {
- printf ("Invalid character \"%c\" found in %saddress \"%s\".\n", *p, position_name[position], in_addr);
- return 0;
- }
-
- return (1);
- }
- int ax25_check_addresses (packet_t pp)
- {
- int n;
- char addr[AX25_MAX_ADDR_LEN];
- char ignore1[AX25_MAX_ADDR_LEN];
- int ignore2, ignore3;
- int all_ok = 1;
- for (n = 0; n < ax25_get_num_addr(pp); n++) {
- ax25_get_addr_with_ssid (pp, n, addr);
- all_ok &= ax25_parse_addr (n, addr, 1, ignore1, &ignore2, &ignore3);
- }
- if (! all_ok) {
- printf ("\n");
- printf ("*** The origin and journey of this packet should receive some scrutiny. ***\n");
- printf ("\n");
- }
- return (all_ok);
- }
- packet_t ax25_unwrap_third_party (packet_t from_pp)
- {
- unsigned char *info_p;
- packet_t result_pp;
- if (ax25_get_dti(from_pp) != '}') {
- printf ("Internal error: ax25_unwrap_third_party: wrong data type.\n");
- return (NULL);
- }
- (void) ax25_get_info (from_pp, &info_p);
-
-
- result_pp = ax25_from_text((char *)info_p + 1, 1);
- return (result_pp);
- }
- void ax25_set_addr (packet_t this_p, int n, char *ad)
- {
- int ssid_temp, heard_temp;
- char atemp[AX25_MAX_ADDR_LEN];
- int i;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (n >= 0 && n < AX25_MAX_ADDRS);
-
- if (n >= 0 && n < this_p->num_addr) {
-
-
-
-
- ax25_parse_addr (n, ad, 0, atemp, &ssid_temp, &heard_temp);
- memset (this_p->frame_data + n*7, ' ' << 1, 6);
- for (i=0; i<6 && atemp[i] != '\0'; i++) {
- this_p->frame_data[n*7+i] = atemp[i] << 1;
- }
- ax25_set_ssid (this_p, n, ssid_temp);
- }
- else if (n == this_p->num_addr) {
-
- ax25_insert_addr (this_p, n, ad);
- }
- else {
- printf ("Internal error, ax25_set_addr, bad position %d for '%s'\n", n, ad);
- }
-
-
-
-
- }
- void ax25_insert_addr (packet_t this_p, int n, char *ad)
- {
- int ssid_temp, heard_temp;
- char atemp[AX25_MAX_ADDR_LEN];
- int i;
- int expect;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (n >= AX25_REPEATER_1 && n < AX25_MAX_ADDRS);
-
-
-
- if ( this_p->num_addr >= AX25_MAX_ADDRS) {
- return;
- }
- CLEAR_LAST_ADDR_FLAG;
- this_p->num_addr++;
- memmove (this_p->frame_data + (n+1)*7, this_p->frame_data + n*7, this_p->frame_len - (n*7));
- memset (this_p->frame_data + n*7, ' ' << 1, 6);
- this_p->frame_len += 7;
- this_p->frame_data[n*7+6] = SSID_RR_MASK;
- SET_LAST_ADDR_FLAG;
-
-
-
- ax25_parse_addr (n, ad, 0, atemp, &ssid_temp, &heard_temp);
- memset (this_p->frame_data + n*7, ' ' << 1, 6);
- for (i=0; i<6 && atemp[i] != '\0'; i++) {
- this_p->frame_data[n*7+i] = atemp[i] << 1;
- }
-
- ax25_set_ssid (this_p, n, ssid_temp);
-
- expect = this_p->num_addr;
- this_p->num_addr = (-1);
- if (expect != ax25_get_num_addr (this_p)) {
- printf ("Internal error ax25_remove_addr expect %d, actual %d\n", expect, this_p->num_addr);
- }
- }
- void ax25_remove_addr (packet_t this_p, int n)
- {
- int expect;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (n >= AX25_REPEATER_1 && n < AX25_MAX_ADDRS);
-
- CLEAR_LAST_ADDR_FLAG;
- this_p->num_addr--;
- memmove (this_p->frame_data + n*7, this_p->frame_data + (n+1)*7, this_p->frame_len - ((n+1)*7));
- this_p->frame_len -= 7;
- SET_LAST_ADDR_FLAG;
-
- expect = this_p->num_addr;
- this_p->num_addr = (-1);
- if (expect != ax25_get_num_addr (this_p)) {
- printf ("Internal error ax25_remove_addr expect %d, actual %d\n", expect, this_p->num_addr);
- }
- }
- int ax25_get_num_addr (packet_t this_p)
- {
-
- int a;
- int addr_bytes;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->num_addr >= 0) {
- return (this_p->num_addr);
- }
- this_p->num_addr = 0;
-
- addr_bytes = 0;
- for (a = 0; a < this_p->frame_len && addr_bytes == 0; a++) {
- if (this_p->frame_data[a] & SSID_LAST_MASK) {
- addr_bytes = a + 1;
- }
- }
- if (addr_bytes % 7 == 0) {
- int addrs = addr_bytes / 7;
- if (addrs >= AX25_MIN_ADDRS && addrs <= AX25_MAX_ADDRS) {
- this_p->num_addr = addrs;
- }
- }
-
- return (this_p->num_addr);
- }
- int ax25_get_num_repeaters (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->num_addr >= 2) {
- return (this_p->num_addr - 2);
- }
- return (0);
- }
- void ax25_get_addr_with_ssid (packet_t this_p, int n, char *station)
- {
- int ssid;
- char sstr[8];
- int i;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (n < 0) {
- printf ("Internal error detected in ax25_get_addr_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
- printf ("Address index, %d, is less than zero.\n", n);
- strlcpy (station, "??????", 10);
- return;
- }
- if (n >= this_p->num_addr) {
- printf ("Internal error detected in ax25_get_addr_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
- printf ("Address index, %d, is too large for number of addresses, %d.\n", n, this_p->num_addr);
- strlcpy (station, "??????", 10);
- return;
- }
-
-
-
-
-
- for (i=0; i<6; i++) {
- station[i] = (this_p->frame_data[n*7+i] >> 1) & 0x7f;
- }
- station[6] = '\0';
- for (i=5; i>=0; i--) {
- if (station[i] == ' ')
- station[i] = '\0';
- else
- break;
- }
- ssid = ax25_get_ssid (this_p, n);
- if (ssid != 0) {
- snprintf (sstr, sizeof(sstr), "-%d", ssid);
- strlcat (station, sstr, 10);
- }
- }
- void ax25_get_addr_no_ssid (packet_t this_p, int n, char *station)
- {
- int i;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (n < 0) {
- printf ("Internal error detected in ax25_get_addr_no_ssid, %s, line %d.\n", __FILE__, __LINE__);
- printf ("Address index, %d, is less than zero.\n", n);
- strlcpy (station, "??????", 7);
- return;
- }
- if (n >= this_p->num_addr) {
- printf ("Internal error detected in ax25_get_no_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
- printf ("Address index, %d, is too large for number of addresses, %d.\n", n, this_p->num_addr);
- strlcpy (station, "??????", 7);
- return;
- }
-
-
-
-
-
- for (i=0; i<6; i++) {
- station[i] = (this_p->frame_data[n*7+i] >> 1) & 0x7f;
- }
- station[6] = '\0';
- for (i=5; i>=0; i--) {
- if (station[i] == ' ')
- station[i] = '\0';
- else
- break;
- }
- }
- int ax25_get_ssid (packet_t this_p, int n)
- {
-
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
-
- if (n >= 0 && n < this_p->num_addr) {
- return ((this_p->frame_data[n*7+6] & SSID_SSID_MASK) >> SSID_SSID_SHIFT);
- }
- else {
- printf ("Internal error: ax25_get_ssid(%d), num_addr=%d\n", n, this_p->num_addr);
- return (0);
- }
- }
- void ax25_set_ssid (packet_t this_p, int n, int ssid)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (n >= 0 && n < this_p->num_addr) {
- this_p->frame_data[n*7+6] = (this_p->frame_data[n*7+6] & ~ SSID_SSID_MASK) |
- ((ssid << SSID_SSID_SHIFT) & SSID_SSID_MASK) ;
- }
- else {
- printf ("Internal error: ax25_set_ssid(%d,%d), num_addr=%d\n", n, ssid, this_p->num_addr);
- }
- }
- int ax25_get_h (packet_t this_p, int n)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (n >= 0 && n < this_p->num_addr);
- if (n >= 0 && n < this_p->num_addr) {
- return ((this_p->frame_data[n*7+6] & SSID_H_MASK) >> SSID_H_SHIFT);
- }
- else {
- printf ("Internal error: ax25_get_h(%d), num_addr=%d\n", n, this_p->num_addr);
- return (0);
- }
- }
- void ax25_set_h (packet_t this_p, int n)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (n >= 0 && n < this_p->num_addr) {
- this_p->frame_data[n*7+6] |= SSID_H_MASK;
- }
- else {
- printf ("Internal error: ax25_set_hd(%d), num_addr=%d\n", n, this_p->num_addr);
- }
- }
- int ax25_get_heard(packet_t this_p)
- {
- int i;
- int result;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- result = AX25_SOURCE;
- for (i = AX25_REPEATER_1; i < ax25_get_num_addr(this_p); i++) {
-
- if (ax25_get_h(this_p,i)) {
- result = i;
- }
- }
- return (result);
- }
- int ax25_get_first_not_repeated(packet_t this_p)
- {
- int i;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- for (i = AX25_REPEATER_1; i < ax25_get_num_addr(this_p); i++) {
-
- if ( ! ax25_get_h(this_p,i)) {
- return (i);
- }
- }
- return (-1);
- }
- int ax25_get_rr (packet_t this_p, int n)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (n >= 0 && n < this_p->num_addr);
- if (n >= 0 && n < this_p->num_addr) {
- return ((this_p->frame_data[n*7+6] & SSID_RR_MASK) >> SSID_RR_SHIFT);
- }
- else {
- printf ("Internal error: ax25_get_rr(%d), num_addr=%d\n", n, this_p->num_addr);
- return (0);
- }
- }
- int ax25_get_info (packet_t this_p, unsigned char **paddr)
- {
- unsigned char *info_ptr;
- int info_len;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->num_addr >= 2) {
-
- info_ptr = this_p->frame_data + ax25_get_info_offset(this_p);
- info_len = ax25_get_num_info(this_p);
- }
- else {
-
- info_ptr = this_p->frame_data;
- info_len = this_p->frame_len;
- }
-
-
- assert (info_len >= 0);
- info_ptr[info_len] = '\0';
- *paddr = info_ptr;
- return (info_len);
- }
- int ax25_cut_at_crlf (packet_t this_p)
- {
- unsigned char *info_ptr;
- int info_len;
- int j;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- info_len = ax25_get_info (this_p, &info_ptr);
-
- for (j = 0; j < info_len; j++) {
- if (info_ptr[j] == '\r' || info_ptr[j] == '\n') {
- int chop = info_len - j;
- this_p->frame_len -= chop;
- return (chop);
- }
- }
- return (0);
- }
- int ax25_get_dti (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->num_addr >= 2) {
- return (this_p->frame_data[ax25_get_info_offset(this_p)]);
- }
- return (' ');
- }
- void ax25_set_nextp (packet_t this_p, packet_t next_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
-
- this_p->nextp = next_p;
- }
- packet_t ax25_get_nextp (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- return (this_p->nextp);
- }
- void ax25_set_release_time (packet_t this_p, double release_time)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
-
- this_p->release_time = release_time;
- }
- double ax25_get_release_time (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- return (this_p->release_time);
- }
- void ax25_set_modulo (packet_t this_p, int modulo)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- this_p->modulo = modulo;
- }
- void ax25_format_addrs (packet_t this_p, char *result)
- {
- int i;
- int heard;
- char stemp[AX25_MAX_ADDR_LEN];
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- *result = '\0';
-
-
-
- if (this_p->num_addr == 0) {
- return;
- }
- ax25_get_addr_with_ssid (this_p, AX25_SOURCE, stemp);
- strcat (result, stemp);
- strcat (result, ">");
- ax25_get_addr_with_ssid (this_p, AX25_DESTINATION, stemp);
- strcat (result, stemp);
- heard = ax25_get_heard(this_p);
- for (i=(int)AX25_REPEATER_1; i<this_p->num_addr; i++) {
- ax25_get_addr_with_ssid (this_p, i, stemp);
- strcat (result, ",");
- strcat (result, stemp);
- if (i == heard) {
- strcat (result, "*");
- }
- }
-
- strcat (result, ":");
- }
- void ax25_format_via_path (packet_t this_p, char *result, size_t result_size)
- {
- int i;
- int heard;
- char stemp[AX25_MAX_ADDR_LEN];
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- *result = '\0';
-
-
- if (this_p->num_addr == 0) {
- return;
- }
- heard = ax25_get_heard(this_p);
- for (i=(int)AX25_REPEATER_1; i<this_p->num_addr; i++) {
- if (i > (int)AX25_REPEATER_1) {
- strlcat (result, ",", result_size);
- }
- ax25_get_addr_with_ssid (this_p, i, stemp);
- strlcat (result, stemp, result_size);
- if (i == heard) {
- strlcat (result, "*", result_size);
- }
- }
- }
- int ax25_pack (packet_t this_p, unsigned char result[AX25_MAX_PACKET_LEN])
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (this_p->frame_len >= 0 && this_p->frame_len <= AX25_MAX_PACKET_LEN);
- memcpy (result, this_p->frame_data, this_p->frame_len);
- return (this_p->frame_len);
- }
- #define DESC_SIZ 40
- ax25_frame_type_t ax25_frame_type (packet_t this_p, cmdres_t *cr, char *desc, int *pf, int *nr, int *ns)
- {
- int c;
- int c2 = 0;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- strlcpy (desc, "????", DESC_SIZ);
- *cr = cr_11;
- *pf = -1;
- *nr = -1;
- *ns = -1;
- c = ax25_get_control(this_p);
- if (c < 0) {
- strlcpy (desc, "Not AX.25", DESC_SIZ);
- return (frame_not_AX25);
- }
- if (this_p->modulo == 0 && (c & 3) == 1 && ax25_get_c2(this_p) != -1) {
- this_p->modulo = modulo_128;
- }
- else if (this_p->modulo == 0 && (c & 1) == 0 && this_p->frame_data[ax25_get_info_offset(this_p)] == 0xF0) {
- this_p->modulo = modulo_128;
- }
- else if (this_p->modulo == 0 && (c & 1) == 0 && this_p->frame_data[ax25_get_info_offset(this_p)] == 0x08) {
- this_p->modulo = modulo_128;
- }
- if (this_p->modulo == modulo_128) {
- c2 = ax25_get_c2 (this_p);
- }
- int dst_c = this_p->frame_data[AX25_DESTINATION * 7 + 6] & SSID_H_MASK;
- int src_c = this_p->frame_data[AX25_SOURCE * 7 + 6] & SSID_H_MASK;
- char cr_text[8];
- char pf_text[8];
- if (dst_c) {
- if (src_c) { *cr = cr_11; strcpy(cr_text,"cc=11"); strcpy(pf_text,"p/f"); }
- else { *cr = cr_cmd; strcpy(cr_text,"cmd"); strcpy(pf_text,"p"); }
- }
- else {
- if (src_c) { *cr = cr_res; strcpy(cr_text,"res"); strcpy(pf_text,"f"); }
- else { *cr = cr_00; strcpy(cr_text,"cc=00"); strcpy(pf_text,"p/f"); }
- }
- if ((c & 1) == 0) {
- if (this_p->modulo == modulo_128) {
- *ns = (c >> 1) & 0x7f;
- *pf = c2 & 1;
- *nr = (c2 >> 1) & 0x7f;
- }
- else {
- *ns = (c >> 1) & 7;
- *pf = (c >> 4) & 1;
- *nr = (c >> 5) & 7;
- }
-
- 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));
- return (frame_type_I);
- }
- else if ((c & 2) == 0) {
- if (this_p->modulo == modulo_128) {
- *pf = c2 & 1;
- *nr = (c2 >> 1) & 0x7f;
- }
- else {
- *pf = (c >> 4) & 1;
- *nr = (c >> 5) & 7;
- }
-
- switch ((c >> 2) & 3) {
- 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;
- 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;
- 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;
- 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;
- }
- }
- else {
- *pf = (c >> 4) & 1;
-
- switch (c & 0xef) {
-
- case 0x6f: snprintf (desc, DESC_SIZ, "SABME %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_SABME); break;
- case 0x2f: snprintf (desc, DESC_SIZ, "SABM %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_SABM); break;
- case 0x43: snprintf (desc, DESC_SIZ, "DISC %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_DISC); break;
- case 0x0f: snprintf (desc, DESC_SIZ, "DM %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_DM); break;
- case 0x63: snprintf (desc, DESC_SIZ, "UA %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_UA); break;
- case 0x87: snprintf (desc, DESC_SIZ, "FRMR %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_FRMR); break;
- case 0x03: snprintf (desc, DESC_SIZ, "UI %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_UI); break;
- case 0xaf: snprintf (desc, DESC_SIZ, "XID %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_XID); break;
- case 0xe3: snprintf (desc, DESC_SIZ, "TEST %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_TEST); break;
- default: snprintf (desc, DESC_SIZ, "U other???"); return (frame_type_U); break;
- }
- }
-
-
- return (frame_not_AX25);
- }
- static void hex_dump (unsigned char *p, int len)
- {
- int n, i, offset;
- offset = 0;
- while (len > 0) {
- n = len < 16 ? len : 16;
- printf (" %03x: ", offset);
- for (i=0; i<n; i++) {
- printf (" %02x", p[i]);
- }
- for (i=n; i<16; i++) {
- printf (" ");
- }
- printf (" ");
- for (i=0; i<n; i++) {
- printf ("%c", isprint(p[i]) ? p[i] : '.');
- }
- printf ("\n");
- p += 16;
- offset += 16;
- len -= 16;
- }
- }
- static void ctrl_to_text (int c, char *out, size_t outsiz)
- {
- 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); }
- else if ((c & 0xf) == 0x01) { snprintf (out, outsiz, "S frame RR: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
- else if ((c & 0xf) == 0x05) { snprintf (out, outsiz, "S frame RNR: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
- else if ((c & 0xf) == 0x09) { snprintf (out, outsiz, "S frame REJ: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
- else if ((c & 0xf) == 0x0D) { snprintf (out, outsiz, "S frame sREJ: n(r)=%d, p/f=%d", (c>>5)&7, (c>>4)&1); }
- else if ((c & 0xef) == 0x6f) { snprintf (out, outsiz, "U frame SABME: p=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0x2f) { snprintf (out, outsiz, "U frame SABM: p=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0x43) { snprintf (out, outsiz, "U frame DISC: p=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0x0f) { snprintf (out, outsiz, "U frame DM: f=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0x63) { snprintf (out, outsiz, "U frame UA: f=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0x87) { snprintf (out, outsiz, "U frame FRMR: f=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0x03) { snprintf (out, outsiz, "U frame UI: p/f=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0xAF) { snprintf (out, outsiz, "U frame XID: p/f=%d", (c>>4)&1); }
- else if ((c & 0xef) == 0xe3) { snprintf (out, outsiz, "U frame TEST: p/f=%d", (c>>4)&1); }
- else { snprintf (out, outsiz, "Unknown frame type for control = 0x%02x", c); }
- }
- #define PID_TEXT_SIZE 80
- static void pid_to_text (int p, char out[PID_TEXT_SIZE])
- {
- if ((p & 0x30) == 0x10) { snprintf (out, PID_TEXT_SIZE, "AX.25 layer 3 implemented."); }
- else if ((p & 0x30) == 0x20) { snprintf (out, PID_TEXT_SIZE, "AX.25 layer 3 implemented."); }
- else if (p == 0x01) { snprintf (out, PID_TEXT_SIZE, "ISO 8208/CCITT X.25 PLP"); }
- else if (p == 0x06) { snprintf (out, PID_TEXT_SIZE, "Compressed TCP/IP packet. Van Jacobson (RFC 1144)"); }
- else if (p == 0x07) { snprintf (out, PID_TEXT_SIZE, "Uncompressed TCP/IP packet. Van Jacobson (RFC 1144)"); }
- else if (p == 0x08) { snprintf (out, PID_TEXT_SIZE, "Segmentation fragment"); }
- else if (p == 0xC3) { snprintf (out, PID_TEXT_SIZE, "TEXNET datagram protocol"); }
- else if (p == 0xC4) { snprintf (out, PID_TEXT_SIZE, "Link Quality Protocol"); }
- else if (p == 0xCA) { snprintf (out, PID_TEXT_SIZE, "Appletalk"); }
- else if (p == 0xCB) { snprintf (out, PID_TEXT_SIZE, "Appletalk ARP"); }
- else if (p == 0xCC) { snprintf (out, PID_TEXT_SIZE, "ARPA Internet Protocol"); }
- else if (p == 0xCD) { snprintf (out, PID_TEXT_SIZE, "ARPA Address resolution"); }
- else if (p == 0xCE) { snprintf (out, PID_TEXT_SIZE, "FlexNet"); }
- else if (p == 0xCF) { snprintf (out, PID_TEXT_SIZE, "NET/ROM"); }
- else if (p == 0xF0) { snprintf (out, PID_TEXT_SIZE, "No layer 3 protocol implemented."); }
- else if (p == 0xFF) { snprintf (out, PID_TEXT_SIZE, "Escape character. Next octet contains more Level 3 protocol information."); }
- else { snprintf (out, PID_TEXT_SIZE, "Unknown protocol id = 0x%02x", p); }
- }
- void ax25_hex_dump (packet_t this_p)
- {
- int n;
- unsigned char *fptr = this_p->frame_data;
- int flen = this_p->frame_len;
-
- if (this_p->num_addr >= AX25_MIN_ADDRS && this_p->num_addr <= AX25_MAX_ADDRS) {
- int c, p;
- char cp_text[120];
- char l_text[20];
- c = fptr[this_p->num_addr*7];
- p = fptr[this_p->num_addr*7+1];
- ctrl_to_text (c, cp_text, sizeof(cp_text));
- if ( (c & 0x01) == 0 ||
- c == 0x03 || c == 0x13) {
- char pid_text[PID_TEXT_SIZE];
- pid_to_text (p, pid_text);
- strlcat (cp_text, ", ", sizeof(cp_text));
- strlcat (cp_text, pid_text, sizeof(cp_text));
- }
- snprintf (l_text, sizeof(l_text), ", length = %d", flen);
- strlcat (cp_text, l_text, sizeof(cp_text));
- printf ("%s\n", cp_text);
- }
- printf (" dest %c%c%c%c%c%c %2d c/r=%d res=%d last=%d\n",
- fptr[0]>>1, fptr[1]>>1, fptr[2]>>1, fptr[3]>>1, fptr[4]>>1, fptr[5]>>1,
- (fptr[6]&SSID_SSID_MASK)>>SSID_SSID_SHIFT,
- (fptr[6]&SSID_H_MASK)>>SSID_H_SHIFT,
- (fptr[6]&SSID_RR_MASK)>>SSID_RR_SHIFT,
- fptr[6]&SSID_LAST_MASK);
- printf (" source %c%c%c%c%c%c %2d c/r=%d res=%d last=%d\n",
- fptr[7]>>1, fptr[8]>>1, fptr[9]>>1, fptr[10]>>1, fptr[11]>>1, fptr[12]>>1,
- (fptr[13]&SSID_SSID_MASK)>>SSID_SSID_SHIFT,
- (fptr[13]&SSID_H_MASK)>>SSID_H_SHIFT,
- (fptr[13]&SSID_RR_MASK)>>SSID_RR_SHIFT,
- fptr[13]&SSID_LAST_MASK);
- for (n=2; n<this_p->num_addr; n++) {
- printf (" digi %d %c%c%c%c%c%c %2d h=%d res=%d last=%d\n",
- n - 1,
- 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,
- (fptr[n*7+6]&SSID_SSID_MASK)>>SSID_SSID_SHIFT,
- (fptr[n*7+6]&SSID_H_MASK)>>SSID_H_SHIFT,
- (fptr[n*7+6]&SSID_RR_MASK)>>SSID_RR_SHIFT,
- fptr[n*7+6]&SSID_LAST_MASK);
- }
- hex_dump (fptr, flen);
- }
- int ax25_is_aprs (packet_t this_p)
- {
- int ctrl, pid, is_aprs;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->frame_len == 0) return(0);
- ctrl = ax25_get_control(this_p);
- pid = ax25_get_pid(this_p);
- is_aprs = this_p->num_addr >= 2 && ctrl == AX25_UI_FRAME && pid == AX25_PID_NO_LAYER_3;
- #if 0
- printf ("ax25_is_aprs(): ctrl=%02x, pid=%02x, is_aprs=%d\n", ctrl, pid, is_aprs);
- #endif
- return (is_aprs);
- }
- int ax25_is_null_frame (packet_t this_p)
- {
- int is_null;
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- is_null = this_p->frame_len == 0;
- #if 0
- printf ("ax25_is_null_frame(): is_null=%d\n", is_null);
- #endif
- return (is_null);
- }
- int ax25_get_control (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->frame_len == 0) return(-1);
- if (this_p->num_addr >= 2) {
- return (this_p->frame_data[ax25_get_control_offset(this_p)]);
- }
- return (-1);
- }
- int ax25_get_c2 (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- if (this_p->frame_len == 0) return(-1);
- if (this_p->num_addr >= 2) {
- int offset2 = ax25_get_control_offset(this_p)+1;
- if (offset2 < this_p->frame_len) {
- return (this_p->frame_data[offset2]);
- }
- else {
- return (-1);
- }
- }
- return (-1);
- }
- int ax25_get_pid (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
-
-
- if (this_p->frame_len == 0) return(-1);
- if (this_p->num_addr >= 2) {
- return (this_p->frame_data[ax25_get_pid_offset(this_p)]);
- }
- return (-1);
- }
- int ax25_get_frame_len (packet_t this_p)
- {
- assert (this_p->magic1 == MAGIC);
- assert (this_p->magic2 == MAGIC);
- assert (this_p->frame_len >= 0 && this_p->frame_len <= AX25_MAX_PACKET_LEN);
- return (this_p->frame_len);
- }
- unsigned short ax25_dedupe_crc (packet_t pp)
- {
- unsigned short crc;
- char src[AX25_MAX_ADDR_LEN];
- char dest[AX25_MAX_ADDR_LEN];
- unsigned char *pinfo;
- int info_len;
- ax25_get_addr_with_ssid(pp, AX25_SOURCE, src);
- ax25_get_addr_with_ssid(pp, AX25_DESTINATION, dest);
- info_len = ax25_get_info (pp, &pinfo);
- while (info_len >= 1 && (pinfo[info_len-1] == '\r' ||
- pinfo[info_len-1] == '\n' ||
- pinfo[info_len-1] == ' ')) {
-
-
-
-
- info_len--;
- }
- crc = 0xffff;
- crc = crc16((unsigned char *)src, strlen(src), crc);
- crc = crc16((unsigned char *)dest, strlen(dest), crc);
- crc = crc16(pinfo, info_len, crc);
- return (crc);
- }
- unsigned short ax25_m_m_crc (packet_t pp)
- {
- unsigned short crc;
- unsigned char fbuf[AX25_MAX_PACKET_LEN];
- int flen;
- flen = ax25_pack (pp, fbuf);
- crc = 0xffff;
- crc = crc16(fbuf, flen, crc);
- return (crc);
- }
- #define MAXSAFE 500
- void ax25_safe_print (char *pstr, int len, int ascii_only)
- {
- int ch;
- char safe_str[MAXSAFE*6+1];
- int safe_len;
- safe_len = 0;
- safe_str[safe_len] = '\0';
- if (len < 0)
- len = strlen(pstr);
- if (len > MAXSAFE)
- len = MAXSAFE;
- while (len > 0)
- {
- ch = *((unsigned char *)pstr);
- if (ch == ' ' && (len == 1 || pstr[1] == '\0')) {
- snprintf (safe_str + safe_len, sizeof(safe_str)-safe_len, "<0x%02x>", ch);
- safe_len += 6;
- }
- else if (ch < ' ' || ch == 0x7f || ch == 0xfe || ch == 0xff ||
- (ascii_only && ch >= 0x80) ) {
-
-
-
- snprintf (safe_str + safe_len, sizeof(safe_str)-safe_len, "<0x%02x>", ch);
- safe_len += 6;
- }
- else {
-
-
-
- safe_str[safe_len++] = ch;
- safe_str[safe_len] = '\0';
- }
- pstr++;
- len--;
- }
- printf ("%s", safe_str);
- }
|