-
- while(p)
- {
- log(LOG_DEBUG,"resolving host %s", p->name);
-
- bzero(&p->dest,sizeof(p->dest));
- p->dest.sin_family=AF_INET;
- if((p->dest.sin_addr.s_addr=gethostaddr(p->name))<=0)
- {
- log(LOG_ERR,"Can't resolve host. Skipping client %s.",p->name);
- p->socket=-1;
- } else
- {
- if((p->socket=socket(AF_INET,SOCK_RAW,proto->p_proto))<0)
- {
- log(LOG_ERR,"Can't create socket. Skipping client %s.",p->name);
- p->socket=-1;
- } else
- {
- if(ok==0)
- send_delay = p->ping_interval;
- else
- send_delay = gcd(send_delay, p->ping_interval);
- ok++;
- }
+
+ struct host_entry * host_list_end = NULL;
+ for (int i=0; i < host_count; i++) {
+ /* Allocate a reusable buffer large enough to hold the full 'section:key' string. */
+ int section_len = strlen(iniparser_getsecname(conf, i));
+ char * key_buf = malloc(section_len + 1 + MAX_CONF_KEY_LEN + 1); /* +1 for ':' and '\0' */
+ strcpy(key_buf, iniparser_getsecname(conf, i));
+ key_buf[section_len++] = ':';
+
+ struct host_entry * cur_host = malloc(sizeof(struct host_entry));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "host", MAX_CONF_KEY_LEN);
+ cur_host->name = strdup(iniparser_getstring(conf, key_buf, NULL));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "interval", MAX_CONF_KEY_LEN);
+ cur_host->ping_interval = iniparser_getint(conf, key_buf, -1);
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "max_delay", MAX_CONF_KEY_LEN);
+ cur_host->max_delay = iniparser_getint(conf, key_buf, -1);
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "up_cmd", MAX_CONF_KEY_LEN);
+ cur_host->up_cmd = strdup(iniparser_getstring(conf, key_buf, NULL));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "down_cmd", MAX_CONF_KEY_LEN);
+ cur_host->down_cmd = strdup(iniparser_getstring(conf, key_buf, NULL));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "start_condition", MAX_CONF_KEY_LEN);
+ const char * value = iniparser_getstring(conf, key_buf, NULL);
+ if (value) cur_host->host_up = *value == 'u' ? true : false;
+
+ if (cur_host->name == NULL || cur_host->ping_interval == -1 || cur_host->max_delay == -1) {
+ fprintf(stderr, "ERROR: Problems parsing section %s.\n", iniparser_getsecname(conf, i));
+ exit(EXIT_FAILURE);