Exemplo n.º 1
0
void
init(void)
{
	app_init();
	doi_init();
	exchange_init();
	group_init();
	ipsec_init();
	isakmp_doi_init();
	libcrypto_init();

	timer_init();

	/* The following group are depending on timer_init having run.  */
	conf_init();
	connection_init();

	/* This depends on conf_init, thus check as soon as possible. */
	log_reinit();

	/* policy_init depends on conf_init having run.  */
	policy_init();

	/* Depends on conf_init and policy_init having run */
	cert_init();
	crl_init();

	sa_init();
	transport_init();
	virtual_init();
	udp_init();
	nat_t_init();
	udp_encap_init();
	vendor_init();
}
Exemplo n.º 2
0
int logger_init(unsigned int _logLevel, const char *endpoint, int timeout_milliseconds)
{
  prong_assert(logLevel >= LOG_SEVERITY_DEBUG);
  prong_assert(logLevel <= LOG_SEVERITY_SEVERE);
  prong_assert(endpoint != NULL);

  if (id != NULL)
  {
    g_free(id);
  }
  id = g_strdup_printf("PID(%d) %s", getpid(), basename_safe(PROCESS_NAME));

  logLevel = _logLevel;

  if (transport == NULL)
  {
    transport_close(transport);
  }
  transport = transport_init(TRANSPORT_TYPE_PUSH, endpoint);
  if (transport == NULL)
  {
    g_free(id);
    return -1;
  }

  transport_set_send_timeout(transport, timeout_milliseconds);

  return 0;
}
Exemplo n.º 3
0
static MsgStatus msg_is_spam(FILE *fp)
{
	struct transport trans;
	struct message m;
	gboolean is_spam = FALSE;

	if (!config.enable)
		return MSG_IS_HAM;

	transport_init(&trans);
	switch (config.transport) {
	case SPAMASSASSIN_TRANSPORT_LOCALHOST:
		trans.type = TRANSPORT_LOCALHOST;
		trans.port = config.port;
		break;
	case SPAMASSASSIN_TRANSPORT_TCP:
		trans.type = TRANSPORT_TCP;
		trans.hostname = config.hostname;
		trans.port = config.port;
		break;
	case SPAMASSASSIN_TRANSPORT_UNIX:
		trans.type = TRANSPORT_UNIX;
		trans.socketpath = config.socket;
		break;
	default:
		return MSG_IS_HAM;
	}

	if (transport_setup(&trans, flags) != EX_OK) {
		log_error(LOG_PROTOCOL, _("SpamAssassin plugin couldn't connect to spamd.\n"));
		debug_print("failed to setup transport\n");
		return MSG_FILTERING_ERROR;
	}

	m.type = MESSAGE_NONE;
	m.max_len = config.max_size * 1024;
	m.timeout = config.timeout;

	if (message_read(fileno(fp), flags, &m) != EX_OK) {
		debug_print("failed to read message\n");
		message_cleanup(&m);
		return MSG_FILTERING_ERROR;
	}

	if (message_filter(&trans, config.username, flags, &m) != EX_OK) {
		log_error(LOG_PROTOCOL, _("SpamAssassin plugin filtering failed.\n"));
		debug_print("filtering the message failed\n");
		message_cleanup(&m);
		return MSG_FILTERING_ERROR;
	}

	if (m.is_spam == EX_ISSPAM)
		is_spam = TRUE;

	message_cleanup(&m);

	return is_spam ? MSG_IS_SPAM:MSG_IS_HAM;
}
void usb_monitor(void)
{
#if ASIC_TYP==ASIC_TYP_NETX56 || ASIC_TYP==ASIC_TYP_NETX10
	options_set_default();
#endif

	transport_init();

	while(1)
	{
		transport_loop();
	}
}
Exemplo n.º 5
0
/* transport layer thread; transport_init() should not return until the
 * transport layer finishes (i.e. the connection is over).
 */
static void *transport_thread_func(void *arg_ptr)
{
    mysock_context_t *ctx = (mysock_context_t *) arg_ptr;
    char eof_packet;

    assert(ctx);
    ASSERT_VALID_MYSOCKET_DESCRIPTOR(ctx, ctx->my_sd);

    /* enter the STCP control loop.  transport_init() doesn't return until the
     * connection's finished.  that function should first signal establishment
     * of the connection after SYN/SYN-ACK (or an error condition if the
     * connection couldn't be established) to the application by using
     * stcp_unblock_application(); as the name suggests, this unblocks the
     * calling code.  transport_init() then handles the connection,
     * returning only after the connection is closed.
     */
    transport_init(ctx->my_sd, ctx->is_active);

    /* transport_init() has returned; both sides have closed the connection,
     * do some final cleanup here...
     */

    PTHREAD_CALL(pthread_mutex_lock(&ctx->blocking_lock));
    if (ctx->blocking)
    {
        /* if we're still blocked, STCP must not have indicated the
         * connection completed.  pass the error up to the application.
         */
        if (errno == 0 || errno == EINTR)
        {
            /* this is a bit of a kludge--this should really be set by STCP
             * itself, but it's a reasonable guess if for some reason (e.g.
             * oversight) the transport layer hasn't announced why it
             * bailed out...
             */
            errno = (ctx->is_active) ? ECONNREFUSED : ECONNABORTED;
        }
        PTHREAD_CALL(pthread_mutex_unlock(&ctx->blocking_lock));
        stcp_unblock_application(ctx->my_sd);
    }
    else
    {
        PTHREAD_CALL(pthread_mutex_unlock(&ctx->blocking_lock));
    }

    /* force final myread() to return 0 bytes (this should have been done
     * by the transport layer already in response to the peer's FIN).
     */
    _mysock_enqueue_buffer(ctx, &ctx->app_send_queue, &eof_packet, 0);
    return NULL;
}
Exemplo n.º 6
0
static int
snmpd_init(lua_State *L)
{
  int port;
  
  mib_init();

  port = luaL_checkint(L, 1);
  transport_init(port, snmpd_receive);

  lua_pushboolean(L, 1);

  return 1;  
}
Exemplo n.º 7
0
Channel* channel_new(gint handle, gint linkedHandle, enum ChannelType type) {
	Channel* channel = g_new0(Channel, 1);
	MAGIC_INIT(channel);

	transport_init(&(channel->super), &channel_functions, DT_PIPE, handle);

	channel->type = type;
	channel->buffer = bytequeue_new(8192);
	channel->bufferSize = CONFIG_PIPE_BUFFER_SIZE;
	channel->linkedHandle = linkedHandle;

	descriptor_adjustStatus((Descriptor*)channel, DS_ACTIVE, TRUE);
	if(!(type & CT_READONLY)) {
		descriptor_adjustStatus((Descriptor*)channel, DS_WRITABLE, TRUE);
	}

	return channel;
}
Exemplo n.º 8
0
void* TransportCreate(TransportParams* params)
{
  int rc = 0;
  Transport_IF* hTransporter;
  if (params->modType == params->singleTransporter) {
    hTransporter = transport_load_transporter();
  } else {
    hTransporter = transport_load_transport_client();
  }
  if (hTransporter) {rc = transport_init(hTransporter, params);}
  //init error
  if (rc) {
    transport_final(hTransporter);
    transport_release(hTransporter);
    hTransporter = 0;
  }
  return hTransporter;
};
Exemplo n.º 9
0
static int upnp_renderer_init(void)
{
    int err_no = 0;
	
	err_no = control_init();
    if (err_no != 0)
    {
        return err_no;
    }
	
	err_no = transport_init();
	if (err_no != 0)
    {
        return err_no;
    }
	
    return connmgr_init();
}
Exemplo n.º 10
0
void socket_init(Socket* socket, SocketFunctionTable* vtable, DescriptorType type, gint handle,
        guint receiveBufferSize, guint sendBufferSize) {
    utility_assert(socket && vtable);

    transport_init(&(socket->super), &socket_functions, type, handle);

    MAGIC_INIT(socket);
    MAGIC_INIT(vtable);

    socket->vtable = vtable;

    socket->protocol = type == DT_TCPSOCKET ? PTCP : type == DT_UDPSOCKET ? PUDP : PLOCAL;
    socket->inputBuffer = g_queue_new();
    socket->inputBufferSize = receiveBufferSize;
    socket->outputBuffer = g_queue_new();
    socket->outputBufferSize = sendBufferSize;

    Tracker* tracker = host_getTracker(worker_getCurrentHost());
    Descriptor* descriptor = (Descriptor *)socket;
    tracker_addSocket(tracker, descriptor->handle, socket->protocol, socket->inputBufferSize, socket->outputBufferSize);
}
Exemplo n.º 11
0
/*****************************************************************************
* Interface functions
*****************************************************************************/
void bootloader_init(void)
{
    rtc_init();

    memset(&m_flash_fifo, 0, sizeof(fifo_t));
    m_flash_fifo.elem_array = m_flash_fifo_buf;
    m_flash_fifo.elem_size = sizeof(flash_queue_entry_t);
    m_flash_fifo.array_len = FLASH_FIFO_SIZE;
    fifo_init(&m_flash_fifo);
    NVIC_SetPriority(FLASH_HANDLER_IRQn, 3);
    NVIC_EnableIRQ(FLASH_HANDLER_IRQn);

    bootloader_app_bridge_init();

    bl_cmd_t init_cmd;
    init_cmd.type = BL_CMD_TYPE_INIT;
    init_cmd.params.init.bl_if_version = BL_IF_VERSION;
    init_cmd.params.init.event_callback = bl_evt_handler;
    init_cmd.params.init.timer_count = 1;
    init_cmd.params.init.tx_slots = TRANSPORT_TX_SLOTS;
    init_cmd.params.init.in_app = false;
    APP_ERROR_CHECK(bl_cmd_handler(&init_cmd));

#ifdef RBC_MESH_SERIAL
    mesh_aci_init();
#endif

    transport_init(rx_cb, RBC_MESH_ACCESS_ADDRESS_BLE_ADV);

    bool dfu_bank_flash_start;
    dfu_bank_scan(&dfu_bank_flash_start);
    if (dfu_bank_flash_start)
    {
        /* A bank is to be flashed. Attempt to go to app if possible. */
        NRF_POWER->GPREGRET = RBC_MESH_GPREGRET_CODE_GO_TO_APP;
    }
}
void uart_monitor(void)
{
#if ASIC_TYP==ASIC_TYP_NETX56
	unsigned long ulRomId;
	unsigned long ulConsoleDevice;
#endif
#if ASIC_TYP==ASIC_TYP_NETX50
	unsigned long ulConsoleDevice;
#endif


	systime_init();

#if ASIC_TYP==ASIC_TYP_NETX500
	/* Both ASICs in this group can not use the ROM routines for UART
	 * communication.
	 * 
	 * The netX500 and netX100 ROM code UART put routine converts LF (0x0a)
	 * to CR LF (0x0d 0x0a). It is not possible to send binary data with
	 * it. Replace the vectors with custom routines.
	 */

	/* Initialize the UART. */
	uart_init(&tUartCfg);

	/* Set the new vectors. */
	tSerialV1Vectors.fn.fnGet   = uart_get;
	tSerialV1Vectors.fn.fnPut   = uart_put;
	tSerialV1Vectors.fn.fnPeek  = uart_peek;
	tSerialV1Vectors.fn.fnFlush = uart_flush;
#elif ASIC_TYP==ASIC_TYP_NETX10
	/* The netX10 ROM code uses areas in bank0 around offset 0x8180. This
	 * is outside the RAM area reserved for the monitor code.
	 */

	/* Set the new vectors. */
	tSerialV1Vectors.fn.fnGet   = uart_get;
	tSerialV1Vectors.fn.fnPut   = uart_put;
	tSerialV1Vectors.fn.fnPeek  = uart_peek;
	tSerialV1Vectors.fn.fnFlush = uart_flush;
#elif ASIC_TYP==ASIC_TYP_NETX50
	/* Compare vectors to netx50 USB. This one needs special treatment. */
	if( memcmp(&tSerialV2Vectors, &tSerialNetx50UsbVectors, sizeof(SERIAL_V2_COMM_FN_T))==0 )
	{
		/* USB CDC */
		usb_init();
		ulConsoleDevice = (unsigned long)CONSOLE_DEVICE_USB;
	}
	else
	{
		/* UART */

		/* In this mode the USB endpoints are in the state "not configured". */
		tReceiveEpState = USB_EndpointState_Unconfigured;

		/* Copy the ROM code vectors to an internal buffer. */
		memcpy(&tSerialV1Vectors, &tSerialV2Vectors, sizeof(SERIAL_V2_COMM_FN_T));
		ulConsoleDevice = (unsigned long)CONSOLE_DEVICE_UART;
	}
	transport_set_vectors(ulConsoleDevice);
#elif ASIC_TYP==ASIC_TYP_NETX56
	ulRomId = aulRomId[2];
	if( ulRomId==ROM_CODE_ID_NETX56 )
	{
		ulConsoleDevice = aulConsoleDevices_netx56[0];
	}
	else if( ulRomId==ROM_CODE_ID_NETX56B )
	{
		ulConsoleDevice = aulConsoleDevices_netx56b[0];
	}
	else
	{
		ulConsoleDevice = (unsigned long)CONSOLE_DEVICE_NONE;
	}
	if( ulConsoleDevice!=((unsigned long)CONSOLE_DEVICE_USB) && ulConsoleDevice!=((unsigned long)CONSOLE_DEVICE_UART0) )
	{
		while(1) {};
	}
	transport_set_vectors(ulConsoleDevice);
#else
#       error "Unknown ASIC_TYP!"
#endif

	transport_init();

	while(1)
	{
		transport_loop();
	}
}
Exemplo n.º 13
0
/**
 * Starts the harness.
 *
 * \param argc Num of args
 * \param argv The args
 * \returns 0 on success, -1 on error
 */
int main(int argc, char *argv[])
{
  SHORT_PROCESS_NAME = basename_safe(argv[0]);
  PROCESS_NAME = argv[0];

  // We accept only one argument, namely the transport address of the
  // global configuration server
  if (argc != 2)
  {
    print_usage();
    return -1;
  }

  if (config_init(argv[1]) != 0)
  {
    error_log("Unable to setup test harness. Is there a problem with the endpoint syntax? %s", argv[1]);
    return -1;
  }

  int ret = 0;

  ret = logger_config_init();

  if (ret != 0)
  {
    error_log("Failed to create log transport");
    cleanup(NULL);
    return -1;
  }

  char *test_harness_listen_endpoint = g_strdup_printf("%s-%d", TEST_SOCKET, getpid());

  debug_log("Test harness is listening on %s", test_harness_listen_endpoint);
  transport_t transport = transport_init(TRANSPORT_TYPE_PUSHPULL, test_harness_listen_endpoint);

  if (transport == NULL)
  {
    error_log("Unable to create transport");
    g_free(test_harness_listen_endpoint);
    cleanup(NULL);
    return -1;
  }

  if (spawn_subcontractor(argv[1], test_harness_listen_endpoint) != 0)
  {
    error_log("Unable to spawn subcontractor");
    g_free(test_harness_listen_endpoint);
    cleanup(transport);
    return -1;
  }
  g_free(test_harness_listen_endpoint);
  test_harness_listen_endpoint = NULL;

  char *input_file;

  if ((config_get(NULL, CONFIG_INPUT_FILE_OPTION_NAME, &input_file) != 0) || (input_file == NULL))
  {
    error_log("Could not get input file name");
    cleanup(transport);
    return -1;
  }

  char *subcontractor;

  if ((config_get(NULL, CONFIG_TEST_HARNESS_SUBCONTRACTOR_TO_TEST_OPTION_NAME, &subcontractor) != 0) || (subcontractor == NULL))
  {
    error_log("Unable to get the name of the subcontractor which means I couldn't calculate the timeout!");
    return -1;
  }

  const char *sub_name = basename_safe(subcontractor);

  long timeout = CONFIG_CONTRACTOR_SUBCONTRACTOR_TRANSPORT_TIMEOUT_DEFAULT;

  if (config_get_long_group_or_general_with_default_macro(sub_name, CONFIG_CONTRACTOR_SUBCONTRACTOR_TRANSPORT_TIMEOUT, &timeout) != 0)
  {
    info_log("Couldn't find out what timeout option to use, using default");
  }

  transport_set_recv_timeout(transport, timeout);
  debug_log("Using %li for timeout to subcontractor", timeout);

  unsigned int contract_string_size;
  contract_t c = contract_init(NULL, 0);

  contract_set_path(c, input_file);
  contract_set_contiguous(c, 1);
  contract_set_absolute_offset(c, 0);
  char *contract_string = contract_serialise(c, &contract_string_size);

  contract_close(c);
  g_free(input_file);
  if (contract_string == NULL)
  {
    perror("Failed to create contract_string");
    g_free(contract_string);
    g_free(subcontractor);
    cleanup(transport);
    return -1;
  }

  info_log("Sending message");

  unsigned int subcontractor_result_msg_size;
  const char *subcontractor_result_msg = transport_sendrecv(transport, contract_string, contract_string_size, &child_pid, &subcontractor_result_msg_size);

  if (subcontractor_result_msg == NULL)
  {

    if (errno == EAGAIN)
    {
      // Timeout.
      error_log
        ("Timeout in receiving message from subcontractor! Either your subcontractor hung (it will be killed when running in pronghorn) or it didn't compelete in time. You need to make sure that the timeout is long enough, by setting subcontractor_timeout (specifcally for your sub contractor or the general case). In pronghorn, this would be via the config file. Using the test harness, you can specificy this using \"-o %s.subcontractor_timeout=<timeout in milliseconds>\" or \"-o general.subcontractor_timeout=<timeout in milliseconds>\" at the end of the command line",
         sub_name);

    } else if (errno == EINTR)
    {
      // Interupted.
      error_log("Something when wrong waiting for your subcontractor to return. Did it crash?! You might like to try adding \"-o %s.valgrind_opts=<path to valgrind>\" when testing.", sub_name);
    }

    cleanup(transport);
    g_free(contract_string);
    g_free(subcontractor);
    return -1;
  }

  g_free(subcontractor);

  contract_completion_report_t report = contract_completion_report_init(subcontractor_result_msg, subcontractor_result_msg_size);

  if (report == NULL)
  {
    perror("Recreating contract_completion_report");
    g_free(contract_string);
    cleanup(transport);
    return -1;
  } else
  {
    info_log("Reconstructed message");
  }

  info_log("=====================");
  info_log("Results: ");
  unsigned int results_count;
  const result_t *results = contract_completion_report_get_results(report, &results_count);

  c = contract_completion_report_get_original_contract(report);;

  for (int i = 0; i < results_count; i++)
  {
    const result_t r = results[i];

    info_log("Path=%s. Type=(%s) %s. Confidence=%d", contract_get_path(c), result_get_brief_data_description(r), result_get_data_description(r), result_get_confidence(r));

#if PRINT_BLOCK_ARRAY
    unsigned int block_range_size;
    const block_range_t *ranges = result_get_block_ranges(r, &block_range_size);

    if (block_range_size > 0)
    {
      int j;

      info_log("Blocks:");

      // This is inefficient. But it works and this function is rarely used
      char *big_s = g_strdup("");

      for (j = 0; j < block_range_size; j++)
      {
        unsigned long long pos;
        unsigned long long len;

        block_range_get_range(ranges[j], &pos, &len);
        char *new_s = g_strdup_printf("%s %llu-%llu", big_s, pos, pos + len - 1);

        g_free(big_s);
        big_s = new_s;
      }
      info_log("%s", big_s);
      g_free(big_s);
    }
#endif // PRINT_BLOCK_ARRAY

    unsigned int new_contracts_count;

#if SIMPLE_CONTRACT_ENUMERATION
    result_get_new_contracts(r, &new_contracts_count);

    info_log("\t- With %d new contracts!", new_contracts_count);
#else // SIMPLE_CONTRACT_ENUMERATION
    // Yes, this looks dodgy, but it's fine until r gets destroyed (which is after we destroy our copy)
    const contract_t *new_contracts_const = result_get_new_contracts(r, &new_contracts_count);
    contract_t *new_contracts = (contract_t *) g_malloc(sizeof(contract_t) * new_contracts_count);

    memcpy(new_contracts, new_contracts_const, sizeof(contract_t) * new_contracts_count);

    if (new_contracts_count > 0)
    {
      // Sorting the array
#if SORT_CONTRACT_ARRAY
      {
        int j;

        for (j = 0; j < new_contracts_count - 1; j++)
        {
          int k;

          for (k = 0; k < new_contracts_count - j - 1; k++)
          {
            int a = atoi(contract_get_path(new_contracts[k]));
            int b = atoi(contract_get_path(new_contracts[k + 1]));

            if (a > b)
            {
              contract_t temp = new_contracts[k];

              new_contracts[k] = new_contracts[k + 1];
              new_contracts[k + 1] = temp;
            } else if (a == b)
            {
              warning_log("Duplicate entry found! %s", contract_get_path(new_contracts[k]));
            }
          }
        }
      }
#endif // SORT_CONTRACT_ARRAY
#if PRINT_CONTRACT_ARRAY
      {
        info_log("New contracts: ");
        int j;

        for (j = new_contracts_count - 1; j >= 0; j--)
        {
          const char *path = contract_get_path(new_contracts[j]);

          if (access(path, R_OK) == 0)
          {
            info_log("OK %s", path);
            print_md5(path);
          } else
          {
            info_log("Couldn't access advertised new path %s", path);
          }
        }
      }
#endif // PRINT_CONTRACT_ARRAY
    }
    g_free(new_contracts);
#endif // SIMPLE_CONTRACT_ENUMERATION
  }
  contract_completion_report_close(report);


  if (NUM_EXERCISE_LOOPS != 0)
  {
    info_log("=====================");
    info_log("Exercising the plugin");
    for (int i = 0; i < NUM_EXERCISE_LOOPS; i++)
    {
      subcontractor_result_msg = transport_sendrecv(transport, (char *) contract_string, contract_string_size, &child_pid, &subcontractor_result_msg_size);
    }

    info_log("Finished exercising the plugin");
  }

  info_log("=====================");

  info_log("Telling subcontractor to close nicely");

  g_free(contract_string);

  // Send empty contract to tell child to close
  c = contract_init(NULL, 0);
  contract_set_path(c, "");
  contract_string = contract_serialise(c, &contract_string_size);
  contract_close(c);
  subcontractor_result_msg = transport_sendrecv(transport, (char *) contract_string, contract_string_size, &child_pid, &subcontractor_result_msg_size);
  g_free(contract_string);

  if (child_pid != -1)
  {
    sleep(100);
  }

  volatile int local_child_pid = child_pid;

  if (local_child_pid != -1)
  {
    warning_log("Process hasn't died automatically. Sending SIGTERM");
    kill(local_child_pid, SIGTERM);
    sleep(10);
    local_child_pid = child_pid;
    if (local_child_pid != -1)
    {
      warning_log("Process STILL hasn't died... Killing it");
      kill(local_child_pid, SIGKILL);
    }
  }

  cleanup(transport);

  return 0;
}