From bdf8a8367e79e6224f8078b5d1bb9b6ad8584079 Mon Sep 17 00:00:00 2001 From: Dave Hibberd Date: Sat, 22 Mar 2025 00:22:09 +0000 Subject: New upstream version 6.0.24.67+repack --- L2Code.c | 416 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 392 insertions(+), 24 deletions(-) (limited to 'L2Code.c') diff --git a/L2Code.c b/L2Code.c index f4c83e5..94fc9f5 100644 --- a/L2Code.c +++ b/L2Code.c @@ -33,6 +33,15 @@ along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses #include "cheaders.h" #include "tncinfo.h" +// This is needed to link with a lib built from source + +#ifdef WIN32 +#define ZEXPORT __stdcall +#endif + +#include + + #define PFBIT 0x10 // POLL/FINAL BIT IN CONTROL BYTE #define REJSENT 1 // SET WHEN FIRST REJ IS SENT IN REPLY @@ -111,7 +120,7 @@ int CheckKissInterlock(struct PORTCONTROL * MYPORT, int Exclusive); void hookL2SessionAccepted(int Port, char * fromCall, char * toCall, struct _LINKTABLE * LINK); void hookL2SessionDeleted(struct _LINKTABLE * LINK); void hookL2SessionAttempt(int Port, char * fromCall, char * toCall, struct _LINKTABLE * LINK); - +int L2Compressit(unsigned char * Out, int OutSize, unsigned char * In, int Len); extern int REALTIMETICKS; @@ -126,7 +135,9 @@ extern int REALTIMETICKS; #define SDINVC 1 // INVALID COMMAND #define SDNRER 8 // INVALID N(R) - +extern int L2Compress; +extern int L2CompMaxframe; +extern int L2CompPaclen; UCHAR NO_CTEXT = 0; UCHAR ALIASMSG = 0; @@ -842,6 +853,7 @@ VOID ProcessXIDCommand(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESS while (xidlen > 0) { + unsigned char * typeptr = ptr; Type = *ptr++; Len = *ptr++; @@ -889,6 +901,23 @@ VOID ProcessXIDCommand(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESS case 8: //RX Window break; + + case 16: + + // Compression + + if (L2Compress) + { + LINK->AllowCompress = 1; + // return as 17 + *typeptr = 17; + } + else + { + ptr = &ADJBUFFER->PID; + ptr[3] -= 2; // Length field - remove compress option + Buffer->LENGTH -=2; + } } } @@ -900,6 +929,8 @@ VOID ProcessXIDCommand(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESS LINK->LINKPORT = PORT; + LINK->KILLTIMER = L2KILLTIME - 60*3; // Time out after 60 secs if SABM not received + // save calls so we can match up SABM when it comes memcpy(LINK->LINKCALL, Buffer->ORIGIN, 7); @@ -999,6 +1030,7 @@ VOID L2LINKACTIVE(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * // MESSAGE ON AN ACTIVE LINK int CTLlessPF = CTL & ~PFBIT; + unsigned char * ptr; PORT->L2FRAMESFORUS++; @@ -1054,7 +1086,7 @@ VOID L2LINKACTIVE(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * LINK->L2STATE = 2; LINK->Ver2point2 = FALSE; - LINK->L2TIMER = 1; // USe retry to send SABM + LINK->L2TIMER = 1; // Use retry to send SABM } else if (CTLlessPF == XID) { @@ -1062,7 +1094,49 @@ VOID L2LINKACTIVE(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * LINK->L2STATE = 2; LINK->Ver2point2 = TRUE;// Must support 2.2 if responded to XID - LINK->L2TIMER = 1; // USe retry to send SABM + + // if Compress enabled set it + + ptr = &ADJBUFFER->PID; + + if (*ptr++ == 0x82 && *ptr++ == 0x80) + { + int Type; + int Len; + unsigned int value; + int xidlen = *(ptr++) << 8; + xidlen += *ptr++; + + // XID is set of Type, Len, Value n-tuples + + while (xidlen > 0) + { + Type = *ptr++; + Len = *ptr++; + + value = 0; + xidlen -= (Len + 2); + + while (Len--) + { + value <<=8; + value += *ptr++; + } + switch(Type) + { + case 17: + + // Compression + + if (L2Compress) + LINK->AllowCompress = 1; + + } + } + + } + + LINK->L2TIMER = 1; // Use retry to send SABM } ReleaseBuffer(Buffer); @@ -1721,9 +1795,9 @@ BOOL InternalL2SETUPCROSSLINK(PROUTE ROUTE, int Retries) else LINK->LINKWINDOW = PORT->PORTWINDOW; -// if (SUPPORT2point2) -// LINK->L2STATE = 1; // Send XID -// else + if (SUPPORT2point2) + LINK->L2STATE = 1; // Send XID + else LINK->L2STATE = 2; memcpy(LINK->LINKCALL, ROUTE->NEIGHBOUR_CALL, 7); @@ -2018,7 +2092,6 @@ VOID SDUFRM(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffe SDFRMR(LINK, PORT); // PROCESS FRAME REJECT CONDITION } - ReleaseBuffer(Buffer); } @@ -2450,6 +2523,8 @@ CheckPF: } +int doinflate(unsigned char * source, unsigned char * dest, int Len, int destlen, int * outLen); + VOID PROC_I_FRAME(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer) { @@ -2479,6 +2554,7 @@ VOID PROC_I_FRAME(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Info = &Buffer->PID; LINK->bytesRXed += Length; + LINK->Received += Length - 1; // Exclude PID // Adjust for DIGIS @@ -2495,6 +2571,111 @@ VOID PROC_I_FRAME(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * switch(PID) { + case 0xf2: + + // Intermediate fragment of compressed. Save + + // Length and Info include pid + + Length--; + Info++; + + if (LINK->unCompress == 0) + LINK->unCompress = malloc(8192); + + // Save data + + memcpy(&LINK->unCompress[LINK->unCompressLen], Info, Length); + LINK->unCompressLen += Length; + + ReleaseBuffer(Buffer); + + LINK->L2ACKREQ = PORT->PORTT2; // SET RR NEEDED + LINK->KILLTIMER = 0; // RESET IDLE LINK TIMER + return; + + + case 0xf1: + + // Compressed last or only + + { + char exBuffer[8192]; + int Len; + int outLen; + int sendLen; + char * sendptr = exBuffer; + + Length--; + Info++; + + // we may have previous fragments + + if (LINK->unCompressLen) + { + memcpy(&LINK->unCompress[LINK->unCompressLen], Info, Length); + LINK->unCompressLen += Length; + Len = doinflate(LINK->unCompress, exBuffer, LINK->unCompressLen, 8192, &outLen); + LINK->ReceivedAfterExpansion += outLen - 1; + + LINK->unCompressLen = 0; + } + else + { + Len = doinflate(Info, exBuffer, Length, 8192, &outLen); + LINK->ReceivedAfterExpansion += outLen - 1; + } + sendLen = outLen; + + // Send first bit in input buffer. If still some left get new buffers for it + + if (sendLen > 257) + sendLen = 257; + + // First byte is original PID + + memcpy(&Msg->PID, exBuffer, sendLen); + Msg->LENGTH = sendLen + MSGHDDRLEN; + + C_Q_ADD(&LINK->RX_Q, Msg); + + outLen -= sendLen; + sendptr += sendLen; + + while (outLen > 0) + { + sendLen = outLen; + + if (sendLen > 236) + sendLen = 236; + + Msg = GetBuff(); + + if (Msg) + { + // Just ignore if no buffers - shouldn't happen + + Msg->PID = exBuffer[0]; + Msg->PORT = LINK->LINKPORT->PORTNUMBER; + + memcpy(Msg->L2DATA, sendptr, sendLen); + Length = sendLen + 1; + + Msg->LENGTH = Length + MSGHDDRLEN; + C_Q_ADD(&LINK->RX_Q, Msg); + } + + outLen -= sendLen; + sendptr += sendLen; + } + + LINK->L2ACKREQ = PORT->PORTT2; // SET RR NEEDED + LINK->KILLTIMER = 0; // RESET IDLE LINK TIMER + + return; + } + + case 0xcc: case 0xcd: @@ -2544,6 +2725,7 @@ VOID PROC_I_FRAME(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * // Copy Data back over memmove(&Msg->PID, Info, Length); + LINK->ReceivedAfterExpansion += Length - 1; Buffer->LENGTH = Length + MSGHDDRLEN; @@ -2758,7 +2940,7 @@ VOID SDETX(struct _LINKTABLE * LINK) UCHAR * ptr1, * ptr2; UCHAR CTL; int count; - MESSAGE * Msg; + struct DATAMESSAGE * Msg; MESSAGE * Buffer; // DONT SEND IF RESEQUENCING RECEIVED FRAMES - CAN CAUSE FRMR PROBLEMS @@ -2766,11 +2948,6 @@ VOID SDETX(struct _LINKTABLE * LINK) // if (LINK->L2RESEQ_Q) // return; - if (LINK->LINKPORT->PORTNUMBER == 19) - { - int i = 0; - } - Outstanding = LINK->LINKNS - LINK->LINKOWS; // Was WS not NS if (Outstanding < 0) @@ -2783,13 +2960,150 @@ VOID SDETX(struct _LINKTABLE * LINK) while (LINK->TX_Q && LINK->FRAMES[LINK->SDTSLOT] == NULL) { + // Try compressing here. Only Compress PID 0xF0 frames - NETROM doesn't treat L2 session as a byte stream + Msg = Q_REM(&LINK->TX_Q); Msg->CHAIN = NULL; - LINK->FRAMES[LINK->SDTSLOT] = Msg; - LINK->SDTSLOT ++; - LINK->SDTSLOT &= 7; - } + + if (LINK->AllowCompress && Msg->LENGTH > 20 && LINK->TX_Q && Msg->PID == 240) // if short and no more not worth trying compression + { + int complen = 0; + int dataLen; + int savePort = Msg->PORT; + int savePID = Msg->PID; + unsigned char Compressed[8192]; + unsigned char toCompress[8192]; + int toCompressLen = 0; + + int slots = 0; + int n = LINK->SDTSLOT; + int maxcompsize; + int PACLEN = LINK->LINKPORT->PORTPACLEN; + unsigned char * compdata; + int sendLen = complen; + int uncompressed = 0; + + if (PACLEN == 0) + PACLEN = 256; + + // I think I need to know how many slots are available, so I don't compress too much + // Then collect data, compressing after each frame to make sure will fit in available space + + while (LINK->FRAMES[n] == NULL && slots < 8) + { + slots++; + n++; + n &= 7; + } + + maxcompsize = slots * PACLEN; + + // Save first packet, then see if more on TX_Q + + toCompressLen = 0; + + dataLen = Msg->LENGTH - MSGHDDRLEN; + + LINK->Sent += dataLen; + + memcpy(&toCompress[toCompressLen], &Msg->PID, dataLen); + toCompressLen += dataLen; + + complen = L2Compressit(Compressed, 8192, toCompress, toCompressLen); + + ReleaseBuffer(Msg); + + while (LINK->TX_Q) + { + Msg = LINK->TX_Q; // Leave on queue until sure it will fit + dataLen = Msg->LENGTH - MSGHDDRLEN -1; // PID only on 1st fragment + + memcpy(&toCompress[toCompressLen], &Msg->L2DATA, dataLen); + toCompressLen += dataLen; + + // Need to make sure we don't go over maxcompsize + + complen = L2Compressit(Compressed, 8192, toCompress, toCompressLen); + + if (complen > maxcompsize) + { + // Remove last fragment and compress again + + toCompressLen -= dataLen; + complen = L2Compressit(Compressed, 8192, toCompress, toCompressLen); + break; + } + else + { + LINK->Sent += dataLen; + Msg = Q_REM(&LINK->TX_Q); + Msg->CHAIN = NULL; + + ReleaseBuffer(Msg); + } + } + + if (complen >= toCompressLen) + { + // Won't compress, so just send original data + // May still need to fragment + + memcpy(Compressed, toCompress, toCompressLen); + complen = toCompressLen - 1; // Remove leading PID + uncompressed = 1; + compdata = &Compressed[1]; + } + else + compdata = Compressed; + + // We now need to packetize and add to FRAMES + + LINK->SentAfterCompression += complen; + + sendLen = PACLEN; + while (complen > 0) + { + int PID = 0xF1; + + if (complen > sendLen) + PID = 0xF2; // More to come + else + sendLen = complen; + + if (uncompressed) + PID = Compressed[0]; + + Msg = GetBuff(); + + if (!Msg) + return; + + Msg->PORT = savePort; + Msg->PID = PID; + + memcpy(&Msg->L2DATA, compdata, sendLen); + Msg->LENGTH = sendLen + MSGHDDRLEN + 1; + + LINK->FRAMES[LINK->SDTSLOT] = Msg; + LINK->SDTSLOT ++; + LINK->SDTSLOT &= 7; + + compdata += sendLen; + complen -= sendLen; + } + + toCompressLen = 0; + + } + else + { + LINK->FRAMES[LINK->SDTSLOT] = Msg; + LINK->SDTSLOT ++; + LINK->SDTSLOT &= 7; + } + } + // dont send while poll outstanding while ((LINK->L2FLAGS & POLLSENT) == 0) @@ -3000,13 +3314,25 @@ VOID L2TimerProc() { // CIRCUIT HAS BEEN IDLE TOO LONG - SHUT IT DOWN - LINK->KILLTIMER = 0; - LINK->L2TIMER = 1; // TO FORCE DISC - LINK->L2STATE = 4; // DISCONNECTING + // if in XID received state session was never established so don't send DISC - // TELL OTHER LEVELS + if (LINK->L2STATE == 1) + { + if (PORT->TNC && PORT->TNC->Hardware == H_KISSHF) + DetachKISSHF(PORT); - InformPartner(LINK, NORMALCLOSE); + CLEAROUTLINK(LINK); + } + else + { + LINK->KILLTIMER = 0; + LINK->L2TIMER = 1; // TO FORCE DISC + LINK->L2STATE = 4; // DISCONNECTING + + // TELL OTHER LEVELS + + InformPartner(LINK, NORMALCLOSE); + } } LINK++; } @@ -3311,6 +3637,9 @@ VOID CLEAROUTLINK(struct _LINKTABLE * LINK) CLEARL2QUEUES(LINK); // TO RELEASE ANY BUFFERS + if (LINK->unCompress) + free(LINK->unCompress); + memset(LINK, 0, sizeof(struct _LINKTABLE)); } @@ -3345,7 +3674,11 @@ VOID L2SENDXID(struct _LINKTABLE * LINK) *ptr++ = 0x82; // FI *ptr++ = 0x80; // GI *ptr++ = 0x0; - *ptr++ = 0x10; // Length 16 + + if (L2Compress) + *ptr++ = 0x12; // Length 18 + else + *ptr++ = 0x10; // Length 16 *ptr++ = 0x02; // Classes of Procedures *ptr++ = 0x02; // Length @@ -3375,6 +3708,14 @@ VOID L2SENDXID(struct _LINKTABLE * LINK) *ptr++ = 0x01; // Len *ptr++ = 0x07; // 7 + // if L2Compress Enabled request it + + if (L2Compress) + { + *ptr++ = 0x10; // Compress + *ptr++ = 0x00; // Len + } + Buffer->LENGTH = (int)(ptr - (UCHAR *)Buffer); // SET LENGTH LINK->L2TIMER = ONEMINUTE; // (RE)SET TIMER @@ -4178,6 +4519,33 @@ int seeifUnlockneeded(struct _LINKTABLE * LINK) return 0; } +int L2Compressit(unsigned char * Out, int OutSize, unsigned char * In, int Len) +{ + z_stream defstream; + int maxSize; + + defstream.zalloc = Z_NULL; + defstream.zfree = Z_NULL; + defstream.opaque = Z_NULL; + + defstream.avail_in = Len; // size of input + defstream.next_in = (Bytef *)In; // input char array + + deflateInit(&defstream, Z_BEST_COMPRESSION); + maxSize = deflateBound(&defstream, Len); + + if (maxSize > OutSize) + return 0; + + defstream.avail_out = maxSize; // size of output + defstream.next_out = (Bytef *)Out; // output char array + + deflate(&defstream, Z_FINISH); + deflateEnd(&defstream); + + return defstream.total_out; +} + -- cgit v1.2.3