Exemplo n.º 1
0
void *send_msg_thread(void *arg)
{
	assert(arg);
	wtp_handle_t* handle = (wtp_handle_t *) arg;
	aslan_msg_t *msg = NULL;
	uint8_t *buf = NULL;
	uint16_t length_ns;
	u8 *mac;
	int count, ret;

	while (pipe_pop(handle->msg_send_consumer, &msg, 1))
	{
		buf = calloc(1, msg->msg_length);
		if (!buf) break;
		mac = NULL;
		buf[0] = msg->msg_id;

		switch (msg->msg_id)
		{
			case MSG_ID_CTX_REQ:
				memcpy(buf + 1, msg->msg.ctx_req->MAC, 6);
				mac = msg->msg.ctx_req->MAC;
				break;

			case MSG_ID_ASSOC_RESP:
				memcpy(buf + 1, msg->msg.assoc_resp->MAC, 6);
				length_ns = htons(msg->msg.assoc_resp->sta_wtp_ctx_length);
				memcpy(buf + 7, &length_ns, 2);
				memcpy(buf + 9, msg->msg.assoc_resp->sta_wtp_ctx, msg->msg.assoc_resp->sta_wtp_ctx_length);
				break;

			case MSG_ID_DISASSOC_RESP:
				memcpy(buf + 1, msg->msg.disassoc_resp->MAC, 6);
				break;
		}

		count = 0;
		while (!count)
		{
			if (udp_lock(handle) != 0) continue;
			ret = sendto(handle->udp_socket, buf, msg->msg_length, 0, (struct sockaddr *) &handle->hds_inet_addr, sizeof(handle->hds_inet_addr));
			udp_unlock(handle);

			if (ret == -1) continue;

			sleep(WAIT_FOR_ACK_INTERVAL);
			pthread_mutex_lock(&handle->ack_mutex);
			count = handle->received_ack_count;
			handle->received_ack_count = 0;
			if ((count) && (mac) && (handle->ack_last_flag == 1)) wtp_sta_set_reject(mac);
			pthread_mutex_unlock(&handle->ack_mutex);
		}
		free(buf);
		free_msg(&msg);
	}

	pthread_exit(NULL);
    return NULL;
}
Exemplo n.º 2
0
static void* process_pipe(void* param)
{
    connect_data_t p = *(connect_data_t*)param;
    free(param);

    char* buf = malloc(DEFAULT_BUFFER_SIZE * pipe_elem_size(PIPE_GENERIC(p.in)));

    size_t elems_read;

    while((elems_read = pipe_pop(p.in, buf, DEFAULT_BUFFER_SIZE)))
        p.proc(buf, elems_read, p.out, p.aux);

    p.proc(NULL, 0, NULL, p.aux);

    free(buf);

    pipe_consumer_free(p.in);
    pipe_producer_free(p.out);

    return NULL;
}
Exemplo n.º 3
0
void *process_msg_thread(void *arg)
{
	assert(arg);
	wtp_handle_t* handle = (wtp_handle_t *) arg;
	aslan_msg_t *msg = NULL;

	while (pipe_pop(handle->msg_recv_consumer, &msg, 1))
	{
		if (msg->msg_id == MSG_ID_ACK)
		{
			pthread_mutex_lock(&handle->ack_mutex);
			handle->received_ack_count++;
			handle->ack_last_flag = msg->msg.ack->flag;
			pthread_mutex_unlock(&handle->ack_mutex);
		}
		(handle->msg_cb)(msg);
		free_msg(&msg);
	}

	pthread_exit(NULL);
    return NULL;
}
Exemplo n.º 4
0
void *
__dt_worker(void *arg) {
	verbose(VERB_OPS, "dnstap: starting dt_worker thread");

	dt_env_t *env = (dt_env_t *) arg;
	struct sockaddr_in so_service;

	so_service.sin_family = AF_INET;
	so_service.sin_port = htons(5354);
	so_service.sin_addr.s_addr = inet_addr("127.0.0.1");
	memset(so_service.sin_zero, 0, sizeof so_service.sin_zero);

	if(!__dt_so_connect(env, so_service)) return NULL;

	// Pop events off of the queue
	dt_message_t * event;

	while(pipe_pop(env->so_consumer, &event, 1)) {
		if(send(env->so_socket, event, dt_message_size(event), 0) < 0) {
			verbose(VERB_OPS, "dnstap: error sending message: %s. Trying to reconnect", strerror(errno));

			// Requeue the event for later
			pipe_push(env->so_producer, &event, 1);

			// And try to reconnect
			close(env->so_socket);
			if(!__dt_so_connect(env, so_service)) return NULL;
		} else {
			verbose(VERB_OPS, "dnstap: sent event to dt_service");
			dt_message_free(event);
		}
	}

	verbose(VERB_OPS, "dnstap: stopping dt_worker thread");
	close(env->so_socket);
	pipe_consumer_free(env->so_consumer);
}
static void* file_writer(void* pc_ptr){
  pipe_consumer_t* reader = pc_ptr;
  uint8_t *pipebuf, *filebuf;
  size_t pipe_chunk = pack_1bit ? write_chunk * 4 : write_chunk;

  const char *filename_ext;
  char filename[2222], timestr[22];
  ssize_t basename_len = 0;

  time_t t_prev = 0;
  
  pipebuf = filebuf = malloc(write_chunk);
  if (pack_1bit)
    pipebuf = malloc(pipe_chunk);

  if (!pipebuf || !filebuf) {
    fprintf(stderr, "Unable to allocate file write buffers\n");
    exitRequested = 1;
    return NULL;
  }

  if (rotate_interval) {
    filename_ext = strrchr(output_filename, '.');
    if (filename_ext) {
      basename_len = filename_ext - output_filename;
    } else {
      basename_len = strlen(output_filename);
      filename_ext = "";
    }
    if (sizeof(filename) - basename_len < 22)
        basename_len = sizeof(filename) - 22; /* leave room for the date */
    strncpy(filename, output_filename, basename_len);
    filename[basename_len] = 0;
    t_prev = time(NULL);
    strftime(timestr, sizeof(timestr), "%Y%m%d-%H%M%S",
             localtime(&t_prev));
    sprintf(&filename[basename_len], "-%s%s", timestr, filename_ext);
    if (verbose)
      printf("Rotating files every %d seconds, starting with %s\n",
             rotate_interval, filename);
  } else {
    strncpy(filename, output_filename, sizeof(filename));
  }

  if ((outputFile = fopen(filename, "w")) == 0) {
      fprintf(stderr,"Can't open output file %s, Error %s\n", filename,
              strerror(errno));
      exitRequested = 1;
      return NULL;
  }
                                      
  size_t bytes_read, bytes_to_write;
  while (!exitRequested){
    if (rotate_interval) {
      time_t t = time(NULL);
      if (t / rotate_interval != t_prev / rotate_interval) {
        t_prev = t;
        strftime(timestr, sizeof(timestr), "%Y%m%d-%H%M%S",
             localtime(&t));
        sprintf(&filename[basename_len], "-%s%s", timestr, filename_ext);
        if (verbose)
          printf("Rotating to new file %s\n", filename);
        fclose(outputFile);
        if ((outputFile = fopen(filename, "w")) == 0) {
          fprintf(stderr,"Can't open output file %s, Error %s\n", filename,
                  strerror(errno));
          exitRequested = 1;
          return NULL;
        }
      }
    }
    bytes_read = pipe_pop(reader, pipebuf, pipe_chunk);
    if (bytes_read > 0) {
      if (pack_1bit) {
	const uint8_t *p = pipebuf;
        bytes_to_write = bytes_read / 4;
	for (size_t i = 0; i < bytes_to_write; i++) {
	  uint8_t pack = 0;
	  for (int j = 0; j < 4; j++) {
	    pack <<= 2;  // Will end up with first sample in MSB of packed output
	    pack |= ((*p) & 0x80) >> 6;  // First sample sign in MSB of byte from piksi
	    pack |= ((*p) & 0x10) >> 4;  // Second sample sign in bit 4
	    p++;
	  }
	  filebuf[i] = pack;
	}
      } else {
        bytes_to_write = bytes_read;
      }
      if (fwrite(filebuf, bytes_to_write, 1, outputFile) != 1){
        perror("Write error\n");
        exitRequested = 1;
      }
    }
  }