Ejemplo n.º 1
0
    void RpcProtocolServer::HandleRequest(const std::string& request,
                                          std::string& retValue, bool fixParams)
    {

        Json::Reader reader;
        Json::Value req;
        Json::Value response, resp;
        Json::FastWriter w;

        std::string errorMessage;

        try {
			if (reader.parse(request, req, false))
			{
				//It could be a Batch Request
				if (req.isArray())
				{
					this->HandleBatchRequest(req, response);
				} //It could be a simple Request
				else if (req.isObject())
				{
					this->HandleSingleRequest(req, response, fixParams);
				}
			}
			else
			{
				response = Errors::GetErrorBlock(Json::nullValue, Errors::ERROR_RPC_JSON_PARSE_ERROR);
			}
			retValue = w.write(response);

    	} catch (Poco::Exception & e){
			errorMessage = e.displayText();
			std::cout << "Exception: " << e.displayText() << std::endl;
			LNOTICE("JSON") << "Exception: " << e.displayText() << LE;
		} catch (std::exception & e){
			std::cout << "Exception: " << e.what() << std::endl;
			errorMessage = e.what();
			LNOTICE("JSON") << "Exception: " << e.what() << LE;
		} catch (...){
			std::cout << "Exception: ??" << std::endl;
			LNOTICE("JSON") << "Exception: ???"  << LE;
			errorMessage = "Unknown error";
		}

		if (!errorMessage.empty()){
			response = Errors::GetCustomErrorBlock(req, -30000, errorMessage);
			retValue = w.write(response);
		}

    }
Ejemplo n.º 2
0
static int unload_module(void) {
	unsigned int i = 0;

	LNOTICE("unloaded module %s", module_name);

	for (i = 0; i < profile_size; i++) {

		if(sniffer_proto[i]) {
  		    pcap_breakloop(sniffer_proto[i]);
  		    pthread_join(call_thread[i],NULL);
		}

		if (reasm[i] != NULL) {
                	reasm_ip_free(reasm[i]);  
                        reasm[i] = NULL;
                }

                if (tcpreasm[i] != NULL) {
                        tcpreasm_ip_free(tcpreasm[i]);
                        tcpreasm[i] = NULL;
                }


		free_profile(i);
	}
	/* Close socket */
	//pcap_close(sniffer_proto);
	return 0;
}
Ejemplo n.º 3
0
static int unload_module(void) {
	unsigned int i = 0;

	LNOTICE("unloaded module %s", module_name);

	for (i = 0; i < profile_size; i++) {

		close_socket(i);
		free_profile(i);
	}
	
	                 
#if UV_VERSION_MAJOR == 0
	uv_async_send(&async_handle);
	uv_loop_delete(loop);
#else

	if (uv_loop_alive(loop)) {
        	uv_async_send(&async_handle);
	}
   
	uv_stop(loop);
	uv_loop_close(loop);
	free(loop);
#endif
	/* Close socket */
	return 0;
}
Ejemplo n.º 4
0
char *description(void)
{
        LNOTICE("Loaded description\n");
        char *description = "test description";
        
        return description;
}
Ejemplo n.º 5
0
int unload_module(void)
{
        LNOTICE("unloaded module core_hep\n");

        LNOTICE("count sends:[%d]\n", sendPacketsCount);
	 /* Close socket */
	if(sock) close(sock);

#ifdef USE_SSL
        if(ssl) SSL_free(ssl);
        if(ctx) SSL_CTX_free(ctx);
                        
#endif /* use SSL */ 
        
        pthread_mutex_destroy(&lock);
                
        return 0;
}
Ejemplo n.º 6
0
int unload_module(void)
{
        LNOTICE("unloaded module proto_rtcp\n");

	 /* Close socket */
        pcap_close(sniffer_rtp);        

        return 0;
}
Ejemplo n.º 7
0
int w_clog(msg_t *_m, char *param1, char* param2)
{

    if(param1[0] == 'E' || param1[0] == 'e') LERR("%s\n", param2);    
    else if(param1[0] == 'N' || param1[0] == 'n') LNOTICE("%s\n", param2);    
    else LDEBUG("%s\n", param2);    
    
    return 1;
}
Ejemplo n.º 8
0
static int unload_module(void)
{
  unsigned int i = 0;

  LNOTICE("unloaded module %s", module_name);

  for (i = 0; i < profile_size; i++)
    free_profile(i);

  return 0;
}
Ejemplo n.º 9
0
static int unload_module(void)
{
	unsigned int i = 0;

	LNOTICE("unloaded module transport_json");

	for (i = 0; i < profile_size; i++) {

			free_profile(i);
	}

    return 0;
}
Ejemplo n.º 10
0
static int unload_module(void)
{

	LNOTICE("Unloaded %s", module_name);

	unsigned int i = 0;

	for (i = 0; i < profile_size; i++) {

		free_profile(i);
	}

    return 0;
}
Ejemplo n.º 11
0
int reload_config (char *erbuf, int erlen) {

	char module_config_name[500];
	xml_node *config = NULL;

	LNOTICE("reloading config for [%s]", module_name);

	snprintf(module_config_name, 500, "%s/%s.xml", global_config_path, module_name);

	if(xml_parse_with_report(module_config_name, erbuf, erlen)) {
		unload_module();
		load_module(config);
		return 1;
	}

	return 0;
}
Ejemplo n.º 12
0
static int unload_module(void)
{

	LNOTICE("unloaded module protocol_sip");

	unsigned int i = 0;

	for (i = 0; i < profile_size; i++) {

		free_profile(i);
	}

	 /* Close socket */
       //pcap_close(sniffer_proto);

    return 0;
}
Ejemplo n.º 13
0
	static Node::Data resolveValue(std::vector<Node::Data> & values, Collecting::Type t) {
		Node::Data candidate = values.back();

		LDEBUG("Filter") << "resolving: " << values.size() << " type: " << t << LE;
		for (auto d : values) {
			LDEBUG("Filter") << "resolving: at# " << d << LE;
		}

		if (t == Collecting::ReturnSum) {
			double sum = 0;
			for (auto d : values) {
				sum += d.getValue();
			}
			candidate.setValue(sum);
		}

		if (t == Collecting::ReturnMin) {
			for (auto d : values) {
				if (d.getValue() < candidate.getValue()) {
					candidate.setValue(d.getValue());
					candidate.tags() = d.tags();
				}
			}
		}

		if (t == Collecting::ReturnMax) {
			for (auto d : values) {
				if (d.getValue() > candidate.getValue()) {
					candidate.setValue(d.getValue());
					candidate.tags() = d.tags();
				}
			}
		}

		if (t == Collecting::ReturnAvg) {
			double sum = 0;
			for (auto d : values) {
				sum += d.getValue();
			}
			candidate.setValue(sum / values.size());
		}

		LNOTICE("Filter") << "resolving: final: " << candidate << LE;

		return candidate;
	}
Ejemplo n.º 14
0
SocketCan::SocketCan(int netId) {
	int canTxQueue = TX_QUEUE_SIZE;
	int canRxQueue = RX_QUEUE_SIZE;
	int txTimeout = 500;
	int rxTimeout = 500;

	int so_timestamping_flags = 0;
	/* Create the socket */
	if ((socketHandler = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
		throw Poco::Exception("Cannot create can socket");
	}

	/* Locate the interface you wish to use */
	struct ifreq ifr;
	sprintf(ifr.ifr_name, "can%d", netId);
	LNOTICE("INFO") << "open interface: " << ifr.ifr_name << LE;
	int resi = ioctl(socketHandler, SIOCGIFINDEX, &ifr); // ifr.ifr_ifindex gets filled with that device's index
	if (resi == -1){
		throw Poco::Exception("Cannot create can socket - ioctl");
	}

	/* Select that CAN interface, and bind the socket to it. */
	struct sockaddr_can addr;
	addr.can_family = AF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;
	int resb = bind(socketHandler, (struct sockaddr*) &addr, sizeof(addr));
	if (resb == -1){
		throw Poco::Exception("Cannot create can socket - bind");
	}
	int flags;
	if (-1 == (flags = fcntl(socketHandler, F_GETFL, 0)))
		flags = 0;
	int resf = fcntl(socketHandler, F_SETFL, flags | O_NONBLOCK);
	if (resf == -1){
		throw Poco::Exception("Cannot create can socket - fcntl");
	}
}
Ejemplo n.º 15
0
int load_module(xml_node *config)
{
        char *dev = NULL, *usedev = NULL;
        char errbuf[PCAP_ERRBUF_SIZE];                                
        xml_node *modules;
        char *key, *value = NULL;
        
        LNOTICE("Loaded proto_rtcp\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)) {
                            LERR( "bad keys in the config\n");
                            goto next;
                        }

                        key =  modules->attr[1];
                        value = modules->attr[3];

                        if(key == NULL || value == NULL) {
                            LERR( "bad values in the config\n");
                            goto next;

                        }

                        if(!strncmp(key, "dev", 3)) usedev = value;                        
                        else if(!strncmp(key, "portrange", 9)) rtcp_portrange = value;
                        else if(!strncmp(key, "promisc", 7) && !strncmp(value, "false", 5)) rtcp_promisc = 0;
                        else if(!strncmp(key, "filter", 6)) rtcp_userfilter = value;
                        else if(!strncmp(key, "rtcp-json", 9) && !strncmp(value, "false", 5) ) rtcp_as_json = 0;
                        else if(!strncmp(key, "send-sdes", 9) && !strncmp(value, "false", 5) ) send_sdes = 0;
                        else if(!strncmp(key, "vlan", 4) && !strncmp(value, "true", 4)) rtcp_vlan = 1;
                        else if(!strncmp(key, "debug", 5) && !strncmp(value, "true", 4)) debug_proto_rtcp_enable = 1;
                }
next:

                modules = modules->next;
        }

        /* DEV || FILE */
        if(!usefile) {
          dev = usedev ? usedev : pcap_lookupdev(errbuf);
          if (!dev) {
              perror(errbuf);
              exit(-1);
          }
        }

        // start thread
        pthread_create(&rtp_thread, NULL, rtp_collect, (void *)dev);
        
                                         
        return 0;
}
Ejemplo n.º 16
0
int init_socket(unsigned int loc_idx) {

	struct bpf_program filter;
	char errbuf[PCAP_ERRBUF_SIZE];
	char filter_expr[FILTER_LEN];
	int len=0, buffer_size = 0;

	LDEBUG("Activating device: %s\n", profile_socket[loc_idx].device);
        
	if (profile_socket[loc_idx].device) {
	
	        buffer_size =  1024 * 1024 * profile_socket[loc_idx].ring_buffer;
	
		if ((sniffer_proto[loc_idx] = pcap_create((char *) profile_socket[loc_idx].device, errbuf)) == NULL) {
			LERR("Failed to open packet sniffer on %s: pcap_create(): %s", (char * )profile_socket[loc_idx].device, errbuf);
			return -1;
		};
		
		if (pcap_set_promisc(sniffer_proto[loc_idx], profile_socket[loc_idx].promisc) == -1) {
			LERR("Failed to set promisc \"%s\": %s", (char *) profile_socket[loc_idx].device, pcap_geterr(sniffer_proto[loc_idx]));
			return -1;
		};
		
		if (pcap_set_timeout(sniffer_proto[loc_idx], profile_socket[loc_idx].timeout) == -1) {
			LERR("Failed to set timeout \"%s\": %s", (char *) profile_socket[loc_idx].device, pcap_geterr(sniffer_proto[loc_idx]));
			return -1;
		};
		
		if (pcap_set_snaplen(sniffer_proto[loc_idx], profile_socket[loc_idx].snap_len) == -1) {
			LERR("Failed to set snap_len [%d], \"%s\": %s", profile_socket[loc_idx].snap_len, (char *) profile_socket[loc_idx].device, pcap_geterr(sniffer_proto[loc_idx]));
			return -1;						
		};
		
		if (pcap_set_buffer_size(sniffer_proto[loc_idx], buffer_size) == -1) {
			LERR("Failed to set buffer_size [%d] \"%s\": %s", buffer_size,  (char *) profile_socket[loc_idx].device, pcap_geterr(sniffer_proto[loc_idx]));
			return -1;									
		};
		
		if (pcap_activate(sniffer_proto[loc_idx]) != 0) {
			LERR("Failed to activate  \"%s\": %s", (char *) profile_socket[loc_idx].device, pcap_geterr(sniffer_proto[loc_idx]));
			return -1;									
		};
		
		LDEBUG("Activated device: [%s]\n", profile_socket[loc_idx].device);
						
	} else {

		if ((sniffer_proto[loc_idx] = pcap_open_offline(usefile, errbuf)) == NULL) {
			LERR("%s: Failed to open packet sniffer on %s: pcap_open_offline(): %s", module_name, usefile, errbuf);
			return -1;
		}
		
		LNOTICE("Sending file: %s", usefile);
	}

	/* create filter string */
	if(profile_socket[loc_idx].filter && strlen(profile_socket[loc_idx].filter) > 0)
	{
		len += snprintf(filter_expr+len, sizeof(filter_expr)-len, "(%s)", profile_socket[loc_idx].filter);

		if(ipv4fragments || ipv6fragments)
		{
			if (ipv4fragments)
			{
				LDEBUG("Reassembling of IPv4 packets is enabled, adding '%s' to filter", BPF_DEFRAGMENTION_FILTER_IPV4);
				len += snprintf(filter_expr+len, sizeof(filter_expr), " or %s", BPF_DEFRAGMENTION_FILTER_IPV4);
			}
			if (ipv6fragments)
			{
				LDEBUG("Reassembling of IPv6 packets is enabled, adding '%s' to filter", BPF_DEFRAGMENTION_FILTER_IPV6);
				len += snprintf(filter_expr+len, sizeof(filter_expr), " or %s", BPF_DEFRAGMENTION_FILTER_IPV6);
			}
		}
	}

	if(profile_socket[loc_idx].capture_filter)
	{
		if(!strncmp(profile_socket[loc_idx].capture_filter, "rtcp", 4))
		{
			len += snprintf(filter_expr+len, sizeof(filter_expr), "%s %s", len ? " and" : "", RTCP_FILTER);
		}
		else if(!strncmp(profile_socket[loc_idx].capture_filter, "rtp", 3))
		{
			len += snprintf(filter_expr+len, sizeof(filter_expr), "%s %s", len ? " and" : "", RTP_FILTER);
		}
	}

	LNOTICE("Using filter: %s", filter_expr);
	/* compile filter expression (global constant, see above) */
	if (pcap_compile(sniffer_proto[loc_idx], &filter, filter_expr, 1, 0) == -1) {
		LERR("Failed to compile filter \"%s\": %s", filter_expr, pcap_geterr(sniffer_proto[loc_idx]));
		return -1;
	}

	/* install filter on sniffer session */
	if (pcap_setfilter(sniffer_proto[loc_idx], &filter)) {
		LERR("Failed to install filter: %s", pcap_geterr(sniffer_proto[loc_idx]));
		return -1;
	}
	
	//disabled temporaly
	//pcap_freecode(&filter);

	return 1;
}
Ejemplo n.º 17
0
static int description(char *descr) {
	LNOTICE("Loaded description of %s", module_name);
	descr = module_description;
	return 1;
}
Ejemplo n.º 18
0
static int load_module(xml_node *config) {
	xml_node *params, *profile, *settings;
	char *key, *value = NULL;

	LNOTICE("Loaded %s", module_name);

	load_module_xml_config();
	/* READ CONFIG */
	profile = module_xml_config;

	/* reset profile */
	profile_size = 0;

	while (profile) {

		profile = xml_get("profile", profile, 1);

		if (profile == NULL)
			break;

		if (!profile->attr[4] || strncmp(profile->attr[4], "enable", 6)) {
			goto nextprofile;
		}

		/* if not equals "true" */
		if (!profile->attr[5] || strncmp(profile->attr[5], "true", 4)) {
			goto nextprofile;
		}

		/* set values */
		profile_protocol[profile_size].name = strdup(profile->attr[1]);
		profile_protocol[profile_size].description = strdup(profile->attr[3]);
		profile_protocol[profile_size].serial = atoi(profile->attr[7]);

		/* SETTINGS */
		settings = xml_get("settings", profile, 1);

		if (settings != NULL) {

			params = settings;

			while (params) {

				params = xml_get("param", params, 1);
				if (params == NULL)
					break;

				if (params->attr[0] != NULL) {

					/* bad parser */
					if (strncmp(params->attr[0], "name", 4)) {
						LERR("bad keys in the config");
						goto nextparam;
					}

					key = params->attr[1];

					if (params->attr[2] && params->attr[3] && !strncmp(params->attr[2], "value", 5)) {
						value = params->attr[3];
					} else {
						value = params->child->value;
					}

					if (key == NULL || value == NULL) {
						LERR("bad values in the config");
						goto nextparam;

					}

					/*
					if (!strncmp(key, "ignore", 6))
						profile_protocol[profile_size].ignore = strdup(value);
					else if (!strncmp(key, "dialog-type", 11))
						profile_protocol[profile_size].dialog_type = atoi(value);
					else if (!strncmp(key, "dialog-timeout", 14))
						profile_protocol[profile_size].dialog_timeout = atoi(value);
						*/

				}

				nextparam: params = params->next;

			}
		}

		profile_size++;

		nextprofile: profile = profile->next;
	}

	/* free it */
	free_module_xml_config();


	return 0;
}
Ejemplo n.º 19
0
static int description(char *descr)
{
       LNOTICE("Loaded description");
       descr = module_description;
       return 1;
}
Ejemplo n.º 20
0
static int load_module(xml_node *config) {

	xml_node *params, *profile=NULL, *settings;
	char *key, *value = NULL;
	unsigned int i = 0;
	//char module_api_name[256];
	char loadplan[1024];
	FILE* cfg_stream;

	LNOTICE("Loaded %s", module_name);

	load_module_xml_config();

	/* READ CONFIG */
	profile = module_xml_config;

	/* reset profile */
	profile_size = 0;

	while (profile) {

		profile = xml_get("profile", profile, 1);

		memset(&profile_socket[i], 0, sizeof(profile_socket_t));

		if (profile == NULL)
			break;

		if (!profile->attr[4] || strncmp(profile->attr[4], "enable", 6)) {
			goto nextprofile;
		}

		/* if not equals "true" */
		if (!profile->attr[5] || strncmp(profile->attr[5], "true", 4)) {
			goto nextprofile;
		}

		/* set values */
		profile_socket[profile_size].name = strdup(profile->attr[1]);
		profile_socket[profile_size].description = strdup(profile->attr[3]);
		profile_socket[profile_size].serial = atoi(profile->attr[7]);
		profile_socket[profile_size].protocol = PROTO_SIP; //we extract SIP and send as SIP packet
		profile_socket[profile_size].port = TZSP_PORT;
		profile_socket[profile_size].host = TZSP_HOST;
		
		/* SETTINGS */
		settings = xml_get("settings", profile, 1);

		if (settings != NULL) {

			params = settings;

			while (params) {

				params = xml_get("param", params, 1);
				if (params == NULL)
					break;

				if (params->attr[0] != NULL) {

					/* bad parser */
					if (strncmp(params->attr[0], "name", 4)) {
						LERR("bad keys in the config");
						goto nextparam;
					}

					key = params->attr[1];

					if (params->attr[2] && params->attr[3] && !strncmp(params->attr[2], "value", 5)) {
						value = params->attr[3];
					} else {
						value = params->child->value;
					}

					if (key == NULL || value == NULL) {
						LERR("bad values in the config");
						goto nextparam;

					}

					if (!strncmp(key, "host", 4))
						profile_socket[profile_size].host = strdup(value);
					else if (!strncmp(key, "port", 4))
						profile_socket[profile_size].port = strdup(value);
					else if (!strncmp(key, "protocol-type", 13))
						profile_socket[profile_size].protocol = atoi(value);						
					else if (!strncmp(key, "capture-plan", 12))
						profile_socket[profile_size].capture_plan = strdup(value);
				}

				nextparam: params = params->next;

			}
		}
		
		profile_size++;

		nextprofile: profile = profile->next;
	}

	/* free */		
		
	free_module_xml_config();

#if UV_VERSION_MAJOR == 0
    loop = uv_loop_new();
#else               
    loop = malloc(sizeof *loop);
    uv_loop_init(loop);
#endif

	for (i = 0; i < profile_size; i++) {

		if(profile_socket[i].capture_plan != NULL)
		{

			snprintf(loadplan, sizeof(loadplan), "%s/%s", global_capture_plan_path, profile_socket[i].capture_plan);
			cfg_stream=fopen (loadplan, "r");

			if (cfg_stream==0){
			   fprintf(stderr, "ERROR: loading config file(%s): %s\n", loadplan, strerror(errno));
			}

			yyin=cfg_stream;
			if ((yyparse()!=0)||(cfg_errors)){
			          fprintf(stderr, "ERROR: bad config file (%d errors)\n", cfg_errors);
			}

			profile_socket[i].action = main_ct.idx;
		}

		// start thread
		if (init_socket(i)) {
			LERR("couldn't init tzsp");
			return -1;
		}

		//pthread_create(&call_thread, NULL, proto_collect, arg);

	}
	
	uv_async_init(loop, &async_handle, _async_callback);
	uv_thread_create(&runthread, _run_uv_loop, loop);

	return 0;
}
Ejemplo n.º 21
0
int load_module(xml_node *config)
{
	xml_node *modules;
        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)) {                        
                            LERR( "bad keys in the config\n");
                            goto next;
                        
                        }
                        
                        key =  modules->attr[1];
                        value = modules->attr[3];
                        
                        if(key == NULL || value == NULL) {
                            LERR( "bad values in the config\n");
                            goto next;                        
                        
                        }                        

                        if(!strncmp(key, "capture-host", 10)) capt_host = value;
                        else if(!strncmp(key, "capture-port", 13)) capt_port = value;
                        else if(!strncmp(key, "capture-proto", 14)) capt_proto = value;
                        else if(!strncmp(key, "capture-password", 17)) capt_password = value;
                        else if(!strncmp(key, "capture-id", 11)) capt_id = atoi(value);
                        else if(!strncmp(key, "payload-compression", 19) && !strncmp(value, "true", 5)) pl_compress = 1;
                        else if(!strncmp(key, "version", 7)) hep_version = atoi(value);
                                	                	                	
		}
next:		
		
                modules = modules->next;
	}

#ifndef USE_ZLIB   
    if(pl_compress) LDEBUG("The captagent has not compiled with zlib. Please reconfigure with --enable-compression\n");    
#endif /* USE_ZLIB */

        LNOTICE("Loaded core_hep\n");
                                           
        hints->ai_flags = AI_NUMERICSERV;
        hints->ai_family = AF_UNSPEC;

        if(!strncmp(capt_proto, "udp", 3)) {
            hints->ai_socktype = SOCK_DGRAM;
            hints->ai_protocol = IPPROTO_UDP;
        }        
        else if(!strncmp(capt_proto, "tcp", 3) || !strncmp(capt_proto, "ssl", 3)) {
            hints->ai_socktype = SOCK_STREAM;
            hints->ai_protocol = IPPROTO_TCP;

            /*TLS || SSL*/
            if(!strncmp(capt_proto, "ssl", 3)) {

#ifdef USE_SSL
                usessl = 1;
                 /* init SSL library */
                SSL_library_init();
#else
		LERR("The captagent has not compiled with ssl support. Please reconfigure with --enable-ssl\n");    

#endif /* end USE_SSL */

            }   
        }

        else {        
            LERR("Unsupported protocol\n");
            return -1;
        }

      /*  if ((s = getaddrinfo(capt_host, capt_port, hints, &ai)) != 0) {            
            LERR( "capture: getaddrinfo: %s\n", gai_strerror(s));
            return 2;
        }
      */

	if(!usessl) {

	        if(init_hepsocket_blocking()) {
        	    LERR("capture: couldn't init socket\r\n");              
	            return 2;            
        	}        
	}
#ifdef USE_SSL       
   	     else {
                if(initSSL()) {
                    LERR("capture: couldn't init SSL socket\r\n");
                    handler(1);
                    return 2;
                }

                // start select thread
                /* pthread_create(&call_thread, NULL, (void *)select_loop, NULL);

                if (pthread_mutex_init(&lock, NULL) != 0)
                {
                    LERR("mutex init failed\n");
                    return 3;
                }
                */
          }
#endif /* use SSL */  

       sigPipe();

       return 0;
}
Ejemplo n.º 22
0
static int load_module(xml_node *config) {

	char errbuf[PCAP_ERRBUF_SIZE];
	xml_node *params, *profile=NULL, *settings;
	char *key, *value = NULL;
	unsigned int i = 0;
	char loadplan[1024];
        FILE* cfg_stream;

	LNOTICE("Loaded %s", module_name);

	load_module_xml_config();

	/* READ CONFIG */
	profile = module_xml_config;

	/* reset profile */
	profile_size = 0;

	memset(sniffer_proto, 0, sizeof sniffer_proto);
	        
	
	//global_scripts_path


	while (profile) {

		profile = xml_get("profile", profile, 1);

		if (profile == NULL)
			break;

		if (!profile->attr[4] || strncmp(profile->attr[4], "enable", 6)) {
			goto nextprofile;
		}

		/* if not equals "true" */
		if (!profile->attr[5] || strncmp(profile->attr[5], "true", 4)) {
			goto nextprofile;
		}

		if(profile_size == MAX_SOCKETS) {
			break;
		}

		memset(&profile_socket[profile_size], 0, sizeof(profile_socket_t));

		/* set values */
		profile_socket[profile_size].name = strdup(profile->attr[1]);
		profile_socket[profile_size].description = strdup(profile->attr[3]);
		profile_socket[profile_size].serial = atoi(profile->attr[7]);
		profile_socket[profile_size].capture_plan = NULL;
		profile_socket[profile_size].capture_filter = NULL;
		profile_socket[profile_size].action = -1;
		profile_socket[profile_size].ring_buffer = 12;
		profile_socket[profile_size].snap_len = 3200;
		profile_socket[profile_size].promisc = 1;
		profile_socket[profile_size].timeout = 100;
		profile_socket[profile_size].full_packet = 0;
		profile_socket[profile_size].reasm = 0;         		                
		profile_socket[profile_size].erspan = 0;

		/* SETTINGS */
		settings = xml_get("settings", profile, 1);

		if (settings != NULL) {

			params = settings;

			while (params) {

				params = xml_get("param", params, 1);
				if (params == NULL)
					break;

				if (params->attr[0] != NULL) {

					/* bad parser */
					if (strncmp(params->attr[0], "name", 4)) {
						LERR("bad keys in the config");
						goto nextparam;
					}

					key = params->attr[1];

					if (params->attr[2] && params->attr[3] && !strncmp(params->attr[2], "value", 5)) {
						value = params->attr[3];
					} else {
						value = params->child->value;
					}

					if (key == NULL || value == NULL) {
						LERR("bad values in the config");
						goto nextparam;
					}


					if (!usefile && !strncmp(key, "dev", 3))
						profile_socket[profile_size].device = strdup(value);
					else if (!strncmp(key, "reasm", 5) && !strncmp(value, "true", 4))
						profile_socket[profile_size].reasm = +1;
                                        else if (!strncmp(key, "ipv4fragments", 13) && !strncmp(value, "true", 4))
						ipv4fragments = 1;
                                        else if (!strncmp(key, "ipv6fragments", 13) && !strncmp(value, "true", 4))
						ipv6fragments = 1;
                                        else if(!strncmp(key, "tcpdefrag", 9) && !strncmp(value, "true", 4))
                                                profile_socket[profile_size].reasm +=2;                                                    						
					else if (!strncmp(key, "ring-buffer", 11))					        
						profile_socket[profile_size].ring_buffer = atoi(value);		
					else if (!strncmp(key, "full-packet",11) && !strncmp(value, "true", 4))					        
						profile_socket[profile_size].full_packet = 1;												
					else if (!strncmp(key, "timeout", 7))					        
						profile_socket[profile_size].timeout = atoi(value);												
					else if (!strncmp(key, "snap-len", 8))					        
						profile_socket[profile_size].snap_len = atoi(value);												
					else if (!strncmp(key, "promisc", 7) && !strncmp(value, "true", 4))
						profile_socket[profile_size].promisc = 1;
					else if (!strncmp(key, "filter", 6))
						profile_socket[profile_size].filter = strdup(value);
					else if (!strncmp(key, "capture-plan", 12))
						profile_socket[profile_size].capture_plan = strdup(value);
                                        else if (!strncmp(key, "capture-filter", 14))
						profile_socket[profile_size].capture_filter = strdup(value);
					else if(!strncmp(key, "debug", 5) && !strncmp(value, "true", 4))
                                                debug_socket_pcap_enable = 1;	
					else if (!strncmp(key, "erspan", 6) && !strncmp(value, "true", 4))
						profile_socket[profile_size].erspan = 1;
				}

				nextparam: params = params->next;

			}
		}

		profile_size++;

		nextprofile: profile = profile->next;
	}

	/* free */
	free_module_xml_config();

	for (i = 0; i < profile_size; i++) {

		unsigned int *arg = malloc(sizeof(arg));		

		*arg = i;
		
		/* DEV || FILE */
		if (!usefile) {
			if (!profile_socket[i].device)
				profile_socket[i].device = pcap_lookupdev(errbuf);
			if (!profile_socket[i].device) {
				perror(errbuf);
				exit(-1);
			}
		}
		
		// start thread
		if (!init_socket(i)) {
			LERR("couldn't init pcap");
			return -1;
		}

		 /* REASM */
                if (profile_socket[i].reasm == 1 || profile_socket[i].reasm == 3) {
                        reasm[i] = reasm_ip_new();
                        reasm_ip_set_timeout(reasm[i], 30000000);
                }
                else reasm[i] = NULL;

                /* TCPREASM */
                if (profile_socket[i].reasm == 2 || profile_socket[i].reasm == 3) {
                        tcpreasm[i] = tcpreasm_ip_new ();
                        tcpreasm_ip_set_timeout(tcpreasm[i], 30000000);
                }
                else tcpreasm[i] = NULL;

		if(profile_socket[i].capture_plan != NULL)
		{

			snprintf(loadplan, sizeof(loadplan), "%s/%s", global_capture_plan_path, profile_socket[i].capture_plan);

						cfg_stream=fopen (loadplan, "r");
			if (cfg_stream==0){
			   fprintf(stderr, "ERROR: loading config file(%s): %s\n", loadplan, strerror(errno));
			}

			yyin=cfg_stream;
			if ((yyparse()!=0)||(cfg_errors)){
			          fprintf(stderr, "ERROR: bad config file (%d errors)\n", cfg_errors);
			          //goto error;
			}

			profile_socket[i].action = main_ct.idx;
			
		}

		pthread_create(&call_thread[i], NULL, proto_collect, arg);		
	}

	return 0;
}
Ejemplo n.º 23
0
static int load_module(xml_node *config) {
	xml_node *params, *profile, *settings, *condition, *action;
	char *key, *value = NULL;
	unsigned int i = 0;
	char module_api_name[256];

	LNOTICE("Loaded %s", module_name);

	load_module_xml_config();
	/* READ CONFIG */
	profile = module_xml_config;

	/* reset profile */
	profile_size = 0;

	while (profile) {

		profile = xml_get("profile", profile, 1);

		if (profile == NULL)
			break;

		if(!profile->attr[4] || strncmp(profile->attr[4], "enable", 6)) {
			goto nextprofile;
		}

		/* if not equals "true" */
		if(!profile->attr[5] || strncmp(profile->attr[5], "true", 4)) {
			goto nextprofile;
		}

		/* set values */
		profile_transport[profile_size].name = strdup(profile->attr[1]);
		profile_transport[profile_size].description = strdup(profile->attr[3]);
		profile_transport[profile_size].serial = atoi(profile->attr[7]);
		profile_transport[profile_size].statistic_pipe = NULL;
		profile_transport[profile_size].flag = 1;

		/* SETTINGS */
		settings = xml_get("settings", profile, 1);

		if (settings != NULL) {

			params = settings;

			while (params) {

				params = xml_get("param", params, 1);
				if (params == NULL) break;

				if (params->attr[0] != NULL) {

					/* bad parser */
					if (strncmp(params->attr[0], "name", 4)) {
						LERR("bad keys in the config");
						goto nextparam;
					}

					key = params->attr[1];

					if(params->attr[2] && params->attr[3] && !strncmp(params->attr[2], "value", 5)) {
							value = params->attr[3];
					}
					else {
						value = params->child->value;
					}

					if (key == NULL || value == NULL) {
						LERR("bad values in the config");
						goto nextparam;

					}

					if(!strncmp(key, "capture-host", 10)) profile_transport[profile_size].capt_host = strdup(value);
					else if(!strncmp(key, "capture-port", 13)) profile_transport[profile_size].capt_port = strdup(value);
					else if(!strncmp(key, "capture-proto", 14)) profile_transport[profile_size].capt_proto = strdup(value);
					else if(!strncmp(key, "capture-password", 17)) profile_transport[profile_size].capt_password = strdup(value);
					else if(!strncmp(key, "capture-id", 11)) profile_transport[profile_size].capt_id = atoi(value);
					else if(!strncmp(key, "payload-compression", 19) && !strncmp(value, "true", 5)) profile_transport[profile_size].compression = 1;
					else if(!strncmp(key, "version", 7)) profile_transport[profile_size].version = atoi(value);
					else if(!strncmp(key, "payload-send", 12) && !strncmp(value, "false", 5)) profile_transport[profile_size].flag = 0;


					//if (!strncmp(key, "ignore", 6))
					//	profile_transport[profile_size].ignore = value;
				}

				nextparam:
					params = params->next;

			}
		}


		/* STATS */

		condition = xml_get("statistic", profile, 1);

		while (condition) {

			condition = xml_get("condition", condition, 1);

			if (condition == NULL)	break;

			if (condition->attr[0] != NULL && condition->attr[2] != NULL) {

						/* bad parser */
						if (strncmp(condition->attr[0], "field", 5) || strncmp(condition->attr[2], "expression", 10)) {
							LERR("bad keys in the config");
							goto nextstatistic;
						}

						key = condition->attr[1];
						value = condition->attr[3];

						if (key == NULL || value == NULL) {
							LERR("bad values in the config");
							goto nextstatistic;
						}

						action = condition->child;
						if (action && !strncmp(action->key, "action", 6)) {
							for (i = 0; action->attr[i]; i++) {
								if (!strncmp(action->attr[i], "application", 4)) {
									profile_transport[profile_size].statistic_pipe = strdup(action->attr[i + 1]);
								}
								else if (!strncmp(action->attr[i], "profile", 7)) {
									profile_transport[profile_size].statistic_profile = strdup(action->attr[i + 1]);
								}
							}
						}
			}

			nextstatistic: condition = condition->next;
		}

		profile_size++;

		nextprofile:
			profile = profile->next;
	}

	/* free it */
	free_module_xml_config();

	for (i = 0; i < profile_size; i++) {

#ifndef USE_ZLIB
			if(profile_transport[i].compression) {
				printf("The captagent has not compiled with zlib. Please reconfigure with --enable-compression\n");
				LERR("The captagent has not compiled with zlib. Please reconfigure with --enable-compression");
			}
#endif /* USE_ZLIB */

			/*TLS || SSL*/
			if(!strncmp(profile_transport[i].capt_proto, "ssl", 3)) {

#ifdef USE_SSL
				profile_transport[i].usessl = 1;
				/* init SSL library */
				if(sslInit == 0) {
					SSL_library_init();
					sslInit = 1;
				}
#else
				printf("The captagent has not compiled with ssl support. Please reconfigure with --enable-ssl\n");
				LERR("The captagent has not compiled with ssl support. Please reconfigure with --enable-ssl");

#endif /* end USE_SSL */
			}

			if(!profile_transport[i].usessl) {
				if(init_jsonsocket_blocking(i)) {
					LERR("capture: couldn't init socket");
				}
			}

#ifdef USE_SSL
			else {
				if(initSSL(i)) {
					LERR("capture: couldn't init SSL socket");
				}
			}
#endif /* use SSL */

			if(profile_transport[i].statistic_pipe) {
				snprintf(module_api_name, 256, "%s_bind_api", profile_transport[i].statistic_pipe);
				//stats_bind_api = (bind_statistic_module_api_t) find_export(module_api_name, 1, 0);
				//stats_bind_api(&profile_transport[i].stats_api);
			}
	}

	sigPipe();

	return 0;
}