src/mn-slaved/client.c
struct client*
nol_s_client_create(const char *addr, const char *user)
{
    struct client* cl;
    time_t now;
    int    a, x;
    char  *p;
    char   q[128];
    int    qlen;

    MYSQL_RES *res;
    MYSQL_ROW *row;

    if ((cl = calloc(1, sizeof(struct client)))) {
        if ((cl->addr.s_addr = inet_addr(addr)) == (in_addr_t)-1
                || !(cl->user = strdup(user))) {
            syslog(LOG_ERR, "invalid address/user");
            free(cl);
            return 0;
        }
        qlen = sprintf(q,
                "SELECT SHA1(CONCAT(CONCAT(CONCAT('%s', NOW()), RAND()), RAND()))",
                addr);
        if (mysql_real_query(srv.mysql, q, qlen) != 0
                || !(res = mysql_store_result(srv.mysql))
                || !(mysql_num_rows(res))
                || !(row = mysql_fetch_row(res))
                ) {
            free(cl);
            return 0;
        }
        if (row[0])
            memcpy(cl->token, row[0], 40);
        else
            return 0;
        cl->token[40] = (char)0;

        mysql_free_result(res);

        /** 
         * add the client to the _client table which will give us an 
         * integer identifier to be used when adding URLs to the log
         * and save target urls
         **/
        qlen = sprintf(q,
                "INSERT INTO nol_client (token) VALUES ('%.40s');",
                cl->token);
        if (mysql_real_query(srv.mysql, q, qlen) != 0) {
            free(cl);
            return 0;
        }
        cl->id = (long)mysql_insert_id(srv.mysql);
        cl->session_id = 0;

        syslog(LOG_INFO,"new client %d:'%.6s...' from %s",
                (int)cl->id, cl->token, addr);
    }

    return cl;
}
/** 
 * Create a new client with a random login-token
 *
 * the token is a 40-byte string representation
 * of a SHA1 hash generated by an SQL query on
 * some random values and the IP-address 
 * concatenated.
 *
 * addr should be the clients IP-address
 **/
Create a new client with a random login-token
the token is a 40-byte string representation
of a SHA1 hash generated by an SQL query on
some random values and the IP-address
concatenated.

addr should be the clients IP-address
Create a new client with a random login - token the token is a 40 - byte string representation of a SHA1 hash generated by an SQL query on some random values and the IP - address concatenated . addr should be the clients IP - address
struct client*
nol_s_client_create(const char *addr, const char *user)
{
    struct client* cl;
    time_t now;
    int    a, x;
    char  *p;
    char   q[128];
    int    qlen;
    MYSQL_RES *res;
    MYSQL_ROW *row;
    if ((cl = calloc(1, sizeof(struct client)))) {
        if ((cl->addr.s_addr = inet_addr(addr)) == (in_addr_t)-1
                || !(cl->user = strdup(user))) {
            syslog(LOG_ERR, "invalid address/user");
            free(cl);
            return 0;
        }
        qlen = sprintf(q,
                "SELECT SHA1(CONCAT(CONCAT(CONCAT('%s', NOW()), RAND()), RAND()))",
                addr);
        if (mysql_real_query(srv.mysql, q, qlen) != 0
                || !(res = mysql_store_result(srv.mysql))
                || !(mysql_num_rows(res))
                || !(row = mysql_fetch_row(res))
                ) {
            free(cl);
            return 0;
        }
        if (row[0])
            memcpy(cl->token, row[0], 40);
        else
            return 0;
        cl->token[40] = (char)0;
        mysql_free_result(res);
        qlen = sprintf(q,
                "INSERT INTO nol_client (token) VALUES ('%.40s');",
                cl->token);
        if (mysql_real_query(srv.mysql, q, qlen) != 0) {
            free(cl);
            return 0;
        }
        cl->id = (long)mysql_insert_id(srv.mysql);
        cl->session_id = 0;
        syslog(LOG_INFO,"new client %d:'%.6s...' from %s",
                (int)cl->id, cl->token, addr);
    }
    return cl;
}
struct client * nol_s_client_create ( const char * addr , const char * user ) { struct client * cl ; time_t now ; int a , x ; char * p ; char q [ 128 ] ; int qlen ; MYSQL_RES * res ; MYSQL_ROW * row ; if ( ( cl = calloc ( 1 , sizeof ( struct client ) ) ) ) { if ( ( cl -> addr . s_addr = inet_addr ( addr ) ) == ( in_addr_t ) - 1 || ! ( cl -> user = strdup ( user ) ) ) { syslog ( LOG_ERR , " " ) ; free ( cl ) ; return 0 ; } qlen = sprintf ( q , " " , addr ) ; if ( mysql_real_query ( srv . mysql , q , qlen ) != 0 || ! ( res = mysql_store_result ( srv . mysql ) ) || ! ( mysql_num_rows ( res ) ) || ! ( row = mysql_fetch_row ( res ) ) ) { free ( cl ) ; return 0 ; } if ( row [ 0 ] ) memcpy ( cl -> token , row [ 0 ] , 40 ) ; else return 0 ; cl -> token [ 40 ] = ( char ) 0 ; mysql_free_result ( res ) ; qlen = sprintf ( q , " " , cl -> token ) ; if ( mysql_real_query ( srv . mysql , q , qlen ) != 0 ) { free ( cl ) ; return 0 ; } cl -> id = ( long ) mysql_insert_id ( srv . mysql ) ; cl -> session_id = 0 ; syslog ( LOG_INFO , " " , ( int ) cl -> id , cl -> token , addr ) ; } return cl ; }
Create a new client with a random login-token
 the token is a 40-byte string representation
 of a SHA1 hash generated by an SQL query on
 some random values and the IP-address
 concatenated.
Create a new client with a random login - token the token is a 40 - byte string representation of a SHA1 hash generated by an SQL query on some random values and the IP - address concatenated .
/** 
         * add the client to the _client table which will give us an 
         * integer identifier to be used when adding URLs to the log
         * and save target urls
         **/