diff --git a/core-test/app/communication-client.c b/core-test/app/communication-client.c index 1bb10fe..dde96c9 100644 --- a/core-test/app/communication-client.c +++ b/core-test/app/communication-client.c @@ -35,6 +35,7 @@ int main (int argc, char *argv[], char *env[]) return EXIT_FAILURE; } + printf ("msg to send: %s\n", MSG); if (app_write (&srv, MSG, strlen(MSG)) < 0) { handle_err("main", "app_write < 0"); return EXIT_FAILURE; diff --git a/core/communication.c b/core/communication.c index d66cb16..95ff7b5 100644 --- a/core/communication.c +++ b/core/communication.c @@ -1,6 +1,7 @@ #include "communication.h" #include "usocket.h" #include "msg-format.h" +#include "utils.h" #include #include @@ -46,11 +47,15 @@ int srv_init (int argc, char **argv, char **env int srv_accept (struct service *srv, struct process *p) { usock_accept (srv->service_fd, &p->proc_fd); + char *buf; + size_t msgsize = BUFSIZ; - char buf[3]; - msg_format_ack (buf); + srv_read (p, &buf, &msgsize); - srv_write (p, buf, 3); + msgsize = 0; + msg_format_ack (buf, NULL, &msgsize); + + srv_write (p, buf, msgsize); return 0; } @@ -106,12 +111,12 @@ int app_connection (int argc, char **argv, char **env app_write (srv, send_buffer, msize); char *buffer; - size_t read_msg_size; + size_t read_msg_size = BUFSIZ; app_read (srv, &buffer, &read_msg_size); - assert (read_msg_size == 3); assert (buffer[0] == MSG_TYPE_ACK); + assert (read_msg_size == 3); return 0; } diff --git a/core/msg-format.c b/core/msg-format.c index 26eae8d..2e55ba6 100644 --- a/core/msg-format.c +++ b/core/msg-format.c @@ -22,17 +22,18 @@ int msg_format_con (char *buf, const char *constr, size_t *msgsize) short final_size = (short) *msgsize; memcpy (buf + 1, &final_size, 2); memcpy (buf + 3, constr, *msgsize); - + *msgsize += 3; return 0; } // [type] [len] // 4 0 -int msg_format_ack (char *buf) +int msg_format_ack (char *buf, const char *constr, size_t *msgsize) { assert (buf != NULL); memset(buf, 0, 3); buf[0] = MSG_TYPE_ACK; + *msgsize += 3; return 0; } diff --git a/core/msg-format.h b/core/msg-format.h index a815db5..26424bf 100644 --- a/core/msg-format.h +++ b/core/msg-format.h @@ -8,6 +8,6 @@ #define MSG_TYPE_ACK 4 int msg_format_con (char *buf, const char *constr, size_t *msgsize); -int msg_format_ack (char *buf); +int msg_format_ack (char *buf, const char *constr, size_t *msgsize); #endif diff --git a/core/usocket.c b/core/usocket.c index 4c42e35..b28dca2 100644 --- a/core/usocket.c +++ b/core/usocket.c @@ -1,4 +1,5 @@ #include "usocket.h" +#include "utils.h" #include #include #include @@ -10,13 +11,15 @@ int usock_send (const int fd, const char *buf, const int msize) { + print_hexa ("msg send", (unsigned char *)buf, msize); + printf ("msg to send: (%d) \n", msize); + fflush(stdout); + int ret = 0; //printf ("%ld bytes to write\n", msize); ret = send (fd, buf, msize, 0); - if (ret <= 0) { - fprintf (stderr, "usock_send: file %s line %d send ret <= 0\n" - , __FILE__, __LINE__); - } + if (ret <= 0) + handle_err ("usock_send", "send ret <= 0"); return ret; } @@ -37,6 +40,8 @@ int usock_recv (const int fd, char **buf, size_t *msize) if (*buf == NULL) { // do not allocate too much memory + if (*msize > BUFSIZ) + handle_err ("usock_recv", "msize > BUFSIZ"); *buf = malloc ((*msize < BUFSIZ) ? *msize : BUFSIZ); } @@ -48,6 +53,7 @@ int usock_recv (const int fd, char **buf, size_t *msize) return ret; } *msize = (size_t) ret; + print_hexa ("msg recv", (unsigned char *)*buf, *msize); return ret; } diff --git a/core/utils.c b/core/utils.c new file mode 100644 index 0000000..2c0bcb0 --- /dev/null +++ b/core/utils.c @@ -0,0 +1,22 @@ +#include "utils.h" + +void print_hexa (const char *prefix, unsigned char *val, size_t size) +{ + if (! val) + return ; + + size_t i; + for(i = 0; i < size; i++) + { + if(! (i % 4)) + printf("\n%s (%ld) ", prefix, size); + printf("%2x ", val[i]); + } + printf("\n"); +} + + +void mprint_hexa (char *prefix, unsigned char *buf, size_t length) +{ + print_hexa (prefix, buf, length); +} diff --git a/core/utils.h b/core/utils.h new file mode 100644 index 0000000..a55cccd --- /dev/null +++ b/core/utils.h @@ -0,0 +1,11 @@ +#ifndef __UTIL_H__ +#define __UTIL_H__ + +#include +#include +#include + +void print_hexa (const char *prefix, unsigned char *val, size_t size); +void mprint_hexa (char *prefix, unsigned char *buf, size_t length); + +#endif