[pisa-src] r1817 - in trunk/pairing: common.c libconfig_wrapper.c packet_handler.c

  • From: Diego Biurrun <diego@xxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Thu, 26 Nov 2009 16:05:11 +0100

Author: biurrun
Date: Thu Nov 26 16:05:11 2009
New Revision: 1817

Log:
Reorder some functions to avoid forward declarations.

Modified:
   trunk/pairing/common.c
   trunk/pairing/libconfig_wrapper.c
   trunk/pairing/packet_handler.c

Modified: trunk/pairing/common.c
==============================================================================
--- trunk/pairing/common.c      Thu Nov 26 15:24:43 2009        (r1816)
+++ trunk/pairing/common.c      Thu Nov 26 16:05:11 2009        (r1817)
@@ -156,6 +156,25 @@
 }
 
 /**
+ * Converts time_t time to EXPIRATION_DATE_FORMAT.
+ * @param time Pointer to the time_t time
+ * @param buffer Pointer to the buffer holding the formatted string
+ *
+ * @return 1 -> Success
+ * @return 0 -> Failure
+ */
+int convert_time_t_to_formatted_string(time_t *t, char *buffer)
+{
+       struct tm *ts;
+       size_t temp;
+    // Convert to format: dd-mm-yyyy hh:mm:ss
+    ts = localtime(t);
+    temp = strftime(buffer, LENGTH_TIMEOUT, EXPIRATION_DATE_FORMAT, ts);
+    return temp!=0;
+
+}
+
+/**
  * Reads default expiration from filename
  *
  * @param buffer Buffer for the expiration string
@@ -201,25 +220,6 @@
 }
 
 
-/**
- * Converts time_t time to EXPIRATION_DATE_FORMAT.
- * @param time Pointer to the time_t time
- * @param buffer Pointer to the buffer holding the formatted string
- *
- * @return 1 -> Success
- * @return 0 -> Failure
- */
-int convert_time_t_to_formatted_string(time_t *t, char *buffer)
-{
-       struct tm *ts;
-       size_t temp;
-    // Convert to format: dd-mm-yyyy hh:mm:ss
-    ts = localtime(t);
-    temp = strftime(buffer, LENGTH_TIMEOUT, EXPIRATION_DATE_FORMAT, ts);
-    return temp!=0;
-
-}
-
 int valid_date_string(const char *expiration_date,struct tm *tm_expiration)
 {
     DEBUG_HIGH("Parsing date.");

Modified: trunk/pairing/libconfig_wrapper.c
==============================================================================
--- trunk/pairing/libconfig_wrapper.c   Thu Nov 26 15:24:43 2009        (r1816)
+++ trunk/pairing/libconfig_wrapper.c   Thu Nov 26 16:05:11 2009        (r1817)
@@ -21,121 +21,61 @@
 
 /** @todo Add a function which periodically scans files containing expiration 
dates and removes expired entries **/
 
-/** Writes a particular configuration.
- *
- *  @param info HIT or password
- *  @param expiration Expiration date
- *  @param type CONFIG_TYPE_HIT or CONFIG_TYPE_PWD (indicates which should be 
written)
+/**
+ * Checks if a list with list_name exists in the given config. If not, the 
list will be created.
+ * @param list_name The list name
+ * @param config The config to check for the list
  *
- *  @return 0 on failure; 1 on success
+ * @return A pointer to the list.
  */
-int write_config(char *info, char *expiration, configtype type)
+config_setting_t *check_list_in_config(const char * list_name, config_t 
*config)
 {
-    assert(info != NULL);
-    assert(expiration != NULL);
+    config_setting_t *root;
+    config_setting_t *list;
 
-    switch(type)
+    list = config_lookup(config, list_name);
+    if (!list)
     {
-    case CONFIG_TYPE_HIT:
-        {
-            add_hit(info, expiration);
-            break;
-        }
-    case CONFIG_TYPE_PWD:
-        {
-            config_setting_t *allowed_passwords;
-            config_setting_t *new_pwd_grp, *new_pwd, *new_expire;
-
-            allowed_passwords = check_list("allowed_passwords");
-
-            // Add a new group
-            new_pwd_grp = config_setting_add(allowed_passwords, NULL, 
CONFIG_TYPE_GROUP);
-            // Add the password
-            new_pwd = config_setting_add(new_pwd_grp, "password", 
CONFIG_TYPE_STRING);
-            config_setting_set_string(new_pwd, info);
-            // Add the expiration date
-            new_expire = config_setting_add(new_pwd_grp, "expires", 
CONFIG_TYPE_STRING);
-            config_setting_set_string(new_expire, expiration);
-
-            break;
-        }
-    default:
-        {
-            DEBUG("Invalid information type.");
-            return 0;
-        }
+        root = config_root_setting(config);
+        DEBUG_HIGH("List %s did not previously exist. Creating...",list_name);
+        list = config_setting_add(root, list_name, CONFIG_TYPE_LIST);
     }
-
-    pisa_cfg_write_authorized_hosts_file();
-
-    return 1;
+    return list;
 }
 
+/**
+ * Checks if a list with list_name exists in the authorized hosts config. If 
not, the list will be created.
+ * @param list_name The list name
+ * @return A pointer to the list.
+ */
+config_setting_t *check_list(const char * list_name)
+{
+    return check_list_in_config(list_name,&authorized_cfg);
+}
 
-/** Fetches the specified setting from the specified config file.
- *
- *  @note Config types are CONFIG_TYPE_INT, CONFIG_TYPE_INT64, 
CONFIG_TYPE_FLOAT, CONFIG_TYPE_STRING, CONFIG_TYPE_BOOL
- *
- *  @param filename Pointer to a string containing the file name.
- *  @param path Pointer to a string containing the path (libconfig style)
- *  @param type Type of data desired (e.g. CONFIG_TYPE_INT); use defined 
values from libconfig
- *  @param return_val Pointer to the variable where the information should be 
stored
- *  @param len Length of the string (if looking up a string).
+/**
+ * Searches for a HIT which is given in text format in the HITlist.
  *
- *  @return 1 on success; 0 on failure
+ * @param hit The HIT we wish to find.
  *
+ * @return The corresponding pisa_hitlist_entry if the HIT was found. 
Otherwise NULL.
  */
-int get_config_setting(const char *filename, const char *path, int type, void 
*return_val, int len)
+pisa_hitlist_entry *find_hit_in_hitlist(char *hit)
 {
-       config_t cfg;
-       config_setting_t *root;
-       const char *temp;
-       int success;
 
-       assert(filename != NULL);
-       assert(path != NULL);
-       assert(return_val != NULL);
-
-       config_init(&cfg);
-       if (!config_read_file(&cfg, filename))
-       {
-               DEBUG("Could not read file \'%s\'.", filename);
-               return 0;
-       }
-
-       root = config_root_setting(&cfg);
+    pisa_hitlist_entry *con;
+    pisa_hitlist *hitlist_allowed_hosts;
+    struct sockaddr_in6 clientIp;
 
-       switch(type)
-       {
-               case CONFIG_TYPE_INT:
-                       success = config_lookup_int(&cfg, path, return_val);
-                       break;
-               case CONFIG_TYPE_INT64:
-                       success = config_lookup_int64(&cfg, path, return_val);
-                       break;
-               case CONFIG_TYPE_FLOAT:
-                       success = config_lookup_float(&cfg, path, return_val);
-                       break;
-               case CONFIG_TYPE_STRING:
-                       success = config_lookup_string(&cfg, path, &temp);
-                       if (success != CONFIG_TRUE || temp == NULL)
-                       {
-                               DEBUG("Problem getting setting at path \'%s\' 
from file \'%s\'.", path, filename);
-                               return 0;
-                       }
-                       strncpy(*(char **)return_val, temp, len);
-                       break;
-               case CONFIG_TYPE_BOOL:
-                       success = config_lookup_bool(&cfg, path, return_val);
-                       break;
-               default:
-                       DEBUG_HIGH("Type not found.");
-                       return 0;
-       }
+    config_setting_t *allowed_hosts;
+    allowed_hosts = check_list("allowed_hosts");
+    hitlist_allowed_hosts = pisa_hitlist_build("allowed_hosts");
+    inet_pton(AF_INET6,hit,&(clientIp.sin6_addr));
 
-       config_destroy(&cfg);
+    // Checks if the HIT already exists to update the expiration date
+    con = pisa_hitlist_find(hitlist_allowed_hosts,&(clientIp.sin6_addr));
 
-       return 1;
+    return con;
 }
 
 /** Adds a HIT to the authorized hosts config. If an entry already exists, it 
updates
@@ -237,60 +177,119 @@
     return 0;
 }
 
-/**
- * Searches for a HIT which is given in text format in the HITlist.
+/** Writes a particular configuration.
  *
- * @param hit The HIT we wish to find.
+ *  @param info HIT or password
+ *  @param expiration Expiration date
+ *  @param type CONFIG_TYPE_HIT or CONFIG_TYPE_PWD (indicates which should be 
written)
  *
- * @return The corresponding pisa_hitlist_entry if the HIT was found. 
Otherwise NULL.
+ *  @return 0 on failure; 1 on success
  */
-pisa_hitlist_entry *find_hit_in_hitlist(char *hit)
+int write_config(char *info, char *expiration, configtype type)
 {
+    assert(info != NULL);
+    assert(expiration != NULL);
 
-    pisa_hitlist_entry *con;
-    pisa_hitlist *hitlist_allowed_hosts;
-    struct sockaddr_in6 clientIp;
+    switch(type)
+    {
+    case CONFIG_TYPE_HIT:
+        {
+            add_hit(info, expiration);
+            break;
+        }
+    case CONFIG_TYPE_PWD:
+        {
+            config_setting_t *allowed_passwords;
+            config_setting_t *new_pwd_grp, *new_pwd, *new_expire;
 
-    config_setting_t *allowed_hosts;
-    allowed_hosts = check_list("allowed_hosts");
-    hitlist_allowed_hosts = pisa_hitlist_build("allowed_hosts");
-    inet_pton(AF_INET6,hit,&(clientIp.sin6_addr));
+            allowed_passwords = check_list("allowed_passwords");
 
-    // Checks if the HIT already exists to update the expiration date
-    con = pisa_hitlist_find(hitlist_allowed_hosts,&(clientIp.sin6_addr));
+            // Add a new group
+            new_pwd_grp = config_setting_add(allowed_passwords, NULL, 
CONFIG_TYPE_GROUP);
+            // Add the password
+            new_pwd = config_setting_add(new_pwd_grp, "password", 
CONFIG_TYPE_STRING);
+            config_setting_set_string(new_pwd, info);
+            // Add the expiration date
+            new_expire = config_setting_add(new_pwd_grp, "expires", 
CONFIG_TYPE_STRING);
+            config_setting_set_string(new_expire, expiration);
 
-    return con;
-}
+            break;
+        }
+    default:
+        {
+            DEBUG("Invalid information type.");
+            return 0;
+        }
+    }
 
-/**
- * Checks if a list with list_name exists in the authorized hosts config. If 
not, the list will be created.
- * @param list_name The list name
- * @return A pointer to the list.
- */
-config_setting_t *check_list(const char * list_name)
-{
-    return check_list_in_config(list_name,&authorized_cfg);
+    pisa_cfg_write_authorized_hosts_file();
+
+    return 1;
 }
 
-/**
- * Checks if a list with list_name exists in the given config. If not, the 
list will be created.
- * @param list_name The list name
- * @param config The config to check for the list
+
+/** Fetches the specified setting from the specified config file.
+ *
+ *  @note Config types are CONFIG_TYPE_INT, CONFIG_TYPE_INT64, 
CONFIG_TYPE_FLOAT, CONFIG_TYPE_STRING, CONFIG_TYPE_BOOL
+ *
+ *  @param filename Pointer to a string containing the file name.
+ *  @param path Pointer to a string containing the path (libconfig style)
+ *  @param type Type of data desired (e.g. CONFIG_TYPE_INT); use defined 
values from libconfig
+ *  @param return_val Pointer to the variable where the information should be 
stored
+ *  @param len Length of the string (if looking up a string).
+ *
+ *  @return 1 on success; 0 on failure
  *
- * @return A pointer to the list.
  */
-config_setting_t *check_list_in_config(const char * list_name, config_t 
*config)
+int get_config_setting(const char *filename, const char *path, int type, void 
*return_val, int len)
 {
-    config_setting_t *root;
-    config_setting_t *list;
+       config_t cfg;
+       config_setting_t *root;
+       const char *temp;
+       int success;
 
-    list = config_lookup(config, list_name);
-    if (!list)
-    {
-        root = config_root_setting(config);
-        DEBUG_HIGH("List %s did not previously exist. Creating...",list_name);
-        list = config_setting_add(root, list_name, CONFIG_TYPE_LIST);
-    }
-    return list;
-}
+       assert(filename != NULL);
+       assert(path != NULL);
+       assert(return_val != NULL);
+
+       config_init(&cfg);
+       if (!config_read_file(&cfg, filename))
+       {
+               DEBUG("Could not read file \'%s\'.", filename);
+               return 0;
+       }
 
+       root = config_root_setting(&cfg);
+
+       switch(type)
+       {
+               case CONFIG_TYPE_INT:
+                       success = config_lookup_int(&cfg, path, return_val);
+                       break;
+               case CONFIG_TYPE_INT64:
+                       success = config_lookup_int64(&cfg, path, return_val);
+                       break;
+               case CONFIG_TYPE_FLOAT:
+                       success = config_lookup_float(&cfg, path, return_val);
+                       break;
+               case CONFIG_TYPE_STRING:
+                       success = config_lookup_string(&cfg, path, &temp);
+                       if (success != CONFIG_TRUE || temp == NULL)
+                       {
+                               DEBUG("Problem getting setting at path \'%s\' 
from file \'%s\'.", path, filename);
+                               return 0;
+                       }
+                       strncpy(*(char **)return_val, temp, len);
+                       break;
+               case CONFIG_TYPE_BOOL:
+                       success = config_lookup_bool(&cfg, path, return_val);
+                       break;
+               default:
+                       DEBUG_HIGH("Type not found.");
+                       return 0;
+       }
+
+       config_destroy(&cfg);
+
+       return 1;
+}

Modified: trunk/pairing/packet_handler.c
==============================================================================
--- trunk/pairing/packet_handler.c      Thu Nov 26 15:24:43 2009        (r1816)
+++ trunk/pairing/packet_handler.c      Thu Nov 26 16:05:11 2009        (r1817)
@@ -12,6 +12,25 @@
 
 #include "packet_handler.h"
 
+
+/** Handles an error packet
+ *
+ *  @param socket_addr Pointer to sockaddr_in6 structure connected to peer.
+ *  @param hdr_error Pointer to the header_error structure.
+ *
+ *  @return The error type
+ */
+int handle_packet_error(struct sockaddr_in6 *socket_addr, header_error 
*hdr_error)
+{
+       assert(socket_addr != NULL);
+       assert(hdr_error != NULL);
+
+       DEBUG_HIGH("Error type: %d.", hdr_error->error_type);
+       USER_MSG("Error: \'%s\'", hdr_error->error_string);
+
+       return hdr_error->error_type;
+}
+
 /** This function handles any packet received.
  *
  * @param socket_addr IP address where the packet was received
@@ -73,21 +92,3 @@
 
        return 1;
 }
-
-/** Handles an error packet
- *
- *  @param socket_addr Pointer to sockaddr_in6 structure connected to peer.
- *  @param hdr_error Pointer to the header_error structure.
- *
- *  @return The error type
- */
-int handle_packet_error(struct sockaddr_in6 *socket_addr, header_error 
*hdr_error)
-{
-       assert(socket_addr != NULL);
-       assert(hdr_error != NULL);
-
-       DEBUG_HIGH("Error type: %d.", hdr_error->error_type);
-       USER_MSG("Error: \'%s\'", hdr_error->error_string);
-
-       return hdr_error->error_type;
-}

Other related posts:

  • » [pisa-src] r1817 - in trunk/pairing: common.c libconfig_wrapper.c packet_handler.c - Diego Biurrun