Beispiel #1
0
char *lo_address_get_url(lo_address a)
{
    char *buf;
    int ret=0;
    int needquote = strchr(a->host, ':') ? 1 : 0;
    char *fmt;

    if (needquote) {
        fmt = "osc.%s://[%s]:%s/";
    } else {
        fmt = "osc.%s://%s:%s/";
    }
#ifndef _MSC_VER
    ret = snprintf(NULL, 0, fmt,
                   get_protocol_name(a->protocol), a->host, a->port);
#endif
    if (ret <= 0) {
        /* this libc is not C99 compliant, guess a size */
        ret = 1023;
    }
    buf = malloc((ret + 2) * sizeof(char));
    snprintf(buf, ret+1, fmt,
             get_protocol_name(a->protocol), a->host, a->port);

    return buf;
}
static void print_ciphersuite_data(BIO *io, SSL *ssl, int js)
{
	SSL_SESSION* session = SSL_get_session(ssl);
	long protocol = SSL_version(ssl);
	const char *protocol_name = get_protocol_name(protocol);

	const char *eol = js ? "\\n\\\n" : "\n";
	if(BIO_printf(io, "Version: 0x%lx %s%s", protocol, protocol_name, eol) <= 0)
		err_exit("Write error");

	if(BIO_printf(io, "Current cipher: %s%s", SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)), eol) <= 0)
		err_exit("Write error");

	STACK_OF(SSL_CIPHER) *ciphers = session->ciphers;
	SSL_CIPHER *c;
	int n = sk_SSL_CIPHER_num(ciphers);
	if(BIO_printf(io, "client sent %d ciphers%s", n, eol) <= 0)
		err_exit("Write error");

	int i;
	for (i = 0; i < n; i++)
	{
		c = sk_SSL_CIPHER_value(ciphers, i);
		if(BIO_printf(io, "client [%2d of %2d]: %s%s", i, n, SSL_CIPHER_get_name(c), eol) <= 0)
			err_exit("Write error");
	}
}
Beispiel #3
0
int url_bypasses_socks(unsigned char *url)
{
	int ret = 0;	/* against warning */
	unsigned char *p;
	if (!(p = get_protocol_name(url))) return 1;
	get_prot_info(p, NULL, NULL, NULL, NULL, &ret);
	mem_free(p);
	return ret;
}
Beispiel #4
0
void (*get_external_protocol_function(unsigned char *url))(struct session *, unsigned char *)
{
	unsigned char *p;
	void (*f)(struct session *, unsigned char *) = NULL;
	int post = 0;
	if (!(p = get_protocol_name(url))) return NULL;
	get_prot_info(p, NULL, NULL, &f, &post, NULL);
	mem_free(p);
	if (!post && strchr(cast_const_char url, POST_CHAR)) return NULL;
	return f;
}
Beispiel #5
0
void (*get_protocol_handle(unsigned char *url))(struct connection *)
{
	unsigned char *p;
	void (*f)(struct connection *) = NULL;
	int post = 0;
	if (!(p = get_protocol_name(url))) return NULL;
	get_prot_info(p, NULL, &f, NULL, &post, NULL);
	mem_free(p);
	if (!post && strchr(cast_const_char url, POST_CHAR)) return NULL;
	return f;
}
Beispiel #6
0
int get_port(unsigned char *url)
{
	unsigned char *h;
	int hl;
	long n = -1;
	if (parse_url(url, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &h, &hl, NULL, NULL, NULL)) return -1;
	if (h) {
		n = strtol(cast_const_char h, NULL, 10);
		if (n > 0 && n < 65536) return (int)n;
		return -1;
	}
	if ((h = get_protocol_name(url))) {
		int nn = -1;	/* against warning */
		get_prot_info(h, &nn, NULL, NULL, NULL, NULL);
		mem_free(h);
		n = nn;
	}
	return (int)n;
}
Beispiel #7
0
char* Flow::print(char *buf, u_int buf_len) {
  char buf1[32], buf2[32];

  buf[0] = '\0';

  if((cli_host == NULL) || (srv_host == NULL)) return(buf);

  snprintf(buf, buf_len,
	   "%s %s:%u > %s:%u [proto: %u/%s][%u/%u pkts][%llu/%llu bytes]\n",
	   get_protocol_name(),
	   cli_host->get_ip()->print(buf1, sizeof(buf1)), ntohs(cli_port),
	   srv_host->get_ip()->print(buf2, sizeof(buf2)), ntohs(srv_port),
	   ndpi_detected_protocol,
	   ndpi_get_proto_name(iface->get_ndpi_struct(), ndpi_detected_protocol),
	   cli2srv_packets, srv2cli_packets,
	   (long long unsigned) cli2srv_bytes, (long long unsigned) srv2cli_bytes);

  return(buf);
}
const Ekiga::CallManager::InterfaceList Opal::H323::CallManager::get_interfaces () const
{
  Ekiga::CallManager::InterfaceList ilist;

  OpalListenerList listeners = h323_endpoint.GetListeners ();
  for (int i = 0 ; i < listeners.GetSize () ; i++) {
    Ekiga::CallManager::Interface iface;
    PIPSocket::Address address;
    WORD port;
    PString proto_prefix = listeners[i].GetLocalAddress ().GetProtoPrefix ();
    listeners[i].GetLocalAddress ().GetIpAndPort (address, port);

    iface.voip_protocol = get_protocol_name ();
    iface.id = "*";
    iface.protocol = (const char*) proto_prefix.Left (proto_prefix.GetLength () - 1); // Strip final $ delimiter
    iface.port = (unsigned int) port;

    ilist.push_back (iface);
  }

  return ilist;
}
Beispiel #9
0
int
_protocol_init(const char *prot_name, PROTO_PARA *ppp)
{
	/* check whether the protocol version match the csf core version */
	if (CORE_VER_REQUIRE > ppp->vcbp->lowest_ver)
		return PROTO_VER_ERR;
		
	logger_init(ppp->chp->logp, NULL, 0, 0);
	mp_init(20, 100);   
	monitor_init(ppp->chp->mntp);
	submit_request_init(ppp->chp->sribp);
	
	g_pcp = ppp->pcp;

	set_protocol_name(prot_name);
	
    pthread_once(&init_done, pthr_key_create);
    pthread_setspecific(pthread_key, NULL);
	
	if (protocol_init(get_protocol_name()) < 0)
        return PROTO_INIT_ERR;
	
	return PROTO_INIT_OK;
}
Beispiel #10
0
/*
 *	把PAR_ITF_ANALYZE_ID结构转换成EA_ITF_PAR_INFO_ID结构.
 */
void convet_par_itf(
	EA_ITF_PAR_INFO_ID	itf_par_info_id,
	PAR_ITF_ANALYZE_ID	par_itf_analysis_id,
	char			redirect_flag
	)
{

	PROTECTED_RESOURCE_ID	tmp = NULL;
	int			i   = 0;
	
	get_protocol_name(itf_par_info_id->protocol_name, par_itf_analysis_id);
	
	itf_par_info_id->cfg_file_set    = par_itf_analysis_id->cfg_file_set;
	itf_par_info_id->func_switch     = par_itf_analysis_id->func_switch;
	strcpy(itf_par_info_id->pkt_file_dir, par_itf_analysis_id->pkt_file_dir);
	itf_par_info_id->deposit_ivl_sec = par_itf_analysis_id->deposit_ivl_sec;
	
	itf_par_info_id->usr_list_id     = (USR_LIST_MEM_ID)			\
		get_shm_addr( par_itf_analysis_id->usr_list_key, SHM_RDONLY);
	itf_par_info_id->usr_all_num     = par_itf_analysis_id->usr_num;
	
   	tmp = (PROTECTED_RESOURCE_ID)get_shm_addr(par_itf_analysis_id->rule_pool_key, SHM_RDONLY);
	itf_par_info_id->protect_res_num = par_itf_analysis_id->rule_num;

	if(tmp == -1)	exit(-1);
	else {
		if((itf_par_info_id->protect_res_id = (PROTECTED_RESOURCE_ID)	\
			calloc(itf_par_info_id->protect_res_num,		\
				PROTECTED_RESOURCE_SIZE)) == NULL) {
			printf("protected_resource_id calloc error\n");
			exit(-1);	
		}

		memcpy(itf_par_info_id->protect_res_id, tmp,			\
			(itf_par_info_id->protect_res_num) * PROTECTED_RESOURCE_SIZE);
		for(i = 0; i < itf_par_info_id->protect_res_num; i++)
			if(get_resource_port(itf_par_info_id->protect_res_id, i)\
				 == FALSE)	exit(-1);/* where is get_resource_port */
				
	}  /* ended of if-else */

	itf_par_info_id->authorize_network_id  = (AUTHORIZE_ACCESS_NETWORK_ID)	\
		get_shm_addr(par_itf_analysis_id->authorize_network_key, SHM_RDONLY);
	itf_par_info_id->authorize_network_num = par_itf_analysis_id->authorize_network_num;

	itf_par_info_id->authorize_cmd_id      = (AUTHORIZE_CMD_ID)		\
		get_shm_addr(par_itf_analysis_id->authorize_cmd_key, SHM_RDONLY);
	itf_par_info_id->authorize_cmd_num     = par_itf_analysis_id->authorize_cmd_num;

	itf_par_info_id->authorize_account_id  = (AUTHORIZE_ACCOUNT_ID)		\
		get_shm_addr(par_itf_analysis_id->authorize_account_key, SHM_RDONLY);
	itf_par_info_id->authorize_account_num = par_itf_analysis_id->authorize_account_num;

   	itf_par_info_id->authorize_custom_id   = (AUTHORIZE_CUSTOM_ID)		\
		get_shm_addr( par_itf_analysis_id->authorize_custom_key, SHM_RDONLY);
	itf_par_info_id->authorize_custom_num  = par_itf_analysis_id->authorize_custom_num;

	itf_par_info_id->authorize_pro_feature_id  = (AUTHORIZE_PROTOCOL_FEATURE_ID)
		get_shm_addr( par_itf_analysis_id->authorize_feature_key, SHM_RDONLY);
	itf_par_info_id->authorize_pro_feature_num = par_itf_analysis_id->authorize_feature_num;

	if(redirect_flag)  { /* is true */
		itf_par_info_id->redirection_port_info_id = (REDIRECTION_PORT_INFO_ID)
			get_shm_addr(par_itf_analysis_id->redirect_key, 0);
		itf_par_info_id->redirect_pid = par_itf_analysis_id->redirect_pid;
		itf_par_info_id->semid        = semget(par_itf_analysis_id->sem_key, 0, IPC_CREAT);
	}
}
Beispiel #11
0
static int http_serve(SSL *ssl, int s)
{
	char buf[BUFSIZZ];
	int r,len;
	BIO *io,*ssl_bio;

	io=BIO_new(BIO_f_buffer());
	ssl_bio=BIO_new(BIO_f_ssl());
	BIO_set_ssl(ssl_bio,ssl,BIO_CLOSE);
	BIO_push(io,ssl_bio);

	r=BIO_gets(io,buf,BUFSIZZ-1);

	switch(SSL_get_error(ssl,r)){
	case SSL_ERROR_NONE:
		len=r;
		break;
	default:
		berr_exit("SSL read problem");
	}

	char *saveptr;
	char resource[512] = {'\0'};
	char *token = strtok_r(buf, " ", &saveptr);
	if (token && strcasecmp(token, "GET") == 0)
	{
		token = strtok_r(NULL, " ", &saveptr);
		if (token)
		{
			strncpy(resource, token, sizeof(resource));
		}
	}

	if (resource[0])
	{
		while(1){
			r=BIO_gets(io,buf,BUFSIZZ-1);

			switch(SSL_get_error(ssl,r)){
			case SSL_ERROR_NONE:
				len=r;
				break;
			default:
				berr_exit("SSL read problem");
			}

			/* Look for the blank line that signals
			   the end of the HTTP headers */
			if(!strcmp(buf,"\r\n") || !strcmp(buf,"\n"))
				break;
		}
	}

	if (strcasecmp(resource, "/ciphersuites.txt") == 0)
	{
		http_serve_headers(io, 200, "OK", "text/plain");
		print_ciphersuite_data(io, ssl, 0);
	}
	else if (strcasecmp(resource, "/ciphersuites.js") == 0)
	{
		long protocol = SSL_version(ssl);
		http_serve_headers(io, 200, "OK", "text/javascript");
		if(BIO_printf(io, "$(function() {\n  insert_text('%s', '", get_protocol_name(protocol)) <= 0)
			err_exit("Write error");
		print_ciphersuite_data(io, ssl, 1);
		if(BIO_printf(io, "');\n});") <= 0)
			err_exit("Write error");
	}
	else
	{
		http_serve_headers(io, 404, "Not Found", "text/plain");
		if(BIO_puts(io, "Not found.") <= 0)
			err_exit("Write error");
	}

	if((r=BIO_flush(io))<0)
		err_exit("Error flushing BIO");

	r=SSL_shutdown(ssl);
	if(!r){
		/* If we called SSL_shutdown() first then
		   we always get return value of '0'. In
		   this case, try again, but first send a
		   TCP FIN to trigger the other side's
		   close_notify*/
		shutdown(s,1);
		r=SSL_shutdown(ssl);
	}

	switch(r){  
	case 1:
		break; /* Success */
	case 0:
	case -1:
	default:
		berr_exit("Shutdown failed");
	}

	SSL_free(ssl);
	close(s);

	return(0);
}
bool C_ProtocolControl::fromXml (C_XmlData *P_data,
				 T_pConfigValueList P_config_value_list,
				 bool               P_display_protocol_stats) {
  
  bool                          L_ret       = true   ;
  T_pXmlData_List               L_subList            ;
  T_XmlData_List::iterator      L_subListIt          ;
  C_XmlData                    *L_data               ;
  char                         *L_protocol_name, 
                               *L_protocol_type      ;
  int                           L_protocol_id        ;
  T_ProtocolInstList            L_protocol_inst_list ;
  T_pProtocolInstanceInfo       L_protocol_info      ;
  T_ProtocolInstList::iterator  L_it                 ;

  

  GEN_DEBUG(1, "C_ProtocolControl::fromXml() start");
  if (P_data != NULL) {
    if ((L_subList = P_data->get_sub_data()) != NULL) {
      for (L_subListIt = L_subList->begin() ;
	   L_subListIt != L_subList->end() ;
	   L_subListIt++) {

	L_data = *L_subListIt ;

	if (L_data != NULL) {
	  if (strcmp(L_data->get_name(), XML_PROTOCOL_SECTION) == 0) {
	    // protocol section definition found
	    L_protocol_name = get_protocol_name (L_data) ;
	    // check protocol type for creation
	    L_protocol_type = get_protocol_type (L_data) ;
	    
	    // check name/type presence
	    if (L_protocol_name == NULL) {
	      GEN_ERROR(E_GEN_FATAL_ERROR, 
			"name mandatory for section " << XML_PROTOCOL_SECTION);
	      L_ret = false ;
	      break ;
	    }
	    if (L_protocol_type == NULL) {
	      GEN_ERROR(E_GEN_FATAL_ERROR, 
			"type mandatory for section " << XML_PROTOCOL_SECTION);
	      L_ret = false ;
	      break ;
	    }

	    // check protocol name unicity
	    if (m_name_map->find(T_ProtocolNameMap::key_type(L_protocol_name)) 
		!= m_name_map->end()) {
	      GEN_ERROR(E_GEN_FATAL_ERROR, 
		        XML_PROTOCOL_SECTION << " with name ["
			<< L_protocol_name << "] already defined");
	      L_ret = false ;
	      break ;
	    }

	    // check protocol type or sub type
	    if (strcmp(L_protocol_type, "binary") == 0) {
	      // create protocol instance
	      
	      C_ProtocolBinary    *L_protocol_instance = NULL ;
	      T_ConstructorResult  L_res ;

	      NEW_VAR(L_protocol_instance,
		      C_ProtocolBinary());
              
              L_protocol_instance->construction_data(L_data, &L_protocol_name, &L_res) ;

	      if (L_res != E_CONSTRUCTOR_OK) {
		GEN_ERROR(E_GEN_FATAL_ERROR,
			  "Error found in protocol definition");
		DELETE_VAR(L_protocol_instance);
		L_ret = false ;
		break ;
	      } else {
		// store new instance
		if (P_display_protocol_stats == true) {
		  C_ProtocolStats  *L_protocol_stats ;
		  NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
		  L_protocol_instance->set_stats(L_protocol_stats);
		}

		L_protocol_id = m_id_gen->new_id() ;
		ALLOC_VAR(L_protocol_info,
			  T_pProtocolInstanceInfo,
			  sizeof(T_ProtocolInstanceInfo));

		L_protocol_info->m_instance = L_protocol_instance ;
		L_protocol_info->m_id = L_protocol_id ;
		L_protocol_info->m_name = L_protocol_name ;
		L_protocol_inst_list.push_back(L_protocol_info);
		m_name_map
		  ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
							 L_protocol_id)) ;
	      }
	    } else if (strcmp(L_protocol_type, "binary-tlv") == 0) {
	      // create protocol instance
	      C_ProtocolTlv    *L_protocol_instance = NULL ;
	      T_ConstructorResult  L_res ;

	      NEW_VAR(L_protocol_instance,
		      C_ProtocolTlv());
              
              L_protocol_instance->construction_data(L_data, &L_protocol_name, &L_res) ;
              
	      if (L_res != E_CONSTRUCTOR_OK) {
		GEN_ERROR(E_GEN_FATAL_ERROR,
			  "Error found in protocol definition");
		DELETE_VAR(L_protocol_instance);
		L_ret = false ;
		break ;
	      } else {
		// store new instance
		if (P_display_protocol_stats == true) {
		  C_ProtocolStats  *L_protocol_stats ;
		  NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
		  L_protocol_instance->set_stats(L_protocol_stats);
		}
                
		L_protocol_id = m_id_gen->new_id() ;
		ALLOC_VAR(L_protocol_info,
			  T_pProtocolInstanceInfo,
			  sizeof(T_ProtocolInstanceInfo));
                
		L_protocol_info->m_instance = L_protocol_instance ;
		L_protocol_info->m_id = L_protocol_id ;
		L_protocol_info->m_name = L_protocol_name ;
		L_protocol_inst_list.push_back(L_protocol_info);
		m_name_map
		  ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
							 L_protocol_id)) ;
	      }
	    } else if (strcmp(L_protocol_type, "binary-body-not-interpreted") == 0) {
	      // create protocol instance
	      C_ProtocolBinaryBodyNotInterpreted    *L_protocol_instance = NULL ;

	      T_ConstructorResult  L_res ;


	      NEW_VAR(L_protocol_instance,
		      C_ProtocolBinaryBodyNotInterpreted());
              
              L_protocol_instance->construction_data(L_data, &L_protocol_name, &L_res) ;
              
	      if (L_res != E_CONSTRUCTOR_OK) {
		GEN_ERROR(E_GEN_FATAL_ERROR,
			  "Error found in protocol definition");
		DELETE_VAR(L_protocol_instance);
		L_ret = false ;
		break ;
	      } else {
		// store new instance
		if (P_display_protocol_stats == true) {
		  C_ProtocolStats  *L_protocol_stats ;
		  NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
		  L_protocol_instance->set_stats(L_protocol_stats);
		}
                
		L_protocol_id = m_id_gen->new_id() ;
		ALLOC_VAR(L_protocol_info,
			  T_pProtocolInstanceInfo,
			  sizeof(T_ProtocolInstanceInfo));
                
		L_protocol_info->m_instance = L_protocol_instance ;
		L_protocol_info->m_id = L_protocol_id ;
		L_protocol_info->m_name = L_protocol_name ;
		L_protocol_inst_list.push_back(L_protocol_info);
		m_name_map
		  ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
							 L_protocol_id)) ;
	      }

	    } else if (strcmp(L_protocol_type, "binary-separator") == 0) {

	      // create protocol instance
	      C_ProtocolBinarySeparator    *L_protocol_instance = NULL ;

	      T_ConstructorResult  L_res ;


	      NEW_VAR(L_protocol_instance,
		      C_ProtocolBinarySeparator());
              
              L_protocol_instance
		->construction_data(L_data, &L_protocol_name, &L_res) ;
              
	      if (L_res != E_CONSTRUCTOR_OK) {
		GEN_ERROR(E_GEN_FATAL_ERROR,
			  "Error found in protocol definition");
		DELETE_VAR(L_protocol_instance);
		L_ret = false ;
		break ;
	      } else {
		// store new instance
		if (P_display_protocol_stats == true) {
		  C_ProtocolStats  *L_protocol_stats ;
		  NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
		  L_protocol_instance->set_stats(L_protocol_stats);
		}
                
		L_protocol_id = m_id_gen->new_id() ;
		ALLOC_VAR(L_protocol_info,
			  T_pProtocolInstanceInfo,
			  sizeof(T_ProtocolInstanceInfo));
                
		L_protocol_info->m_instance = L_protocol_instance ;
		L_protocol_info->m_id = L_protocol_id ;
		L_protocol_info->m_name = L_protocol_name ;
		L_protocol_inst_list.push_back(L_protocol_info);
		m_name_map
		  ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
							 L_protocol_id)) ;
	      }
	    } else if (strcmp(L_protocol_type, "external-library") == 0) {
              
	      C_ProtocolExternal    *L_protocol_instance = NULL ;
	      T_ConstructorResult    L_res = E_CONSTRUCTOR_OK ;
	      NEW_VAR(L_protocol_instance,
		      C_ProtocolExternal(m_transport_control, 
					 L_data, &L_protocol_name, 
					 P_config_value_list,
					 &L_res));

	      if (L_res != E_CONSTRUCTOR_OK) {
		GEN_ERROR(E_GEN_FATAL_ERROR,
			  "Error found in protocol definition");
		DELETE_VAR(L_protocol_instance);
		L_ret = false ;
		break ;
	      } else {

		if (P_display_protocol_stats == true) {
		  C_ProtocolStats  *L_protocol_stats ;
		  NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
		  L_protocol_instance->set_stats(L_protocol_stats);
		}

		// store new instance
		L_protocol_id = m_id_gen->new_id() ;
		ALLOC_VAR(L_protocol_info,
			  T_pProtocolInstanceInfo,
			  sizeof(T_ProtocolInstanceInfo));

		L_protocol_info->m_instance = L_protocol_instance ;
		L_protocol_info->m_id = L_protocol_id ;
		L_protocol_info->m_name = L_protocol_name ;
		L_protocol_inst_list.push_back(L_protocol_info);
		m_name_map
		  ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
							 L_protocol_id)) ;
	      }
	    }  else if (strcmp(L_protocol_type, "text") == 0) {
              
	      C_ProtocolText    *L_protocol_instance = NULL ;
	      T_ConstructorResult    L_res = E_CONSTRUCTOR_OK ;

	      NEW_VAR(L_protocol_instance,
		      C_ProtocolText());
              
              L_protocol_instance->analyze_data(L_data, 
                                                &L_protocol_name, 
                                                P_config_value_list,
                                                &L_res);

            if (L_res != E_CONSTRUCTOR_OK) {
		GEN_ERROR(E_GEN_FATAL_ERROR,
			  "Error found in protocol definition");
		DELETE_VAR(L_protocol_instance);
		L_ret = false ;
		break ;
	      } else {

		if (P_display_protocol_stats == true) {
		  C_ProtocolStats  *L_protocol_stats ;
		  NEW_VAR(L_protocol_stats,C_ProtocolStats(L_protocol_instance));
		  L_protocol_instance->set_stats(L_protocol_stats);
		}

		// store new instance
		L_protocol_id = m_id_gen->new_id() ;
		ALLOC_VAR(L_protocol_info,
			  T_pProtocolInstanceInfo,
			  sizeof(T_ProtocolInstanceInfo));

		L_protocol_info->m_instance = L_protocol_instance ;
		L_protocol_info->m_id = L_protocol_id ;
		L_protocol_info->m_name = L_protocol_name ;
		L_protocol_inst_list.push_back(L_protocol_info);
		m_name_map
		  ->insert(T_ProtocolNameMap::value_type(L_protocol_name,
							 L_protocol_id)) ;
	      }
	    } else {
	      GEN_ERROR(E_GEN_FATAL_ERROR,
		        XML_PROTOCOL_SECTION 
			<< " [" 
			<< L_protocol_name
			<< "] with type ["
			<< L_protocol_type << "] unsupported");
	      L_ret = false ;
	    }
	  }
	}
      }
      if (L_ret != false) {
	if (!L_protocol_inst_list.empty()) {
	  m_protocol_table_size = L_protocol_inst_list.size() ;
	  ALLOC_TABLE(m_protocol_table,
		      T_pC_ProtocolFrame*,
		      sizeof(T_pC_ProtocolFrame),
		      m_protocol_table_size) ;
	  ALLOC_TABLE(m_protocol_name_table,
		      char**,
		      sizeof(char*),
		      m_protocol_table_size) ;
	  for (L_it  = L_protocol_inst_list.begin();
	       L_it != L_protocol_inst_list.end()  ;
	       L_it++) {
	    L_protocol_info = *L_it ;
	    m_protocol_table[L_protocol_info->m_id] 
	      = L_protocol_info->m_instance ;
	    m_protocol_name_table[L_protocol_info->m_id]
	      = L_protocol_info->m_name ;
	  }
	} else {
	  GEN_ERROR(E_GEN_FATAL_ERROR,
		    "No protocol definition found"); 
	  L_ret = false ;
	}
      } // if L_ret != false 
      
      if (!L_protocol_inst_list.empty()) {
	  for (L_it  = L_protocol_inst_list.begin();
	       L_it != L_protocol_inst_list.end()  ;
	       L_it++) {
	    FREE_VAR(*L_it);
	  }
	  L_protocol_inst_list.erase(L_protocol_inst_list.begin(),
				     L_protocol_inst_list.end());
      }
    } 
int main(int argc, char ** argv)
{
    int                       exit_code = EXIT_FAILURE;
    char                    * version = strdup("version 1.0");
    const char              * usage = "usage: %s [options] host\n";
    void                    * algorithm_options;
    traceroute_options_t      traceroute_options;
    traceroute_options_t    * ptraceroute_options;
    mda_options_t             mda_options;
    probe_t                 * probe;
    pt_loop_t               * loop;
    int                       family;
    address_t                 dst_addr;
    options_t               * options;
    char                    * dst_ip;
    const char              * algorithm_name;
    const char              * protocol_name;
    bool                      use_icmp, use_udp, use_tcp;

    // Prepare the commande line options
    if (!(options = init_options(version))) {
        fprintf(stderr, "E: Can't initialize options\n");
        goto ERR_INIT_OPTIONS;
    }

    // Retrieve values passed in the command-line
    if (options_parse(options, usage, argv) != 1) {
        fprintf(stderr, "%s: destination required\n", basename(argv[0]));
        goto ERR_OPT_PARSE;
    }

    // We assume that the target IP address is always the last argument
    dst_ip         = argv[argc - 1];
    algorithm_name = algorithm_names[0];
    protocol_name  = protocol_names[0];

    // Checking if there is any conflicts between options passed in the commandline
    if (!check_options(is_icmp, is_tcp, is_udp, is_ipv4, is_ipv6, dst_port[3], src_port[3], protocol_name, algorithm_name)) {
        goto ERR_CHECK_OPTIONS;
    }

    use_icmp = is_icmp || strcmp(protocol_name, "icmp") == 0;
    use_tcp  = is_tcp  || strcmp(protocol_name, "tcp")  == 0;
    use_udp  = is_udp  || strcmp(protocol_name, "udp")  == 0;

    // If not any ip version is set, call address_guess_family.
    // If only one is set to true, set family to AF_INET or AF_INET6
    if (is_ipv4) {
        family = AF_INET;
    } else if (is_ipv6) {
        family = AF_INET6;
    } else {
        // Get address family if not defined by the user
        if (!address_guess_family(dst_ip, &family)) goto ERR_ADDRESS_GUESS_FAMILY;
    }

    // Translate the string IP / FQDN into an address_t * instance
    if (address_from_string(family, dst_ip, &dst_addr) != 0) {
        fprintf(stderr, "E: Invalid destination address %s\n", dst_ip);
        goto ERR_ADDRESS_IP_FROM_STRING;
    }

    // Probe skeleton definition: IPv4/UDP probe targetting 'dst_ip'
    if (!(probe = probe_create())) {
        fprintf(stderr,"E: Cannot create probe skeleton");
        goto ERR_PROBE_CREATE;
    }

    // Prepare the probe skeleton
    probe_set_protocols(
        probe,
        get_ip_protocol_name(family),                          // "ipv4"   | "ipv6"
        get_protocol_name(family, use_icmp, use_tcp, use_udp), // "icmpv4" | "icmpv6" | "tcp" | "udp"
        NULL
    );

    probe_set_field(probe, ADDRESS("dst_ip", &dst_addr));

    if (send_time[3]) {
        if(send_time[0] <= 10) { // seconds
            probe_set_delay(probe, DOUBLE("delay", send_time[0]));
        } else { // milli-seconds
            probe_set_delay(probe, DOUBLE("delay", 0.001 * send_time[0]));
        }
    }

    // ICMPv* do not support src_port and dst_port fields nor payload.
    if (!use_icmp) {
        uint16_t sport = 0,
                 dport = 0;

        if (use_udp) {
            // Option -U sets port to 53 (DNS) if dst_port is not explicitely set
            sport = src_port[3] ? src_port[0] : UDP_DEFAULT_SRC_PORT;
            dport = dst_port[3] ? dst_port[0] : (is_udp ? UDP_DST_PORT_USING_U : UDP_DEFAULT_DST_PORT);
        } else if (use_tcp) {
            // Option -T sets port to 80 (http) if dst_port is not explicitely set
            sport = src_port[3] ? src_port[0] : TCP_DEFAULT_SRC_PORT;
            dport = dst_port[3] ? dst_port[0] : (is_tcp ? TCP_DST_PORT_USING_T : TCP_DEFAULT_DST_PORT);
        }

        // Update ports
        probe_set_fields(
            probe,
            I16("src_port", sport),
            I16("dst_port", dport),
            NULL
        );

        // Resize payload (it will be use to set our customized checksum in the {TCP, UDP} layer)
        probe_payload_resize(probe, 2);
    }

    // Algorithm options (dedicated options)
    if (strcmp(algorithm_name, "paris-traceroute") == 0) {
        traceroute_options  = traceroute_get_default_options();
        ptraceroute_options = &traceroute_options;
        algorithm_options   = &traceroute_options;
        algorithm_name      = "traceroute";
    } else if ((strcmp(algorithm_name, "mda") == 0) || options_mda_get_is_set()) {
        mda_options         = mda_get_default_options();
        ptraceroute_options = &mda_options.traceroute_options;
        algorithm_options   = &mda_options;
        options_mda_init(&mda_options);
    } else {
        fprintf(stderr, "E: Unknown algorithm");
        goto ERR_UNKNOWN_ALGORITHM;
    }

    // Algorithm options (common options)
    options_traceroute_init(ptraceroute_options, &dst_addr);

    // Create libparistraceroute loop
    if (!(loop = pt_loop_create(loop_handler, NULL))) {
        fprintf(stderr, "E: Cannot create libparistraceroute loop");
        goto ERR_LOOP_CREATE;
    }

    // Set network options (network and verbose)
    options_network_init(loop->network, is_debug);

    printf("%s to %s (", algorithm_name, dst_ip);
    address_dump(&dst_addr);
    printf("), %u hops max, %u bytes packets\n",
           ptraceroute_options->max_ttl,
           (unsigned int)packet_get_size(probe->packet)
          );

    // Add an algorithm instance in the main loop
    if (!pt_add_instance(loop, algorithm_name, algorithm_options, probe)) {
        fprintf(stderr, "E: Cannot add the chosen algorithm");
        goto ERR_INSTANCE;
    }

    // Wait for events. They will be catched by handler_user()
    if (pt_loop(loop, 0) < 0) {
        fprintf(stderr, "E: Main loop interrupted");
        goto ERR_PT_LOOP;
    }
    exit_code = EXIT_SUCCESS;

    // Leave the program
ERR_PT_LOOP:
ERR_INSTANCE:
    // pt_loop_free() automatically removes algorithms instances,
    // probe_replies and events from the memory.
    // Options and probe must be manually removed.
    pt_loop_free(loop);
ERR_LOOP_CREATE:
ERR_UNKNOWN_ALGORITHM:
    probe_free(probe);
ERR_PROBE_CREATE:
ERR_ADDRESS_IP_FROM_STRING:
ERR_ADDRESS_GUESS_FAMILY:
    if (errno) perror(gai_strerror(errno));
ERR_CHECK_OPTIONS:
ERR_OPT_PARSE:
ERR_INIT_OPTIONS:
    free(version);
    exit(exit_code);
}
Beispiel #14
0
void Flow::lua(lua_State* vm, bool detailed_dump) {
  char buf[64];

  lua_newtable(vm);

  if(get_cli_host()) {
    if(detailed_dump) lua_push_str_table_entry(vm, "cli.host", get_cli_host()->get_name(buf, sizeof(buf), false));
    lua_push_int_table_entry(vm, "cli.source_id", get_cli_host()->getSourceId());
    lua_push_str_table_entry(vm, "cli.ip", get_cli_host()->get_ip()->print(buf, sizeof(buf)));
    lua_push_bool_table_entry(vm, "cli.systemhost", get_cli_host()->isSystemHost());
  } else {
    lua_push_nil_table_entry(vm, "cli.host");
    lua_push_nil_table_entry(vm, "cli.ip");
  }

  lua_push_int_table_entry(vm, "cli.port", get_cli_port());

  if(get_srv_host()) {
    if(detailed_dump) lua_push_str_table_entry(vm, "srv.host", get_srv_host()->get_name(buf, sizeof(buf), false));
    lua_push_int_table_entry(vm, "srv.source_id", get_cli_host()->getSourceId());
    lua_push_str_table_entry(vm, "srv.ip", get_srv_host()->get_ip()->print(buf, sizeof(buf)));
    lua_push_bool_table_entry(vm, "srv.systemhost", get_srv_host()->isSystemHost());
  } else {
    lua_push_nil_table_entry(vm, "srv.host");
    lua_push_nil_table_entry(vm, "srv.ip");
  }

  lua_push_int_table_entry(vm, "srv.port", get_srv_port());
  lua_push_int_table_entry(vm, "vlan", get_vlan_id());
  lua_push_str_table_entry(vm, "proto.l4", get_protocol_name());

  if(((cli2srv_packets+srv2cli_packets) > NDPI_MIN_NUM_PACKETS)
     || (ndpi_detected_protocol != NDPI_PROTOCOL_UNKNOWN)
     || iface->is_ndpi_enabled()
     || iface->is_sprobe_interface()) {
    lua_push_str_table_entry(vm, "proto.ndpi", get_detected_protocol_name());
  } else
    lua_push_str_table_entry(vm, "proto.ndpi", (char*)CONST_TOO_EARLY);

  lua_push_int_table_entry(vm, "bytes", cli2srv_bytes+srv2cli_bytes);
  lua_push_int_table_entry(vm, "bytes.last", get_current_bytes_cli2srv() + get_current_bytes_srv2cli());
  lua_push_int_table_entry(vm, "seen.first", get_first_seen());
  lua_push_int_table_entry(vm, "seen.last", get_last_seen());
  lua_push_int_table_entry(vm, "duration", get_duration());

  lua_push_int_table_entry(vm, "cli2srv.bytes", cli2srv_bytes);
  lua_push_int_table_entry(vm, "srv2cli.bytes", srv2cli_bytes);

  if(detailed_dump) {
    lua_push_int_table_entry(vm, "tcp_flags", getTcpFlags());
    lua_push_str_table_entry(vm, "category", categorization.category ? categorization.category : (char*)"");
    lua_push_str_table_entry(vm, "moreinfo.json", get_json_info());
  }

  if(client_proc) processLua(vm, client_proc, true);
  if(server_proc) processLua(vm, server_proc, false);

  //ntop->getTrace()->traceEvent(TRACE_NORMAL, "%.2f", bytes_thpt);
  lua_push_float_table_entry(vm, "throughput", bytes_thpt);
  lua_push_int_table_entry(vm, "throughput_trend", bytes_thpt_trend);

  if(!detailed_dump) {
    lua_pushinteger(vm, key()); // Index
    lua_insert(vm, -2);
    lua_settable(vm, -3);
  } else {
    lua_push_int_table_entry(vm, "cli2srv.packets", cli2srv_packets);
    lua_push_int_table_entry(vm, "srv2cli.packets", srv2cli_packets);
  }
}