MOREP_Connect returns now a file descriptor
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Mon, 9 Jun 2025 16:34:54 +0000 (18:34 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Mon, 9 Jun 2025 16:34:54 +0000 (18:34 +0200)
morep.c
morep.h
morep_valid.c

diff --git a/morep.c b/morep.c
index 11be582468f8d452a12b0dc218347cb57f534f36..b9be600502e82593a4befa24bcc499797e19898e 100644 (file)
--- a/morep.c
+++ b/morep.c
@@ -70,7 +70,6 @@ typedef struct {
     uint8_t *tx_buffer;
     uint8_t *rx_buffer;
     uint16_t n_etype;
-    int used;
 } MOREP_descriptor_t ;
 
 /**
@@ -88,13 +87,13 @@ void alloc_all_moreps ()
 {
     VERBOSE (morep, TRACE, FPRINTF (stdout, "alloc_all_moreps\n"));
 
-    int i;
-    for (i = 0; i < MAX_MOREP_NUMBER; i++) {
+    for (int i = 0; i < MAX_MOREP_NUMBER; i++) {
         MOREP_list[i] = (MOREP_descriptor_t *) calloc (1, sizeof (MOREP_descriptor_t));
         MOREP_list[i]->tx_buffer = (uint8_t *) calloc (ETHER_PREAMBLE + MAX_ETHER_SIZE, 1);
         MOREP_list[i]->rx_buffer = (uint8_t *) calloc (ETHER_PREAMBLE + MAX_ETHER_SIZE, 1);
+        MOREP_list[i]->sock = -1;
     }
-    for (i = 0; i < MAX_FRAGMENTS; i++) {
+    for (int i = 0; i < MAX_FRAGMENTS; i++) {
         fragment_list[i] = (MOREP_fragment_t *) calloc (1, sizeof (MOREP_fragment_t));
         fragment_list[i]->buffer = (uint8_t *) calloc (ETHER_PREAMBLE + MAX_ETHER_SIZE, 1);
     }
@@ -104,18 +103,17 @@ void free_all_moreps (void)
 {
     VERBOSE (morep, TRACE, FPRINTF (stdout, "free_all_moreps\n"));
 
-    int i;
-    for (i = 0; i < MAX_MOREP_NUMBER; i++) {
+    for (int i = 0; i < MAX_MOREP_NUMBER; i++) {
         if (MOREP_list[i]) {
-            if (MOREP_list[i]->used) {
-                MOREP_Close (i);
+            if (MOREP_list[i]->sock >= 0) {
+                close (MOREP_list[i]->sock);
             }
             free (MOREP_list[i]->tx_buffer);
             free (MOREP_list[i]->rx_buffer);
         }
         free (MOREP_list[i]);
     }
-    for (i = 0; i < MAX_FRAGMENTS; i++) {
+    for (int i = 0; i < MAX_FRAGMENTS; i++) {
         if (fragment_list[i]) {
             free (fragment_list[i]->buffer);
         }
@@ -155,8 +153,8 @@ int parse_mac (MOREP_addr_t *addr, char *url)
     VERBOSE (morep, TRACE, PRINTF ("parse_mac\n"));
 
     /* MAC address format: XX:XX:XX:XX:XX:XX/... */
-    int i, j;
-    for (i = j = 0; i < 6; i++) {
+    int j = 0;
+    for (int i = 0; i < 6; i++) {
         if (url[j] == 0) {
             break;
         }
@@ -238,8 +236,7 @@ int get_mac_address (int sock, char *ifname, uint8_t *mac)
     }
 
     uint8_t *p = (unsigned char *)ifr.ifr_hwaddr.sa_data;
-    int i;
-    for (i = 0; i < 6; i++) {
+    for (int i = 0; i < 6; i++) {
         mac[i] = *p++;
     }
 
@@ -250,8 +247,7 @@ int get_mac_address (int sock, char *ifname, uint8_t *mac)
 
 void mactoa (char *str, uint8_t *mac)
 {
-    int i;
-    for (i = 0; i < 6; i++) {
+    for (int i = 0; i < 6; i++) {
         int d = mac[i] / 16;
         str[3 * i] = d + ((d > 9) ? 'A' - 10 : '0');
         d = mac[i] % 16;
@@ -261,26 +257,38 @@ void mactoa (char *str, uint8_t *mac)
     str[17] = '\0';
 }
 
-int find_unused_morep ()
+int find_morep_index (int sock)
+{
+    VERBOSE (morep, TRACE, PRINTF ("find_morep_index\n"));
+
+    if (sock < 0) {
+        return -1;
+    }
+    for (int i = 0; i < MAX_MOREP_NUMBER; i++) {
+        if (MOREP_list[i]->sock == sock) {
+            return i;
+        }
+    }
+
+    return -1;
+}
+
+int find_unused_morep (int sock)
 {
     VERBOSE (morep, TRACE, PRINTF ("find_unused_morep\n"));
 
-    int i;
-    for (i = 0; i < MAX_MOREP_NUMBER; i++) {
+    for (int i = 0; i < MAX_MOREP_NUMBER; i++) {
         // FIXIT: not thread safe
-        if (MOREP_list[i]->used == 0) {
-            MOREP_list[i]->used = 1;
-            break;
+        if (MOREP_list[i]->sock < 0) {
+            MOREP_list[i]->sock = sock;
+            VERBOSE (morep, DEBUG, PRINTF ("find morep: %d\n", i));
+            return i;
         }
     }
-    if (i == MAX_MOREP_NUMBER) {
-        VERBOSE (morep, WARNING, PRINTF ("can't find avaliable morep descriptor\n"));
-        return -1;
-    }
 
-    VERBOSE (morep, DEBUG, PRINTF ("find morep: %d\n", i));
+    VERBOSE (morep, WARNING, PRINTF ("can't find avaliable morep descriptor\n"));
 
-    return i;
+    return -1;
 }
 
 int MOREP_Connect (char *url)
@@ -334,22 +342,21 @@ int MOREP_Connect (char *url)
     /* info message */
     char lmac[MAC_ADDR_STRING] = {0};
     mactoa (lmac, sock_addr.sll_addr);
-    VERBOSE (morep, INFO, PRINTF ("I/O on %s local=[%s] etype=[%04x]\n", addr.ifname, lmac, addr.ether_type));
+    VERBOSE (morep, INFO, PRINTF ("I/O on %s local=[%s] etype=[%04x] sock=%d\n", addr.ifname, lmac, addr.ether_type, sock));
 
     /* store connection */
-    int index = find_unused_morep ();
+    int index = find_unused_morep (sock);
     if (index >= 0) {
-        MOREP_list[index]->sock = sock;
         memcpy (MOREP_list[index]->tx_buffer, (void *) addr.mac, 6);
         memcpy (MOREP_list[index]->tx_buffer + 6, sock_addr.sll_addr, 6);
         MOREP_list[index]->n_etype = htons (addr.ether_type);
         memcpy (MOREP_list[index]->tx_buffer + 12, (void *) &(MOREP_list[index]->n_etype), 2);
     }
 
-    return index;
+    return sock;
 }
 
-int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
+int MOREP_Send (int sock, uint8_t msgtype, uint8_t *buffer, int len)
 {
     VERBOSE (morep, TRACE, PRINTF ("MOREP_Send\n"));
 
@@ -369,9 +376,9 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
         VERBOSE (morep, ERROR, PRINTF ("too large buffer to send (%d)\n", len));
         return -1;
     }
-    if ((index < 0) || (index >= MAX_MOREP_NUMBER) ||
-        (MOREP_list[index] == NULL) || (!MOREP_list[index]->used)) {
-        VERBOSE (morep, ERROR, PRINTF ("incorrect MOREP descriptor (%d)\n", index));
+    int index = find_morep_index (sock);
+    if (index < 0) {
+        VERBOSE (morep, ERROR, PRINTF ("can't find MOREP for socket (%d)\n", sock));
         return -1;
     }
 
@@ -385,7 +392,7 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
         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 + ETHER_PREAMBLE + 2, (void *) &msglen, 2);
-        VERBOSE (morep, DEBUG, PRINTF ("sent preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->tx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':'));
+        VERBOSE (morep, DEBUG, PRINTF ("sent preamble: "); for (int i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->tx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':'));
 
         memcpy (morep->tx_buffer + ETHER_PREAMBLE + MOREP_PREAMBLE, buffer, pklen);
         int txlen = pklen + ETHER_PREAMBLE + MOREP_PREAMBLE;
@@ -394,7 +401,7 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
             memset (morep->tx_buffer + txlen, 0, ETHER_PREAMBLE + MIN_ETHER_SIZE - txlen);
             txlen = ETHER_PREAMBLE + MIN_ETHER_SIZE;
         }
-        rc = sendto (morep->sock, morep->tx_buffer, txlen, 0, NULL, 0);
+        rc = sendto (sock, morep->tx_buffer, txlen, 0, NULL, 0);
 
         len -= pklen;
         buffer += pklen;
@@ -404,19 +411,18 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len)
     return (rc > 0) ? seqnum : -1;
 }
 
-int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
+int MOREP_Receive (int sock, 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)) ||
-        (MOREP_list[index] == NULL) || (!MOREP_list[index]->used)) {
-        VERBOSE (morep, ERROR, PRINTF ("incorrect MOREP descriptor (%d)\n", index));
+    int index = find_morep_index (sock);
+    if (index < 0) {
+        VERBOSE (morep, ERROR, PRINTF ("can't find MOREP for socket (%d)\n", sock));
         return -1;
     }
     MOREP_descriptor_t *morep =  MOREP_list[index];
-    for (i = 0; i < MAX_FRAGMENTS; i++) {
+    for (int i = 0; i < MAX_FRAGMENTS; i++) {
         fragment_list[i]->len = 0;
         //fragment_list[i]->status = 0;
     }
@@ -427,8 +433,8 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
     int flag = 0;
     do {
         do {
-            rxlen = recvfrom (morep->sock, morep->rx_buffer, ETHER_PREAMBLE + MAX_ETHER_SIZE, 0, NULL, 0);
-            VERBOSE (morep, DEBUG, PRINTF ("rec. preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->rx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':'));
+            rxlen = recvfrom (sock, morep->rx_buffer, ETHER_PREAMBLE + MAX_ETHER_SIZE, 0, NULL, 0);
+            VERBOSE (morep, DEBUG, PRINTF ("rec. preamble: "); for (int i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->rx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':'));
 
             /* avoid alien packets */
             uint16_t rx_n_etype;
@@ -468,7 +474,7 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
         /* check padding */
         if (pkglen < MIN_ETHER_SIZE - MOREP_PREAMBLE) {
             VERBOSE (morep, DEBUG, PRINTF ("padding of %d bytes\n", MIN_ETHER_SIZE - MOREP_PREAMBLE - pkglen));
-            VERBOSE (morep, DEBUG, int _i; for (_i = pkglen + ETHER_PREAMBLE + MOREP_PREAMBLE; _i < ETHER_PREAMBLE + MIN_ETHER_SIZE; _i++) if (morep->rx_buffer[_i] != 0) { VERBOSE (morep, WARNING, PRINTF ("incorrect padding (%d)\n", _i)); break; });
+            VERBOSE (morep, DEBUG, for (int _i = pkglen + ETHER_PREAMBLE + MOREP_PREAMBLE; _i < ETHER_PREAMBLE + MIN_ETHER_SIZE; _i++) if (morep->rx_buffer[_i] != 0) { VERBOSE (morep, WARNING, PRINTF ("incorrect padding (%d)\n", _i)); break; });
         }
         if (((rxlen == ETHER_PREAMBLE + MIN_ETHER_SIZE) && (pkglen > MIN_ETHER_SIZE - MOREP_PREAMBLE)) || ((rxlen > ETHER_PREAMBLE + MIN_ETHER_SIZE) && (pkglen != rxlen - (ETHER_PREAMBLE + MOREP_PREAMBLE)))) {
             VERBOSE (morep, WARNING, PRINTF ("incorrect size (%d/%d)\n", pkglen, rxlen - (ETHER_PREAMBLE + MOREP_PREAMBLE)));
@@ -486,7 +492,7 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
             // frag->status = 1;
         } else {
             rxlen = 0;
-            for (i = 0; (i < MAX_FRAGMENTS) && (fragment_list[i]->len != 0); i++) {
+            for (int i = 0; (i < MAX_FRAGMENTS) && (fragment_list[i]->len != 0); i++) {
                 MOREP_fragment_t *frag = fragment_list[i];
                 memcpy (buffer + rxlen, frag->buffer + ETHER_PREAMBLE + MOREP_PREAMBLE, frag->len);
                 rxlen += frag->len;
@@ -508,49 +514,46 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
     return rxseqnum;
 }
 
-int MOREP_Close (int index)
+int MOREP_Close (int sock)
 {
     VERBOSE (morep, TRACE, PRINTF ("MOREP_Close\n"));
 
-    if ((index < 0) || (index >= MAX_MOREP_NUMBER) ||
-        (MOREP_list[index] == NULL)) {
-        VERBOSE (morep, ERROR, PRINTF ("incorrect MOREP descriptor (%d)\n", index));
+    int index = find_morep_index (sock);
+    if (index < 0) {
+        VERBOSE (morep, ERROR, PRINTF ("can't find MOREP for socket (%d)\n", sock));
         return -1;
     }
 
-    if (MOREP_list[index]->used) {
-        close (MOREP_list[index]->sock);
-        MOREP_list[index]->used = 0;
-    }
+    close (sock);
+    MOREP_list[index]->sock = -1;
 
     return 0;
 }
 
-int MOREP_Receive_timeout (int index, uint8_t *msgtype, uint8_t *buffer, int *len, int ms)
+int MOREP_Receive_timeout (int sock, uint8_t *msgtype, uint8_t *buffer, int *len, int ms)
 {
 
     VERBOSE (morep, TRACE, PRINTF ("MOREP_Receive_timeout\n"));
 
-    if ((index < 0) || (index >= MAX_MOREP_NUMBER) ||
-        (MOREP_list[index] == NULL) || (!MOREP_list[index]->used)) {
-        VERBOSE (morep, ERROR, PRINTF ("incorrect MOREP descriptor (%d)\n", index));
+    int index = find_morep_index (sock);
+    if (index < 0) {
+        VERBOSE (morep, ERROR, PRINTF ("can't find MOREP for socket (%d)\n", sock));
         return -1;
     }
-    VERBOSE (morep, DEBUG, PRINTF ("working with MOREP descriptor %d (%d)\n", index, MOREP_list[index]->sock));
-    int fid = MOREP_list[index]->sock;
+    VERBOSE (morep, DEBUG, PRINTF ("working with MOREP %d (%d)\n", index, sock));
 
     fd_set rfds;
     struct timeval tv = { 0, ms * 1000};
     FD_ZERO (&rfds);
-    FD_SET (fid, &rfds);
+    FD_SET (sock, &rfds);
 
-    int retval = select (fid + 1, &rfds, NULL, NULL, &tv);
+    int retval = select (sock + 1, &rfds, NULL, NULL, &tv);
     VERBOSE (morep, TRACE, PRINTF ("select: %d (%ld, %ld)\n", retval, tv.tv_sec, tv.tv_usec));
     if (retval != 1) {
         return retval;
     }
 
-    return MOREP_Receive (index, msgtype, buffer, len);
+    return MOREP_Receive (sock, msgtype, buffer, len);
 }
 
 /* vim: set ts=4 sw=4 si et: */
diff --git a/morep.h b/morep.h
index 0b883f83642f9461d93e72f62b116d33485590ef..79d54fe0acf9acdf22588903f36ac896026e0430 100644 (file)
--- a/morep.h
+++ b/morep.h
@@ -26,6 +26,9 @@ __BEGIN_DECLS
    The common message passing library offers unreliable connection oriented
    data blocks exchange messaging between software modules over raw Ethernet
    packets.
+
+   morep values returned by MOREP_Connect are standard Linux file descriptors
+   that can be used in select()/poll() system calls
 */
 
 /**
@@ -39,7 +42,7 @@ __BEGIN_DECLS
    - macaddress format is xx:xx:xx:xx:xx:xx
    - ethertype is a number from 1536 to 65355
 
-   @return a new MOREP descriptor associated with this connection
+   @return a new file descriptor associated with this connection
 
    @see MOREP_Close()
    @see MOREP_Send()
@@ -52,11 +55,11 @@ int MOREP_Connect (char *url);
 
    Close a connection and relase its descriptor.
 
-   @param index descriptor retuned by MOREP_Connect()
+   @param sock file descriptor retuned by MOREP_Connect()
 
    @see MOREP_Connect()
 */
-int MOREP_Close (int index);
+int MOREP_Close (int sock);
 
 /**
    @ingroup MOREP
@@ -64,7 +67,7 @@ int MOREP_Close (int index);
    Send a message over an existing MOREP connection. Will block until
    resolution in case of congestion.
 
-   @param index MOREP descriptor retuned by MOREP_Connect()
+   @param sock file descriptor retuned by MOREP_Connect()
    @param msgtype a user defined message type identifier, transported
    transparently
    @param buffer a pointer to the message to be transmitted
@@ -74,14 +77,14 @@ int MOREP_Close (int index);
 
    @see MOREP_Connect()
 */
-int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len);
+int MOREP_Send (int sock, uint8_t msgtype, uint8_t *buffer, int len);
 
 /**
    @ingroup MOREP
 
    Received a message over an existing MOREP connection.
 
-   @param index MOREP descriptor retuned by MOREP_Connect()
+   @param sock file descriptor retuned by MOREP_Connect()
    @param msgtype a pointer to retrieve the user defined message type
    identifier, transported transparently
    @param buffer a pointer to the reception buffer (pre-allocated)
@@ -94,14 +97,14 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len);
    @see MOREP_Connect()
 */
 
-int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len);
+int MOREP_Receive (int sock, uint8_t *msgtype, uint8_t *buffer, int *len);
 
 /**
    @ingroup MOREP
 
    Received a message over an existing MOREP connection with a timeout.
 
-   @param index MOREP descriptor retuned by MOREP_Connect()
+   @param sock file descriptor retuned by MOREP_Connect()
    @param msgtype a pointer to retrieve the user defined message type
    identifier, transported transparently
    @param buffer a pointer to the reception buffer (pre-allocated)
@@ -112,10 +115,10 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len);
 
    @return sequence number of the received message
 
-   @see MOREP_Connect()
+   @see MOREP_Connect(), MOREP_Receive()
 */
 
-int MOREP_Receive_timeout (int index, uint8_t *msgtype, uint8_t *buffer, int *len, int ms);
+int MOREP_Receive_timeout (int sock, uint8_t *msgtype, uint8_t *buffer, int *len, int ms);
 
 __END_DECLS
 
index 167061a7e6056e16988f3ca8310398dc7c85d916..95ea42ec2781a310dec8215f512f4777a2d7feec 100644 (file)
@@ -261,8 +261,7 @@ int main (int argc, char **argv)
         /* find ethertype */
         comm_t *comm = NULL;
         int offset = 1;
-        int i;
-        for (i = 0; i < nbcomms; i++) {
+        for (int i = 0; i < nbcomms; i++) {
             comm_t *c = comm_list + i;
             VERBOSE (morep, TRACE, PRINTF ("test %c[%s]\n", c->mode ? 'T' : 'R', c->etype));
             if ((strncmp (line + offset, c->etype, strlen (c->etype)) == 0) && (c->mode == mode)) {
@@ -337,7 +336,8 @@ int main (int argc, char **argv)
     /* cleaning */
     free (script);
     while (nbcomms) {
-        MOREP_Close (--nbcomms);
+        comm_t *c = comm_list + --nbcomms;
+        MOREP_Close (c->morep);
     }
     if (log) {
         fclose (log);