time values in strongswan.conf can be optionally specified in days (d), hours (h...
[strongswan.git] / src / libstrongswan / settings.c
index 57325b5..6f9e403 100644 (file)
@@ -164,7 +164,7 @@ static bool get_bool(private_settings_t *this, char *key, bool def)
        if (value)
        {
                if (strcasecmp(value, "true") == 0 ||
-                       strcasecmp(value, "enables") == 0 ||
+                       strcasecmp(value, "enabled") == 0 ||
                        strcasecmp(value, "yes") == 0 ||
                        strcasecmp(value, "1") == 0)
                {
@@ -203,8 +203,44 @@ static int get_int(private_settings_t *this, char *key, int def)
 }
 
 /**
- * destry a section
-*/
+ * Implementation of settings_t.get_time.
+ */
+static u_int32_t get_time(private_settings_t *this, char *key, u_int32_t def)
+{
+       char *value, *endptr;
+       u_int32_t timeval;
+       
+       value = find(this->top, key);
+       if (value)
+       {
+               errno = 0;
+               timeval = strtol(value, &endptr, 10);
+               if (errno == 0 && timeval >= 0)
+               {
+                       switch (*endptr)
+                       {
+                               case 'd':               /* time in days */
+                                       timeval *= 24 * 3600;
+                                       break;
+                               case 'h':               /* time in hours */
+                                       timeval *= 3600;
+                                       break;
+                               case 'm':               /* time in minutes */
+                                       timeval *= 60;
+                                       break;
+                               case 's':               /* time in seconds */
+                                       default:
+                                       break;
+                       }
+                       return timeval;
+               }
+       }
+       return def;
+}
+
+/**
+ * destroy a section
+ */
 static void section_destroy(section_t *this)
 {
        this->kv->destroy_function(this->kv, free);
@@ -318,6 +354,7 @@ static section_t* parse_section(char **text, char *name)
                                                continue;
                                        }
                                }
+                               DBG1("matching '}' not found near %s", *text);
                                break;
                        case '=':
                                if (parse(text, "\t ", "\n", NULL, &value))
@@ -328,6 +365,7 @@ static section_t* parse_section(char **text, char *name)
                                        section->kv->insert_last(section->kv, kv);
                                        continue;
                                }
+                               DBG1("parsing value failed near %s", *text);
                                break;
                        case '#':
                                parse(text, "", "\n", NULL, &value);
@@ -363,7 +401,8 @@ settings_t *settings_create(char *file)
        private_settings_t *this = malloc_thing(private_settings_t);
        
        this->public.get_str = (char*(*)(settings_t*, char *key, char* def))get_str;
-       this->public.get_int = (int(*)(settings_t*, char *key, bool def))get_int;
+       this->public.get_int = (int(*)(settings_t*, char *key, int def))get_int;
+       this->public.get_time = (u_int32_t(*)(settings_t*, char *key, u_int32_t def))get_time;
        this->public.get_bool = (bool(*)(settings_t*, char *key, bool def))get_bool;
        this->public.destroy = (void(*)(settings_t*))destroy;
        
@@ -379,6 +418,7 @@ settings_t *settings_create(char *file)
                fd = fopen(file, "r");
                if (fd == NULL)
                {
+                       DBG1("'%s' does not exist or is not readable", file);
                        return &this->public;
                }
                fseek(fd, 0, SEEK_END);