more fixes
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Thu, 27 Mar 2025 18:52:36 +0000 (19:52 +0100)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Thu, 27 Mar 2025 18:52:36 +0000 (19:52 +0100)
morep.c
morep_server.c
verbose.h

diff --git a/morep.c b/morep.c
index bf177262b7dd2ec4c2a492a0bef94fd15d9ecaab..4f2323ca2fe317c3bb295f63864b4b75a204d229 100644 (file)
--- a/morep.c
+++ b/morep.c
@@ -40,7 +40,7 @@ typedef struct {
 
 #define MAX_MOREP_NUMBER 256
 #define MAX_ETHER_SIZE 1500 /* 9000 for jumbo frame */
-#define MAX_FRAGMENTS (2^4)
+#define MAX_FRAGMENTS 15
 
 /**
    Fragment structure
@@ -326,7 +326,8 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
     int rc = 0;
     do {
         int pklen = (len > MAX_ETHER_SIZE - 4) ? MAX_ETHER_SIZE - 4 : len;
-        uint16_t msglen = htons (len | (nfrag << 11) | ((pklen != len) << 15));
+        VERBOSE (morep, DEBUG, PRINTF ("flag/frag/length: %d/%d/%d\n", (pklen != len), nfrag, pklen));
+        uint16_t msglen = htons (pklen | (nfrag << 11) | ((pklen != len) << 15));
         memcpy (morep->tx_buffer + 16, (void *) &msglen, 2);
         VERBOSE (morep, DEBUG, PRINTF ("sent preamble: "); int i; for (i = 0; i < 18; i++) printf ("%02x:", morep->tx_buffer[i]); printf ("\n"));
 
@@ -338,7 +339,7 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
             txlen = 64;
         }
         rc = sendto (morep->sock, morep->tx_buffer, txlen, 0, NULL, 0);
-        
+
         len -= pklen;
         buffer -= pklen;
         nfrag++;
@@ -350,6 +351,7 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
 int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
 {
     VERBOSE (morep, TRACE, PRINTF ("MOREP_Receive\n"));
+    int i;
 
     *msgtype = 0;
     if (((index < 0) || (index >= MAX_MOREP_NUMBER)) ||
@@ -358,7 +360,6 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
         return -1;
     }
     MOREP_descriptor_t *morep =  MOREP_list[index];
-    int i;
     for (i = 0; i < MAX_FRAGMENTS; i++) {
         (morep->fragment_list + i)->len = 0;
         //(morep->fragment_list + i)->status = 0;
@@ -398,7 +399,7 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
         /* check padding */
         if (pkglen < 64 - 18) {
             VERBOSE (morep, DEBUG, PRINTF ("padding of %d bytes\n", 64 - 18 - pkglen));
-            VERBOSE (morep, DEBUG, int i; for (i = pkglen + 18; i < 64; i++) if (morep->rx_buffer[i] != 0) { VERBOSE (morep, WARNING, PRINTF ("incorrect padding (%d)\n", i)); break; });
+            VERBOSE (morep, DEBUG, int _i; for (_i = pkglen + 18; _i < 64; _i++) if (morep->rx_buffer[_i] != 0) { VERBOSE (morep, WARNING, PRINTF ("incorrect padding (%d)\n", _i)); break; });
         }
         if (((rxlen == 64) && (pkglen > 64 - 18)) || ((rxlen > 64) && (pkglen != rxlen - 18))) {
             VERBOSE (morep, WARNING, PRINTF ("incorrect size (%d/%d)\n", pkglen, rxlen - 18));
@@ -413,7 +414,6 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
             frag->len = pkglen;
             //frag->status = 1;
         } else {
-            int i;
             rxlen = 0;
             for (i = 0; (i < MAX_FRAGMENTS) && ((morep->fragment_list + i)->len != 0); i++) {
                 MOREP_fragment_t *frag = morep->fragment_list + i;
index 6161815878703141de7a65bfa65cf0cacb7f8094..569aa485e3f074cb4249f40d3adaff2c61ec9b3a 100644 (file)
@@ -34,6 +34,8 @@ DECLARE_VERBOSE_LEVEL (morep, INFO);
 
 char *words[] = {"Quo", "cognito", "Constantius", "ultra", "mortalem", "modum", "exarsit", "ac", "nequo", "casu", "idem", "Gallus", "de", "futuris", "incertus", "agitare", "quaedam", "conducentia", "saluti", "suae", "per", "itinera", "conaretur,", "remoti", "sunt", "omnes", "de", "industria", "milites", "agentes", "in", "civitatibus", "perviis.", NULL};
 
+#define BUFMAX 4096
+
 void sig_handler (int sig)
 {
     switch (sig) {
@@ -67,16 +69,16 @@ int server (char *url)
 
         uint8_t rxmsgtype = 0;
         int rxmsglen = 0;
-        uint8_t rxbuffer[1500] = {0};
+        uint8_t rxbuffer[1496 * 16] = {0};
         int rxseqnum = MOREP_Receive (morep, &rxmsgtype, rxbuffer, &rxmsglen);
         VERBOSE (morep, INFO, PRINTF ("rx %d bytes (msgid %d) [%d]\n", rxmsglen, rxmsgtype, rxseqnum));
-        VERBOSE (morep, DEBUG, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
+        VERBOSE (morep, TRACE, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
 
         uint8_t txmsgtype = 0x01;
-       int txmsglen = strlen (words[i]);
+        int txmsglen = strlen (words[i]);
         int txseqnum = MOREP_Send (morep, txmsgtype, (uint8_t *)words[i], txmsglen);
         VERBOSE (morep, INFO, PRINTF ("tx %d bytes (msgid %d) [%d]\n", txmsglen, txmsgtype, txseqnum));
-        VERBOSE (morep, DEBUG, PRINTF ("tx msg: '%s'\n", words[i]));
+        VERBOSE (morep, TRACE, PRINTF ("tx msg: '%s'\n", words[i]));
         if (words[++i] == NULL) {
             i = 0;
         }
@@ -87,7 +89,7 @@ int server (char *url)
     return 0;
 }
 
-int client (char *url, uint8_t txmsgtype, char *filename)
+int clientline (char *url, uint8_t txmsgtype, char *filename)
 {
     VERBOSE (morep, TRACE, PRINTF ("client\n"));
 
@@ -109,8 +111,8 @@ int client (char *url, uint8_t txmsgtype, char *filename)
     }
 
     while (!feof (fid)) {
-        int txmsglen = 1500;
-        uint8_t txbuffer[1500] = {0};
+        int txmsglen = 1496;
+        uint8_t txbuffer[1496] = {0};
         int i = 0;
         do {
             fread (txbuffer + i, 1, 1, fid);
@@ -129,14 +131,14 @@ int client (char *url, uint8_t txmsgtype, char *filename)
 
         int txseqnum = MOREP_Send (morep, txmsgtype, txbuffer, txmsglen);
         VERBOSE (morep, INFO, PRINTF ("tx %d bytes (msgid %d) [%d]\n", txmsglen, txmsgtype, txseqnum));
-       VERBOSE (morep, DEBUG, PRINTF ("tx msg: '%s'\n", txbuffer));
+        VERBOSE (morep, TRACE, PRINTF ("tx msg: '%s'\n", txbuffer));
 
         uint8_t rxmsgtype = 0;
         int rxmsglen = 0;
-        uint8_t rxbuffer[1500] = {0};
+        uint8_t rxbuffer[1496] = {0};
         int rxseqnum = MOREP_Receive (morep, &rxmsgtype, rxbuffer, &rxmsglen);
         VERBOSE (morep, INFO, PRINTF ("rx %d bytes (msgid %d) [%d]\n", rxmsglen, rxmsgtype, rxseqnum));
-        VERBOSE (morep, DEBUG, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
+        VERBOSE (morep, TRACE, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
     }
 
     if (fid != stdin) {
@@ -147,11 +149,95 @@ int client (char *url, uint8_t txmsgtype, char *filename)
     return 0;
 }
 
+
+char *readstream (FILE *sd, int *length)
+{
+    VERBOSE (morep, TRACE, PRINTF ("readstream\n"));
+
+    char *buffer = NULL;
+    size_t size = 0;
+    int blocklen = 0;
+    int _length = 0;
+    do {
+        size += BUFMAX + (size == 0);
+        buffer = (char *) realloc (buffer, size);
+        memset (buffer + size - BUFMAX - 1, 0, BUFMAX + 1);
+        blocklen = fread (buffer + size - BUFMAX - 1, 1, BUFMAX, sd);
+        _length += blocklen;
+        VERBOSE (morep, DEBUG, PRINTF ("buffer: 0x%p 0x%p %d %d %d\n", buffer, buffer + size - BUFMAX - 1, (int)size, _length, blocklen));
+    } while (blocklen > 0);
+
+    /* check size */
+    VERBOSE (morep, DEBUG, PRINTF ("read length: %d\n", _length));
+    if (_length == 0) {
+        free (buffer);
+        buffer = NULL;
+    }
+    if (length) {
+        *length = _length;
+    }
+
+    return buffer;
+}
+
+int clientfile (char *url, uint8_t txmsgtype, char *filename)
+{
+    VERBOSE (morep, TRACE, PRINTF ("client\n"));
+
+    /* open connection */
+    int morep = MOREP_Connect (url);
+    if (morep < 0) {
+        VERBOSE (morep, ERROR, PRINTF ("can't connect on url '%s'\n", url));
+        return -1;
+    }
+
+    /* read file */
+    FILE *fid = stdin;
+    if (filename != NULL) {
+        fid = fopen (filename, "r");
+        if (fid == NULL) {
+            VERBOSE (morep, ERROR, PRINTF ("can't open file '%s' for reading\n", filename));
+            return -1;
+        }
+    }
+    int len = 0;
+    uint8_t *buffer = (uint8_t *) readstream (fid, &len);
+    if (fid != stdin) {
+        fclose (fid);
+    }
+
+    uint8_t *txbuffer = buffer;
+    while (len > 0) {
+        int txmsglen = 1496 * 16 - 1;
+        if (len < txmsglen) {
+            txmsglen = len;
+        }
+        int txseqnum = MOREP_Send (morep, txmsgtype, txbuffer, txmsglen);
+        VERBOSE (morep, INFO, PRINTF ("tx %d bytes (msgid %d) [%d]\n", txmsglen, txmsgtype, txseqnum));
+        VERBOSE (morep, TRACE, PRINTF ("tx msg: '"); fwrite ((char *)txbuffer, 1, txmsglen, stdout); printf ("'\n"));
+        len -= txmsglen;
+        txbuffer -= txmsglen;
+
+        uint8_t rxmsgtype = 0;
+        int rxmsglen = 0;
+        uint8_t rxbuffer[1496] = {0};
+        int rxseqnum = MOREP_Receive (morep, &rxmsgtype, rxbuffer, &rxmsglen);
+        VERBOSE (morep, INFO, PRINTF ("rx %d bytes (msgid %d) [%d]\n", rxmsglen, rxmsgtype, rxseqnum));
+        VERBOSE (morep, TRACE, PRINTF ("rx msg: '"); fwrite ((char *)rxbuffer, 1, rxmsglen, stdout); printf ("'\n"));
+    }
+
+    free (buffer);
+    MOREP_Close (morep);
+
+    return 0;
+}
+
 int main (int argc, char **argv)
 {
     char *filename = NULL;
     int msgtype = -1;
     char *url = NULL;
+    int linebyline = 1;
 
     /* get basename */
     char *pt = progname = argv[0];
@@ -171,6 +257,9 @@ int main (int argc, char **argv)
         }
         char c = arg[1];
         switch (c) {
+        case 'b':
+            linebyline = 0;
+            break;
         case 'f':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -179,6 +268,9 @@ int main (int argc, char **argv)
             }
             filename = arg;
             break;
+        case 'l':
+            linebyline = 1;
+            break;
         case 's':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -197,7 +289,7 @@ int main (int argc, char **argv)
             break;
         case 'h':
         default:
-            printf ("usage: %s [-h] [-f file] [-s msgtype] [-v int] <url>\n", progname);
+            printf ("usage: %s [-b|-l] [-h] [-f file] [-s msgtype] [-v int] <url>\n", progname);
             return (c != 'h');
         }
     }
@@ -206,7 +298,11 @@ int main (int argc, char **argv)
     if (url == NULL) {
         VERBOSE (morep, ERROR, PRINTF ("missing url\n"));
     } else if (msgtype != -1) {
-        rc = client (url, msgtype, filename);
+        if (linebyline) {
+            rc = clientline (url, msgtype, filename);
+        } else {
+            rc = clientfile (url, msgtype, filename);
+        }
     } else {
         rc = server (url);
     }
@@ -220,4 +316,4 @@ int main (int argc, char **argv)
 /* test: morep_server -h */
 /* test: morep_server -v 4 */
 
-/* vi:set tabstop=4 expandtab shiftwidth=4: this line set vi mode*/
+/* vim: set tabstop=4 expandtab shiftwidth=4: */
index 2965ef339cfb253c231c04d0b8b2bf5bb408146e..614e0959a7427147e3ee7d9eae09f72022c21f39 100644 (file)
--- a/verbose.h
+++ b/verbose.h
@@ -84,7 +84,8 @@ __BEGIN_DECLS
                 printf ("\e[1;34m");         \
         }                                    \
         statement;                           \
-        printf ("\e[0;0m");                  \
+        if (VERBOSE_COLOR)                   \
+            printf ("\e[0;0m");              \
     } while(0)
 
 /**