#define MAX_MOREP_NUMBER 256
#define ETHER_PREAMBLE 14
+#define MIN_ETHER_SIZE 48
#define MAX_ETHER_SIZE 1500 /* 9000 for jumbo frame */
#define MOREP_PREAMBLE 4
#define MAX_FRAGMENTS 15
int i;
for (i = 0; i < MAX_MOREP_NUMBER; i++) {
- MOREP_list[i] = (MOREP_descriptor_t *) calloc (sizeof (MOREP_descriptor_t), 1);
- MOREP_list[i]->tx_buffer = (uint8_t *) calloc (1, ETHER_PREAMBLE + MAX_ETHER_SIZE);
- MOREP_list[i]->rx_buffer = (uint8_t *) calloc (1, ETHER_PREAMBLE + MAX_ETHER_SIZE);
+ 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);
}
for (i = 0; i < MAX_FRAGMENTS; i++) {
- fragment_list[i] = (MOREP_fragment_t *) calloc (sizeof (MOREP_fragment_t), 1);
- fragment_list[i]->buffer = (uint8_t *) calloc (1, ETHER_PREAMBLE + MAX_ETHER_SIZE);
+ 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);
}
}
}
free (fragment_list[i]);
}
+
+ VERBOSE (morep, INFO, FPRINTF (stdout, "memory cleaned\n"));
}
void __attribute__((constructor)) _init_morep_ (void)
}
}
- if (j != ETHER_PREAMBLE + MOREP_PREAMBLE) {
+ if (j != MAC_ADDR_STRING) {
VERBOSE (morep, WARNING, PRINTF ("can't parse mac address '%s'(%d) \n", url, j));
return -1;
}
* padding l<46 : zeros
*/
- if ((len < 0) || ((len + MAX_ETHER_SIZE - 5) / (MAX_ETHER_SIZE - 4) > 16) ||
+ if ((len < 0) ||
+ ((len + MAX_ETHER_SIZE - MOREP_PREAMBLE - 1) / (MAX_ETHER_SIZE - MOREP_PREAMBLE) > MAX_FRAGMENTS) ||
(index < 0) || (index >= MAX_MOREP_NUMBER) || (MOREP_list[index] == NULL)) {
VERBOSE (morep, ERROR, PRINTF ("incorrect MOREP descriptor (%d)\n", index));
return -1;
}
MOREP_descriptor_t *morep = MOREP_list[index];
- morep->tx_buffer[14] = msgtype;
+ morep->tx_buffer[ETHER_PREAMBLE + 0] = msgtype;
int nfrag = 0;
- int seqnum = morep->tx_buffer[15] = morep->seqnum++;
+ int seqnum = morep->tx_buffer[ETHER_PREAMBLE + 1] = morep->seqnum++;
int rc = 0;
do {
- int pklen = (len > MAX_ETHER_SIZE - 4) ? MAX_ETHER_SIZE - 4 : len;
+ int pklen = (len > MAX_ETHER_SIZE - MOREP_PREAMBLE) ? MAX_ETHER_SIZE - MOREP_PREAMBLE : 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 + 16, (void *) &msglen, 2);
+ 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:", morep->tx_buffer[i]); printf ("\n"));
memcpy (morep->tx_buffer + ETHER_PREAMBLE + MOREP_PREAMBLE, buffer, pklen);
int txlen = pklen + ETHER_PREAMBLE + MOREP_PREAMBLE;
- if (txlen < 64) {
- VERBOSE (morep, DEBUG, PRINTF ("need padding: %d bytes\n", 64 - txlen));
- memset (morep->tx_buffer + txlen, 0, 64 - txlen);
- txlen = 64;
+ if (txlen < ETHER_PREAMBLE + MIN_ETHER_SIZE) {
+ VERBOSE (morep, DEBUG, PRINTF ("need padding: %d bytes\n", ETHER_PREAMBLE + MIN_ETHER_SIZE - txlen));
+ 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);
len -= pklen;
- buffer -= pklen;
+ buffer += pklen;
nfrag++;
} while ((rc > 0) && (len > 0));
int rxlen = 0;
int flag = 0;
do {
- rxlen = recvfrom (morep->sock, morep->rx_buffer, sizeof (morep->rx_buffer), 0, NULL, 0);
+ 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:", morep->rx_buffer[i]); printf ("\n"));
/* sequence number */
- int pkgseqnum = morep->rx_buffer[15];
+ int pkgseqnum = morep->rx_buffer[ETHER_PREAMBLE + 1];
if ((rxseqnum != -1) && (rxseqnum != pkgseqnum)) {
VERBOSE (morep, WARNING, PRINTF ("incoherent seqnum: %d != %d\n", rxseqnum, pkgseqnum));
}
rxseqnum = pkgseqnum;
/* msessage type */
- int pkgmsgtype = morep->rx_buffer[14];
+ int pkgmsgtype = morep->rx_buffer[ETHER_PREAMBLE + 0];
if ((rxmsgtype != -1) && (rxmsgtype != pkgmsgtype)) {
VERBOSE (morep, WARNING, PRINTF ("incoherent msgtype: %d != %d\n", rxmsgtype, pkgmsgtype));
}
/* flag, fragment number and payload length */
uint16_t pkgflagfraglen = 0;
- memcpy ((void *) &pkgflagfraglen, morep->rx_buffer + 16, 2);
+ memcpy ((void *) &pkgflagfraglen, morep->rx_buffer + ETHER_PREAMBLE + 2, 2);
pkgflagfraglen = ntohs (pkgflagfraglen);
flag = (pkgflagfraglen >> 15) & 0x0001;
int pkgfrag = (pkgflagfraglen >> 11) & 0x000f;
VERBOSE (morep, DEBUG, PRINTF ("flag/frag/length: %d/%d/%d\n", flag, pkgfrag, pkglen));
/* check padding */
- if (pkglen < 64 - ETHER_PREAMBLE + MOREP_PREAMBLE) {
- VERBOSE (morep, DEBUG, PRINTF ("padding of %d bytes\n", 64 - ETHER_PREAMBLE + MOREP_PREAMBLE - pkglen));
- VERBOSE (morep, DEBUG, int _i; for (_i = pkglen + ETHER_PREAMBLE + MOREP_PREAMBLE; _i < 64; _i++) if (morep->rx_buffer[_i] != 0) { VERBOSE (morep, WARNING, PRINTF ("incorrect padding (%d)\n", _i)); break; });
+ 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; });
}
- if (((rxlen == 64) && (pkglen > 64 - ETHER_PREAMBLE + MOREP_PREAMBLE)) || ((rxlen > 64) && (pkglen != rxlen - ETHER_PREAMBLE + MOREP_PREAMBLE))) {
- VERBOSE (morep, WARNING, PRINTF ("incorrect size (%d/%d)\n", pkglen, rxlen - ETHER_PREAMBLE + MOREP_PREAMBLE));
- pkglen = rxlen - ETHER_PREAMBLE + MOREP_PREAMBLE;
+ 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)));
+ pkglen = rxlen - (ETHER_PREAMBLE + MOREP_PREAMBLE);
}
/* store fragment */
}
close (MOREP_list[index]->sock);
- free (MOREP_list[index]);
- MOREP_list[index] = NULL;
+ MOREP_list[index]->used = 0;
return 0;
}