コード例 #1
0
ファイル: snprintf_default.c プロジェクト: OPSF/uClinux
int __snprintf_expect_default(char *buf, 
			      unsigned int len,
			      const struct nf_expect *exp,
			      unsigned int msg_type,
			      unsigned int flags) 
{
	int ret = 0, size = 0, offset = 0;

	switch(msg_type) {
		case NFCT_T_NEW:
			ret = snprintf(buf, len, "%9s ", "[NEW]");
			break;
		default:
			break;
	}

	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_expect_proto(buf+offset, len, exp);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_address(buf+offset, len, &exp->expected.tuple[__DIR_ORIG]);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_proto(buf+offset, len, &exp->expected.tuple[__DIR_ORIG]);
	BUFFER_SIZE(ret, size, len, offset);

	/* Delete the last blank space */
	size--;

	return size;
}
コード例 #2
0
ファイル: sound.c プロジェクト: jpanikulam/visar
void *speaker_thread(void* ptr){
  audiobuffer* buf = ((spk_pcm_package*)ptr)->buffer; //cast pointer, get buffer struct
  snd_pcm_t* speaker_handle = ((spk_pcm_package*)ptr)->pcm_handle; //cast pointer, get device pointer
  free(ptr); //free message memory
    
  snd_pcm_nonblock(speaker_handle, SND_PCM_NONBLOCK); //set in nonblocking mode
    
  char started = 0;  //track when to start reading data
  while(!global_kill && !speaker_kill_flag) { //loop until program stops us
    //wait until adequate buffer is achieved and can be written
    if((!started && BUFFER_SIZE(*buf) < (MIN_BUFFER)) \
        || BUFFER_EMPTY(*buf) \
        || !snd_pcm_avail_update(speaker_handle)) {
      //printf("Speaker Waiting\n");
      usleep(PERIOD_UTIME*2); //wait to reduce CPU usage
      continue;     //don't start yet
    } else {
      if(!started) snd_pcm_prepare(speaker_handle); //reset speaker
      started = 1; //indicate that we've startd
    }
    
    //write data to speaker buffer, check responses
    int write_count = MIN(BUFFER_SIZE(*buf), snd_pcm_avail_update(speaker_handle)/(buf->period));
    
#ifdef DEBUG_MODE
    printf("Writing %d packets to speaker\n", write_count);
#endif

    //loop over avaliable buffer entries
    while(write_count-- > 0 && started){
      /* Note: This call performs a syscall to copy data to kernel space 
          so it would be better to write multiple entries in one operation, 
          but using the audiobuffer without the abstraction was something 
          I didn't want to do at the time of writing. */
      int rc = snd_pcm_writei(speaker_handle, GET_QUEUE_HEAD(*buf), buf->period);      
      INC_QUEUE_HEAD(*buf);
      if (rc == -EPIPE){ //Catch underruns (not enough data)
        fprintf(stderr, "underrun occurred\n");
        started = 0;  //stop and wait for buffer to buildup
      } else if (rc < 0) fprintf(stderr, "error from writei: %s\n", snd_strerror(rc)); //other errors
      else if (rc != (int)buf->period) fprintf(stderr, "short write, write %d frames\n", rc);
      //else fprintf(stderr, "audio written correctly\n");
      //snd_pcm_wait(speaker_handle, 1000); //wait for IO to be ready
    }
    
#ifdef DEBUG_MODE
    printf("%d unwritten\n", write_count);
#endif    
  }
  
  // notify kernel to empty/close the speakers
  snd_pcm_drain(speaker_handle);  //finish transferring the audio
  snd_pcm_close(speaker_handle);  //close the device
  printf("Audio Controller: Speaker Thread shutdown\n");
  
  pthread_exit(NULL); //exit thread safetly
}
コード例 #3
0
ファイル: mifare_desfire.c プロジェクト: jrayner/bv_embedded
int
mifare_desfire_get_value_ex (MifareTag tag, uint8_t file_no, int32_t *value, int cs)
{
    if (!value)
	return errno = EINVAL, -1;

    void *p;

    ASSERT_ACTIVE (tag);
    ASSERT_MIFARE_DESFIRE (tag);

    BUFFER_INIT (cmd, 2);
    BUFFER_INIT (res, 9);

    BUFFER_APPEND (cmd, 0x6C);
    BUFFER_APPEND (cmd, file_no);

    DESFIRE_TRANSCEIVE (tag, cmd, res);

    p = (uint8_t *)res + 1;

    if (cs) {
	ssize_t rdl = BUFFER_SIZE (res) - 1;
	p = mifare_cryto_postprocess_data (tag, p, &rdl, cs);
	if (rdl != 4) {
	    printf ("invalid data length");
	    return -1;
	}
    }

    *value = le32toh (*(int32_t *)(p));

    return 0;
}
コード例 #4
0
ファイル: main.cpp プロジェクト: jcchiang/Joseph-SampleCode
void RaytracerApplication::toggle_raytracing( int width, int height )
{
    assert( width > 0 && height > 0 );

    // do setup if starting a new raytrace
    if ( !raytracing ) {

        // only re-allocate if the dimensions changed
        if ( buf_width != width || buf_height != height )
    {
            free( buffer );
            buffer = (unsigned char*) malloc( BUFFER_SIZE( width, height ) );
            if ( !buffer ) {
                std::cout << "Unable to allocate buffer.\n";
                return; // leave untoggled since we have no buffer.
            }
            buf_width = width;
            buf_height = height;
        }

        // initialize the raytracer (first make sure camera aspect is correct)
        scene.camera.aspect = real_t( width ) / real_t( height );

        if (!raytracer.initialize(&scene, options.num_samples, width, height))
    {
            std::cout << "Raytracer initialization failed.\n";
            return; // leave untoggled since initialization failed.
        }

        // reset flag that says we are done
        raytrace_finished = false;
    }

    raytracing = !raytracing;
}
コード例 #5
0
ファイル: main.cpp プロジェクト: SarahW48/Raytracing
void RaytracerApplication::toggle_raytracing( int width, int height )
{
    assert( width > 0 && height > 0 );

    if ( !raytracing ) {

        if ( buf_width != width || buf_height != height ) {
            free( buffer );
            buffer = (unsigned char*) malloc( BUFFER_SIZE( width, height ) );
            if ( !buffer ) {
                std::cout << "Unable to allocate buffer.\n";
                return; 
            }
            buf_width = width;
            buf_height = height;
        }

        scene.camera.aspect = real_t( width ) / real_t( height );

        if ( !raytracer.initialize( &scene, width, height ) ) {
            std::cout << "Raytracer initialization failed.\n";
            return; 
        }

        raytrace_finished = false;
    }

    raytracing = !raytracing;
}
コード例 #6
0
ファイル: nfct-snatlog.c プロジェクト: deba12/nfct-snatlog
void print_snatlog(struct nf_conntrack *ct, 
      time_t *timestamp, char *proto_str) {
   int ret = 0, size = 0, offset = 0, len = BUF_LEN;
   char buf[BUF_LEN];

   ret = __snprintf_start_log(buf, len, "SNAT_LOG");
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " proto=%s", proto_str);
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " o-src=%s",
         net2addr(nfct_get_attr_u32(ct,ATTR_ORIG_IPV4_SRC)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " o-spt=%d",
         ntohs(nfct_get_attr_u16(ct,ATTR_ORIG_PORT_SRC)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " t-src=%s",
         net2addr(nfct_get_attr_u32(ct,ATTR_REPL_IPV4_DST)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " t-spt=%d",
         ntohs(nfct_get_attr_u16(ct,ATTR_REPL_PORT_DST)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " duration=%.0lfs", 
         difftime(time(NULL),*timestamp));
   BUFFER_SIZE(ret, size, len, offset);

   buf[size+1 > len ? len-1 : size] = '\0';

   write_msg(LOG_INFO, buf);
}
コード例 #7
0
static int
__snprintf_localtime_xml(char *buf, unsigned int len, const struct tm *tm)
{
	int ret = 0;
	unsigned int size = 0, offset = 0;

	ret = snprintf(buf+offset, len, "<hour>%d</hour>", tm->tm_hour);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<min>%02d</min>", tm->tm_min);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<sec>%02d</sec>", tm->tm_sec);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<wday>%d</wday>", tm->tm_wday + 1);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<day>%d</day>", tm->tm_mday);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<month>%d</month>", tm->tm_mon + 1);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<year>%d</year>", 1900 + tm->tm_year);
	BUFFER_SIZE(ret, size, len, offset);

	return size;
}
コード例 #8
0
static int __snprintf_counters_xml(char *buf,
				   unsigned int len,
				   const struct nf_conntrack *ct,
				   unsigned int type)
{
	int ret;
	unsigned int size = 0, offset = 0;

	ret = snprintf(buf, len, "<packets>%llu</packets>",
		       (unsigned long long)ct->counters[type].packets);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "<bytes>%llu</bytes>",
		       (unsigned long long)ct->counters[type].bytes);
	BUFFER_SIZE(ret, size, len, offset);

	return size;
}
コード例 #9
0
ファイル: nfct-snatlog.c プロジェクト: deba12/nfct-snatlog
int __snprintf_start_log(char *buf, unsigned int len, char *log_type) {
   time_t now;
   int ret, size = 0, offset = 0;

   // if we run in daemon mode, we does not need print
   // the timestamp part... syslog does it for us. Otherwise
   // this code is needed
   if (!daemon_flag) {
      time(&now);
      ret = strftime(buf, len, "%Y-%m-%d %H:%M:%S %z", localtime(&now));
      BUFFER_SIZE(ret, size, len, offset);

      ret = snprintf(buf+offset, len, " %s: ", PROGNAME);
      BUFFER_SIZE(ret, size, len, offset);
   }

   ret = snprintf(buf+offset, len, "[%s]", log_type);
   BUFFER_SIZE(ret, size, len, offset);

   return size;
}
コード例 #10
0
ファイル: ogg.c プロジェクト: hdijkema/BackToBasics
static audio_result_t init(audio_worker_t* worker, const char* file_or_url, el_bool file)
{
  //static int ogg_initialized = 0;
  //static int ogg_error = 0;
  
  ogg_t* ogg=(ogg_t*) mc_malloc(sizeof(ogg_t));
  
  worker->can_seek = can_seek;
  worker->play = play;
  worker->pause = pause;
  worker->seek = seek;
  worker->guard = guard;
  worker->destroy = destroy;
  worker->length_in_ms = length_in_ms;
  worker->load_file = load_file;
  worker->load_url = load_url;
  worker->set_volume = set_volume;
  worker->worker_data = (void*) ogg;
  
  ogg->volume_scale = 1.0;
    
  ogg->client_notification = worker->fifo;
  ogg->player_control = audio_event_fifo_new();
  
  //int error;
  
  ogg->is_open = el_false;
  ogg->length  = -1;
  //sem_init(&ogg->length_set, 0, 0);
  ogg->length_set = psem_new(0);
  ogg->buffer = (char*) mc_malloc(BUFFER_SIZE(ogg));
  
  ogg->ao_handle = aodev_new();

  if (file) {
    ogg->can_seek = el_true;
    ogg->is_file = el_true;
    ogg->file_or_url = mc_strdup(file_or_url);
    post_event(ogg->player_control, INTERNAL_CMD_LOAD_FILE, -1); 
  } else { // URL
    ogg->can_seek = el_false;
    ogg->is_file = el_false;
    ogg->file_or_url = mc_strdup(file_or_url);
    post_event(ogg->player_control, INTERNAL_CMD_LOAD_URL, -1);
  }
  
  int thread_id = pthread_create(&ogg->player_thread, NULL, player_thread, ogg);

  // wait until fully loaded (length is set)
  psem_wait(ogg->length_set);

  return AUDIO_OK;
}
コード例 #11
0
ファイル: mifare_desfire.c プロジェクト: jrayner/bv_embedded
static ssize_t
read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_t offset, size_t length, void *data, int cs)
{
    ssize_t bytes_read = 0;

    void *p = data;

    ASSERT_ACTIVE (tag);
    ASSERT_MIFARE_DESFIRE (tag);

    BUFFER_INIT (cmd, 8);
    BUFFER_INIT (res, MAX_FRAME_SIZE);

    BUFFER_APPEND (cmd, command);
    BUFFER_APPEND (cmd, file_no);
    BUFFER_APPEND_LE (cmd, offset, 3, sizeof (off_t));
    BUFFER_APPEND_LE (cmd, length, 3, sizeof (size_t));

    if (cs) {
	if (!(p = assert_crypto_buffer_size (tag, MAX_FRAME_SIZE - 1)))
	    return -1;
    }

    do {
	ssize_t frame_bytes;

	DESFIRE_TRANSCEIVE (tag, cmd, res);

	frame_bytes = BUFFER_SIZE (res) - 1;
	memcpy ((uint8_t *)p + bytes_read, res + 1, frame_bytes);
	bytes_read += frame_bytes;

	if (res[0] == 0xAF) {
	    if (p != data) {
		// If we are handling memory, request more for next frame.
		if (!(p = assert_crypto_buffer_size (tag, bytes_read + MAX_FRAME_SIZE - 1)))
		    return -1;

	    }
	BUFFER_CLEAR (cmd);
	BUFFER_APPEND (cmd, 0xAF);
	}

    } while (res[0] != 0x00);

    if (cs) {
	if (mifare_cryto_postprocess_data (tag, p, &bytes_read, cs))
	    memcpy (data, p, bytes_read);
    }

    return bytes_read;
}
コード例 #12
0
static int __snprintf_proto_xml(char *buf,
				unsigned int len,
				const struct __nfct_tuple *tuple, 
				unsigned int type)
{
	int ret = 0;
	unsigned int size = 0, offset = 0;

	switch(tuple->protonum) {
	case IPPROTO_TCP:
	case IPPROTO_UDP:
	case IPPROTO_UDPLITE:
	case IPPROTO_SCTP:
	case IPPROTO_DCCP:
		if (type == __ADDR_SRC) {
			ret = snprintf(buf, len, "<sport>%u</sport>", 
				       ntohs(tuple->l4src.tcp.port));
			BUFFER_SIZE(ret, size, len, offset);
		} else {
			ret = snprintf(buf, len, "<dport>%u</dport>",
				       ntohs(tuple->l4dst.tcp.port));
			BUFFER_SIZE(ret, size, len, offset);
		}
		break;
	case IPPROTO_GRE:
		if (type == __ADDR_SRC) {
			ret = snprintf(buf, len, "<srckey>0x%x</srckey>", 
				       ntohs(tuple->l4src.all));
			BUFFER_SIZE(ret, size, len, offset);
		} else {
			ret = snprintf(buf, len, "<dstkey>0x%x</dstkey>",
				       ntohs(tuple->l4dst.all));
			BUFFER_SIZE(ret, size, len, offset);
		}
		break;
	}

	return ret;
}
コード例 #13
0
static int
__snprintf_timestamp_stop(char *buf, unsigned int len,
			  const struct nf_conntrack *ct)
{
	int ret;
	unsigned int size = 0, offset = 0;

	ret = snprintf(buf, len, "<stop>%llu</stop>",
		       (unsigned long long)ct->timestamp.stop);
	BUFFER_SIZE(ret, size, len, offset);

	return size;
}
コード例 #14
0
static int
__snprintf_deltatime(char *buf, unsigned int len, const struct nf_conntrack *ct)
{
	int ret;
	unsigned int size = 0, offset = 0;
	time_t delta_time = (time_t)((ct->timestamp.stop -
				ct->timestamp.start) / NSEC_PER_SEC);

	ret = snprintf(buf+offset, len, "<deltatime>%llu</deltatime>",
		(unsigned long long)delta_time);
	BUFFER_SIZE(ret, size, len, offset);

	return size;
}
コード例 #15
0
ファイル: mifare_desfire.c プロジェクト: jrayner/bv_embedded
int
mifare_desfire_get_application_ids (MifareTag tag, MifareDESFireAID *aids[], size_t *count)
{
    ASSERT_ACTIVE (tag);
    ASSERT_MIFARE_DESFIRE (tag);

    BUFFER_INIT (cmd, 1);
    BUFFER_INIT (res, MAX_FRAME_SIZE);

    BUFFER_APPEND (cmd, 0x6A);

    DESFIRE_TRANSCEIVE (tag, cmd, res);
    *count = (BUFFER_SIZE (res)-1)/3;
    *aids = malloc ((*count + 1) * sizeof (MifareDESFireAID));
    for (size_t i = 0; (3*i + 1) < BUFFER_SIZE (res); i++) {
	(*aids)[i] = memdup (res + 3*i + 1, 3);
    }

    if (res[0] == 0xAF) {
	cmd[0] = 0xAF;
	DESFIRE_TRANSCEIVE (tag, cmd, res);
	*count += (BUFFER_SIZE (res)-1) / 3;

	MifareDESFireAID *p;
	if ((p = realloc (*aids, (*count + 1) * sizeof (MifareDESFireAID)))) {
	    *aids = p;

	    for (size_t i = 0; (3*i + 1) < BUFFER_SIZE (res); i++) {
		(*aids)[19+i] = memdup (res + 3*i + 1, 3);
	    }
	}
    }

    (*aids)[*count] = NULL;

    return 0;
}
コード例 #16
0
ファイル: vbuf.c プロジェクト: Azarien/open-watcom-v2
void VbufReqd(                  // ENSURE BUFFER IS OF SUFFICIENT SIZE
    VBUF *vbuf,                 // - VBUF structure
    size_t reqd )               // - required size
{
    char    *new_buffer;        // - old buffer

    if( reqd >= vbuf->len ) {
        reqd = BUFFER_SIZE( reqd );
        new_buffer = _MemoryAllocate( reqd );
        stxvcpy( new_buffer, vbuf->buf, vbuf->used );
        FREE_BUFFER( vbuf );
        vbuf->buf = new_buffer;
        vbuf->len = reqd;
    }
}
コード例 #17
0
static int
nfacct_snprintf_xml(char *buf, size_t rem, struct nfacct *nfacct,
		    uint16_t flags)
{
	int ret = 0;
	unsigned int size = 0, offset = 0;

	ret = snprintf(buf, rem,
			"<obj><name>%s</name>"
			"<pkts>%.20llu</pkts>"
			"<bytes>%.20llu</bytes>",
			nfacct_attr_get_str(nfacct, NFACCT_ATTR_NAME),
			(unsigned long long)
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_BYTES),
			(unsigned long long)
			nfacct_attr_get_u64(nfacct, NFACCT_ATTR_PKTS));
	BUFFER_SIZE(ret, size, rem, offset);

	if (flags & NFACCT_SNPRINTF_F_TIME) {
		time_t t;
		struct tm tm;

		t = time(NULL);
		if (localtime_r(&t, &tm) == NULL)
			goto err;

		ret = nfacct_snprintf_xml_localtime(buf+offset, rem, &tm);
	        BUFFER_SIZE(ret, size, rem, offset);
	}

	ret = snprintf(buf+offset, rem, "</obj>");
	BUFFER_SIZE(ret, size, rem, offset);

err:
	return ret;
}
コード例 #18
0
ファイル: cgpt_show.c プロジェクト: ccaapton/vboot_reference
void PrintSignature(const char *indent, const char *sig, size_t n, int raw) {
  size_t i;
  printf("%sSig: ", indent);
  if (!raw) {
    printf("[");
    for (i = 0; i < n; ++i)
      printf("%c", sig[i]);
    printf("]");
  } else {
    char *buf = malloc(BUFFER_SIZE(n));
    RawDump((uint8_t *)sig, n, buf, 1);
    printf("%s", buf);
    free(buf);
  }
  printf("\n");
}
コード例 #19
0
ファイル: mifare_desfire.c プロジェクト: jrayner/bv_embedded
int
mifare_desfire_get_file_settings (MifareTag tag, uint8_t file_no, struct mifare_desfire_file_settings *settings)
{
    ASSERT_ACTIVE (tag);
    ASSERT_MIFARE_DESFIRE (tag);

    BUFFER_INIT (cmd, 2);
    BUFFER_INIT (res, 18);

    BUFFER_APPEND (cmd, 0xF5);
    BUFFER_APPEND (cmd, file_no);

    DESFIRE_TRANSCEIVE (tag, cmd, res);

    struct mifare_desfire_raw_file_settings raw_settings;
    memcpy (&raw_settings, res+1, BUFFER_SIZE (res)-1);

    settings->file_type = raw_settings.file_type;
    settings->communication_settings = raw_settings.communication_settings;
    settings->access_rights = le16toh (raw_settings.access_rights);
    
    switch (settings->file_type) {
	case MDFT_STANDARD_DATA_FILE:
	case MDFT_BACKUP_DATA_FILE:
	    settings->settings.standard_file.file_size = le24toh (raw_settings.settings.standard_file.file_size);
	    break;
	case MDFT_VALUE_FILE_WITH_BACKUP:
	    settings->settings.value_file.lower_limit = le32toh (raw_settings.settings.value_file.lower_limit);
	    settings->settings.value_file.upper_limit = le32toh (raw_settings.settings.value_file.upper_limit);
	    settings->settings.value_file.limited_credit_value = le32toh (raw_settings.settings.value_file.limited_credit_value);
	    settings->settings.value_file.limited_credit_enabled = raw_settings.settings.value_file.limited_credit_enabled;
	    break;
	case MDFT_LINEAR_RECORD_FILE_WITH_BACKUP:
	case MDFT_CYCLIC_RECORD_FILE_WITH_BACKUP:
	    settings->settings.linear_record_file.record_size = le24toh (raw_settings.settings.linear_record_file.record_size);
	    settings->settings.linear_record_file.max_number_of_records = le24toh (raw_settings.settings.linear_record_file.max_number_of_records);
	    settings->settings.linear_record_file.current_number_of_records = le24toh (raw_settings.settings.linear_record_file.current_number_of_records);
	    break;
    }

    return 0;
}
コード例 #20
0
ファイル: buffers.c プロジェクト: liexusong/linux2.0-comment
void
BufPoolInit(struct BufPool *bp, int order, int bpps,
	    int maxpages)
{
#ifdef DEBUG_MAGIC
	generateerror
	    bp->magic = 010167;
#endif

#if 0
	printk(KERN_DEBUG "BufPoolInit bp %x\n", bp);
#endif

	bp->freelist = NULL;
	bp->pageslist = NULL;
	bp->pageorder = order;
	bp->pagescount = 0;
	bp->bpps = bpps;
	bp->bufsize = BUFFER_SIZE(order, bpps);
	bp->maxpages = maxpages;
}
コード例 #21
0
ファイル: mifare_desfire.c プロジェクト: jrayner/bv_embedded
int
mifare_desfire_get_file_ids (MifareTag tag, uint8_t *files[], size_t *count)
{
    ASSERT_ACTIVE (tag);
    ASSERT_MIFARE_DESFIRE (tag);

    BUFFER_INIT (cmd, 1);
    BUFFER_INIT (res, 16);

    BUFFER_APPEND (cmd, 0x6F);

    DESFIRE_TRANSCEIVE (tag, cmd, res);

    *count = BUFFER_SIZE (res) - 1;

    if (!(*files = malloc (*count))) {
	errno = ENOMEM;
	return -1;
    }
    memcpy (*files, res+1, *count);

    return 0;
}
コード例 #22
0
static int __snprintf_ipv4_xml(char *buf,
			       unsigned int len,
			       const struct __nfct_tuple *tuple,
			       unsigned int type)
{
	struct in_addr addr = { 
		.s_addr = (type == __ADDR_SRC) ? tuple->src.v4 : tuple->dst.v4,
	};

	return snprintf(buf, len, "%s", inet_ntoa(addr));
}

static int __snprintf_ipv6_xml(char *buf,
			       unsigned int len,
			       const struct __nfct_tuple *tuple,
			       unsigned int type)
{
	struct in6_addr addr;
	static char tmp[INET6_ADDRSTRLEN];
	const void *p = (type == __ADDR_SRC) ? &tuple->src.v6 : &tuple->dst.v6;

	memcpy(&addr, p, sizeof(struct in6_addr));

	if (!inet_ntop(AF_INET6, &addr, tmp, sizeof(tmp)))
		return -1;

	return snprintf(buf, len, "%s", tmp);
}

static int __snprintf_addr_xml(char *buf,
			       unsigned int len,
			       const struct __nfct_tuple *tuple, 
			       unsigned int type)
{
	int ret;
	unsigned int size = 0, offset = 0;

	switch(type) {
	case __ADDR_SRC:
		ret = snprintf(buf, len, "<src>");
		BUFFER_SIZE(ret, size, len, offset);
		break;
	case __ADDR_DST:
		ret = snprintf(buf+offset, len, "<dst>");
		BUFFER_SIZE(ret, size, len, offset);
		break;
	}

	switch (tuple->l3protonum) {
	case AF_INET:
		ret = __snprintf_ipv4_xml(buf+offset, len, tuple, type);
		BUFFER_SIZE(ret, size, len, offset);
		break;
	case AF_INET6:
		ret = __snprintf_ipv6_xml(buf+offset, len, tuple, type);
		BUFFER_SIZE(ret, size, len, offset);
		break;
	}

	switch(type) {
	case __ADDR_SRC:
		ret = snprintf(buf+offset, len, "</src>");
		BUFFER_SIZE(ret, size, len, offset);
		break;
	case __ADDR_DST:
		ret = snprintf(buf+offset, len, "</dst>");
		BUFFER_SIZE(ret, size, len, offset);
		break;
	}

	return size;
}
コード例 #23
0
int __snprintf_conntrack_xml(char *buf,
			     unsigned int len,
			     const struct nf_conntrack *ct,
			     const unsigned int msg_type,
			     const unsigned int flags) 
{
	int ret = 0;
	unsigned int size = 0, offset = 0;

	switch(msg_type) {
		case NFCT_T_NEW:
			ret = snprintf(buf, len, "<flow type=\"new\">");
			break;
		case NFCT_T_UPDATE:
			ret = snprintf(buf, len, "<flow type=\"update\">");
			break;
		case NFCT_T_DESTROY:
			ret = snprintf(buf, len, "<flow type=\"destroy\">");
			break;
		default:
			ret = snprintf(buf, len, "<flow>");
			break;
	}

	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_tuple_xml(buf+offset, len, ct, __DIR_ORIG);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_tuple_xml(buf+offset, len, ct, __DIR_REPL);
	BUFFER_SIZE(ret, size, len, offset);

	if (test_bit(ATTR_TCP_STATE, ct->head.set) ||
	    test_bit(ATTR_SCTP_STATE, ct->head.set) ||
	    test_bit(ATTR_DCCP_STATE, ct->head.set) ||
	    test_bit(ATTR_TIMEOUT, ct->head.set) ||
	    test_bit(ATTR_MARK, ct->head.set) ||
	    test_bit(ATTR_SECMARK, ct->head.set) ||
	    test_bit(ATTR_ZONE, ct->head.set) ||
	    test_bit(ATTR_USE, ct->head.set) ||
	    test_bit(ATTR_STATUS, ct->head.set) ||
	    test_bit(ATTR_ID, ct->head.set) ||
	    test_bit(ATTR_TIMESTAMP_START, ct->head.set) ||
	    test_bit(ATTR_TIMESTAMP_STOP, ct->head.set)) {
		ret = snprintf(buf+offset, len, 
			       "<meta direction=\"independent\">");
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_TCP_STATE, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<state>%s</state>",
			       ct->protoinfo.tcp.state < TCP_CONNTRACK_MAX ?
			       states[ct->protoinfo.tcp.state] :
			       states[TCP_CONNTRACK_NONE]);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_SCTP_STATE, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<state>%s</state>",
			       ct->protoinfo.sctp.state < SCTP_CONNTRACK_MAX ?
			       states[ct->protoinfo.sctp.state] :
			       states[SCTP_CONNTRACK_NONE]);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_DCCP_STATE, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<state>%s</state>",
			       ct->protoinfo.sctp.state < DCCP_CONNTRACK_MAX ?
			       states[ct->protoinfo.dccp.state] :
			       states[DCCP_CONNTRACK_NONE]);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_TIMEOUT, ct->head.set)) {
		ret = snprintf(buf+offset, len,
				"<timeout>%u</timeout>", ct->timeout);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_MARK, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<mark>%u</mark>", ct->mark);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_SECMARK, ct->head.set)) {
		ret = snprintf(buf+offset, len, 
				"<secmark>%u</secmark>", ct->secmark);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_SECCTX, ct->head.set)) {
		ret = snprintf(buf+offset, len,
				"<secctx>%s</secctx>", ct->secctx);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_ZONE, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<zone>%u</zone>", ct->zone);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_USE, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<use>%u</use>", ct->use);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_ID, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<id>%u</id>", ct->id);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_STATUS, ct->head.set)
	    && ct->status & IPS_ASSURED) {
		ret = snprintf(buf+offset, len, "<assured/>");
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_STATUS, ct->head.set) 
	    && !(ct->status & IPS_SEEN_REPLY)) {
		ret = snprintf(buf+offset, len, "<unreplied/>");
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (flags & NFCT_OF_TIMESTAMP) {
		if (test_bit(ATTR_TIMESTAMP_START, ct->head.set) ||
		    test_bit(ATTR_TIMESTAMP_STOP, ct->head.set)) {
			ret = snprintf(buf+offset, len, "<timestamp>");
			BUFFER_SIZE(ret, size, len, offset);
		}
		if (test_bit(ATTR_TIMESTAMP_START, ct->head.set)) {
	 		ret = __snprintf_timestamp_start(buf+offset, len, ct);
			BUFFER_SIZE(ret, size, len, offset);
		}
		if (test_bit(ATTR_TIMESTAMP_STOP, ct->head.set)) {
	 		ret = __snprintf_timestamp_stop(buf+offset, len, ct);
			BUFFER_SIZE(ret, size, len, offset);
		}
		if (test_bit(ATTR_TIMESTAMP_START, ct->head.set) ||
		    test_bit(ATTR_TIMESTAMP_STOP, ct->head.set)) {
			ret = snprintf(buf+offset, len, "</timestamp>");
			BUFFER_SIZE(ret, size, len, offset);
		}
	}
	if (test_bit(ATTR_TIMESTAMP_START, ct->head.set) &&
	    test_bit(ATTR_TIMESTAMP_STOP, ct->head.set)) {
		ret = __snprintf_deltatime(buf+offset, len, ct);
		BUFFER_SIZE(ret, size, len, offset);
	} else if (test_bit(ATTR_TIMESTAMP_START, ct->head.set)) {
		ret = __snprintf_deltatime_now(buf+offset, len, ct);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (test_bit(ATTR_TCP_STATE, ct->head.set) ||
	    test_bit(ATTR_SCTP_STATE, ct->head.set) ||
	    test_bit(ATTR_DCCP_STATE, ct->head.set) ||
	    test_bit(ATTR_TIMEOUT, ct->head.set) ||
	    test_bit(ATTR_MARK, ct->head.set) ||
	    test_bit(ATTR_SECMARK, ct->head.set) ||
	    test_bit(ATTR_ZONE, ct->head.set) ||
	    test_bit(ATTR_USE, ct->head.set) ||
	    test_bit(ATTR_STATUS, ct->head.set) ||
	    test_bit(ATTR_ID, ct->head.set) ||
	    test_bit(ATTR_TIMESTAMP_START, ct->head.set) ||
	    test_bit(ATTR_TIMESTAMP_STOP, ct->head.set)) {
	    	ret = snprintf(buf+offset, len, "</meta>");
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (flags & NFCT_OF_TIME) {
		time_t t;
		struct tm tm;

		t = time(NULL);
		if (localtime_r(&t, &tm) == NULL)
			goto err_out;

		ret = snprintf(buf+offset, len, "<when>");
		BUFFER_SIZE(ret, size, len, offset);

		ret = __snprintf_localtime_xml(buf+offset, len, &tm);
		BUFFER_SIZE(ret, size, len, offset);

		ret = snprintf(buf+offset, len, "</when>");
		BUFFER_SIZE(ret, size, len, offset);
	}

err_out:
	ret = snprintf(buf+offset, len, "</flow>");
	BUFFER_SIZE(ret, size, len, offset);

	return size;
}
コード例 #24
0
static int __snprintf_tuple_xml(char *buf,
				unsigned int len,
				const struct nf_conntrack *ct,
				unsigned int dir)
{
	int ret;
	unsigned int size = 0, offset = 0;
	const struct __nfct_tuple *tuple = NULL;

	switch(dir) {
	case __DIR_ORIG:
		tuple = &ct->head.orig;
		break;
	case __DIR_REPL:
		tuple = &ct->repl;
		break;
	}
	ret = snprintf(buf, len, "<meta direction=\"%s\">",
		       dir == __DIR_ORIG ? "original" : "reply");
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, 
		       "<layer3 protonum=\"%d\" protoname=\"%s\">",
		       tuple->l3protonum, __l3proto2str(tuple->l3protonum));
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_addr_xml(buf+offset, len, tuple, __DIR_ORIG);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_addr_xml(buf+offset, len, tuple, __DIR_REPL);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "</layer3>");
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, 
		       "<layer4 protonum=\"%d\" protoname=\"%s\">",
		       tuple->protonum, __proto2str(tuple->protonum));
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_proto_xml(buf+offset, len, tuple, __DIR_ORIG);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_proto_xml(buf+offset, len, tuple, __DIR_REPL);
	BUFFER_SIZE(ret, size, len, offset);

	ret = snprintf(buf+offset, len, "</layer4>");
	BUFFER_SIZE(ret, size, len, offset);

	if (test_bit(ATTR_ORIG_COUNTER_PACKETS, ct->head.set) &&
	    test_bit(ATTR_ORIG_COUNTER_BYTES, ct->head.set)) {
		ret = snprintf(buf+offset, len, "<counters>");
		BUFFER_SIZE(ret, size, len, offset);

		ret = __snprintf_counters_xml(buf+offset, len, ct, dir);
		BUFFER_SIZE(ret, size, len, offset);

		ret = snprintf(buf+offset, len, "</counters>");
		BUFFER_SIZE(ret, size, len, offset);
	}

	ret = snprintf(buf+offset, len, "</meta>");
	BUFFER_SIZE(ret, size, len, offset);

	return size;
}
コード例 #25
0
ファイル: buffer.c プロジェクト: jaz303/buffer.c
size_t buffer_size(buffer_t *buffer) { return BUFFER_SIZE(buffer); }
コード例 #26
0
ファイル: ogg.c プロジェクト: hdijkema/BackToBasics
static void* player_thread(void* _minfo) 
{
  ogg_t* minfo = (ogg_t*) _minfo;
  
  log_debug("ogg player started");
  
  long current_position_in_ms = 0;
  long previous_position_in_ms = -1; 
  long guard_position_in_ms = -1;
  el_bool playing = el_false;
  
  post_event(minfo->client_notification, AUDIO_READY, current_position_in_ms);
 
  audio_event_t *event;
  event = audio_event_fifo_dequeue(minfo->player_control);
  while (event->state != INTERNAL_CMD_DESTROY) {
    
    audio_state_t event_state = event->state;
    long event_position = event->position_in_ms;
    audio_event_destroy(event);
    
    switch (event_state) {
      case INTERNAL_CMD_LOAD_FILE: {
        playing = el_false;
        if (minfo->is_open) {
          ov_clear(&minfo->vf);
          fclose(minfo->fh);
          aodev_close(minfo->ao_handle);
        }

        minfo->fh = fopen(minfo->file_or_url, "rb");
        if (minfo->fh != NULL) {
          if (ov_open_callbacks(minfo->fh , &minfo->vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
            fclose(minfo->fh);
            post_event(minfo->client_notification, AUDIO_NOT_SUPPORTED, -1);
            minfo->is_open = el_false;
          } else {
            minfo->is_open = el_true;
            minfo->can_seek = ov_seekable(&minfo->vf);
            minfo->length = (long) (ov_time_total(&minfo->vf, -1) * 1000.0);
            psem_post(minfo->length_set);
            minfo->current_section = 0;
            vorbis_info* vi = ov_info(&minfo->vf, -1);
            aodev_set_format(minfo->ao_handle, 16, vi->rate, vi->channels);
            aodev_set_endian(minfo->ao_handle, AO_FMT_LITTLE);
            aodev_open(minfo->ao_handle);
          }
        } else {
          post_event(minfo->client_notification, AUDIO_IO_ERROR, -1);
          minfo->is_open = el_false;
        }
        
        current_position_in_ms = 0;
        guard_position_in_ms = -1; 
        
        log_debug("Stream initialized");
          
      }
      break;
      case INTERNAL_CMD_LOAD_URL: {
      }
      break;
      case INTERNAL_CMD_SEEK: {
        ov_time_seek_lap(&minfo->vf, ((double) event_position / 1000.0));
      }
      break;
      case INTERNAL_CMD_PLAY: {
        playing = el_true;
      }
      break;
      case INTERNAL_CMD_PAUSE: {
        playing = el_false;
      }
      break;
      case INTERNAL_CMD_GUARD: {
        guard_position_in_ms = event_position;
      }
      break;
      case INTERNAL_CMD_SET_VOLUME: {
        double scale = ((double) event_position) / 1000.0;
        minfo->volume_scale = scale;
        log_debug2("setting volume to %lf", scale);
      }
      break;
      case INTERNAL_CMD_NONE:
      break;
      default:
      break;
    }
    
    if (guard_position_in_ms >= 0 && current_position_in_ms >= guard_position_in_ms) {

      guard_position_in_ms = -1;
      post_event(minfo->client_notification, AUDIO_GUARD_REACHED, current_position_in_ms);
      
    } else if (playing) {
      if (minfo->is_file) {
        int n = ov_read_filter(&minfo->vf, minfo->buffer, BUFFER_SIZE(minfo), 0, 2, 1, &minfo->current_section,
                               adjust_volume, minfo
                              );
        if (n > 0) {
          aodev_play_buffer(minfo->ao_handle, minfo->buffer, n);
          //log_debug("buffer played");
          
          double tm = ov_time_tell(&minfo->vf);
          current_position_in_ms = (long) (tm * 1000.0);
          
          if ((current_position_in_ms - previous_position_in_ms) >= STATE_REPORT_THRESHOLD) {
            post_event(minfo->client_notification, AUDIO_PLAYING, current_position_in_ms);
          }
          previous_position_in_ms = current_position_in_ms;
          
        } else {
          post_event(minfo->client_notification, AUDIO_EOS, current_position_in_ms);
          playing = el_false;
        } 
      } else { // Stream playing
        post_event(minfo->client_notification, AUDIO_STATE_ERROR, -1);
        playing = el_false;
      } 
    
    }
    
    if (playing) {
      if (audio_event_fifo_peek(minfo->player_control) != NULL) {
        event = audio_event_fifo_dequeue(minfo->player_control);
      } else {
        event = (audio_event_t*) mc_malloc(sizeof(audio_event_t));
        event->state = INTERNAL_CMD_NONE;
        event->position_in_ms = -1;
      }
    } else {
      event = audio_event_fifo_dequeue(minfo->player_control);
    }
  }

  // destroy event received
  audio_event_destroy(event);

  // exit thread  
  return NULL;
}
コード例 #27
0
ファイル: nfct-snatlog.c プロジェクト: deba12/nfct-snatlog
void print_verbose(struct nf_conntrack *ct,
      enum nf_conntrack_msg_type type, char *proto_str) {
   int ret = 0, size = 0, offset = 0, len = BUF_LEN;
   char buf[BUF_LEN];

   ret = __snprintf_start_log(buf, len, "DEBUG");
   BUFFER_SIZE(ret, size, len, offset);

   switch(type) {
      case NFCT_T_NEW:
         ret = snprintf(buf+offset, len, " NEW");
         BUFFER_SIZE(ret, size, len, offset);
         break;
      case NFCT_T_DESTROY:
         ret = snprintf(buf+offset, len, " DESTROY");
         BUFFER_SIZE(ret, size, len, offset);
         break;
      default:
         break;
   }

   ret = snprintf(buf+offset, len, " id=%u", nfct_get_attr_u32(ct,ATTR_ID));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " proto=%s", proto_str);
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " orig-src=%s", 
         net2addr(nfct_get_attr_u32(ct,ATTR_ORIG_IPV4_SRC)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " orig-dst=%s", 
         net2addr(nfct_get_attr_u32(ct,ATTR_ORIG_IPV4_DST)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " orig-sport=%d", 
         ntohs(nfct_get_attr_u16(ct,ATTR_ORIG_PORT_SRC)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " orig-dport=%d", 
         ntohs(nfct_get_attr_u16(ct,ATTR_ORIG_PORT_DST)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " repl-src=%s", 
         net2addr(nfct_get_attr_u32(ct,ATTR_REPL_IPV4_SRC)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " repl-dst=%s", 
         net2addr(nfct_get_attr_u32(ct,ATTR_REPL_IPV4_DST)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " repl-sport=%d", 
         ntohs(nfct_get_attr_u16(ct,ATTR_REPL_PORT_SRC)));
   BUFFER_SIZE(ret, size, len, offset);

   ret = snprintf(buf+offset, len, " repl-dport=%d", 
         ntohs(nfct_get_attr_u16(ct,ATTR_REPL_PORT_DST)));
   BUFFER_SIZE(ret, size, len, offset);

   buf[size+1 > len ? len-1 : size] = '\0';

   write_msg(LOG_INFO, buf);
}
コード例 #28
0
ファイル: soundtest.c プロジェクト: jpanikulam/visar
int main() {
  kill_flag = 0;  //not killed yet

  // Open PCM device for playback, check for errors
  snd_pcm_t *speaker_handle; //handler struct
  int rc = snd_pcm_open(&speaker_handle, "default",SND_PCM_STREAM_PLAYBACK, 0);
  if (rc < 0) {
    fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(rc));
    exit(1);
  }

  /* Allocate a hardware parameters object. */
  snd_pcm_hw_params_t *params;
  snd_pcm_hw_params_alloca(&params);

  /* Fill it in with default values. */
  snd_pcm_hw_params_any(speaker_handle, params);

  /* Set the desired hardware parameters. */

  /* Interleaved mode */
  snd_pcm_hw_params_set_access(speaker_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

  /* Signed 16-bit little-endian format */
  snd_pcm_hw_params_set_format(speaker_handle, params, SND_PCM_FORMAT_S16_LE);

  /* Two channels (stereo) */
  snd_pcm_hw_params_set_channels(speaker_handle, params, 2);

  /* 44100 bits/second sampling rate (CD quality) */
  unsigned int val = SAMPLE_RATE; //assign value
  int dir; //direction (input/output)
  snd_pcm_hw_params_set_rate_near(speaker_handle, params, &val, &dir); //get closest match
  printf("rate is %d\n",val);

  /* Set period size to the constant frames. */
  size_t frames = PERIOD;
  snd_pcm_hw_params_set_period_size_near(speaker_handle, params, &frames, &dir);

  /* Write the parameters to the driver */
  rc = snd_pcm_hw_params(speaker_handle, params);
  if (rc < 0) {
    fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
    exit(1);
  }

  /* Allocate stream buffer and input buffer */
  snd_pcm_hw_params_get_period_size(params, &frames, &dir);
  printf("Actual frames: %d\n",(int)frames);
  frame_size = frames * 4; /* 2 bytes/sample, 2 channels */
  input_buffer = (char*) malloc(frame_size * MAX_BUFFER);  //allocate buffer for network input
  in_buff_start = 0; in_buff_end = 0; //setup the queue
  
  /* create a server socket and thread to handle input */
  pthread_t thread;
  int flags = 0;
  rc = pthread_create(&thread, NULL, reciever_thread, (void*)&flags);
  if (rc){
     printf("ERROR; return code from pthread_create() is %d\n", rc);
     exit(-1);
  }

  char started = 0;  //track when to start reading data
  while(!kill_flag) { //loop until broken
    //rc = read(0, speaker_buffer, size); //read values in from console
    //wait for some buffer before starting, stop if empty
    if((!started && BUFFER_SIZE(in_buff_start,in_buff_end) < (MAX_BUFFER/2)) \
          || BUFFER_EMPTY(in_buff_start, in_buff_end)){
      //printf("Skipped (%d,%d)\n",BUFFER_SIZE(in_buff_start,in_buff_end),BUFFER_EMPTY(in_buff_start,in_buff_end));
      //if(started) printf("stopping\n");
      started = 0;
      continue; 
    }
    else {
      //if(!started) printf("starting\n");
      started = 1; //indicate that we've startd
    }
    
    //write data to speaker buffer, check responses
    rc = snd_pcm_writei(speaker_handle, input_buffer+(frame_size*in_buff_start), frames);
    //printf("wrote data to speakers\n");
    in_buff_start = (in_buff_start+1)%MAX_BUFFER;
    if (rc == -EPIPE){ /* EPIPE means underrun */
      fprintf(stderr, "underrun occurred\n");
      snd_pcm_prepare(speaker_handle);
    } else if (rc < 0) fprintf(stderr, "error from writei: %s\n", snd_strerror(rc));
    else if (rc != (int)frames) fprintf(stderr, "short write, write %d frames\n", rc);
  }

  // notify kernel to empty/close the buffers
  snd_pcm_drain(speaker_handle);
  snd_pcm_close(speaker_handle);

  free(input_buffer); //free our buffers

  kill_flag = 1;  //signal that threads should die
  pthread_exit(NULL); //exit without killing children
  return 0;
}