+Implemented -v|--verbose functionality.
[udpgate.git] / src / packet.c
index 142c1bf..fa86e88 100644 (file)
@@ -27,6 +27,8 @@
 #include <glib/gslist.h>
 
 #include "packet.h"
+#include "main.h"      /* for optarg_verbose */
+
 
 static void packet_assembly_guint32(GString *gstring,guint32 value_guint32)
 {
@@ -105,13 +107,23 @@ guint32 elem_type_uint32;
        switch (elem_type_uint32) {
                case PACKET_ELEM_TYPE_PROGRAM_VERSION:
                        packet_assembly_data_string(gstring,value);
+                       if (optarg_verbose)
+                               g_message(_("Assembled PROGRAM_VERSION: %s"),(const char *)value);
                        break;
                case PACKET_ELEM_TYPE_CLIENT_INADDR:
+                       packet_assembly_data_guint32(gstring,GPOINTER_TO_UINT(value));
+                       if (optarg_verbose)
+                               g_message(_("Assembled CLIENT_INADDR: %s"),HOSTIP_GUINT32_TO_STRING(GPOINTER_TO_UINT(value)));
+                       break;
                case PACKET_ELEM_TYPE_DATA_GUINT32:
                        packet_assembly_data_guint32(gstring,GPOINTER_TO_UINT(value));
+                       if (optarg_verbose)
+                               g_message(_("Assembled DATA_GUINT32: 0x%08x"),(unsigned)GPOINTER_TO_UINT(value));
                        break;
                case PACKET_ELEM_TYPE_CLIENT_PORT:
                        packet_assembly_data_guint16(gstring,GPOINTER_TO_UINT(value));
+                       if (optarg_verbose)
+                               g_message(_("Assembled CLIENT_PORT: %d"),(int)GPOINTER_TO_UINT(value));
                        break;
                default:
                        g_assert_not_reached();
@@ -125,6 +137,9 @@ GString *gstring;
        g_return_val_if_fail(packet_length_pointer!=NULL,NULL);
        g_return_val_if_fail(values!=NULL,NULL);
 
+       if (optarg_verbose)
+               g_message(_("Assembling packet..."));
+
        gstring=g_string_new(NULL);
 
        g_string_append(gstring,PACKET_HEADER);
@@ -135,6 +150,9 @@ GString *gstring;
                        (GHFunc)packet_assembly_foreach,        /* func */
                        gstring);       /* user_data */
 
+       if (optarg_verbose)
+               g_message(_("Packet assembly done."));
+
        *packet_length_pointer=gstring->len;
        return g_string_free(gstring,
                        FALSE); /* free_segment */
@@ -268,6 +286,9 @@ GSList *items;
 
        g_return_val_if_fail(packet!=NULL,NULL);
 
+       if (optarg_verbose)
+               g_message(_("Decoding packet..."));
+
        r=g_hash_table_new_full(
                        g_direct_hash,  /* hash_func */
                        g_direct_equal, /* key_equal_func */
@@ -306,20 +327,37 @@ err_packet_disassembly_destroy_value_destroy_func_items:
                case PACKET_ELEM_TYPE_PROGRAM_VERSION:
                        if (!packet_disassembly_data_string(&value,elem_data,elem_data_length_guint32,&items))
                                goto err_packet_disassembly_destroy_value_destroy_func_items;
+                       if (optarg_verbose)
+                               g_message(_("Decoded PROGRAM_VERSION: %s"),(const char *)value);
                        break;
                case PACKET_ELEM_TYPE_CLIENT_INADDR:
+                       if (!packet_disassembly_data_guint32(&value,elem_data,elem_data_length_guint32,&items))
+                               goto err_packet_disassembly_destroy_value_destroy_func_items;
+                       if (optarg_verbose)
+                               g_message(_("Decoded CLIENT_INADDR: %s"),HOSTIP_GUINT32_TO_STRING(GPOINTER_TO_UINT(value)));
+                       break;
                case PACKET_ELEM_TYPE_DATA_GUINT32:
                        if (!packet_disassembly_data_guint32(&value,elem_data,elem_data_length_guint32,&items))
                                goto err_packet_disassembly_destroy_value_destroy_func_items;
+                       if (optarg_verbose)
+                               g_message(_("Decoded DATA_GUINT32: 0x%08x"),(unsigned)GPOINTER_TO_UINT(value));
                        break;
                case PACKET_ELEM_TYPE_CLIENT_PORT:
                        if (!packet_disassembly_data_guint16(&value,elem_data,elem_data_length_guint32,&items))
                                goto err_packet_disassembly_destroy_value_destroy_func_items;
+                       if (optarg_verbose)
+                               g_message(_("Decoded CLIENT_PORT: %d"),GPOINTER_TO_UINT(value));
                        break;
                default:
-                       if (elem_type_guint32&PACKET_ELEM_ATTR_MANDATORY)
+                       if (elem_type_guint32&PACKET_ELEM_ATTR_MANDATORY) {
+                               if (optarg_verbose)
+                                       g_message(_("Decoding failed on ATTR_MANDATORY ELEM code 0x%08x"),(unsigned)elem_type_guint32);
                                goto err_packet_disassembly_destroy_value_destroy_func_items;
-                       else /* ignore the PACKET_ELEM_ATTR_OPTIONAL packet */;
+                               }
+                       else {
+                               if (optarg_verbose)
+                                       g_message(_("Decoding ignores ATTR_OPTIONAL ELEM code 0x%08x"),(unsigned)elem_type_guint32);
+                               }
                        continue;
                        }
                g_hash_table_insert(r,GUINT_TO_POINTER(elem_type_guint32),value);
@@ -329,5 +367,8 @@ err_packet_disassembly_destroy_value_destroy_func_items:
        packet_disassembly_destroy_registry_init();
        g_hash_table_insert(packet_disassembly_destroy_registry,r,items);
 
+       if (optarg_verbose)
+               g_message(_("Decoding done."));
+
        return r;
 }