All pastes #926161 Raw Edit

Mine

public text v1 · immutable
·published 2008-03-03 16:20 UTC
rendered paste body
#include <sys/socket.h>       	/*  socket definitions        */
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>       	/*  socket types              */
#include <arpa/inet.h>        	/*  inet (3) funtions         */
#include <unistd.h>           	/*  misc. UNIX functions      */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <signal.h>
#include <pthread.h>
#include <mysql/mysql.h>

/* Helper Functions */
#include "helper.h"

/* Configuration */
#include "../c.config/database.h"
#include "../c.config/errormsg.h"

/*  Standard Configuration 	*/
#define	BLISTENER_VERSION		"1.1.2"
#define BLISTENER_PORT          	(10100)
#define MAX_LINE           	(512)

void * tcp_service(void *arg)
{
	MYSQL mysql;
	MYSQL_RES *res;
    	MYSQL_ROW row;
	pthread_t   tid;
	int conn_s = (int)arg;
	char        buffer[MAX_LINE], sqlquery[MAX_LINE];
	pthread_detach(tid);

	/* Initialize MySQL code */
	if( !mysql_init(&mysql) ) {
	   fprintf(stderr, "bListener.(Thread).(MySQL): %s\n", ERR_MYSQL_NOINIT);
	   exit(EXIT_FAILURE);
	} else {
        if( !mysql_real_connect(&mysql,CONF_MYSQL_HOSTNAME,CONF_MYSQL_USERNAME,CONF_MYSQL_PASSWORD,CONF_MYSQL_DATABASE,0,NULL,0) ){
                fprintf(stderr, "bListener.(Thread).(MySQL): %s\n", ERR_MYSQL_NOCONNECT);
                exit(EXIT_FAILURE);
        } else {
                fprintf(stdout,"bListener.(Thread).(MySQL): Connected to %s, Database: %s\n",CONF_MYSQL_HOSTNAME,CONF_MYSQL_DATABASE);
	    }
	}
        fprintf(stdout, "bListener.(Thread).(ClientConn): Connection Opened\n");
        while(Readline(conn_s, buffer, MAX_LINE-1)) {

                sprintf(sqlquery, "INSERT INTO `%s`.`datalog` VALUES (%s,NULL);", CONF_MYSQL_DATABASE, buffer);
                if (mysql_query(&mysql, sqlquery)) {
                      fprintf(stderr, "bListener.(Thread).(MySQL): %s\n -- %s\n", mysql_error(&mysql), sqlquery);
                } else {
                      fprintf(stdout, "bListener.(Thread).(MySQL): Data successfully entered into realtime database\n");
                }
                Writeline(conn_s, "220- RECV\n", strlen("220- RECV\n"));
        }
        fprintf(stdout, "bListener.(Thread).(ClientConn): Connection Closed\n");
	mysql_close(&mysql);
	fprintf(stderr, "bListener.(Thread).(MySQL): Disconnected from %s, Database: %s\n", CONF_MYSQL_HOSTNAME,CONF_MYSQL_DATABASE);
        if ( close(conn_s) < 0 ) {
            fprintf(stderr, "bListener.(Thread).(ClientConn): %s\n",ERR_UNABLE_TO_CLOSE);
            exit(EXIT_FAILURE);
        }
	fprintf(stdout, "---------------------------------------------------------------------------\n");
	pthread_exit(NULL);
}

void sighandle(int signo) {
	if ((signo==SIGTERM) || (signo==SIGINT)) {
		exit(0);
	}
	if (signo == SIGHUP) {
	}
	if (signo == SIGALRM) {
	}
}	

int main(int argc, char **argv) {
    int       	list_s;                /*  listening socket          */
    int       	conn_s;                /*  connection socket         */
    struct    	sockaddr_in servaddr;  /*  socket address structure  */
    pthread_t 	tid;
    int yes = 1, err;

    fprintf(stdout, "bListener.(System): Initialising...\n");

    if (signal(SIGPIPE, sighandle) == SIG_ERR)
        fprintf(stderr, "bListener.(System): Can't catch SIGPIPE signals\n");
    if (signal(SIGTERM, sighandle) == SIG_ERR)
        fprintf(stderr, "bListener.(System): Can't catch SIGTERM signals\n");
    if (signal(SIGINT, sighandle) == SIG_ERR)
        fprintf(stderr, "bListener.(System): Can't catch SIGINT signals\n");
    if (signal(SIGALRM, sighandle) == SIG_ERR)
        fprintf(stderr, "bListener.(System): Can't catch SIGALRM signals\n");
    if (signal(SIGHUP, sighandle) == SIG_ERR)
        fprintf(stderr, "bListener.(System): Can't catch SIGHUP signals\n");

    /*  Create the listening socket  */
    if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
	fprintf(stderr, "bListener.(TCP): %s",ERR_UNABLE_TO_MKSOCK);
	exit(EXIT_FAILURE);
    }

    /*  Set all bytes in socket address structure to zero, and fill in the relevant data members   */

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family      = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port        = htons(BLISTENER_PORT);

    /*  Bind our socket addresss to the listening socket, and call listen()  */
    if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
	fprintf(stderr, "bListener.(TCP): %s\n", ERR_UNABLE_TO_BIND);
	exit(EXIT_FAILURE);
    }
    if ( listen(list_s, LISTENQ) < 0 ) {
	fprintf(stderr, "bListener.(TCP): %s\n", ERR_UNABLE_TO_LISTEN);
	exit(EXIT_FAILURE);
    }

    /* If we have gotten to this point, I think we have initialised everything! */
    fprintf(stdout, "bListener.(System): Initialised...\n");

    /*  Enter an infinite loop to respond to client requests */
    while ( 1 ) {
	if ( (conn_s = accept(list_s, NULL, NULL) ) < 0 ) {
	    fprintf(stderr, "bListener.(TCP): %s\n", ERR_UNABLE_TO_ACCEPT);
	    exit(EXIT_FAILURE);
	}
    	err = pthread_create(&tid, NULL, tcp_service, (void *)conn_s);
	if (err != 0)
	{
		fprintf(stderr, "bListener.(Thread): Error Starting Thread, Message: %d\n", err);
	}
    }
    exit(EXIT_SUCCESS);
}