]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Get rid of warnings in rlm_counter
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 9 Dec 2012 15:59:44 +0000 (15:59 +0000)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 9 Dec 2012 15:59:44 +0000 (15:59 +0000)
src/modules/rlm_counter/rlm_counter.c

index cf884e03c610cd3164a80d482ffadb077df97eec..03e8a38019ec7831a3bcd9b9c4424d0f85328037 100644 (file)
@@ -60,23 +60,27 @@ RCSID("$Id$")
  *     be used as the instance handle.
  */
 typedef struct rlm_counter_t {
-       char *filename;         /* name of the database file */
-       char *reset;            /* daily, weekly, monthly, never or user defined */
-       char *key_name;         /* User-Name */
-       char *count_attribute;  /* Acct-Session-Time */
-       char *counter_name;     /* Daily-Session-Time */
-       char *check_name;       /* Daily-Max-Session */
-       char *reply_name;       /* Session-Timeout */
-       char *service_type;     /* Service-Type to search for */
+       const char *filename;           /* name of the database file */
+       const char *reset;              /* daily, weekly, monthly, never or user defined */
+       const char *key_name;           /* User-Name */
+       const char *count_attribute;    /* Acct-Session-Time */
+       const char *counter_name;       /* Daily-Session-Time */
+       const char *check_name;         /* Daily-Max-Session */
+       const char *reply_name;         /* Session-Timeout */
+       const char *service_type;       /* Service-Type to search for */
+       
        int cache_size;
-       int service_val;
+       uint32_t service_val;
+       
        int key_attr;
        int count_attr;
        int check_attr;
        int reply_attr;
+       int dict_attr;          /* attribute number for the counter. */
+       
        time_t reset_time;      /* The time of the next reset. */
        time_t last_reset;      /* The time of the last reset. */
-       int dict_attr;          /* attribute number for the counter. */
+
        GDBM_FILE gdbm;         /* The gdbm file handle */
 #ifdef HAVE_PTHREAD_H
        pthread_mutex_t mutex;  /* A mutex to lock the gdbm file for only one reader/writer */
@@ -132,7 +136,7 @@ static int counter_cmp(void *instance,
                       VALUE_PAIR *request, VALUE_PAIR *check,
                       VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
 {
-       rlm_counter_t *data = (rlm_counter_t *) instance;
+       rlm_counter_t *inst = instance;
        datum key_datum;
        datum count_datum;
        VALUE_PAIR *key_vp;
@@ -145,7 +149,7 @@ static int counter_cmp(void *instance,
        /*
         *      Find the key attribute.
         */
-       key_vp = pairfind(request, data->key_attr, 0);
+       key_vp = pairfind(request, inst->key_attr, 0);
        if (key_vp == NULL) {
                return RLM_MODULE_NOOP;
        }
@@ -153,7 +157,7 @@ static int counter_cmp(void *instance,
        key_datum.dptr = key_vp->vp_strvalue;
        key_datum.dsize = key_vp->length;
 
-       count_datum = gdbm_fetch(data->gdbm, key_datum);
+       count_datum = gdbm_fetch(inst->gdbm, key_datum);
 
        if (count_datum.dptr == NULL) {
                return -1;
@@ -165,69 +169,73 @@ static int counter_cmp(void *instance,
 }
 
 
-static int add_defaults(rlm_counter_t *data)
+static int add_defaults(rlm_counter_t *inst)
 {
        datum key_datum;
        datum time_datum;
-       const char *default1 = "DEFAULT1";
-       const char *default2 = "DEFAULT2";
+       static const char const *default1 = "DEFAULT1";
+       static const char const *default2 = "DEFAULT2";
 
        DEBUG2("rlm_counter: add_defaults: Start");
 
-       key_datum.dptr = (char *) default1;
-       key_datum.dsize = strlen(default1);
-       time_datum.dptr = (char *) &data->reset_time;
+       memcpy(&key_datum.dptr, &default1, sizeof(key_datum.dptr));
+       key_datum.dsize = strlen(key_datum.dptr);
+       time_datum.dptr = (char *) &inst->reset_time;
        time_datum.dsize = sizeof(time_t);
 
-       if (gdbm_store(data->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
+       if (gdbm_store(inst->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
                radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
-                               data->filename, gdbm_strerror(gdbm_errno));
+                               inst->filename, gdbm_strerror(gdbm_errno));
                return RLM_MODULE_FAIL;
        }
-       DEBUG2("rlm_counter: DEFAULT1 set to %d",(int)data->reset_time);
+       DEBUG2("rlm_counter: DEFAULT1 set to %u", (unsigned int) inst->reset_time);
 
-       key_datum.dptr = (char *) default2;
+       memcpy(&key_datum.dptr, &default2, sizeof(key_datum.dptr));
+       key_datum.dsize = strlen(key_datum.dptr);
        key_datum.dsize = strlen(default2);
-       time_datum.dptr = (char *) &data->last_reset;
+       time_datum.dptr = (char *) &inst->last_reset;
        time_datum.dsize = sizeof(time_t);
 
-       if (gdbm_store(data->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
+       if (gdbm_store(inst->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
                radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
-                               data->filename, gdbm_strerror(gdbm_errno));
+                               inst->filename, gdbm_strerror(gdbm_errno));
                return RLM_MODULE_FAIL;
        }
-       DEBUG2("rlm_counter: DEFAULT2 set to %d",(int)data->last_reset);
+       DEBUG2("rlm_counter: DEFAULT2 set to %u", (unsigned int) inst->last_reset);
        DEBUG2("rlm_counter: add_defaults: End");
 
        return RLM_MODULE_OK;
 }
 
-static int reset_db(rlm_counter_t *data)
+static int reset_db(rlm_counter_t *inst)
 {
-       int cache_size = data->cache_size;
+       int cache_size = inst->cache_size;
        int ret;
 
        DEBUG2("rlm_counter: reset_db: Closing database");
-       gdbm_close(data->gdbm);
+       gdbm_close(inst->gdbm);
 
        /*
         *      Open a completely new database.
         */
-       data->gdbm = gdbm_open(data->filename, sizeof(int),
+       inst->gdbm = gdbm_open(inst->filename, sizeof(int),
                        GDBM_NEWDB | GDBM_COUNTER_OPTS, 0600, NULL);
-       if (data->gdbm == NULL) {
+       if (inst->gdbm == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
-                               data->filename, strerror(errno));
+                               inst->filename, strerror(errno));
                return RLM_MODULE_FAIL;
        }
-       if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
+       if (gdbm_setopt(inst->gdbm, GDBM_CACHESIZE, &cache_size,
+                       sizeof(cache_size)) == -1) {
                radlog(L_ERR, "rlm_counter: Failed to set cache size");
+       }
+       
        DEBUG2("rlm_counter: reset_db: Opened new database");
 
        /*
         * Add defaults
         */
-       ret = add_defaults(data);
+       ret = add_defaults(inst);
        if (ret != RLM_MODULE_OK)
                return ret;
 
@@ -236,7 +244,7 @@ static int reset_db(rlm_counter_t *data)
        return RLM_MODULE_OK;
 }
 
-static int find_next_reset(rlm_counter_t *data, time_t timeval)
+static int find_next_reset(rlm_counter_t *inst, time_t timeval)
 {
        int ret = 0;
        size_t len;
@@ -250,55 +258,55 @@ static int find_next_reset(rlm_counter_t *data, time_t timeval)
        if (len == 0) *sCurrentTime = '\0';
        tm->tm_sec = tm->tm_min = 0;
 
-       if (data->reset == NULL)
+       if (inst->reset == NULL)
                return -1;
-       if (isdigit((int) data->reset[0])){
-               len = strlen(data->reset);
+       if (isdigit((int) inst->reset[0])){
+               len = strlen(inst->reset);
                if (len == 0)
                        return -1;
-               last = data->reset[len - 1];
+               last = inst->reset[len - 1];
                if (!isalpha((int) last))
                        last = 'd';
-               num = atoi(data->reset);
+               num = atoi(inst->reset);
                DEBUG("rlm_counter: num=%d, last=%c",num,last);
        }
-       if (strcmp(data->reset, "hourly") == 0 || last == 'h') {
+       if (strcmp(inst->reset, "hourly") == 0 || last == 'h') {
                /*
                 *  Round up to the next nearest hour.
                 */
                tm->tm_hour += num;
-               data->reset_time = mktime(tm);
-       } else if (strcmp(data->reset, "daily") == 0 || last == 'd') {
+               inst->reset_time = mktime(tm);
+       } else if (strcmp(inst->reset, "daily") == 0 || last == 'd') {
                /*
                 *  Round up to the next nearest day.
                 */
                tm->tm_hour = 0;
                tm->tm_mday += num;
-               data->reset_time = mktime(tm);
-       } else if (strcmp(data->reset, "weekly") == 0 || last == 'w') {
+               inst->reset_time = mktime(tm);
+       } else if (strcmp(inst->reset, "weekly") == 0 || last == 'w') {
                /*
                 *  Round up to the next nearest week.
                 */
                tm->tm_hour = 0;
                tm->tm_mday += (7 - tm->tm_wday) +(7*(num-1));
-               data->reset_time = mktime(tm);
-       } else if (strcmp(data->reset, "monthly") == 0 || last == 'm') {
+               inst->reset_time = mktime(tm);
+       } else if (strcmp(inst->reset, "monthly") == 0 || last == 'm') {
                tm->tm_hour = 0;
                tm->tm_mday = 1;
                tm->tm_mon += num;
-               data->reset_time = mktime(tm);
-       } else if (strcmp(data->reset, "never") == 0) {
-               data->reset_time = 0;
+               inst->reset_time = mktime(tm);
+       } else if (strcmp(inst->reset, "never") == 0) {
+               inst->reset_time = 0;
        } else {
                radlog(L_ERR, "rlm_counter: Unknown reset timer \"%s\"",
-                       data->reset);
+                       inst->reset);
                return -1;
        }
 
        len = strftime(sNextTime, sizeof(sNextTime), "%Y-%m-%d %H:%M:%S", tm);
        if (len == 0) *sNextTime = '\0';
        DEBUG2("rlm_counter: Current Time: %li [%s], Next reset %li [%s]",
-               timeval, sCurrentTime, data->reset_time, sNextTime);
+               timeval, sCurrentTime, inst->reset_time, sNextTime);
 
        return ret;
 }
@@ -316,7 +324,7 @@ static int find_next_reset(rlm_counter_t *data, time_t timeval)
  */
 static int counter_instantiate(CONF_SECTION *conf, void **instance)
 {
-       rlm_counter_t *data;
+       rlm_counter_t *inst;
        DICT_ATTR *dattr;
        DICT_VALUE *dval;
        ATTR_FLAGS flags;
@@ -331,164 +339,167 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
+       inst = rad_malloc(sizeof(*inst));
+       if (!inst) {
                radlog(L_ERR, "rlm_counter: rad_malloc() failed.");
                return -1;
        }
-       memset(data, 0, sizeof(*data));
+       memset(inst, 0, sizeof(*inst));
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
-       if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
+       if (cf_section_parse(conf, inst, module_config) < 0) {
+               free(inst);
                return -1;
        }
-       cache_size = data->cache_size;
+       cache_size = inst->cache_size;
 
        /*
         *      Discover the attribute number of the key.
         */
-       if (data->key_name == NULL) {
+       if (inst->key_name == NULL) {
                radlog(L_ERR, "rlm_counter: 'key' must be set.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
-       dattr = dict_attrbyname(data->key_name);
+       dattr = dict_attrbyname(inst->key_name);
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_counter: No such attribute %s",
-                               data->key_name);
-               counter_detach(data);
+                               inst->key_name);
+               counter_detach(inst);
                return -1;
        }
-       data->key_attr = dattr->attr;
+       inst->key_attr = dattr->attr;
 
        /*
         *      Discover the attribute number of the counter.
         */
-       if (data->count_attribute == NULL) {
+       if (inst->count_attribute == NULL) {
                radlog(L_ERR, "rlm_counter: 'count-attribute' must be set.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
-       dattr = dict_attrbyname(data->count_attribute);
+       dattr = dict_attrbyname(inst->count_attribute);
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_counter: No such attribute %s",
-                               data->count_attribute);
-               counter_detach(data);
+                               inst->count_attribute);
+               counter_detach(inst);
                return -1;
        }
-       data->count_attr = dattr->attr;
+       inst->count_attr = dattr->attr;
 
        /*
         * Discover the attribute number of the reply attribute.
         */
-       if (data->reply_name != NULL) {
-               dattr = dict_attrbyname(data->reply_name);
+       if (inst->reply_name != NULL) {
+               dattr = dict_attrbyname(inst->reply_name);
                if (dattr == NULL) {
                        radlog(L_ERR, "rlm_counter: No such attribute %s",
-                                       data->reply_name);
-                       counter_detach(data);
+                                       inst->reply_name);
+                       counter_detach(inst);
                        return -1;
                }
                if (dattr->type != PW_TYPE_INTEGER) {
                        radlog(L_ERR, "rlm_counter: Reply attribute %s is not of type integer",
-                               data->reply_name);
-                       counter_detach(data);
+                               inst->reply_name);
+                       counter_detach(inst);
                        return -1;
                }
-               data->reply_attr = dattr->attr;
+               inst->reply_attr = dattr->attr;
        }
 
 
        /*
         *  Create a new attribute for the counter.
         */
-       if (data->counter_name == NULL) {
+       if (inst->counter_name == NULL) {
                radlog(L_ERR, "rlm_counter: 'counter-name' must be set.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
 
        memset(&flags, 0, sizeof(flags));
-       dict_addattr(data->counter_name, -1, 0, PW_TYPE_INTEGER, flags);
-       dattr = dict_attrbyname(data->counter_name);
+       dict_addattr(inst->counter_name, -1, 0, PW_TYPE_INTEGER, flags);
+       dattr = dict_attrbyname(inst->counter_name);
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to create counter attribute %s",
-                               data->counter_name);
-               counter_detach(data);
+                               inst->counter_name);
+               counter_detach(inst);
                return -1;
        }
-       data->dict_attr = dattr->attr;
+       inst->dict_attr = dattr->attr;
        DEBUG2("rlm_counter: Counter attribute %s is number %d",
-                       data->counter_name, data->dict_attr);
+                       inst->counter_name, inst->dict_attr);
 
        /*
         * Create a new attribute for the check item.
         */
-       if (data->check_name == NULL) {
+       if (inst->check_name == NULL) {
                radlog(L_ERR, "rlm_counter: 'check-name' must be set.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
-       dict_addattr(data->check_name, 0, PW_TYPE_INTEGER, -1, flags);
-       dattr = dict_attrbyname(data->check_name);
+       dict_addattr(inst->check_name, 0, PW_TYPE_INTEGER, -1, flags);
+       dattr = dict_attrbyname(inst->check_name);
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to create check attribute %s",
-                               data->counter_name);
-               counter_detach(data);
+                               inst->counter_name);
+               counter_detach(inst);
                return -1;
        }
-       data->check_attr = dattr->attr;
+       inst->check_attr = dattr->attr;
 
        /*
         * Find the attribute for the allowed protocol
         */
-       if (data->service_type != NULL) {
-               if ((dval = dict_valbyname(PW_SERVICE_TYPE, 0, data->service_type)) == NULL) {
+       if (inst->service_type != NULL) {
+               if ((dval = dict_valbyname(PW_SERVICE_TYPE, 0, inst->service_type)) == NULL) {
                        radlog(L_ERR, "rlm_counter: Failed to find attribute number for %s",
-                                       data->service_type);
-                       counter_detach(data);
+                                       inst->service_type);
+                       counter_detach(inst);
                        return -1;
                }
-               data->service_val = dval->value;
+               inst->service_val = dval->value;
        }
 
        /*
         * Find when to reset the database.
         */
-       if (data->reset == NULL) {
+       if (inst->reset == NULL) {
                radlog(L_ERR, "rlm_counter: 'reset' must be set.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
        now = time(NULL);
-       data->reset_time = 0;
-       data->last_reset = now;
+       inst->reset_time = 0;
+       inst->last_reset = now;
 
-       if (find_next_reset(data,now) == -1){
+       if (find_next_reset(inst,now) == -1){
                radlog(L_ERR, "rlm_counter: find_next_reset() returned -1. Exiting.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
 
-       if (data->filename == NULL) {
+       if (inst->filename == NULL) {
                radlog(L_ERR, "rlm_counter: 'filename' must be set.");
-               counter_detach(data);
+               counter_detach(inst);
                return -1;
        }
-       data->gdbm = gdbm_open(data->filename, sizeof(int),
-                       GDBM_WRCREAT | GDBM_COUNTER_OPTS, 0600, NULL);
-       if (data->gdbm == NULL) {
+       inst->gdbm = gdbm_open(inst->filename, sizeof(int),
+                              GDBM_WRCREAT | GDBM_COUNTER_OPTS,
+                              0600, NULL);
+       if (inst->gdbm == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
-                               data->filename, strerror(errno));
-               counter_detach(data);
+                               inst->filename, strerror(errno));
+               counter_detach(inst);
                return -1;
        }
-       if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
+       if (gdbm_setopt(inst->gdbm, GDBM_CACHESIZE, &cache_size,
+                       sizeof(cache_size)) == -1) {
                radlog(L_ERR, "rlm_counter: Failed to set cache size");
+       }
 
        /*
         * Look for the DEFAULT1 entry. This entry if it exists contains the
@@ -503,10 +514,10 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
         * If DEFAULT1 and DEFAULT2 do not exist (new database) we add them to the database
         */
 
-       key_datum.dptr = (char *)default1;
-       key_datum.dsize = strlen(default1);
+       memcpy(&key_datum.dptr, &default1, sizeof(key_datum.dptr));
+       key_datum.dsize = strlen(key_datum.dptr);
 
-       time_datum = gdbm_fetch(data->gdbm, key_datum);
+       time_datum = gdbm_fetch(inst->gdbm, key_datum);
        if (time_datum.dptr != NULL){
                time_t next_reset = 0;
 
@@ -515,30 +526,31 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
                time_datum.dptr = NULL;
                if (next_reset && next_reset <= now){
 
-                       data->last_reset = now;
-                       ret = reset_db(data);
+                       inst->last_reset = now;
+                       ret = reset_db(inst);
                        if (ret != RLM_MODULE_OK){
                                radlog(L_ERR, "rlm_counter: reset_db() failed");
-                               counter_detach(data);
+                               counter_detach(inst);
                                return -1;
                        }
+               } else {
+                       inst->reset_time = next_reset;
                }
-               else
-                       data->reset_time = next_reset;
-               key_datum.dptr = (char *)default2;
-               key_datum.dsize = strlen(default2);
+               
+               memcpy(&key_datum.dptr, &default2, sizeof(key_datum.dptr));
+               key_datum.dsize = strlen(key_datum.dptr);
 
-               time_datum = gdbm_fetch(data->gdbm, key_datum);
+               time_datum = gdbm_fetch(inst->gdbm, key_datum);
                if (time_datum.dptr != NULL){
-                       memcpy(&data->last_reset, time_datum.dptr, sizeof(time_t));
+                       memcpy(&inst->last_reset, time_datum.dptr, sizeof(time_t));
                        free(time_datum.dptr);
                }
        }
        else{
-               ret = add_defaults(data);
+               ret = add_defaults(inst);
                if (ret != RLM_MODULE_OK){
                        radlog(L_ERR, "rlm_counter: add_defaults() failed");
-                       counter_detach(data);
+                       counter_detach(inst);
                        return -1;
                }
        }
@@ -547,14 +559,14 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Register the counter comparison operation.
         */
-       paircompare_register(data->dict_attr, 0, counter_cmp, data);
+       paircompare_register(inst->dict_attr, 0, counter_cmp, inst);
 
        /*
         * Init the mutex
         */
-       pthread_mutex_init(&data->mutex, NULL);
+       pthread_mutex_init(&inst->mutex, NULL);
 
-       *instance = data;
+       *instance = inst;
 
        return 0;
 }
@@ -564,7 +576,7 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
  */
 static int counter_accounting(void *instance, REQUEST *request)
 {
-       rlm_counter_t *data = (rlm_counter_t *)instance;
+       rlm_counter_t *inst = instance;
        datum key_datum;
        datum count_datum;
        VALUE_PAIR *key_vp, *count_vp, *proto_vp, *uniqueid_vp;
@@ -591,27 +603,27 @@ static int counter_accounting(void *instance, REQUEST *request)
         *      Before doing anything else, see if we have to reset
         *      the counters.
         */
-       if (data->reset_time && (data->reset_time <= request->timestamp)) {
+       if (inst->reset_time && (inst->reset_time <= request->timestamp)) {
                int ret;
 
                DEBUG("rlm_counter: Time to reset the database.");
-               data->last_reset = data->reset_time;
-               find_next_reset(data,request->timestamp);
-               pthread_mutex_lock(&data->mutex);
-               ret = reset_db(data);
-               pthread_mutex_unlock(&data->mutex);
+               inst->last_reset = inst->reset_time;
+               find_next_reset(inst,request->timestamp);
+               pthread_mutex_lock(&inst->mutex);
+               ret = reset_db(inst);
+               pthread_mutex_unlock(&inst->mutex);
                if (ret != RLM_MODULE_OK)
                        return ret;
        }
        /*
         * Check if we need to watch out for a specific service-type. If yes then check it
         */
-       if (data->service_type != NULL) {
+       if (inst->service_type != NULL) {
                if ((proto_vp = pairfind(request->packet->vps, PW_SERVICE_TYPE, 0)) == NULL){
                        DEBUG("rlm_counter: Could not find Service-Type attribute in the request. Returning NOOP.");
                        return RLM_MODULE_NOOP;
                }
-               if ((unsigned)proto_vp->vp_integer != data->service_val){
+               if ((unsigned)proto_vp->vp_integer != inst->service_val){
                        DEBUG("rlm_counter: This Service-Type is not allowed. Returning NOOP.");
                        return RLM_MODULE_NOOP;
                }
@@ -623,7 +635,7 @@ static int counter_accounting(void *instance, REQUEST *request)
        key_vp = pairfind(request->packet->vps, PW_ACCT_DELAY_TIME, 0);
        if (key_vp != NULL){
                if (key_vp->vp_integer != 0 &&
-                   (request->timestamp - key_vp->vp_integer) < data->last_reset){
+                   (request->timestamp - key_vp->vp_integer) < inst->last_reset){
                        DEBUG("rlm_counter: This packet is too old. Returning NOOP.");
                        return RLM_MODULE_NOOP;
                }
@@ -635,7 +647,7 @@ static int counter_accounting(void *instance, REQUEST *request)
         *      Look for the key.  User-Name is special.  It means
         *      The REAL username, after stripping.
         */
-       key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr, 0);
+       key_vp = (inst->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, inst->key_attr, 0);
        if (key_vp == NULL){
                DEBUG("rlm_counter: Could not find the key-attribute in the request. Returning NOOP.");
                return RLM_MODULE_NOOP;
@@ -644,7 +656,7 @@ static int counter_accounting(void *instance, REQUEST *request)
        /*
         *      Look for the attribute to use as a counter.
         */
-       count_vp = pairfind(request->packet->vps, data->count_attr, 0);
+       count_vp = pairfind(request->packet->vps, inst->count_attr, 0);
        if (count_vp == NULL){
                DEBUG("rlm_counter: Could not find the count-attribute in the request.");
                return RLM_MODULE_NOOP;
@@ -654,9 +666,9 @@ static int counter_accounting(void *instance, REQUEST *request)
        key_datum.dsize = key_vp->length;
 
        DEBUG("rlm_counter: Searching the database for key '%s'",key_vp->vp_strvalue);
-       pthread_mutex_lock(&data->mutex);
-       count_datum = gdbm_fetch(data->gdbm, key_datum);
-       pthread_mutex_unlock(&data->mutex);
+       pthread_mutex_lock(&inst->mutex);
+       count_datum = gdbm_fetch(inst->gdbm, key_datum);
+       pthread_mutex_unlock(&inst->mutex);
        if (count_datum.dptr == NULL){
                DEBUG("rlm_counter: Could not find the requested key in the database.");
                counter.user_counter = 0;
@@ -684,7 +696,7 @@ static int counter_accounting(void *instance, REQUEST *request)
                DEBUG("rlm_counter: User=%s, Counter=%d.",request->username->vp_strvalue,counter.user_counter);
        }
 
-       if (data->count_attr == PW_ACCT_SESSION_TIME) {
+       if (inst->count_attr == PW_ACCT_SESSION_TIME) {
                /*
                 *      If session time < diff then the user got in after the
                 *      last reset. So add his session time, otherwise add the
@@ -695,7 +707,7 @@ static int counter_accounting(void *instance, REQUEST *request)
                 *      then we will only count one hour (the one in the new
                 *      day). That is the right thing
                 */
-               diff = request->timestamp - data->last_reset;
+               diff = request->timestamp - inst->last_reset;
                counter.user_counter += (count_vp->vp_integer < diff) ? count_vp->vp_integer : diff;
 
        } else if (count_vp->type == PW_TYPE_INTEGER) {
@@ -718,12 +730,12 @@ static int counter_accounting(void *instance, REQUEST *request)
        count_datum.dsize = sizeof(rad_counter);
 
        DEBUG("rlm_counter: Storing new value in database.");
-       pthread_mutex_lock(&data->mutex);
-       rcode = gdbm_store(data->gdbm, key_datum, count_datum, GDBM_REPLACE);
-       pthread_mutex_unlock(&data->mutex);
+       pthread_mutex_lock(&inst->mutex);
+       rcode = gdbm_store(inst->gdbm, key_datum, count_datum, GDBM_REPLACE);
+       pthread_mutex_unlock(&inst->mutex);
        if (rcode < 0) {
                radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
-                               data->filename, gdbm_strerror(gdbm_errno));
+                               inst->filename, gdbm_strerror(gdbm_errno));
                return RLM_MODULE_FAIL;
        }
        DEBUG("rlm_counter: New value stored successfully.");
@@ -739,12 +751,12 @@ static int counter_accounting(void *instance, REQUEST *request)
  */
 static int counter_authorize(void *instance, REQUEST *request)
 {
-       rlm_counter_t *data = (rlm_counter_t *) instance;
+       rlm_counter_t *inst = instance;
        int ret=RLM_MODULE_NOOP;
        datum key_datum;
        datum count_datum;
        rad_counter counter;
-       int res=0;
+       unsigned int res = 0;
        VALUE_PAIR *key_vp, *check_vp;
        VALUE_PAIR *reply_item;
        char msg[128];
@@ -757,14 +769,14 @@ static int counter_authorize(void *instance, REQUEST *request)
         *      Before doing anything else, see if we have to reset
         *      the counters.
         */
-       if (data->reset_time && (data->reset_time <= request->timestamp)) {
+       if (inst->reset_time && (inst->reset_time <= request->timestamp)) {
                int ret2;
 
-               data->last_reset = data->reset_time;
-               find_next_reset(data,request->timestamp);
-               pthread_mutex_lock(&data->mutex);
-               ret2 = reset_db(data);
-               pthread_mutex_unlock(&data->mutex);
+               inst->last_reset = inst->reset_time;
+               find_next_reset(inst,request->timestamp);
+               pthread_mutex_lock(&inst->mutex);
+               ret2 = reset_db(inst);
+               pthread_mutex_unlock(&inst->mutex);
                if (ret2 != RLM_MODULE_OK)
                        return ret2;
        }
@@ -775,7 +787,7 @@ static int counter_authorize(void *instance, REQUEST *request)
         *      The REAL username, after stripping.
         */
        DEBUG2("rlm_counter: Entering module authorize code");
-       key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr, 0);
+       key_vp = (inst->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, inst->key_attr, 0);
        if (key_vp == NULL) {
                DEBUG2("rlm_counter: Could not find Key value pair");
                return ret;
@@ -784,7 +796,7 @@ static int counter_authorize(void *instance, REQUEST *request)
        /*
         *      Look for the check item
         */
-       if ((check_vp= pairfind(request->config_items, data->check_attr, 0)) == NULL) {
+       if ((check_vp= pairfind(request->config_items, inst->check_attr, 0)) == NULL) {
                DEBUG2("rlm_counter: Could not find Check item value pair");
                return ret;
        }
@@ -800,9 +812,9 @@ static int counter_authorize(void *instance, REQUEST *request)
        counter.user_counter = 0;
 
        DEBUG("rlm_counter: Searching the database for key '%s'",key_vp->vp_strvalue);
-       pthread_mutex_lock(&data->mutex);
-       count_datum = gdbm_fetch(data->gdbm, key_datum);
-       pthread_mutex_unlock(&data->mutex);
+       pthread_mutex_lock(&inst->mutex);
+       count_datum = gdbm_fetch(inst->gdbm, key_datum);
+       pthread_mutex_unlock(&inst->mutex);
        if (count_datum.dptr != NULL){
                DEBUG("rlm_counter: Key Found.");
                memcpy(&counter, count_datum.dptr, sizeof(rad_counter));
@@ -818,7 +830,7 @@ static int counter_authorize(void *instance, REQUEST *request)
        res=check_vp->vp_integer - counter.user_counter;
        if (res > 0) {
                DEBUG("rlm_counter: res is greater than zero");
-               if (data->count_attr == PW_ACCT_SESSION_TIME) {
+               if (inst->count_attr == PW_ACCT_SESSION_TIME) {
                        /*
                         * Do the following only if the count attribute is
                         * AcctSessionTime
@@ -843,27 +855,26 @@ static int counter_authorize(void *instance, REQUEST *request)
                        *       Before that set the return value to the time
                        *       remaining to next reset
                        */
-                       if (data->reset_time && (
-                               res >= (data->reset_time - request->timestamp))) {
-                               res = data->reset_time - request->timestamp;
+                       if (inst->reset_time && (
+                               res >= (inst->reset_time - request->timestamp))) {
+                               res = inst->reset_time - request->timestamp;
                                res += check_vp->vp_integer;
                        }
 
-                       if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT, 0)) != NULL) {
-                               if (reply_item->vp_integer > res)
-                                       reply_item->vp_integer = res;
+                       reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT, 0);
+                       if (reply_item && (reply_item->vp_integer > res)) {
+                               reply_item->vp_integer = res;
                        } else {
                                reply_item = radius_paircreate(request, &request->reply->vps, PW_SESSION_TIMEOUT, 0, PW_TYPE_INTEGER);
                                reply_item->vp_integer = res;
                        }
                }
-               else if (data->reply_attr) {
-                       if ((reply_item = pairfind(request->reply->vps, data->reply_attr, 0)) != NULL) {
-                               if (reply_item->vp_integer > res)
-                                       reply_item->vp_integer = res;
-                       }
-                       else {
-                               reply_item = radius_paircreate(request, &request->reply->vps, data->reply_attr, 0, PW_TYPE_INTEGER);
+               else if (inst->reply_attr) {
+                       reply_item = pairfind(request->reply->vps, inst->reply_attr, 0);
+                       if (reply_item && (reply_item->vp_integer > res)) {
+                               reply_item->vp_integer = res;
+                       } else {
+                               reply_item = radius_paircreate(request, &request->reply->vps, inst->reply_attr, 0, PW_TYPE_INTEGER);
                                reply_item->vp_integer = res;
                        }
                }
@@ -883,11 +894,11 @@ static int counter_authorize(void *instance, REQUEST *request)
                /*
                 * User is denied access, send back a reply message
                */
-               sprintf(msg, "Your maximum %s usage time has been reached", data->reset);
+               sprintf(msg, "Your maximum %s usage time has been reached", inst->reset);
                reply_item=pairmake("Reply-Message", msg, T_OP_EQ);
                pairadd(&request->reply->vps, reply_item);
 
-               snprintf(module_fmsg,sizeof(module_fmsg), "rlm_counter: Maximum %s usage time reached", data->reset);
+               snprintf(module_fmsg,sizeof(module_fmsg), "rlm_counter: Maximum %s usage time reached", inst->reset);
                module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
                pairadd(&request->packet->vps, module_fmsg_vp);
 
@@ -902,12 +913,14 @@ static int counter_authorize(void *instance, REQUEST *request)
 
 static int counter_detach(void *instance)
 {
-       rlm_counter_t *data = (rlm_counter_t *) instance;
+       rlm_counter_t *inst = instance;
 
-       paircompare_unregister(data->dict_attr, counter_cmp);
-       if (data->gdbm)
-               gdbm_close(data->gdbm);
-       pthread_mutex_destroy(&data->mutex);
+       paircompare_unregister(inst->dict_attr, counter_cmp);
+       if (inst->gdbm) {
+               gdbm_close(inst->gdbm);
+       }
+       
+       pthread_mutex_destroy(&inst->mutex);
 
        free(instance);
        return 0;