38 const unsigned char* src = (
const unsigned char*)&value;
39 unsigned char* dst = (
unsigned char*)&swaped;
61#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
76#if (BYTEORDER_ENDIAN == BYTEORDER_LITTLE_ENDIAN)
91 n_log(
LOG_ERR,
"create_msg destination is valid and should be NULL !");
116 if ((*msg)->tabint) {
119 if ((*msg)->tabflt) {
122 if ((*msg)->tabstr) {
156 double* new_val = NULL;
160 Malloc(new_val,
double, 1);
294 n_log(
LOG_ERR,
"Previous pointer value %p overriden by pointer %p", (*value), val);
316 n_log(
LOG_ERR,
"Previous pointer value %p overriden by pointer %p", (*value), val);
319 (*value) = val->
data;
332 size_t str_length = 0;
335 N_STR *strptr = NULL,
336 *generated_str = NULL;
338 char* charptr = NULL;
351 str_length +=
sizeof(int32_t) +
sizeof(int32_t) + strptr->
written;
358 Malloc(generated_str->data,
char, str_length + 1);
359 __n_assert(generated_str->data,
Free(generated_str);
return NULL);
361 generated_str->length = str_length + 1;
362 generated_str->written = str_length;
365 charptr = generated_str->data;
369 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in int list of message %p", msg);
374 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in float list of message %p", msg);
379 n_log(
LOG_ERR,
"too much items (>=UINT32_MAX) in string list of message %p", msg);
390 memcpy(charptr, &nb_int_items,
sizeof(uint32_t));
391 charptr +=
sizeof(uint32_t);
393 memcpy(charptr, &nb_float_items,
sizeof(uint32_t));
394 charptr +=
sizeof(uint32_t);
396 memcpy(charptr, &nb_str_items,
sizeof(uint32_t));
397 charptr +=
sizeof(uint32_t);
402 uint32_t* nbptr = (uint32_t*)node->
ptr;
403 uint32_t val = htonl(nbptr[0]);
404 memcpy(charptr, &val,
sizeof(uint32_t));
405 charptr +=
sizeof(uint32_t);
412 const double* nbptr = (
const double*)node->
ptr;
413 double val =
htond(nbptr[0]);
414 memcpy(charptr, &val,
sizeof(
double));
415 charptr +=
sizeof(double);
424 if (strptr->
length >= UINT32_MAX) {
425 n_log(
LOG_ERR,
"string too long (>=UINT32_MAX) in string list of message %p", msg);
429 uint32_t var = htonl((uint32_t)strptr->
length);
430 memcpy(charptr, &var,
sizeof(uint32_t));
431 charptr +=
sizeof(uint32_t);
433 if (strptr->
written >= UINT32_MAX) {
434 n_log(
LOG_ERR,
"string too long (>=UINT32_MAX) in string list of message %p", msg);
438 var = htonl((uint32_t)strptr->
written);
439 memcpy(charptr, &var,
sizeof(uint32_t));
440 charptr +=
sizeof(uint32_t);
448 return generated_str;
458 N_STR* tmpstr = NULL;
460 char* charptr = NULL;
475 if (str->
written < 3 *
sizeof(uint32_t)) {
476 n_log(
LOG_ERR,
"message too short: %zu bytes, need at least %zu", str->
written, 3 *
sizeof(uint32_t));
484 memcpy(&nb_int, charptr,
sizeof(uint32_t));
485 charptr +=
sizeof(uint32_t);
486 nb_int = ntohl(nb_int);
488 memcpy(&nb_flt, charptr,
sizeof(uint32_t));
489 charptr +=
sizeof(uint32_t);
490 nb_flt = ntohl(nb_flt);
492 memcpy(&nb_str, charptr,
sizeof(uint32_t));
493 charptr +=
sizeof(uint32_t);
494 nb_str = ntohl(nb_str);
498 size_t remaining = str->
written - 3 *
sizeof(uint32_t);
500 if (nb_int > remaining /
sizeof(uint32_t)) {
501 n_log(
LOG_ERR,
"message claims %u ints but only %zu bytes remain", nb_int, remaining);
505 remaining -= (size_t)nb_int *
sizeof(uint32_t);
507 if (nb_flt > remaining /
sizeof(
double)) {
508 n_log(
LOG_ERR,
"message claims %u floats but only %zu bytes remain", nb_flt, remaining);
512 remaining -= (size_t)nb_flt *
sizeof(
double);
515 for (it = 0; it < nb_int; it++) {
516 memcpy(&tmpnb, charptr,
sizeof(uint32_t));
517 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
518 charptr +=
sizeof(uint32_t);
520 n_log(
LOG_ERR,
"failed to add int %u/%u to message", it, nb_int);
528 for (it = 0; it < nb_flt; it++) {
529 memcpy(&tmpflt, charptr,
sizeof(
double));
530 tmpflt =
ntohd(tmpflt);
531 charptr +=
sizeof(double);
533 n_log(
LOG_ERR,
"failed to add float %u/%u to message", it, nb_flt);
542 if ((
size_t)nb_str > remaining / (2 *
sizeof(uint32_t))) {
543 n_log(
LOG_ERR,
"message claims %u strings but only %zu bytes remain (need at least %zu for headers)",
544 nb_str, remaining, (
size_t)nb_str * 2 *
sizeof(uint32_t));
549 for (it = 0; it < nb_str; it++) {
551 if (remaining < 2 *
sizeof(uint32_t)) {
552 n_log(
LOG_ERR,
"string %u/%u: not enough data for string headers (%zu remaining)", it, nb_str, remaining);
561 memcpy(&var, charptr,
sizeof(uint32_t));
562 charptr +=
sizeof(uint32_t);
563 tmpstr->
length = ntohl(var);
565 memcpy(&var, charptr,
sizeof(uint32_t));
566 charptr +=
sizeof(uint32_t);
568 remaining -= 2 *
sizeof(uint32_t);
572 n_log(
LOG_ERR,
"string %u/%u: written (%zu) >= length (%zu), violates N_STR invariant", it, nb_str, tmpstr->
written, tmpstr->
length);
579 if (tmpstr->
written > remaining) {
580 n_log(
LOG_ERR,
"string %u/%u: written size %zu exceeds remaining %zu bytes", it, nb_str, tmpstr->
written, remaining);
597 __n_assert(tmpstr->
data, { Free(tmpstr); delete_msg(&generated_msg); return NULL; });
613 return generated_msg;
629 N_STR* tmpstr = NULL;
668 N_STR* tmpstr = NULL;
711 N_STR* tmpstr = NULL;
772 N_STR* tmpstr = NULL;
822 N_STR* tmpstr = NULL;
846 char* charptr = NULL;
852 if (msg->
written < 4 *
sizeof(uint32_t))
return -1;
857 memcpy(&nb_int, charptr,
sizeof(uint32_t));
858 nb_int = ntohl(nb_int);
861 n_log(
LOG_ERR,
"netw_msg_get_type: message has no int fields (nb_int=%u), cannot read type", nb_int);
866 charptr += 3 *
sizeof(uint32_t);
867 memcpy(&tmpnb, charptr,
sizeof(uint32_t));
868 tmpnb = (int32_t)ntohl((uint32_t)tmpnb);
893 n_log(
LOG_ERR,
"failed to extract type/ident from ident message");
906 n_log(
LOG_ERR,
"failed to extract name/passwd from ident message");
931int netw_get_position(
N_STR* msg,
int*
id,
double* X,
double* Y,
double* vx,
double* vy,
double* acc_x,
double* acc_y,
int* time_stamp) {
943 n_log(
LOG_ERR,
"failed to extract type from position message");
961 n_log(
LOG_ERR,
"failed to extract fields from position message");
994 n_log(
LOG_ERR,
"failed to extract type from string message");
1008 n_log(
LOG_ERR,
"failed to extract int fields from string message");
1016 n_log(
LOG_ERR,
"failed to extract string fields from string message");
1049 n_log(
LOG_ERR,
"failed to extract type from ping message");
1062 n_log(
LOG_ERR,
"failed to extract fields from ping message");
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define Free(__ptr)
Free Handler to get errors.
void * ptr
void pointer to store
LIST_NODE * start
pointer to the start of the list
size_t nb_items
number of item currently in the list
struct LIST_NODE * next
pointer to the next node
#define list_shift(__LIST_, __TYPE_)
Shift macro helper for void pointer casting.
int list_empty(LIST *list)
Empty a LIST list of pointers.
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Structure of a generic list node.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
size_t written
size of the written data inside the string
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
A box including a string and his lenght.
LIST * tabflt
array of casted double value
LIST * tabstr
array of N_STR
LIST * tabint
array of int
#define NETMSG_IDENT_REQUEST
Network Message is identification request: (int)type , (int)id , (N_STR *)name , (N_STR *)password.
double htond(double value)
If needed swap bytes for a double.
int get_str_from_msg(NETW_MSG *msg, char **value)
Get a string from a message string list.
int add_strdup_to_msg(NETW_MSG *msg, const char *str)
Add a copy of char *str to the string list in the message.
int netw_get_position(N_STR *msg, int *id, double *X, double *Y, double *vx, double *vy, double *acc_x, double *acc_y, int *time_stamp)
Retrieves position from netwmsg.
#define NETMSG_PING_REQUEST
Network Message is ping request: (int)type , (int)id_from , (int)id_to.
#define NETMSG_IDENT_REPLY_NOK
Network Message is identification reply NON OK: (int)type , (int)id , (N_STR *)name ,...
#define NETMSG_STRING
Network Message is string: (int)type , (int)id , (N_STR *)name , (N_STR *)chan , (N_STR *)text ,...
#define NETMSG_PING_REPLY
Network Message is ping reply: (int)type , (int)id_from , (int)id_to.
NETW_MSG * make_msg_from_str(N_STR *str)
Make a single message of the string.
int empty_msg(NETW_MSG **msg)
Empty a NETW_MSG *object.
double double_swap(double value)
Swap bytes endiannes for a double.
N_STR * make_str_from_msg(NETW_MSG *msg)
Make a single string of the message.
int netw_get_ping(N_STR *msg, int *type, int *from, int *to, int *time)
Retrieves a ping travel elapsed time.
int add_nstrptr_to_msg(NETW_MSG *msg, N_STR *str)
Add a string to the string list in the message.
int get_nb_from_msg(NETW_MSG *msg, double *value)
Get a number from a message number list.
#define NETMSG_QUIT
Network asking for exit.
int netw_get_quit(N_STR *msg)
get a formatted NETWMSG_QUIT message from the specified network
int create_msg(NETW_MSG **msg)
Create a NETW_MSG *object.
#define NETMSG_POSITION
Network Message position: (int)type , (int)id , (int)X , (int)Y , (int)x_shift , (int)y_shift ,...
N_STR * netmsg_make_position_msg(int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp)
make a network NETMSG_POSITION message with given parameters
int netw_msg_get_type(N_STR *msg)
Get the type of message without killing the first number. Use with netw_get_XXX.
int get_nstr_from_msg(NETW_MSG *msg, N_STR **value)
Get a string from a message string list.
N_STR * netmsg_make_ident(int type, int id, N_STR *name, N_STR *passwd)
Add a formatted NETWMSG_IDENT message to the specified network.
int add_nstrdup_to_msg(NETW_MSG *msg, N_STR *str)
Add a copy of str to the string list in the message.
int add_int_to_msg(NETW_MSG *msg, int value)
Add an int to the int list int the message.
double ntohd(double value)
If needed swap bytes for a double.
#define NETMSG_IDENT_REPLY_OK
Network Message is identification reply OK : (int)type , (int)id , (N_STR *)name ,...
int netw_get_string(N_STR *msg, int *id, N_STR **name, N_STR **chan, N_STR **txt, int *color)
Retrieves string from netwmsg.
#define NETW_MSG_MAX_STRING_LENGTH
Maximum allowed length for a single string inside a network message (default: 1 GB).
int delete_msg(NETW_MSG **msg)
Delete a NETW_MSG *object.
int add_nb_to_msg(NETW_MSG *msg, double value)
Add an float to the message.
N_STR * netmsg_make_quit_msg(void)
make a generic network NETMSG_QUIT message
N_STR * netmsg_make_ping(int type, int id_from, int id_to, int time)
Make a ping message to send to a network.
int netw_get_ident(N_STR *msg, int *type, int *ident, N_STR **name, N_STR **passwd)
Retrieves identification from netwmsg.
N_STR * netmsg_make_string_msg(int id_from, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
make a network NETMSG_STRING message with given parameters
int get_int_from_msg(NETW_MSG *msg, int *value)
Get an int from a message int list.
network message, array of char and int
Common headers and low-level functions & define.
Network messages , serialization tools.