udp_initialized = FCE_FALSE;
}
-
/*
* Construct a UDP packet for our listeners and return packet size
* */
static ssize_t build_fce_packet( struct fce_packet *packet, char *path, int mode, uint32_t event_id )
{
- size_t pathlen;
+ size_t pathlen = 0;
ssize_t data_len = 0;
+ uint64_t *t;
- strncpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
+ /* Set content of packet */
+ memcpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
packet->version = FCE_PACKET_VERSION;
packet->mode = mode;
- packet->event_id = event_id;
-
- pathlen = strlen(path) + 1; /* include string terminator */
+
+ packet->event_id = event_id;
+ pathlen = strlen(path); /* exclude string terminator */
+
/* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */
if (pathlen >= MAXPATHLEN)
pathlen = MAXPATHLEN - 1;
- /* This is the payload len. Means: the stream has len bytes more until packet is finished */
- /* A server should read the first 16 byte, decode them and then fetch the rest */
- data_len = FCE_PACKET_HEADER_SIZE + pathlen;
packet->datalen = pathlen;
+ /* This is the payload len. Means: the packet has len bytes more until packet is finished */
+ data_len = FCE_PACKET_HEADER_SIZE + pathlen;
+
switch (mode) {
case FCE_TM_SIZE:
- tm_used = hton64(tm_used);
- memcpy(packet->data, &tm_used, sizeof(tm_used));
- strncpy(packet->data + sizeof(tm_used), path, pathlen);
-
- packet->datalen += sizeof(tm_used);
+ t = (uint64_t *)packet->data;
+ *t = hton64(tm_used);
+ memcpy(packet->data + sizeof(tm_used), path, pathlen);
+
+ packet->datalen = pathlen + sizeof(tm_used);
data_len += sizeof(tm_used);
break;
default:
- strncpy(packet->data, path, pathlen);
+ memcpy(packet->data, path, pathlen);
break;
}
return data_len;
}
-static int pack_fce_packet(struct fce_packet *packet, unsigned char *buf)
+/*
+ * Handle Endianess and write into buffer w/o padding
+ **/
+static void pack_fce_packet(struct fce_packet *packet, unsigned char *buf, int maxlen)
{
unsigned char *p = buf;
*p = packet->mode;
p++;
- uint32_t id = htonl(packet->event_id);
- memcpy(p, &id, sizeof(id));
+ uint32_t *id = (uint32_t*)p;
+ *id = htonl(packet->event_id);
p += sizeof(packet->event_id);
- uint16_t l = htons(packet->datalen);
- memcpy(p, &l, sizeof(l));
- p += sizeof(l);
+ uint16_t *l = ( uint16_t *)p;
+ *l = htons(packet->datalen);
+ p += sizeof(packet->datalen);
- memcpy(p, &packet->data[0], packet->datalen);
- p += packet->datalen;
-
- return 0;
+ if (((p - buf) + packet->datalen) < maxlen) {
+ memcpy(p, &packet->data[0], packet->datalen);
+ }
}
/*
/* build our data packet */
ssize_t data_len = build_fce_packet( &packet, path, mode, ++event_id );
- pack_fce_packet(&packet, iobuf);
+ pack_fce_packet(&packet, iobuf, MAXIOBUF);
for (int i = 0; i < udp_sockets; i++)
{
/* Okay, we have a running socket again, send server that we had a problem on our side*/
data_len = build_fce_packet( &packet, "", FCE_CONN_BROKEN, 0 );
- pack_fce_packet(&packet, iobuf);
+ pack_fce_packet(&packet, iobuf, MAXIOBUF);
sendto(udp_entry->sock,
iobuf,
/* Rebuild our original data packet */
data_len = build_fce_packet( &packet, path, mode, event_id );
- pack_fce_packet(&packet, iobuf);
+ pack_fce_packet(&packet, iobuf, MAXIOBUF);
}
sent_data = sendto(udp_entry->sock,