Exemple #1
0
static int test_list(struct harness_t *harness_p)
{
    struct chan_list_t list;
    void *workspace[1];
    struct chan_t chan[2];

    BTASSERT(chan_init(&chan[0],
                       chan_read_null,
                       chan_write_null,
                       chan_size_null) == 0);
    BTASSERT(chan_init(&chan[1],
                       chan_read_null,
                       chan_write_null,
                       chan_size_null) == 0);

    BTASSERT(chan_list_init(&list, &workspace[0], sizeof(workspace)) == 0);
    BTASSERT(chan_list_add(&list, &chan[0]) == 0);
    BTASSERT(chan[0].list_p != NULL);
    BTASSERT(chan_list_add(&list, &chan[1]) == -ENOMEM);
    BTASSERT(chan_list_remove(&list, &chan[1]) == -1);
    BTASSERT(chan_list_remove(&list, &chan[0]) == 0);
    BTASSERT(chan[0].list_p == NULL);

    return (0);
}
Exemple #2
0
static proxy_conn_t* proxy_conn_create(int fd, int is_priv)
{
    proxy_conn_t* proxy = calloc(1, sizeof(proxy_conn_t));
    tcp_conn_init(&proxy->conn, fd);
    proxy->write_ch = chan_init(1024);
    proxy->die_ch = chan_init(0);
    proxy->is_priv = is_priv;
    return proxy;
}
Exemple #3
0
int usb_device_class_cdc_init(struct usb_device_class_cdc_driver_t *self_p,
                              int control_interface,
                              int endpoint_in,
                              int endpoint_out,
                              void *rxbuf_p,
                              size_t size)
{
    self_p->base.start_of_frame_isr = start_of_frame_isr;
    self_p->base.setup_isr = setup_isr;
#if CONFIG_FS_CMD_USB_DEVICE_LIST == 1
    self_p->base.print = print;
#endif

    self_p->control_interface = control_interface;
    self_p->endpoint_in = endpoint_in;
    self_p->endpoint_out = endpoint_out;
    self_p->line_state = 0;

    self_p->line_info.dte_rate = 38400;
    self_p->line_info.char_format = 0;
    self_p->line_info.parity_type = 0;
    self_p->line_info.data_bits = 0;

    chan_init(&self_p->chout,
              chan_read_null,
              write_cb,
              chan_size_null);

    if (size > 0) {
        queue_init(&self_p->chin, rxbuf_p, size);
    }

    return (0);
}
Exemple #4
0
int queue_init(struct queue_t *self_p,
               void *buf_p,
               size_t size)
{
    ASSERTN(self_p != NULL, EINVAL);
    ASSERTN((buf_p == NULL)
            || ((buf_p != NULL) && (size > 0)), EINVAL);

    chan_init(&self_p->base,
              (chan_read_fn_t)queue_read,
              (chan_write_fn_t)queue_write,
              (chan_size_fn_t)queue_size);
    chan_set_write_isr_cb(&self_p->base, (chan_write_fn_t)queue_write_isr);

    self_p->buffer.begin_p = buf_p;
    self_p->buffer.read_p = buf_p;
    self_p->buffer.write_p = buf_p;
    self_p->buffer.end_p = &((char*)buf_p)[size];
    self_p->buffer.size = size;

    self_p->state = QUEUE_STATE_INITIALIZED;

    self_p->buf_p = NULL;
    self_p->size = 0;
    self_p->left = 0;

    return (0);
}
Exemple #5
0
int socket_open_udp(struct socket_t *self_p)
{
    if (number_of_sockets >= membersof(sockets)) {
        return (-1);
    }

    sockets[number_of_sockets++] = self_p;

    return (chan_init(&self_p->base, read, write, size));
}
Exemple #6
0
static int vstr_chan_init(struct vstr_chan_t *self_p)
{
    chan_init(&self_p->base,
              chan_read_null,
              (chan_write_fn_t)vstr_chan_write,
              chan_size_null);
    vstr_init(&self_p->string, 128);

    return (0);
}
Exemple #7
0
static int test_putc(struct harness_t *harness_p)
{
    struct chan_t chan;
    ssize_t res;
    unsigned char character;
    
    BTASSERT(chan_init(&chan,
                       chan_read_null,
                       write_mock,
                       chan_size_null) == 0);

    /* A positive signed char. */
    res = sizeof(character);
    harness_mock_write("write_mock(): return (res)",
                       &res,
                       sizeof(res));

    BTASSERTI(chan_putc(&chan, 'f'), ==, 0);

    character = '\0';
    harness_mock_read("write_mock(buf_p)",
                       &character,
                       sizeof(character));
    BTASSERTI(character, ==, 'f');

    /* A negative signed char. */
    res = sizeof(character);
    harness_mock_write("write_mock(): return (res)",
                       &res,
                       sizeof(res));

    BTASSERTI(chan_putc(&chan, 0xfe), ==, 0);

    character = '\0';
    harness_mock_read("write_mock(buf_p)",
                       &character,
                       sizeof(character));
    BTASSERTI(character, ==, 0xfe);

    /* Input output error gives negative error code. */
    res = -EIO;
    harness_mock_write("write_mock(): return (res)",
                       &res,
                       sizeof(res));

    BTASSERTI(chan_putc(&chan, ' '), ==, -EIO);

    character = '\0';
    harness_mock_read("write_mock(buf_p)",
                       &character,
                       sizeof(character));
    BTASSERTI(character, ==, ' ');
    
    return (0);
}
Exemple #8
0
	int DEFAULT_CC
main(int argc, char** argv)
{
	int pid;
	char text[256];
	if (argc != 2)
	{
		g_printf("Usage : xrdp-chansrv 'username'\n");
		g_exit(1);
	}
	username = argv[1];
	g_init(); /* os_calls */
	read_ini();
	read_logging_conf();
	chan_init();
	log_start(&log_conf);
	pid = g_getpid();
	log_message(&log_conf, LOG_LEVEL_DEBUG, "chansrv[main]: "
			"app started pid %d(0x%8.8x)", pid, pid);
	g_signal_kill(term_signal_handler); /* SIGKILL */
	g_signal_terminate(term_signal_handler); /* SIGTERM */
	g_signal_user_interrupt(term_signal_handler); /* SIGINT */
	g_signal_pipe(nil_signal_handler); /* SIGPIPE */
	g_signal_child_stop(stop_signal_handler);
	g_snprintf(text, 255, "xrdp_chansrv_%8.8x_main_term", pid);
	g_term_event = g_create_wait_obj(text);
	g_snprintf(text, 255, "xrdp_chansrv_%8.8x_thread_done", pid);
	g_thread_done_event = g_create_wait_obj(text);
	tc_thread_create(channel_thread_loop, 0);
	while (!g_is_wait_obj_set(g_term_event))
	{
		if (g_obj_wait(&g_term_event, 1, 0, 0, 0) != 0)
		{
			log_message(&log_conf, LOG_LEVEL_WARNING, "chansrv[main]: "
					"main: error, g_obj_wait failed");
			break;
		}
	}
	while (!g_is_wait_obj_set(g_thread_done_event))
	{
		/* wait for thread to exit */
		if (g_obj_wait(&g_thread_done_event, 1, 0, 0, 0) != 0)
		{
			log_message(&log_conf, LOG_LEVEL_WARNING, "chansrv[main]: "
					"main: error, g_obj_wait failed");
			break;
		}
	}
	/* cleanup */
	main_cleanup();
	log_message(&log_conf, LOG_LEVEL_INFO, "chansrv[main]: "
			"main: app exiting pid %d(0x%8.8x)", pid, pid);
	return 0;
}
Exemple #9
0
int socket_accept(struct socket_t *self_p,
                  struct socket_t *accepted_p,
                  struct inet_addr_t *addr_p)
{
    uint32_t mask;

    chan_init(&accepted_p->base, read, write, size);

    mask = 0x1;
    event_read(&accept_events, &mask, sizeof(mask));

    return (0);
}
Exemple #10
0
int event_init(struct event_t *self_p)
{
    ASSERTN(self_p != NULL, EINVAL);

    chan_init(&self_p->base,
              (ssize_t (*)(void *, void *, size_t))event_read,
              (ssize_t (*)(void *, const void *, size_t))event_write,
              (size_t (*)(void *))event_size);

    self_p->mask = 0;

    return (0);
}
Exemple #11
0
static int test_init(struct harness_t *harness_p)
{
    BTASSERT(xbee_module_init() == 0);
    BTASSERT(xbee_module_init() == 0);

    BTASSERT(queue_init(&queue, &queue_buf[0], sizeof(queue_buf)) == 0);
    BTASSERT(chan_init(&transport,
                       chan_read_null,
                       chan_write_null,
                       chan_size_null) == 0);
    BTASSERT(xbee_init(&xbee, &transport, &transport) == 0);

    return (0);
}
Exemple #12
0
static int test_filter(struct harness_t *harness_p)
{
    struct chan_t chan;
    char buf[2];

    BTASSERT(chan_init(&chan,
                       chan_read_null,
                       write_cb,
                       chan_size_null) == 0);

    /* Write without filter function. */
    buf[0] = 1;
    buf[1] = 2;
    BTASSERT(chan_write(&chan, &buf[0], 2) == 2);
    BTASSERT(memcmp(buffer, buf, 2) == 0);

    /* Filter out the message. */
    BTASSERT(chan_set_write_filter_cb(&chan, write_filter) == 0);
    buf[0] = 1;
    buf[1] = 2;
    write_filter_return_value = 1;
    memset(buffer, -1, sizeof(buffer));
    BTASSERT(chan_write(&chan, &buf[0], 2) == 2);
    buf[0] = -1;
    buf[1] = -1;
    BTASSERT(memcmp(buffer, buf, 2) == 0);
    BTASSERT(chan_set_write_filter_cb(&chan, NULL) == 0);

    /* Write isr without filter function. */
    BTASSERT(chan_set_write_isr_cb(&chan, write_cb) == 0);
    buf[0] = 1;
    buf[1] = 2;
    BTASSERT(chan_write_isr(&chan, &buf[0], 2) == 2);
    BTASSERT(memcmp(buffer, buf, 2) == 0);

    /* Filter out the message. */
    BTASSERT(chan_set_write_filter_isr_cb(&chan, write_filter) == 0);
    buf[0] = 1;
    buf[1] = 2;
    write_filter_return_value = 1;
    memset(buffer, -1, sizeof(buffer));
    BTASSERT(chan_write_isr(&chan, &buf[0], 2) == 2);
    buf[0] = -1;
    buf[1] = -1;
    BTASSERT(memcmp(buffer, buf, 2) == 0);
    BTASSERT(chan_set_write_filter_isr_cb(&chan, NULL) == 0);

    return (0);
}
Exemple #13
0
static int test_null_channels(struct harness_t *harness_p)
{
    struct chan_t chan;
    char value;

    BTASSERT(chan_init(&chan,
                       chan_read_null,
                       chan_write_null,
                       chan_size_null) == 0);

    BTASSERT(chan_read(&chan, &value, 1) == -1);
    BTASSERT(chan_write(&chan, &value, 1) == 1);
    BTASSERT(chan_size(&chan) == 1);

    return (0);
}
Exemple #14
0
int ssl_socket_open(struct ssl_socket_t *self_p,
                    struct ssl_context_t *context_p,
                    void *socket_p,
                    int flags,
                    const char *server_hostname_p)
{
    BTASSERT(self_p != NULL);
    BTASSERT(context_p != NULL);
    BTASSERT(socket_p != NULL);
    BTASSERT(flags & SSL_SOCKET_SERVER_SIDE);

    ssl_open_counter++;

    return (chan_init(&self_p->base,
                      (chan_read_fn_t)ssl_socket_read,
                      (chan_write_fn_t)ssl_socket_write,
                      (chan_size_fn_t)ssl_socket_size));
}
Exemple #15
0
static void init(struct socket_t *self_p,
                 int type,
                 void *pcb_p)
{
    /* Channel functions. */
    chan_init(&self_p->base,
              (chan_read_fn_t)socket_read,
              (chan_write_fn_t)socket_write,
              (chan_size_fn_t)socket_size);

    self_p->type = type;
    self_p->pcb_p = pcb_p;
    self_p->input.cb.state = STATE_IDLE;
    self_p->input.u.recvfrom.pbuf_p = NULL;
    self_p->input.u.recvfrom.left = 0;
    self_p->input.u.recvfrom.closed = 0;
    self_p->output.cb.state = STATE_IDLE;
}
Exemple #16
0
int uart_soft_init(struct uart_soft_driver_t *self_p,
                   struct pin_device_t *tx_dev_p,
                   struct pin_device_t *rx_dev_p,
                   struct exti_device_t *rx_exti_dev_p,
                   int baudrate,
                   void *rxbuf_p,
                   size_t size)
{
    ASSERTN(self_p != NULL, EINVAL);
    ASSERTN(tx_dev_p != NULL, EINVAL);
    ASSERTN(rx_dev_p != NULL, EINVAL);
    ASSERTN(rx_exti_dev_p != NULL, EINVAL);
    ASSERTN(rxbuf_p != NULL, EINVAL);

    self_p->sample_time = BAUDRATE2US(baudrate);

    chan_init(&self_p->chout,
              chan_read_null,
              (ssize_t (*)(void *, const void *, size_t))uart_soft_write_cb,
              chan_size_null);

    pin_init(&self_p->tx_pin, tx_dev_p, PIN_OUTPUT);
    pin_init(&self_p->rx_pin, rx_dev_p, PIN_INPUT);

    /* Keep TX line high when no transmission is ongoing. */
    pin_write(&self_p->tx_pin, 1);

    exti_init(&self_p->rx_exti,
              rx_exti_dev_p,
              EXTI_TRIGGER_FALLING_EDGE,
              rx_isr,
              self_p);

    exti_start(&self_p->rx_exti);

    return (queue_init(&self_p->chin, rxbuf_p, size));
}
Exemple #17
0
Fichier : can.c Projet : wuwx/simba
int can_init(struct can_driver_t *self_p,
             struct can_device_t *dev_p,
             uint32_t speed,
             void *rxbuf_p,
             size_t size)
{
    ASSERTN(self_p != NULL, EINVAL);
    ASSERTN(dev_p != NULL, EINVAL);
    ASSERTN(rxbuf_p != NULL, EINVAL);
    ASSERTN(size > 0, EINVAL);

    self_p->dev_p = dev_p;

    chan_init(&self_p->base,
              base_chan_read,
              (chan_write_fn_t)write_cb,
              base_chan_size);

    queue_init(&self_p->chin, rxbuf_p, size);

    sem_init(&self_p->sem, 0, 1);

    return (can_port_init(self_p, dev_p, speed));
}
Exemple #18
0
static void server_init(proxy_server *srv)
{
    srv->accepts_ch = chan_init(0);
    srv->die_ch = chan_init(0);
    srv->is_die = 0;
}
Exemple #19
0
int socket_open_tcp(struct socket_t *self_p)
{
     return (chan_init(&self_p->base, read, write, size));
}
int main(int argc, char **argv)
{
  char *filename;
  FILE *datafile;
  struct stat fstat;

  long int bufcount;
  char *buff;
  int max_bufsz = DEFMAX_BUFSZ;
  int bufsz;

  //Kluge stuff, count total num samps
  //  long int totsampcount;

  //TCP stuff
  struct tcp_header *tcp_hdr;
  struct tcp_parser *parser;
  //  bool careful_strip = true; //ensures things are where they say they are before junking them
  //  FILE *stripfile;
  char tcp_str[16] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 
			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

  struct dewe_chan *chan[MAX_NUMCHANS];
  char *chanbuff[MAX_NUMCHANS];
  char *chantimestamps[MAX_NUMCHANS];
  char combfname[] = "combined_chans.data";
  FILE *combfile; //File for combining upper 10, lower 6 bits of two different channels
                  //to accommodate the strange 10-bit TM data at Wallops
  
  signal(SIGINT, do_depart);

  //Kluge stuff, init total num samps
  //  totsampcount = 0;

  //tcp header stuff
  tcp_hdr = tcp_header_init();

  //tcp parser stuff
  parser = parser_init();

  parser->hdrsz = 32 + STARTSTR_SZ; //per DEWESoft NET interface docs

  //copy in start and tail string for use by parse_tcp_header() and strip_tcp_packet()
  if( DEF_VERBOSE ) printf("tcp_fileparse.c [main()] Start string:\t");
  for (int i = 0; i < STARTSTR_SZ; i ++){
    strncpy(&(parser->startstr[i]),&(tcp_str[8+i]),1);
    if ( DEF_VERBOSE ) printf("%x",parser->startstr[i]);
  }
  if( DEF_VERBOSE ) printf("\n");
  parser->startstr_sz = STARTSTR_SZ;

  if( DEF_VERBOSE ) printf("tcp_fileparse.c [main()] Tail string:\t");
  strncpy(parser->tlstr,tcp_str,STARTSTR_SZ); 
  if (DEF_VERBOSE ){
    for (int i = 0; i < STARTSTR_SZ; i ++){
      printf("%x",parser->tlstr[i]);
    }
    printf("\n");
  }
  parser->tailsz = STARTSTR_SZ;

  parser->oldhpos = -(parser->hdrsz + parser->tailsz); //Needs to be initialized thusly so that 
                                                       //parse_tcp_header doesn't complain that 
                                                       //the first header isn't where predicted
  parser->do_predict = true;
  parser->isfile = true;

  parser->verbose = DEF_VERBOSE;

  //Handle command line  
  if(argc == 2) {
    filename = strdup(argv[1]);
  }
  else if ( argc == 3 ){
    filename = strdup(argv[1]);
    max_bufsz = atoi(argv[2]);
  }
  else if( ( argc == 4 ) || ( argc == 5) ){

    filename = strdup(argv[1]);
    max_bufsz = atoi(argv[2]);

    //Strip packet modes
    if( atoi(argv[3]) <= 2){ 
      parser->strip_packet = atoi(argv[3]);
      parser->strip_fname = malloc(sizeof(char) * 128);
      parser->strip_fname = "stripped.data";
      //    sprintf(parser->strip_fname,"stripped-%s",filename);
      parser->oldt_in_this_buff = 0;
      parser->t_in_this_buff = 0;
    }
    //Channel modes
    else if( ( atoi(argv[3]) == 4 ) ||  ( atoi(argv[3]) == 5 ) || ( atoi(argv[3]) == 6 ) || ( atoi(argv[3]) == 7 ) ){ // only parse chan info 
      parser->do_chans = atoi(argv[3]) - 3;
      if( argc ==5 ){ //channel num provided
	parser->nchans = atoi(argv[4]);
      }
      else {
	parser->nchans = DEF_NUMCHANS;
      }
      if( DEF_VERBOSE ) printf("tcp_fileparse.c [main()] parser->nchans\t=\t%i\n",parser->nchans);
    }
    else {
      printf("You fool!!! You expect this program to clean your room as well?\n");
      return EXIT_FAILURE;
    }
  }
  else {
    printf("%s <tcp data file> <optional max readbuffer size> <run_mode> [for run_mode 4/5: NUM_CHANS]\n",argv[0]);
    printf("RUN MODES:\n");
    printf("\tFOR STRIPPING PACKETS\n");
    printf("\t0: No stripping of data is done. Prints packet headers to stdout.\n");
    printf("\t1: The packet header and footer are stripped from the data for RTD, but left in the data file\n");
    printf("\t2: The packet header and footer are stripped from the data for RTD AND the saved data file\n");
    printf("\t3: Stripped data are saved and RTDed, and bad packets are output to an error file, badpack.data (NOT YET IMPLEMENTED)\n");
    printf("\n");
    printf("\tFOR DOING CHANNEL TRICKERY\n");
    printf("\t4: Channel information is parsed and printed to stdout, but no files are created.\n");
    printf("\t5: A data file is created for each channel.\n");
    printf("\t6: A data file is created for each channel, AND the first and second channel are combined with"
	   "\n\t   join_upper10_lower6() and outputted as joinupper10lower6.data.\n");
    printf("\t7: Just for RxDSPs! A data file is created for each channel (always assumed asynchronous)"
	   "\n\t   but only the timestamp associated with a Dartmouth header is kept, instead of keeping all timestamps."
	   "\n\t   (Precludes digitizer mode, -g)\n");
    return(EXIT_SUCCESS);
  }

  //Open data file
  printf("Opening TCP data file %s\n",filename);
  if( ( datafile = fopen(filename,"r") ) == NULL ){
    fprintf(stderr,"Gerrorg. Couldn't open file %s.\n",filename);
    return(EXIT_FAILURE);
  }
  stat(filename,&fstat);
  parser->filesize = fstat.st_size;

  //Prepare buffers
  buff = malloc(sizeof(char) * max_bufsz);
  if( DEF_VERBOSE ) printf("Max buffer size:\t%i\n", max_bufsz);
 
  //tcp chan stuff
  if( parser->do_chans ){
    for (int i = 0; i < parser->nchans; i ++) {

      chan[i] = chan_init( i, 3, true, false); //channel num, data type 3 (16-bit unsigned int), async, not singleval

      if( ( parser->do_chans == 2 ) || ( parser->do_chans == 3 ) ){ //open files for chandata
	sprintf(chan[i]->outfname,"chan%i.data",i);
	chan[i]->outfile = fopen(chan[i]->outfname,"w");
	if (chan[i]->outfile == NULL) {
	  fprintf(stderr,"Gerrorg. Couldn't open %s for channel %i.\n", chan[i]->outfname, chan[i]->num );
	  return(EXIT_FAILURE);
	}
	if( DEF_VERBOSE ){
	  printf("Channel %i file:\t\t%p\n", i, chan[i]->outfile );
	  printf("Channel %i filename:\t\t%s\n", i, chan[i]->outfname);
	}
	
	//tstamp files
	if( chan[i]->is_asynchr ){

	  sprintf(chan[i]->ts_fname,"chan%i_tstamps.data",i);
	  chan[i]->ts_file = fopen(chan[i]->ts_fname,"w");
	  if (chan[i]->ts_file == NULL) {
	    fprintf(stderr,"Gerrorg. Couldn't open %s for channel %i.\n", chan[i]->ts_fname, chan[i]->num );
	    return(EXIT_FAILURE);
	  }
	  if( DEF_VERBOSE ){
	    printf("Channel %i timestamp file:\t\t%p\n", i, chan[i]->ts_file );
	    printf("Channel %i timestamp filename:\t\t%s\n", i, chan[i]->ts_fname);
	  }
	}
      }
    }

    //chan buffs
    for(int i = 0; i < parser->nchans; i++){
      chanbuff[i] = malloc( chan[i]->bufsize );
      chan[i]->d.type3 = chanbuff[i];
      chan[i]->packaddr = chan[i]->d.type3;
      chan[i]->oldpackaddr = chan[i]->d.type3;
      if( chan[i]->is_asynchr ){
	chantimestamps[i] = malloc( MAXNUMSAMPS * 8);
	chan[i]->oldtstamps_addr = chan[i]->tstamps_addr = chan[i]->timestamps = chantimestamps[i];
	if( DEF_VERBOSE ) printf("tcp_fileparse.c [main()] Malloc'ed %i bytes for channel %u timestamps buffer...\n", MAXNUMSAMPS * 8, chan[i]->num );
      }
    }
    if( parser->do_chans == 3){ //doing join_upper10_lower6
      combfile = fopen(combfname,"w");
      if (combfile == NULL) {
	fprintf(stderr,"Gerrorg. Couldn't open %s.\n", combfname );
	return(EXIT_FAILURE);
      }
      if( DEF_VERBOSE ){
	printf("Combined data filename: %s\n", combfname);
	printf("Combined data file: %p\n", combfile );
      }
    }
    else {
      combfile = NULL;
    }	
  }  

  //If stripping data, set up file for stripped data
  if(parser->strip_packet == 2){
    printf("stripfname: %s\n",parser->strip_fname);
    parser->stripfile = fopen(parser->strip_fname,"w");
    printf("Stripfile: %p\n",parser->stripfile);
    if (parser->stripfile == NULL) {
      fprintf(stderr,"Gerrorg. Couldn't open stripfile %s.\n",parser->strip_fname);
      return(EXIT_FAILURE);
    }
  }

  if( DEF_VERBOSE ) print_header_memberszinfo(tcp_hdr);

  //Prediction stuff
  if(parser->do_predict){
    parser->hprediction = 0; //At the beginning, we predict that the header will be right at the beginning!
    parser->num_badp = 0;
  }

  //Start looping through file
  printf("Parsing TCP data file %s...\n",filename);
  bufsz = max_bufsz;
  int i = 0;
  int count = 0;
  running = true;
  while( ( bufcount = fread(buff, 1, bufsz, datafile) ) > 0 && running ) {
    
    printf("\n***\nBuffer #%i\n***\n",i+1);

    parser->bufrem = bufcount;
    parser->bufpos = 0;
    parser->delbytes = 0;

    while(  parser->bufpos < parser->bufrem ){

      if(parser->strip_packet) { usleep(4000); }
	else { usleep(6000); }

      parse_tcp_header(parser, buff, tcp_hdr);       //get new header_addr here

      update_after_parse_header(parser, buff, tcp_hdr);       //new hpos, bufpos, packetpos, if applicable 

    
      if( parser->parse_ok ){
	
	printf("*****Packet #%u*****\n",parser->numpackets);
	print_tcp_header(tcp_hdr);

	// Kluge stuff--total num samps
	parser->totsampcount += tcp_hdr->chan0_numsamps;
	printf("Total samples rec'd so far:\t%"PRIi64"\n", parser->totsampcount);
	//	if( DEF_VERBOSE ) print_raw_tcp_header(tcp_hdr);
	
	//Current header not where predicted?
	if( parser->do_predict) {
	  
	  if( parser->hpos != parser->hprediction ) {
	    parser->num_badp +=1;
	    printf("***Header position not where predicted by previous packet size!\n");
	    printf("***Header position relative to beginning of buffer:\t%li\n",parser->hpos);
	    printf("***Predicted position relative to beginning of buffer:\t%li\n",parser->hprediction);
	    printf("***Missed by %li bytes... Try again, Hank.\n",parser->hprediction-parser->hpos);
	  }
	  else {

	    printf("***Header %i was found where predicted: %li\n",parser->hc,parser->hprediction);
	    parser->tc +=1; //Since the prediction was correct, there must have been a tail

	  }

	  //predicted position of the next header
	  parser->hprediction = parser->hpos + tcp_hdr->pack_sz + parser->tailsz + parser->startstr_sz;

	} 	
      }	

      //Strip packet modes
      if( parser->strip_packet ){

	prep_for_strip(parser, buff, tcp_hdr); 	//determines whether there are footers to kill
	strip_tcp_packet(parser, buff, tcp_hdr); 	//do the deed

	if( parser->do_predict ) { 	//update our prediction accordingly	  
	  parser->hprediction -= ( ( (int)parser->oldtkill + (int)parser->tkill) * parser->tailsz +
				   parser->hkill * parser->hdrsz );	  
	}	
	post_strip(parser, buff, tcp_hdr); 	//finish the job	
      } //end strip_packet

      //Channel modes
      if( parser->do_chans ){

      	bool moresamps = true;      
      	long int tmp_buf_pos = parser->bufpos;

  	if( parser->parse_ok ){

	  //get new packet stuff, if applicable
	  for(int i = 0; i < parser->nchans; i++){
	    update_chans_post_parse( chan[i], tcp_hdr, parser, buff );
	    //tell me about it
	    print_chan_info( chan[i] );
	  }

	  //temp set to zero because we want everything in the buffer BEHIND the new header
	  if( parser->oldhpos < 0 ) {
	    parser->bufpos = 0; 
	  }
	  else {
	    parser->bufpos = parser->oldhpos; 
	  }
	    
	  //wrap up old channel data, which will only be here if we got a new header
	  for(int i = 0; i < parser->nchans; i++){
	    //	    if( chan[i]->oldnumsampbytes != chan[i]->oldnumbytes_received && parser->bufpos < parser->hpos  ){
	    if( ( chan[i]->oldnumsampbytes != chan[i]->oldnumbytes_received || 
		  chan[i]->oldnumtbytes != chan[i]->oldtbytes_received) && parser->bufpos < parser->hpos  ){
	      if( DEBUG ) {
		printf("tcp_fileparse.c [main()] CH%i: Doing old samples\n", i );
		printf("tcp_fileparse.c [main()] CH%i: Bufpos = %li\n", i, parser->bufpos );
		printf("tcp_fileparse.c [main()] CH%i: hpos = %li\n", i, parser->hpos );
	      }
	      get_chan_samples( chan[i], buff, parser, tcp_hdr, true);
	    }
	  }
	  if( DEBUG ) {
	    printf("tcp_fileparse.c [main()] Finished oldsamps. Bufpos should be right behind hpos!\n");
	    //	    printf("tcp_fileparse.c [main()] hpos = %li, bufpos == %li\n", parser->hpos, parser->bufpos);
	  }
	  if( (parser->bufpos += parser->tailsz ) != parser->hpos && parser->oldhpos > 0 ) {
	    printf("Channels read incorrectly!!!\n");
	    printf("tcp_fileparse.c [main()] hpos = %li, bufpos == %li\n", parser->hpos, parser->bufpos);
	  }
	  parser->bufpos =  parser->hpos + parser->hdrsz - 4; //skip header for next get_chan_samples
	}  
	  
	moresamps = true; //reset for next bit
      

	for(int j = 0; j < parser->nchans; j++ ){
	  if( moresamps ) {
	    moresamps = get_chan_samples( chan[j], buff, parser, tcp_hdr, false);
	  } else { break; }
	}
	
      	parser->bufpos = tmp_buf_pos; //set it to what it was before channels messed with it

	if( parser->do_chans > 1 ){ //time to write chan data
	  
	  int npacks_ready = 0;
	  int noldpacks_ready = 0;
	  
	  for(int i = 0; i < 2; i ++){
	    noldpacks_ready += (int) chan[i]->oldpack_ready;
	    npacks_ready += (int) chan[i]->pack_ready;
	    
	    if( DEF_VERBOSE ){ 
	      printf("Chan %i old packet ready to combine: %i\n", i, chan[i]->oldpack_ready);
	      printf("Chan %i new packet ready to combine: %i\n", i, chan[i]->pack_ready);
	    }
	  }
	  if( parser-> do_chans >= 2 ){
	    if( npacks_ready == 2 ) {
	      
	      if( parser->do_chans == 3 )combine_and_write_chandata( chan[0], chan[1], 0, parser, combfile );
	      for(int i = 0; i < 2; i ++){
		write_chan_samples( chan[i], false, parser, true );
	      }

	      if( noldpacks_ready == 2 ){
		if( parser->do_chans == 3 )combine_and_write_chandata( chan[0], chan[1], 1, parser, combfile );
		for(int i = 0; i < 2; i ++){
		  write_chan_samples( chan[i], true, parser, true );
		}
	      }
	      for( int i = 0; i < 2; i++ ) {
		clean_chan_buffer( chan[i], true );
	      }
	    }
	    else if( noldpacks_ready == 2 ){
	      if( parser->do_chans == 3 )combine_and_write_chandata( chan[0], chan[1], 1, parser, combfile );
	      for(int i = 0; i < 2; i ++){
		write_chan_samples( chan[i], true, parser, true );
		clean_chan_buffer( chan[i], false );
	      }
	    }
	    else {
	      printf ("Not all channels are prepared to do data combination!\n");
	    }
	  } //end combine channel data
	} //end write chan data
	else { //just clean up, nothing to write
	  for(int i = 0; i < parser->nchans; i++){
	    clean_chan_buffer( chan[i] , chan[i]->pack_ready );
	  }
	}	
      } //end do_chans
    
      update_end_of_loop(parser, buff, tcp_hdr);       //new bufpos, packetpos happens here
      
    } //end of current buffer

    //Update all stuff outside last buffer
    parser->total += bufcount;
    printf("Read %li bytes so far\n", parser->total);
    
    if(parser->strip_packet){

      parser->deltotal += parser->delbytes;
      printf("Killed %li bytes so far\n",parser->deltotal);
      if( parser-> strip_packet == 2 ) { 

}

      if( parser->strip_packet == 2){//Up the ante

	printf("Writing %li bytes to %s\n",parser->bufrem, parser->strip_fname);     	

	count = fwrite(buff, 1, parser->bufrem, parser->stripfile);
    	if( count == 0){
    	  printf("Gerrorg writing to %s\n",parser->strip_fname);
    	}
    	parser->wcount += count;

    	printf("count = %i\n",count);

      }
    }
    printf("Oldhpos BEFORE subtraction:\t%li\n", parser->oldhpos);
    parser->hprediction -= parser->bufrem;
    parser->oldhpos -= parser->bufrem;
    printf("Oldhpos AFTER subtraction:\t%li\n", parser->oldhpos);
    i++;

  }

  //print stats
  print_stats(parser);
  /* if( parser->do_chans ) { for(int i = 0; i < p->nchans; i++ ){ print_chan_stats(chan, p); } } */

  //Clean up shop
  if(datafile != NULL) fclose(datafile);
  free(filename);
  free(tcp_hdr);
  if( parser->do_chans ){
    for(int i = 0; i < parser->nchans; i++ ){
      /* if( ( parser->do_chans == 2 ) || ( parser->do_chans == 3 ) ){ //open files for chandata */
      /* 	free( chan[i]->outfile ); */
      /* }	 */
      //      free_chan( chan[i] );
      free( chan[i] );
    }
    //chan buffs
    /* for(int i = 0; i < parser->nchans; i++){ */
    /*   free ( chanbuff[i] ); */
    /*   if( chan[i]->is_asynchr ){ */
    /* 	free ( chantimestamps[i] ); */
    /*   } */
    /* } */
    
    
    if( parser->do_chans == 3){ //doing join_upper10_lower6
      free( combfile );
    }
  }
  //  free_parser(parser);
  free(parser);
  free(buff);
 
  return(EXIT_SUCCESS);
}