int audio_alloc(struct audio **ap, const struct config *cfg, struct call *call, struct sdp_session *sdp_sess, int label, const struct mnat *mnat, struct mnat_sess *mnat_sess, const struct menc *menc, struct menc_sess *menc_sess, uint32_t ptime, const struct list *aucodecl, audio_event_h *eventh, audio_err_h *errh, void *arg) { struct audio *a; struct autx *tx; struct aurx *rx; struct le *le; int err; if (!ap || !cfg) return EINVAL; a = mem_zalloc(sizeof(*a), audio_destructor); if (!a) return ENOMEM; MAGIC_INIT(a); a->cfg = cfg->audio; tx = &a->tx; rx = &a->rx; err = stream_alloc(&a->strm, &cfg->avt, call, sdp_sess, "audio", label, mnat, mnat_sess, menc, menc_sess, stream_recv_handler, NULL, a); if (err) goto out; stream_set_bw(a->strm, AUDIO_BANDWIDTH); err = sdp_media_set_lattr(stream_sdpmedia(a->strm), true, "ptime", "%u", ptime); if (err) goto out; /* Audio codecs */ for (le = list_head(aucodecl); le; le = le->next) { err = add_audio_codec(a, stream_sdpmedia(a->strm), le->data); if (err) goto out; } tx->mb = mbuf_alloc(STREAM_PRESZ + 4096); tx->sampv = mem_zalloc(AUDIO_SAMPSZ * 2, NULL); rx->sampv = mem_zalloc(AUDIO_SAMPSZ * 2, NULL); if (!tx->mb || !tx->sampv || !rx->sampv) { err = ENOMEM; goto out; } err = telev_alloc(&a->telev, TELEV_PTIME); if (err) goto out; err = add_telev_codec(a); if (err) goto out; tx->ptime = ptime; tx->ts = 160; tx->marker = true; rx->pt = -1; rx->ptime = ptime; a->eventh = eventh; a->errh = errh; a->arg = arg; if (a->cfg.txmode == AUDIO_MODE_TMR) tmr_init(&tx->u.tmr); out: if (err) mem_deref(a); else *ap = a; return err; }
static int stream_ethernet_init(struct stream** stptr, const struct ether_addr* addr, const char* iface, uint16_t proto, size_t buffer_size){ struct iface ifstat; int ret = 0; /* validate arguments */ assert(stptr); if ( !(addr && iface) ){ return EINVAL; } /* query interface properties */ if ( (ret=iface_get(iface, &ifstat)) != 0 ){ return ret; } const unsigned int frame_size = ifstat.if_mtu + sizeof(struct ethhdr); /* default buffer_size of 250*MTU */ if ( buffer_size == 0 ){ buffer_size = 250 * frame_size; } /* ensure buffer is a multiple of MTU and can hold at least one frame */ if ( buffer_size < frame_size ){ return ERROR_BUFFER_LENGTH; } else if ( buffer_size % frame_size != 0 ){ return ERROR_BUFFER_MULTIPLE; } /* slightly backwards calculation, but user want to enter buffer size in bytes (and it maintains compatibility) */ const size_t num_frames = buffer_size / frame_size; buffer_size = stream_frame_buffer_size(num_frames, frame_size); /* Initialize stream */ if ( (ret = stream_alloc(stptr, PROTOCOL_ETHERNET_MULTICAST, sizeof(struct stream_ethernet), buffer_size, ifstat.if_mtu) != 0) ){ return ret; } struct stream_ethernet* st = (struct stream_ethernet*)*stptr; stream_frame_init(&st->fb, (read_frame_callback)stream_ethernet_read_frame, (char*)st->frame, num_frames, frame_size); /* open raw socket */ if ( (st->socket=socket(AF_PACKET, SOCK_RAW, htons(proto))) < 0 ){ return errno; } st->fb.header_offset = sizeof(struct ethhdr); st->if_index = ifstat.if_index; st->base.if_loopback = ifstat.if_loopback; memset(st->seqnum, 0, sizeof(long unsigned int) * MAX_ADDRESS); /* bind MA MAC */ memset(&st->sll, 0, sizeof(st->sll)); st->sll.sll_family=AF_PACKET; st->sll.sll_ifindex=st->if_index; st->sll.sll_protocol=htons(proto); st->sll.sll_pkttype=PACKET_MULTICAST; memcpy(st->sll.sll_addr, &ifstat.if_hwaddr, ETH_ALEN); if ( bind(st->socket, (struct sockaddr *) &st->sll, sizeof(st->sll)) == -1 ) { perror("Binding to interface."); return errno; } /* add membership to group */ if ( (ret=stream_ethernet_add(&st->base, addr)) != 0 ){ return ret; } return 0; }
void nile_device::device_start() { m_sound_ram = region()->base(); m_stream = stream_alloc(0, 2, 44100); }
void specimx_sound_device::device_start() { m_specimx_input[0] = m_specimx_input[1] = m_specimx_input[2] = 0; m_mixer_channel = stream_alloc(0, 1, machine().sample_rate()); }
void vc4000_sound_device::device_start() { m_channel = stream_alloc(0, 1, machine().sample_rate()); }
void filter_volume_device::device_start() { m_gain = 0x100; m_stream = stream_alloc(1, 1, machine().sample_rate()); }
/* ============================================================================= * main * ============================================================================= */ MAIN(argc, argv) { GOTO_REAL(); /* * Initialization */ parseArgs(argc, (char** const)argv); long numThread = global_params[PARAM_THREAD]; SIM_GET_NUM_CPU(numThread); TM_STARTUP(numThread); P_MEMORY_STARTUP(numThread); thread_startup(numThread); long percentAttack = global_params[PARAM_ATTACK]; long maxDataLength = global_params[PARAM_LENGTH]; long numFlow = global_params[PARAM_NUM]; long randomSeed = global_params[PARAM_SEED]; printf("Percent attack = %li\n", percentAttack); printf("Max data length = %li\n", maxDataLength); printf("Num flow = %li\n", numFlow); printf("Random seed = %li\n", randomSeed); dictionary_t* dictionaryPtr = dictionary_alloc(); assert(dictionaryPtr); stream_t* streamPtr = stream_alloc(percentAttack); assert(streamPtr); long numAttack = stream_generate(streamPtr, dictionaryPtr, numFlow, randomSeed, maxDataLength); printf("Num attack = %li\n", numAttack); decoder_t* decoderPtr = decoder_alloc(); assert(decoderPtr); vector_t** errorVectors = (vector_t**)malloc(numThread * sizeof(vector_t*)); assert(errorVectors); long i; for (i = 0; i < numThread; i++) { vector_t* errorVectorPtr = vector_alloc(numFlow); assert(errorVectorPtr); errorVectors[i] = errorVectorPtr; } arg_t arg; arg.streamPtr = streamPtr; arg.decoderPtr = decoderPtr; arg.errorVectors = errorVectors; /* * Run transactions */ TIMER_T startTime; TIMER_READ(startTime); GOTO_SIM(); #ifdef OTM #pragma omp parallel { processPackets((void*)&arg); } #else thread_start(processPackets, (void*)&arg); #endif GOTO_REAL(); TIMER_T stopTime; TIMER_READ(stopTime); printf("\nTime = %lf\n", TIMER_DIFF_SECONDS(startTime, stopTime)); /* * Check solution */ /*long numFound = 0; for (i = 0; i < numThread; i++) { vector_t* errorVectorPtr = errorVectors[i]; long e; long numError = vector_getSize(errorVectorPtr); numFound += numError; for (e = 0; e < numError; e++) { long flowId = (long)vector_at(errorVectorPtr, e); bool_t status = stream_isAttack(streamPtr, flowId); assert(status); } }*/ /*printf("Num found = %li\n", numFound); assert(numFound == numAttack);*/ /* * Clean up */ for (i = 0; i < numThread; i++) { vector_free(errorVectors[i]); } free(errorVectors); decoder_free(decoderPtr); stream_free(streamPtr); dictionary_free(dictionaryPtr); TM_SHUTDOWN(); P_MEMORY_SHUTDOWN(); GOTO_SIM(); thread_shutdown(); MAIN_RETURN(0); }
void tia_device::device_start() { m_channel = stream_alloc(0, 1, clock()); m_chip = tia_sound_init(this, clock(), clock(), 16); assert_always(m_chip != nullptr, "Error creating TIA chip"); }
void voltage_regulator_device::device_start() { m_stream = stream_alloc(0, 1, 500); }
/* ============================================================================= * main * ============================================================================= */ MAIN(argc, argv) { GOTO_REAL(); /* * Initialization */ SETUP_NUMBER_TASKS(3); parseArgs(argc, (char** const)argv); long numThread = global_params[PARAM_THREAD]; SETUP_NUMBER_THREADS(numThread); SIM_GET_NUM_CPU(numThread); TM_STARTUP(numThread, 0); P_MEMORY_STARTUP(numThread); thread_startup(numThread); long percentAttack = global_params[PARAM_ATTACK]; long maxDataLength = global_params[PARAM_LENGTH]; long numFlow = global_params[PARAM_NUM]; long randomSeed = global_params[PARAM_SEED]; /* printf("Percent attack = %li\n", percentAttack); printf("Max data length = %li\n", maxDataLength); printf("Num flow = %li\n", numFlow); printf("Random seed = %li\n", randomSeed); */ double time_total = 0.0; int repeats = global_params[PARAM_REPEAT]; for (; repeats > 0; --repeats) { dictionary_t* dictionaryPtr = dictionary_alloc(); assert(dictionaryPtr); stream_t* streamPtr = stream_alloc(percentAttack); assert(streamPtr); long numAttack = stream_generate(streamPtr, dictionaryPtr, numFlow, randomSeed, maxDataLength); // printf("Num attack = %li\n", numAttack); decoder_t* decoderPtr = decoder_alloc(); assert(decoderPtr); vector_t** errorVectors = (vector_t**)malloc(numThread * sizeof(vector_t*)); assert(errorVectors); long i; for (i = 0; i < numThread; i++) { vector_t* errorVectorPtr = vector_alloc(numFlow); assert(errorVectorPtr); errorVectors[i] = errorVectorPtr; } arg_t arg; arg.streamPtr = streamPtr; arg.decoderPtr = decoderPtr; arg.errorVectors = errorVectors; /* * Run transactions */ TIMER_T startTime; TIMER_READ(startTime); tm_time_t start_clock=TM_TIMER_READ(); GOTO_SIM(); thread_start(processPackets, (void*)&arg); GOTO_REAL(); TIMER_T stopTime; tm_time_t end_clock=TM_TIMER_READ(); TIMER_READ(stopTime); double time_tmp = TIMER_DIFF_SECONDS(startTime, stopTime); time_total += time_tmp; PRINT_STATS(); PRINT_CLOCK_THROUGHPUT(end_clock-start_clock); /* * Check solution */ long numFound = 0; for (i = 0; i < numThread; i++) { vector_t* errorVectorPtr = errorVectors[i]; long e; long numError = vector_getSize(errorVectorPtr); numFound += numError; for (e = 0; e < numError; e++) { long flowId = (long)vector_at(errorVectorPtr, e); bool_t status = stream_isAttack(streamPtr, flowId); assert(status); } } // printf("Num found = %li\n", numFound); assert(numFound == numAttack); /* * Clean up */ for (i = 0; i < numThread; i++) { vector_free(errorVectors[i]); } free(errorVectors); decoder_free(decoderPtr); stream_free(streamPtr); dictionary_free(dictionaryPtr); } printf("Time = %f\n", time_total); TM_SHUTDOWN(); P_MEMORY_SHUTDOWN(); GOTO_SIM(); thread_shutdown(); MAIN_RETURN(0); }
/** * Initialize file stream. * @return Non-zero on error (see errno(3) for descriptions). */ int stream_file_open(struct stream** stptr, FILE* fp, const char* filename, size_t buffer_size){ assert(stptr); *stptr = NULL; int ret; /* validate that filename or fp is set */ if ( !(filename||fp) ){ return ENOENT; } /* try to open the file */ if ( !fp ) { fp = fopen(filename, "rb"); if( !fp ){ return errno; } } /* Use a relative smaller buffer-size by default as it will yield faster * response-times when using pipes. */ if ( buffer_size == 0 ){ buffer_size = BUFSIZ; /* BUFSIZ is set to optimal size for this platform */ } /* Initialize the structure */ if ( (ret = stream_alloc(stptr, PROTOCOL_LOCAL_FILE, sizeof(struct stream_file), buffer_size, BUFSIZ) != 0) ){ return ret; } struct stream_file* st = (struct stream_file*)*stptr; struct file_header_t* fhptr = &(st->base.FH); int i; st->base.num_addresses = 1; st->file = fp; st->force_flush = 0; /* load stream file header */ size_t bytes = fread(fhptr, 1, sizeof(struct file_header_t), st->file); if ( bytes < sizeof(struct file_header_t) ){ /* even if this struct is larger */ return ERROR_CAPFILE_INVALID; /* than legacy, the file would be */ /* to small to be anything useful anyway. */ } if ( fhptr->magic != CAPUTILS_FILE_MAGIC ){ /* try loading legacy headers */ struct file_header_05 fhleg05; struct file_header_06 fhleg06; if ( load_legacy_05(&fhleg05, st->file) ){ fhptr->comment_size = fhleg05.comment_size; fhptr->version.major = 0; fhptr->version.minor = 5; fhptr->header_offset = sizeof(struct file_header_05); memcpy(fhptr->mpid, fhleg05.mpid, 200); } else if ( load_legacy_06(&fhleg06, st->file) ){ fhptr->comment_size = fhleg06.comment_size; fhptr->version.major = 0; fhptr->version.minor = 6; fhptr->header_offset = sizeof(struct file_header_06); memcpy(fhptr->mpid, fhleg06.mpid, 200); } else { return ERROR_CAPFILE_INVALID; } } /* read extension headers */ const int have_extensions = fhptr->header_offset > 216; if ( have_extensions ){ do { struct file_extension ext; if ( fread(&ext, sizeof(struct file_extension), 1, st->file) != 1 ){ return ERROR_CAPFILE_TRUNCATED; } if ( ext.type == HEADER_EXT_NONE ){ /* last extension header */ break; } switch ( ext.type ){ case HEADER_EXT_PADDING: /* padding only, just skip bytes */ break; default: /* unrecognized extension header, ignored */ break; } /* test for invalid offset size (possibly malformed files) */ const size_t min_size = sizeof(struct file_extension); const size_t max_size = fhptr->header_offset; if ( ext.next_offset < min_size || ext.next_offset > max_size ){ return ERROR_CAPFILE_INVALID; } /* move to next */ fseek(st->file, ext.next_offset - sizeof(struct file_extension), SEEK_CUR); } while (1); } fseek(st->file, fhptr->header_offset, SEEK_SET); /* read comment */ st->base.comment = (char*)malloc(fhptr->comment_size+1); if ( (i = fread(st->base.comment, 1, fhptr->comment_size, st->file)) < fhptr->comment_size ){ /** @todo need to be able to set more detailed error */ return ERROR_CAPFILE_TRUNCATED; } st->base.comment[i] = 0; /* the null-terminator might not be included in file */ if ( !is_valid_version(fhptr) ){ /* is_valid_version has side-effects */ return EINVAL; } /* add callbacks */ st->base.fill_buffer = (fill_buffer_callback)stream_file_fillbuffer; st->base.destroy = (destroy_callback)stream_file_destroy; st->base.write = (write_callback)stream_file_write; st->base.flush = (flush_callback)stream_file_flush; return 0; }
void tms3615_device::device_start() { m_channel = stream_alloc(0, 2, clock()/8); m_samplerate = clock()/8; m_basefreq = clock(); }
static int proto_tftp_process(void *proto_priv, struct packet *p, struct proto_process_stack *stack, unsigned int stack_index) { struct proto_process_stack *s = &stack[stack_index]; struct proto_process_stack *s_prev = &stack[stack_index - 1]; struct proto_process_stack *s_next = &stack[stack_index + 1]; if (conntrack_get_unique_from_parent(stack, stack_index) != POM_OK) { pomlog(POMLOG_ERR "Could not get a conntrack entry"); return PROTO_ERR; } struct proto_tftp_conntrack_priv *priv = s->ce->priv; if (!priv) { priv = malloc(sizeof(struct proto_tftp_conntrack_priv)); if (!priv) { pom_oom(sizeof(struct proto_tftp_conntrack_priv)); conntrack_unlock(s->ce); return POM_ERR; } memset(priv, 0, sizeof(struct proto_tftp_conntrack_priv)); s->ce->priv = priv; } if (priv->flags & PROTO_TFTP_CONN_INVALID) { conntrack_unlock(s->ce); return PROTO_INVALID; } void *pload = s->pload; uint32_t plen = s->plen; // proto_tftp only process up to the opcode field // afterwards, it's up to the analyzer to parse the rest uint16_t opcode = ntohs(*((uint16_t*)pload)); PTYPE_UINT16_SETVAL(s->pkt_info->fields_value[proto_tftp_field_opcode], opcode); pload += sizeof(uint16_t); plen -= sizeof(uint16_t); s_next->pload = pload; s_next->plen = plen; switch (opcode) { case tftp_rrq: case tftp_wrq: { // Find the filename char *filename = pload; char *mode = memchr(filename, 0, plen - 1); if (!mode) { priv->flags |= PROTO_TFTP_CONN_INVALID; conntrack_unlock(s->ce); debug_tftp("End of filename not found in read/write request"); return PROTO_INVALID; } mode++; ssize_t filename_len = mode - filename; char *end = memchr(mode, 0, plen - filename_len); if (!end) { priv->flags |= PROTO_TFTP_CONN_INVALID; conntrack_unlock(s->ce); debug_tftp("End of mode not found in read/write request"); return PROTO_INVALID; } debug_tftp("Got read/write request for filename \"%s\" with mode \"%s\"", filename, mode); struct conntrack_session *session = conntrack_session_get(s->ce); if (!session) { conntrack_unlock(s->ce); return POM_ERR; } // We don't need to do anything with the session conntrack_session_unlock(session); struct proto_expectation *expt = proto_expectation_alloc_from_conntrack(s_prev->ce, proto_tftp, NULL); if (!expt) { conntrack_unlock(s->ce); return PROTO_ERR; } proto_expectation_set_field(expt, -1, NULL, POM_DIR_REV); if (proto_expectation_add(expt, session, PROTO_TFTP_EXPT_TIMER, p->ts) != POM_OK) { conntrack_unlock(s->ce); proto_expectation_cleanup(expt); return PROTO_ERR; } break; } case tftp_data: { if (plen < 2) { priv->flags |= PROTO_TFTP_CONN_INVALID; conntrack_unlock(s->ce); return PROTO_INVALID; } uint16_t block_id = ntohs(*((uint16_t*)(pload))); int set_start_seq = 0; if (!priv->stream) { priv->stream = stream_alloc(PROTO_TFTP_STREAM_BUFF, s->ce, 0, proto_tftp_process_payload); if (!priv->stream) { conntrack_unlock(s->ce); return PROTO_ERR; } stream_set_timeout(priv->stream, PROTO_TFTP_PKT_TIMER); set_start_seq = 1; } conntrack_unlock(s->ce); if (set_start_seq) stream_set_start_seq(priv->stream, s->direction, PROTO_TFTP_BLK_SIZE + 2); int res = stream_process_packet(priv->stream, p, stack, stack_index + 1, block_id * (PROTO_TFTP_BLK_SIZE + 2), 0); return (res == PROTO_OK ? PROTO_STOP : res); } case tftp_ack: // Nothing to do break; case tftp_error: // An error occured, cleanup this conntrack soon conntrack_delayed_cleanup(s->ce, 1, p->ts); break; default: priv->flags |= PROTO_TFTP_CONN_INVALID; conntrack_unlock(s->ce); return PROTO_INVALID; } conntrack_delayed_cleanup(s->ce, PROTO_TFTP_PKT_TIMER, p->ts); conntrack_unlock(s->ce); return PROTO_OK; }