Skip to content

Commit

Permalink
Minor binary interface improvements
Browse files Browse the repository at this point in the history
- better debugging on receive operations
- improve readability with a couple of macros
  • Loading branch information
liviuchircu committed Oct 15, 2013
1 parent 3a334c3 commit 0dc6524
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
27 changes: 12 additions & 15 deletions bin_interface.c
Expand Up @@ -39,7 +39,6 @@ static char *cpos;

static char rcv_buf[BUF_SIZE];
static char *rcv_end;
static int rcv_size;

static struct packet_cb_list *reg_modules;

Expand Down Expand Up @@ -262,16 +261,17 @@ int bin_send(union sockaddr_union *dest)
return 0;

st.s = send_buffer + HEADER_SIZE;
st.len = (cpos - send_buffer) - HEADER_SIZE;
st.len = bin_send_size - HEADER_SIZE;

/* compute a checksum of the binary packet content */
crc32_uint(&st, (unsigned int *)(send_buffer + BIN_PACKET_MARKER_SIZE));

LM_DBG("sending packet: %.*s [%d B] from socket %d\n",
(int)(cpos - send_buffer), send_buffer, (int)(cpos - send_buffer),
bin->socket);
LM_DBG("sending packet {'%.*s', %d}: %.*s [%d B] from socket %d\n",
*(int *)(send_buffer + HEADER_SIZE), send_buffer + HEADER_SIZE +
LEN_FIELD_SIZE, bin_send_type, bin_send_size, send_buffer, bin_send_size,
bin->socket);

rc = udp_send(bin, send_buffer, cpos - send_buffer, dest);
rc = udp_send(bin, send_buffer, bin_send_size, dest);
if (rc == -1)
LM_ERR("binary packet UDP send failed!\n");

Expand Down Expand Up @@ -362,6 +362,8 @@ static void bin_receive_loop(void)
goto exit;
}

rcv_end = rcv_buf + rcv_bytes;

#ifndef NO_ZERO_CHECKS
if (rcv_bytes < MIN_UDP_PACKET) {
LM_DBG("probing packet received len = %d\n", rcv_bytes);
Expand All @@ -381,22 +383,17 @@ static void bin_receive_loop(void)
}

get_name(rcv_buf, name);
cpos = name.s + name.len + CMD_FIELD_SIZE;

/* packet will be now processed by a specific module */
for (p = reg_modules; p; p = p->next) {
if (p->module.len == name.len &&
memcmp(name.s, p->module.s, name.len) == 0) {

cpos = name.s + name.len;
rcv_size = rcv_bytes - HEADER_SIZE - LEN_FIELD_SIZE
- name.len - CMD_FIELD_SIZE;
rcv_end = rcv_buf + rcv_bytes;
memcmp(name.s, p->module.s, name.len) == 0) {

cpos += CMD_FIELD_SIZE;
LM_DBG("binary Packet CMD: %d. Module: %.*s\n",
*(int *)(cpos - CMD_FIELD_SIZE), name.len, name.s);
bin_rcv_type, name.len, name.s);

p->cbf(*(int *)(cpos - CMD_FIELD_SIZE));
p->cbf(bin_rcv_type);

break;
}
Expand Down
11 changes: 11 additions & 0 deletions bin_interface.h
Expand Up @@ -35,6 +35,17 @@
#define CMD_FIELD_SIZE sizeof(int)
#define HEADER_SIZE (BIN_PACKET_MARKER_SIZE + CRC_FIELD_SIZE)

#define bin_send_type \
(*(int *)(send_buffer + HEADER_SIZE + LEN_FIELD_SIZE + \
*(int *)(send_buffer + HEADER_SIZE)))

#define bin_rcv_type \
(*(int *)(rcv_buf + HEADER_SIZE + LEN_FIELD_SIZE + \
*(int *)(rcv_buf + HEADER_SIZE)))

#define bin_send_size ((int)(cpos - send_buffer))
#define bin_rcv_size ((int)(rcv_end - rcv_buf))

#define is_valid_bin_packet(_p) \
(memcmp(_p, BIN_PACKET_MARKER, BIN_PACKET_MARKER_SIZE) == 0)

Expand Down

0 comments on commit 0dc6524

Please sign in to comment.