Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_network_msg.c
Go to the documentation of this file.
1
9 #include "n_network_msg.h"
10
11
17 double double_swap( double value )
18 {
19 double swaped = 0 ;
20 unsigned char *src = (unsigned char *)&value ;
21 unsigned char *dst = (unsigned char *)&swaped ;
22
23 /* swap bytes */
24 dst[0] = src[7];
25 dst[1] = src[6];
26 dst[2] = src[5];
27 dst[3] = src[4];
28 dst[4] = src[3];
29 dst[5] = src[2];
30 dst[6] = src[1];
31 dst[7] = src[0];
32
33 return swaped ;
34 } /* double_swap() */
35
36
37
43 double htond( double value )
44 {
45 double ret = value ;
46 /* if byte order is little like x86, PC, ... we swap to network notation */
47 #if __BYTE_ORDER == __LITTLE_ENDIAN
48 ret = double_swap( value );
49 #endif
50 return ret ;
51 }
52
53
54
60 double ntohd( double value )
61 {
62 double ret = value ;
63 /* if byte order is little like x86, PC, ... we swap to network notation */
64 #if __BYTE_ORDER == __LITTLE_ENDIAN
65 ret = double_swap( value );
66 #endif
67 return ret ;
68 }
69
70
71
77 int create_msg( NETW_MSG **msg )
78 {
79 if( (*msg) )
80 {
81 n_log( LOG_ERR , "create_msg destination is valid and should be NULL !" );
82 return FALSE;
83 }
84
85 Malloc( (*msg) , NETW_MSG , 1 );
86 _assert( (*msg) , return FALSE );
87
88 (*msg) -> tabstr = new_generic_list( -1 );
89 _assert( (*msg) -> tabstr , return FALSE );
90 (*msg) -> tabint = new_generic_list( -1 );
91 _assert( (*msg) -> tabint , return FALSE );
92 (*msg) -> tabflt = new_generic_list( -1 );
93 _assert( (*msg) -> tabflt , return FALSE );
94
95 return TRUE;
96 } /* create_msg( ... ) */
97
98
99
105 int delete_msg( NETW_MSG **msg )
106 {
107 if( (*msg) )
108 {
109 if( (*msg) -> tabint )
110 {
111 list_destroy( &(*msg) -> tabint );
112 }
113 if( (*msg) -> tabflt )
114 {
115 list_destroy( &(*msg) -> tabflt );
116 }
117 if( (*msg) -> tabstr )
118 {
119 list_destroy( &(*msg) -> tabstr );
120 }
121 Free( (*msg) );
122 }
123 else
124 {
125 n_log( LOG_ERR , "(*msg) already NULL" );
126 return FALSE ;
127 }
128 return TRUE;
129 } /* delete_msg */
130
131
132
138 int empty_msg( NETW_MSG **msg )
139 {
140 _assert( (*msg) , return FALSE );
141
142 list_empty( (*msg) -> tabint );
143 list_empty( (*msg) -> tabflt );
144 list_empty( (*msg) -> tabstr );
145
146 return TRUE;
147 } /* empty_msg(...) */
148
149
150
158 int add_nb_to_msg( NETW_MSG *msg , double value )
159 {
160 double *new_val = NULL ;
161
162 _assert( msg , return FALSE );
163
164 Malloc( new_val , double , 1 );
165 _assert( new_val , return FALSE );
166
167 new_val[ 0 ] = value ;
168
169 return list_push( msg -> tabflt , new_val , free );
170 } /* add_nb_to_msg( ... ) */
171
172
173
180 int add_int_to_msg( NETW_MSG *msg , int value )
181 {
182 int *new_val = NULL ;
183
184 _assert( msg , return FALSE );
185
186 Malloc( new_val , int , 1 );
187 _assert( new_val , return FALSE );
188
189 new_val[ 0 ] = value ;
190
191 return list_push( msg -> tabint , new_val , free );
192 } /* add_int_to_msg( ... ) */
193
194
195
202 int add_nstrptr_to_msg( NETW_MSG *msg , N_STR *str )
203 {
204 _assert( msg , return FALSE );
205 _assert( str , return FALSE );
206 _assert( str -> data , return FALSE );
207 return list_push( msg -> tabstr , str , free_nstr_ptr );
208 } /* add_nstrptr_to_msg(...) */
209
210
211
218 int add_nstrdup_to_msg( NETW_MSG *msg , N_STR *str )
219 {
220 _assert( msg , return FALSE );
221 _assert( str , return FALSE );
222 _assert( str -> data , return FALSE );
223
224 return list_push( msg -> tabstr , nstrdup( str ) , free_nstr_ptr );
225 } /* add_nstrdup_to_msg(...) */
226
227
228
229
236 int add_strdup_to_msg( NETW_MSG *msg , char *str )
237 {
238 _assert( msg , return FALSE );
239 _assert( str , return FALSE );
240
241 N_STR *dup = char_to_nstr( str );
242 _assert( dup , return FALSE );
243
244 return list_push( msg -> tabstr , dup , free_nstr_ptr );
245 } /* add_strdup_to_msg(...) */
246
247
248
249
256 int get_nb_from_msg( NETW_MSG *msg , double *value )
257 {
258 double *val = NULL ;
259
260 _assert( msg , return FALSE );
261
262 val = list_shift( msg -> tabflt , double );
263 if( val )
264 {
265 (*value) = val[ 0 ] ;
266 Free( val );
267 return TRUE;
268 }
269 return FALSE;
270 } /* get_nb_from_msg(...) */
271
272
273
280 int get_int_from_msg( NETW_MSG *msg , int *value )
281 {
282 int *val = NULL ;
283
284 _assert( msg , return FALSE );
285
286 val = list_shift( msg -> tabint , int );
287 if( val )
288 {
289 (*value) = val[ 0 ];
290 Free( val );
291 return TRUE;
292 }
293 return FALSE;
294
295 } /* get_int_from_msg(...) */
296
297
298
305 int get_nstr_from_msg( NETW_MSG *msg , N_STR **value )
306 {
307 N_STR *val = NULL ;
308
309 _assert( msg , return FALSE );
310
311 val = list_shift( msg -> tabstr , N_STR );
312 if( val )
313 {
314 if( (*value) )
315 {
316 n_log( LOG_ERR , "Previous pointer value %p overriden by pointer %p" , (*value) , val );
317 }
318 (*value) = val ;
319 return TRUE;
320 }
321 return FALSE;
322 } /* get_nstr_from_msg(...) */
323
324
325
332 int get_str_from_msg( NETW_MSG *msg , char **value )
333 {
334 N_STR *val = NULL ;
335
336 _assert( msg , return FALSE );
337
338 val = list_shift( msg -> tabstr , N_STR );
339 if( val )
340 {
341 if( (*value) )
342 {
343 n_log( LOG_ERR , "Previous pointer value %p overriden by pointer %p" , (*value) , val );
344 }
345
346 (*value) = val -> data ;
347 Free( val );
348 return TRUE;
349 }
350 return FALSE;
351 } /* get_str_from_msg(...) */
352
353
354
360 N_STR *make_str_from_msg( NETW_MSG *msg )
361 {
362 int str_length = 0 ;
363 LIST_NODE *node = NULL ;
364
365 N_STR *strptr = NULL ,
366 *generated_str = NULL ;
367
368 char *charptr = NULL ;
369
370 _assert( msg , return NULL );
371
372 /* the first thing to do is to computer the resulting string size
373 the first 12 octets are for the number of ints , floats, strings in the message
374 */
375 str_length = 3 * sizeof( NSTRBYTE ) + ( msg -> tabint -> nb_items * sizeof( NSTRBYTE ) ) + ( msg -> tabflt -> nb_items * sizeof( double ) ) ;
376
377 /* let's count the eventual string blocks */
378 node = msg -> tabstr -> start ;
379 while( node )
380 {
381 strptr = (N_STR *)node -> ptr ;
382 str_length += sizeof( NSTRBYTE ) + sizeof( NSTRBYTE ) + strptr -> written ;
383 node = node -> next ;
384 }
385
386 /* preparing the new string */
387 Malloc( generated_str , N_STR , 1 );
388 _assert( generated_str , return NULL );
389 Malloc( generated_str -> data , char , str_length + 1 );
390 _assert( generated_str -> data , return NULL );
391
392 generated_str -> length = str_length + 1 ;
393 generated_str -> written = str_length ;
394
395
396 /* copying header */
397 charptr = generated_str -> data ;
398
399 /* byte order */
400 msg -> tabint -> nb_items = htonl( msg -> tabint -> nb_items );
401 msg -> tabflt -> nb_items = htonl( msg -> tabflt -> nb_items );
402 msg -> tabstr -> nb_items = htonl( msg -> tabstr -> nb_items );
403
404 /* number of ints inside the message */
405 memcpy ( charptr , &msg -> tabint -> nb_items , sizeof( NSTRBYTE ) );
406 charptr += sizeof( NSTRBYTE ) ;
407 /* number of doubles inside the message */
408 memcpy ( charptr , &msg -> tabflt -> nb_items , sizeof( NSTRBYTE ) );
409 charptr += sizeof( NSTRBYTE ) ;
410 /* number of string inside the message */
411 memcpy ( charptr , &msg -> tabstr -> nb_items , sizeof( NSTRBYTE ) );
412 charptr += sizeof( NSTRBYTE ) ;
413
414 /*reversing byte order to default host order */
415 msg -> tabint -> nb_items = ntohl( msg -> tabint -> nb_items );
416 msg -> tabflt -> nb_items = ntohl( msg -> tabflt -> nb_items );
417 msg -> tabstr -> nb_items = ntohl( msg -> tabstr -> nb_items );
418
419 /* if there are numbers, copy all them ! */
420 node = msg -> tabint -> start ;
421 while( node )
422 {
423 NSTRBYTE *nbptr = (NSTRBYTE *)node -> ptr ;
424 int val = htonl( nbptr[ 0 ] );
425 memcpy( charptr , &val , sizeof( NSTRBYTE ) );
426 charptr += sizeof( NSTRBYTE ) ;
427 node = node -> next ;
428 }
429
430 /* if there are numbers, copy all them ! */
431 node = msg -> tabflt -> start ;
432 while( node )
433 {
434 double *nbptr = (double *)node -> ptr ;
435 double val = htond( nbptr[ 0 ] );
436 memcpy( charptr , &val , sizeof( double ) );
437 charptr += sizeof( double ) ;
438 node = node -> next ;
439 }
440
441 /* if there are strings, copy all them ! */
442 if( msg -> tabstr -> nb_items > 0 )
443 {
444 node = msg -> tabstr -> start ;
445 while( node )
446 {
447 strptr = (N_STR *)node -> ptr ;
448
449 strptr -> length = htonl( strptr -> length );
450 memcpy( charptr , &strptr -> length , sizeof( NSTRBYTE ) );
451 charptr += sizeof( NSTRBYTE );
452 strptr -> length = ntohl( strptr -> length );
453
454 strptr -> written = htonl( strptr -> written );
455 memcpy( charptr , &strptr -> written , sizeof( NSTRBYTE ) );
456 charptr += sizeof( NSTRBYTE );
457 strptr -> written = ntohl( strptr -> written );
458
459 memcpy( charptr , strptr -> data , strptr -> written );
460 charptr += strptr -> written ;
461 node = node -> next ;
462 }
463
464 }
465
466 return generated_str;
467 } /* make_str_from_msg(...) */
468
469
470
476 NETW_MSG *make_msg_from_str( N_STR *str )
477 {
478 NETW_MSG *generated_msg = NULL ;
479 N_STR *tmpstr = NULL;
480
481 char *charptr = NULL;
482
483 NSTRBYTE it ,
484 nb_int = 0 ,
485 nb_flt = 0 ,
486 nb_str = 0 ,
487 tmpnb = 0 ;
488
489 double tmpflt = 0 ;
490
491 _assert( str , return NULL );
492
493 charptr = str -> data;
494
495 create_msg( &generated_msg );
496
497 _assert( generated_msg , return NULL );
498
499 memcpy( &nb_int , charptr , sizeof( NSTRBYTE ) );
500 charptr += sizeof( NSTRBYTE ) ;
501 nb_int = ntohl( nb_int );
502
503 memcpy( &nb_flt , charptr , sizeof( NSTRBYTE ) );
504 charptr += sizeof( NSTRBYTE ) ;
505 nb_flt = ntohl( nb_flt );
506
507 memcpy( &nb_str , charptr , sizeof( NSTRBYTE ) );
508 charptr += sizeof( NSTRBYTE ) ;
509 nb_str = ntohl( nb_str );
510
511 if( nb_int > 0 )
512 {
513 for( it = 0 ; it < nb_int ; it ++ )
514 {
515 memcpy( &tmpnb , charptr , sizeof( NSTRBYTE ) ) ;
516 tmpnb = ntohl( tmpnb );
517 charptr += sizeof( NSTRBYTE ) ;
518 add_int_to_msg( generated_msg , tmpnb );
519 }
520 }
521
522 if( nb_flt > 0 )
523 {
524 for( it = 0 ; it < nb_flt ; it ++ )
525 {
526 memcpy( &tmpflt , charptr , sizeof( double ) ) ;
527 tmpflt = ntohd( tmpflt );
528 charptr += sizeof( double ) ;
529 add_nb_to_msg( generated_msg , tmpflt );
530 }
531 }
532
533
534 if( nb_str > 0 )
535 {
536 for( it = 0 ; it < nb_str ; it ++ )
537 {
538
539 Malloc( tmpstr , N_STR , 1 );
540 _assert( tmpstr , return NULL );
541
542 memcpy( &tmpstr -> length , charptr , sizeof( NSTRBYTE ) );
543 charptr += sizeof( NSTRBYTE ) ;
544 memcpy( &tmpstr -> written , charptr , sizeof( NSTRBYTE ) );
545 charptr += sizeof( NSTRBYTE ) ;
546
547 tmpstr -> length = ntohl( tmpstr -> length );
548 tmpstr -> written = ntohl( tmpstr -> written );
549
550 Malloc( tmpstr -> data , char , tmpstr -> length );
551 _assert( tmpstr -> data , return NULL );
552
553 memcpy( tmpstr -> data , charptr , tmpstr -> written );
554 charptr += tmpstr -> written ;
555
556 add_nstrptr_to_msg( generated_msg , tmpstr );
557 }
558 }
559 return generated_msg ;
560 } /* make_msg_from_str(...) */
561
562
563
569 int netw_msg_get_type( N_STR *msg )
570 {
571 NSTRBYTE tmpnb = 0 ;
572 char *charptr = NULL ;
573
574 _assert( msg , return FALSE );
575
576 charptr = msg -> data ;
577
578 /* here we bypass the number of int, numebr of flt, number of str, (4+4+4) to get the
579 * first number which should be type */
580 charptr += 3 * sizeof( NSTRBYTE );
581 memcpy( &tmpnb , charptr , sizeof( NSTRBYTE ) ) ;
582 tmpnb = ntohl( tmpnb );
583
584 return tmpnb;
585 } /* netw_msg_get_type(...) */
586
587
588
598 int netw_send_ident( NETWORK *netw , int type , int id , N_STR *name , N_STR *passwd )
599 {
600 NETW_MSG *msg = NULL ;
601 N_STR *tmpstr = NULL ;
602
603 _assert( netw , return FALSE );
604
605 create_msg( &msg );
606
607 _assert( msg , return FALSE );
608
609 add_int_to_msg( msg , type );
610 add_int_to_msg( msg , id );
611
612 add_nstrdup_to_msg( msg , name );
613 add_nstrdup_to_msg( msg , passwd );
614
615 tmpstr = make_str_from_msg( msg );
616 delete_msg( &msg );
617 _assert( tmpstr , return FALSE );
618
619 return netw_add_msg( netw , tmpstr );
620 } /* netw_send_ident( ... ) */
621
622
623
637 int netw_send_position( NETWORK *netw , int id , double X , double Y , double vx , double vy , double acc_x , double acc_y , int time_stamp )
638 {
639 NETW_MSG *msg = NULL ;
640 N_STR *tmpstr = NULL ;
641
642 _assert( netw , return FALSE );
643
644 create_msg( &msg );
645
646 _assert( msg , return FALSE );
647
648 add_int_to_msg( msg , NETMSG_POSITION );
649 add_int_to_msg( msg , id );
650 add_nb_to_msg( msg , X );
651 add_nb_to_msg( msg , Y );
652 add_nb_to_msg( msg , vx );
653 add_nb_to_msg( msg , vy );
654 add_nb_to_msg( msg , acc_x );
655 add_nb_to_msg( msg , acc_y );
656 add_int_to_msg( msg , time_stamp );
657
658 tmpstr = make_str_from_msg( msg );
659 delete_msg( &msg );
660 _assert( tmpstr , return FALSE );
661
662 return netw_add_msg( netw , tmpstr );
663 } /* netw_send_position( ... ) */
664
665
666
678 int netw_send_string_to_all( NETWORK *netw , int id , char *name , char *chan , char *txt , int color )
679 {
680 NETW_MSG *msg = NULL ;
681
682 N_STR *namestr = NULL ,
683 *chanstr = NULL ,
684 *txtstr = NULL ,
685 *tmpstr = NULL ;
686
687 _assert( netw , return FALSE );
688
689 create_msg( &msg );
690 _assert( msg , return FALSE );
691
692 namestr = char_to_nstr( name );
693 _assert( namestr , delete_msg( &msg ); return FALSE );
694
695 chanstr = char_to_nstr( chan );
696 _assert( chanstr , delete_msg( &msg ); free_nstr( &namestr ); return FALSE );
697
698 txtstr = char_to_nstr( txt );
699 _assert( txtstr , delete_msg( &msg ); free_nstr( &namestr ); free_nstr( &chanstr ); return FALSE );
700
701 n_log( LOG_DEBUG , "send_string_to_all( %d:%d:%d , %s , %s , %s )" ,NETMSG_STRING,id,color, namestr -> data , chanstr -> data , txtstr -> data );
702
703 add_int_to_msg( msg , NETMSG_STRING );
704 add_int_to_msg( msg , id );
705 add_int_to_msg( msg , color );
706
707 add_nstrptr_to_msg( msg , namestr );
708 add_nstrptr_to_msg( msg , chanstr );
709 add_nstrptr_to_msg( msg , txtstr );
710
711 tmpstr = make_str_from_msg( msg );
712
713 delete_msg( &msg );
714
715 _assert( tmpstr , return FALSE );
716
717 return netw_add_msg( netw , tmpstr );
718 } /* netw_send_string_to_all( ... ) */
719
720
721
732 int netw_send_string_to( NETWORK *netw , int id_from , int id_to , char *name , char *txt , int color )
733 {
734 NETW_MSG *msg = NULL ;
735
736 N_STR *namestr = NULL ,
737 *txtstr = NULL ,
738 *tmpstr = NULL ;
739
740 _assert( netw , return FALSE );
741
742 create_msg( &msg );
743
744 _assert( msg , return FALSE );
745
746 namestr = char_to_nstr( name );
747 _assert( namestr , delete_msg( &msg ); return FALSE );
748
749 txtstr = char_to_nstr( txt );
750 _assert( txtstr , delete_msg( &msg ); free_nstr( &namestr ); return FALSE );
751
752 add_int_to_msg( msg , NETMSG_STRING );
753 add_int_to_msg( msg , id_from );
754 add_int_to_msg( msg , id_to );
755 add_int_to_msg( msg , color );
756
757
758 add_nstrptr_to_msg( msg , namestr );
759 add_nstrptr_to_msg( msg , txtstr );
760
761 tmpstr = make_str_from_msg( msg );
762
763 delete_msg( &msg );
764
765 _assert( tmpstr , return FALSE );
766
767 return netw_add_msg( netw , tmpstr );
768 } /* netw_send_string_to( ... ) */
769
770
771
781 int netw_send_ping( NETWORK *netw , int type , int id_from , int id_to , int time )
782 {
783 NETW_MSG *msg = NULL ;
784
785 N_STR *tmpstr = NULL ;
786
787 _assert( netw , return FALSE );
788
789 create_msg( &msg );
790
791 _assert( msg , return FALSE );
792
793 add_int_to_msg( msg , type );
794 add_int_to_msg( msg , id_from );
795 add_int_to_msg( msg , id_to );
796 add_int_to_msg( msg , time );
797
798 tmpstr = make_str_from_msg( msg );
799
800 delete_msg( &msg );
801
802 _assert( tmpstr , return FALSE );
803
804 return netw_add_msg( netw , tmpstr );
805 } /* netw_send_ping( ... ) */
806
807
817 int netw_get_ident( N_STR *msg , int *type ,int *ident , N_STR **name , N_STR **passwd )
818 {
819 NETW_MSG *netmsg = NULL ;
820
821 _assert( msg , return FALSE);
822
823 netmsg = make_msg_from_str( msg );
824
825 _assert( netmsg , return FALSE);
826
827 get_int_from_msg( netmsg , &(*type) );
828 get_int_from_msg( netmsg , &(*ident) );
829
830 if( (*type) != NETMSG_IDENT_REQUEST && (*type) != NETMSG_IDENT_REPLY_OK && (*type) != NETMSG_IDENT_REPLY_NOK )
831 {
832 n_log( LOG_ERR, "unknow (*type) %d" , (*type) );
833 delete_msg( &netmsg );
834 return FALSE;
835 }
836
837 get_nstr_from_msg( netmsg , &(*name) );
838 get_nstr_from_msg( netmsg , &(*passwd) );
839
840 delete_msg( &netmsg );
841
842 return TRUE;
843 } /* netw_get_ident( ... ) */
844
845
846
860 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 )
861 {
862 NETW_MSG *netmsg = NULL ;
863
864 int type = 0 ;
865
866 _assert( msg , return FALSE );
867
868 netmsg = make_msg_from_str( msg );
869
870 _assert( netmsg , return FALSE );
871
872 get_int_from_msg( netmsg , &type );
873
874 if( type != NETMSG_POSITION )
875 {
876 delete_msg( &netmsg );
877 return FALSE;
878 }
879
880 get_int_from_msg( netmsg , &(*id) );
881 get_nb_from_msg( netmsg , &(*X) );
882 get_nb_from_msg( netmsg , &(*Y) );
883 get_nb_from_msg( netmsg , &(*vx) );
884 get_nb_from_msg( netmsg , &(*vy) );
885 get_nb_from_msg( netmsg , &(*acc_x) );
886 get_nb_from_msg( netmsg , &(*acc_y) );
887 get_int_from_msg( netmsg , &(*time_stamp) );
888
889 delete_msg( &netmsg );
890
891 return TRUE;
892 } /* netw_get_position( ... ) */
893
894
895
907 int netw_get_string( N_STR *msg , int *id , N_STR **name , N_STR **chan , N_STR **txt , int *color )
908 {
909 NETW_MSG *netmsg = NULL;
910
911 int type = 0 ;
912
913 _assert( msg , return FALSE );
914
915 netmsg = make_msg_from_str( msg );
916
917 _assert( netmsg , return FALSE );
918
919 get_int_from_msg( netmsg , &type );
920
921 if( type != NETMSG_STRING )
922 {
923 delete_msg( &netmsg );
924 return FALSE;
925 }
926
927 get_int_from_msg( netmsg , id );
928 get_int_from_msg( netmsg , color );
929
930 get_nstr_from_msg( netmsg , &(*name) );
931 get_nstr_from_msg( netmsg , &(*chan) );
932 get_nstr_from_msg( netmsg , &(*txt) );
933
934 delete_msg( &netmsg );
935
936 return TRUE;
937
938 } /* netw_get_string( ... ) */
939
940
941
953 int netw_get_ping( N_STR *msg , int *type , int *from , int *to , int *time )
954 {
955 NETW_MSG *netmsg;
956
957 _assert( msg , return FALSE );
958
959 netmsg = make_msg_from_str( msg );
960
961 _assert( netmsg , return FALSE );
962
963 get_int_from_msg( netmsg , &(*type) );
964
965 if( (*type) != NETMSG_PING_REQUEST && (*type) != NETMSG_PING_REPLY )
966 {
967 delete_msg( &netmsg );
968 return FALSE;
969 }
970
971 get_int_from_msg( netmsg , &(*from) );
972 get_int_from_msg( netmsg , &(*to) );
973 get_int_from_msg( netmsg , &(*time) );
974
975
976 delete_msg( &netmsg );
977
978 return TRUE;
979 } /* netw_get_ping( ... ) */
980