<commit_before>/*
 * $Id$
 *
 *  captagent - Homer capture agent. Modular
 *  Duplicate SIP messages in Homer Encapulate Protocol [HEP] [ipv6 version]
 *
 *  Author: <NAME> <<EMAIL>>
 *  (C) Homer Project 2012 (http://www.sipcapture.org)
 *
 * Homer capture agent is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version
 *
 * Homer capture agent is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif /* __FAVOR_BSD */

#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>

#ifdef USE_IPV6
#include <netinet/ip6.h>
#endif /* USE_IPV6 */

#include "src/api.h"
#include "capt_cli.h"

pthread_t thread;


int unload_module(void)
{
        printf("unloaded module capt_cli\n");
        
	/* Close socket */
	if(server_sock) close(server_sock);

        return 0;
}

int wait_connect (void) {

	int  client_sock;

	while( (client_sock = accept(server_sock, NULL, NULL)) ) {
	        if( pthread_create( &thread , NULL ,  read_clisocket , (void*) client_sock) < 0) {
			perror("could not create thread");
		        return 1;
	        }         
	}
	
	if(server_sock) close(server_sock);
	return 1;
}


void *read_clisocket(void *client){
    char buffer[MAX_LINE]; 
    char *message = "\r\nWelcome to CLI of Captagent\r\n";
    int ret;
    int mysocket = (int*) client;
    char *command;

    write_line(mysocket, message, strlen(message));
    
    while (1){
    
        write_line(mysocket, "\r\n>", strlen(">\r\n"));	
	ret = read_line(mysocket, buffer, MAX_LINE-1);
	if(ret == 0) return 0;

	/* SIMPLE INTERFACE */
	if(!strncmp(buffer,"quit",4)) {	
            write_line(mysocket, "Bye...\r\n", 9);	
            close(mysocket);
            return 0;
	}	
	else if(!strncmp(buffer,"stats",5)) {	

	    //message = get_basestat("core_hep");	
	    if(!strncmp(buffer,"stats hep",7)) {	
        	command = "core_hep";	        
	    }
	    else {
                command = "all";
	    }	

            message = get_basestat(command);		            
	    if(message == NULL) message = "No stats";        
	    write_line(mysocket, message, strlen(message));
	}	

	else {
	    message = "help - available commands\r\n" \
	              "quit - exit from CLI\r\n" \
	              "stats hep - show HEP statistics\r\n" \
	              "stats - show all statistics\r\n";
            write_line(mysocket, message, strlen(message));	
	}

	//write_line(mysocket, buffer, strlen(buffer));

    }
    return 0;
}


int load_module(xml_node *config)
{
	xml_node *modules;
        //struct addrinfo *ai, hints[1] = {{ 0 }};
        struct addrinfo hints[1] = {{ 0 }};
        char *key, *value;
        int s;

	/* READ CONFIG */
	modules = config;

	while(1) {
        	if(modules ==  NULL) break;
                modules = xml_get("param", modules, 1 );
                if(modules->attr[0] != NULL && modules->attr[2] != NULL) {
                        
                        /* bad parser */
                        if(strncmp(modules->attr[2], "value", 5) || strncmp(modules->attr[0], "name", 4)) {                        
                            fprintf(stderr, "bad keys in the config\n");
                            goto next;
                        
                        }

                        key =  modules->attr[1];
                        value = modules->attr[3];
                        
                        if(key == NULL || value == NULL) {
                            fprintf(stderr, "bad values in the config\n");
                            goto next;                        
                        
                        }                        

                        if(!strncmp(key, "cli-host", 8)) cli_host = value;
                        else if(!strncmp(key, "cli-port", 8)) cli_port = value;
                        else if(!strncmp(key, "cli-password", 13)) cli_password = value;                                	                	                	
		}
next:		
		
                modules = modules->next;
	}

                                           
        hints->ai_flags = AI_NUMERICSERV;
        hints->ai_family = AF_UNSPEC;
        hints->ai_socktype = SOCK_STREAM;
        hints->ai_protocol = IPPROTO_TCP;

        if ((s = getaddrinfo(cli_host, cli_port, hints, &ai)) != 0) {            
            fprintf(stderr, "capture: getaddrinfo: %s\n", gai_strerror(s));
            return 2;
        }

        if(init_clisocket()) {
              fprintf(stderr,"capture: couldn't init socket\r\n");              
              return 2;            
      	}

         printf("Loaded capt_cli\n");
         
	       if(wait_connect()){
              fprintf(stderr,"something wrong with cli socket\r\n");              
              return 3;
         }
        
        return 0;
}

int init_clisocket (void) {

    unsigned int on = 1;
    if(server_sock) close(server_sock);

    server_sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (server_sock < 0) {
             fprintf(stderr,"Sender socket creation failed: %s\n", strerror(errno));
             return 1;
    }    

    if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)<commit_msg>fixed load_module.<commit_after>/*
 * $Id$
 *
 *  captagent - Homer capture agent. Modular
 *  Duplicate SIP messages in Homer Encapulate Protocol [HEP] [ipv6 version]
 *
 *  Author: <NAME> <<EMAIL>>
 *  (C) Homer Project 2012 (http://www.sipcapture.org)
 *
 * Homer capture agent is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version
 *
 * Homer capture agent is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif /* __FAVOR_BSD */

#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>

#ifdef USE_IPV6
#include <netinet/ip6.h>
#endif /* USE_IPV6 */

#include "src/api.h"
#include "capt_cli.h"

pthread_t thread;


int unload_module(void)
{
        printf("unloaded module capt_cli\n");
        
	/* Close socket */
	if(server_sock) close(server_sock);

        return 0;
}

int wait_connect (void) {

	int  client_sock;

	while( (client_sock = accept(server_sock, NULL, NULL)) ) {
	        if( pthread_create( &thread , NULL ,  read_clisocket , (void*) client_sock) < 0) {
			perror("could not create thread");
		        return 1;
	        }         
	}
	
	if(server_sock) close(server_sock);
	return 1;
}


void *read_clisocket(void *client){
    char buffer[MAX_LINE]; 
    char *message = "\r\nWelcome to CLI of Captagent\r\n";
    int ret;
    int mysocket = (int*) client;
    char *command;

    write_line(mysocket, message, strlen(message));
    
    while (1){
    
        write_line(mysocket, "\r\n>", strlen(">\r\n"));	
	ret = read_line(mysocket, buffer, MAX_LINE-1);
	if(ret == 0) return 0;

	/* SIMPLE INTERFACE */
	if(!strncmp(buffer,"quit",4)) {	
            write_line(mysocket, "Bye...\r\n", 9);	
            close(mysocket);
            return 0;
	}	
	else if(!strncmp(buffer,"stats",5)) {	

	    //message = get_basestat("core_hep");	
	    if(!strncmp(buffer,"stats hep",7)) {	
        	command = "core_hep";	        
	    }
	    else {
                command = "all";
	    }	

            message = get_basestat(command);		            
	    if(message == NULL) message = "No stats";        
	    write_line(mysocket, message, strlen(message));
	}	

	else {
	    message = "help - available commands\r\n" \
	              "quit - exit from CLI\r\n" \
	              "stats hep - show HEP statistics\r\n" \
	              "stats - show all statistics\r\n";
            write_line(mysocket, message, strlen(message));	
	}

	//write_line(mysocket, buffer, strlen(buffer));

    }
    return 0;
}


int load_module(xml_node *config)
{
	xml_node *modules;
        //struct addrinfo *ai, hints[1] = {{ 0 }};
        struct addrinfo hints[1] = {{ 0 }};
        char *key, *value;
        int s;

   printf("Loaded capt_cli\n");
	 /* READ CONFIG */
	 modules = config;

	while(1) {
        	if(modules ==  NULL) break;
                modules = xml_get("param", modules, 1 );
                if(modules->attr[0] != NULL && modules->attr[2] != NULL) {
                        
                        /* bad parser */
                        if(strncmp(modules->attr[2], "value", 5) || strncmp(modules->attr[0], "name", 4)) {                        
                            fprintf(stderr, "bad keys in the config\n");
                            goto next;
                        
                        }

                        key =  modules->attr[1];
                        value = modules->attr[3];
                        
                        if(key == NULL || value == NULL) {
                            fprintf(stderr, "bad values in the config\n");
                            goto next;                        
                        
                        }                        

                        if(!strncmp(key, "cli-host", 8)) cli_host = value;
                        else if(!strncmp(key, "cli-port", 8)) cli_port = value;
                        else if(!strncmp(key, "cli-password", 13)) cli_password = value;                                	                	                	
		}
next:		
		
                modules = modules->next;
	}

                                           
        hints->ai_flags = AI_NUMERICSERV;
        hints->ai_family = AF_UNSPEC;
        hints->ai_socktype = SOCK_STREAM;
        hints->ai_protocol = IPPROTO_TCP;

        if ((s = getaddrinfo(cli_host, cli_port, hints, &ai)) != 0) {            
            fprintf(stderr, "capture: getaddrinfo: %s\n", gai_strerror(s));
            return 2;
        }

        if(init_clisocket()) {
              fprintf(stderr,"capture: couldn't init socket\r\n");              
              return 2;            
      	}        
         
	       if(wait_connect()){
              fprintf(stderr,"something wrong with cli socket\r\n");              
              return 3;
         }
        
        return 0;
}

int init_clisocket (void) {

    unsigned int on = 1;
    if(server_sock) close(server_sock);

    server_sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (server_sock < 0) {
             fprintf(stderr,"Sender socket creation failed: %s\n", strerror(errno));
             return 1;
    }    

    if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
{'fraction_non_alphanumeric': 0.12473312530793233, 'fraction_numerical': 0.010510757102972574, 'mean_word_length': 1.780593607305936, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3201807', 'n_tokens_mistral': 3998, 'n_tokens_neox': 3713, 'n_words': 968}