<commit_before>/*
 * =====================================================================================
 *
 *       Filename:  server.c
 *
 *    Description:  Get an IP address and update the server with it.
 *
 *        Created:  07/10/15 20:53:20
 *       Compiler:  gcc
 *
 *         Author:  <NAME> (<EMAIL>), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <malloc.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

#include <curl/curl.h>

#include "utils.h"

static const char my_ip_url[] = "http://ipinfo.io/ip";

struct config
{
    char *service;
    char *domain;
    char *hash;
    long unsigned int update_interval;
};
static struct config cg;

struct string
{
    char *ptr;
    size_t len;

};

void url_upload(struct string *ip)
{
    CURL *curl;
    CURLcode response;
    char post_req[128];

    strcpy(post_req, "ip=");
    strcat(post_req, ip->ptr);
    strcat(post_req, "&domain=");
    strcat(post_req, cg.domain);
    strcat(post_req, "&hash=");
    strcat(post_req, cg.hash);

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, cg.service);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_req);
        response = curl_easy_perform(curl);

        if(response != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(response));
        curl_easy_cleanup(curl);
    }
}

void init_string(struct string *s) {
    s->len = 0;
    s->ptr = malloc(s->len+1);
    if (s->ptr == NULL) {
        fprintf(stderr, "malloc() failed\n");
        exit(EXIT_FAILURE);

    }
    s->ptr[0] = '\0';
}

size_t callback(void *ptr, size_t size, size_t nmemb, struct string *s)
{
    size_t new_len = s->len + size*nmemb;
    s->ptr = realloc(s->ptr, new_len+1);
    if (s->ptr == NULL) {
        fprintf(stderr, "realloc() failed\n");
        exit(EXIT_FAILURE);

    }
    memcpy(s->ptr+s->len, ptr, size*nmemb);
    s->ptr[new_len] = '\0';
    s->len = new_len;

    return size*nmemb;
}

struct string ip_address()
{
    CURL *curl;
    CURLcode response;
    struct string url_data;

    curl = curl_easy_init();

    if (curl) {
        init_string(&url_data);
        curl_easy_setopt(curl, CURLOPT_URL, my_ip_url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &url_data);

        response = curl_easy_perform(curl);

        if (response != CURLE_OK) {
            printf("something went wrong with curl...");
            exit(EXIT_FAILURE);
        }

        curl_easy_cleanup(curl);
    }

    return url_data;
}

void update()
{
    struct string ip = ip_address();
    str_trim_right(&ip.ptr);

    url_upload(&ip);

    free(ip.ptr);
}

void *update_thread()
{
    while (1)
    {
        sleep(cg.update_interval);
        update();
    }
}

void set_config(const char *c_path)
{<commit_msg>real fix for issue #1<commit_after>/*
 * =====================================================================================
 *
 *       Filename:  server.c
 *
 *    Description:  Get an IP address and update the server with it.
 *
 *        Created:  07/10/15 20:53:20
 *       Compiler:  gcc
 *
 *         Author:  <NAME> (<EMAIL>), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <malloc.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

#include <curl/curl.h>

#include "utils.h"

static const char my_ip_url[] = "http://ipinfo.io/ip";

struct config
{
    char *service;
    char *domain;
    char *hash;
    long unsigned int update_interval;
};
static struct config cg;

struct string
{
    char *ptr;
    size_t len;

};

void url_upload(struct string *ip)
{
    CURL *curl;
    CURLcode response;
    char post_req[128];

    strcpy(post_req, "ip=");
    strcat(post_req, ip->ptr);
    strcat(post_req, "&domain=");
    strcat(post_req, cg.domain);
    strcat(post_req, "&hash=");
    strcat(post_req, cg.hash);

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, cg.service);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_req);
        response = curl_easy_perform(curl);

        if(response != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(response));
        curl_easy_cleanup(curl);
    }
}

void init_string(struct string *s) {
    s->len = 0;
    s->ptr = malloc(s->len+1);
    if (s->ptr == NULL) {
        fprintf(stderr, "malloc() failed\n");
        exit(EXIT_FAILURE);

    }
    s->ptr[0] = '\0';
}

size_t callback(void *ptr, size_t size, size_t nmemb, struct string *s)
{
    size_t new_len = s->len + size*nmemb;
    s->ptr = realloc(s->ptr, new_len+1);
    if (s->ptr == NULL) {
        fprintf(stderr, "realloc() failed\n");
        exit(EXIT_FAILURE);

    }
    memcpy(s->ptr+s->len, ptr, size*nmemb);
    s->ptr[new_len] = '\0';
    s->len = new_len;

    return size*nmemb;
}

struct string ip_address()
{
    CURL *curl;
    CURLcode response;
    struct string url_data;

    curl = curl_easy_init();

    if (curl) {
        init_string(&url_data);
        curl_easy_setopt(curl, CURLOPT_URL, my_ip_url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &url_data);

        response = curl_easy_perform(curl);

        if (response != CURLE_OK) {
            printf("something went wrong with curl...");
            exit(EXIT_FAILURE);
        }

        curl_easy_cleanup(curl);
    }

    return url_data;
}

void update()
{
    struct string ip = ip_address();
    str_trim_right(&ip.ptr);

    url_upload(&ip);

    free(ip.ptr);
}

void *update_thread()
{
    while (1)
    {
        update();
        sleep(cg.update_interval);
    }
}

void set_config(const char *c_path)
{
{'fraction_non_alphanumeric': 0.19415481832543444, 'fraction_numerical': 0.0071090047393364926, 'mean_word_length': 2.858013406459476, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7504860', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2233, 'n_words': 456}