Ejemplo n.º 1
0
int procauth_isauthoritative(int service_code, pid_t pid) {
    procpid *pp;

    list_iterator_start(&proclist);
    while (list_iterator_hasnext(&proclist)) {
        pp = (procpid *)list_iterator_next(&proclist);
        if (pp->service_code == service_code) {
            /* wanted service found, compare pids... */
            list_iterator_stop(&proclist);
            if (pp->current_pid == pid) /* authoritative */
                return 1;
            else {
                pp->current_pid = procauth_getprocpid(pp->filename);
                if (pp->current_pid == -1) {        /* error accessing pidfile */
                    return 0;
                } else {
                    if (pp->current_pid != pid) {
                        /* check if this is a child of the parent pid */
                        return procauth_ischildof(pid, pp->current_pid);
                    }
                    /* pid correctly updated and matching */
                    return 1;
                }
            }
        }
    }
    list_iterator_stop(&proclist);

    /* service_code was unknown */
    return 0;
}
Ejemplo n.º 2
0
void free_policy(bool runtime, policy_t* policy)
{
    if (runtime)
        return;
    if (policy->name != NULL)
        bdestroy(policy->name);
    if (policy->instructions != NULL)
    {
        list_iterator_start(policy->instructions);
        while (list_iterator_hasnext(policy->instructions))
            free_policy_instruction(runtime, list_iterator_next(policy->instructions));
        list_iterator_stop(policy->instructions);
        list_destroy(policy->instructions);
        free(policy->instructions);
    }
    if (policy->settings != NULL)
    {
        list_iterator_start(policy->settings);
        while (list_iterator_hasnext(policy->settings))
            free_policy_setting(runtime, list_iterator_next(policy->settings));
        list_iterator_stop(policy->settings);
        list_destroy(policy->settings);
        free(policy->settings);
    }
    free(policy);
}
Ejemplo n.º 3
0
/**
 * This function will search the list of cached pattern IDs for a pattern ID according to the given filename.
 * If it finds one, the found ID will be returned.
 * If not, -1 will be returned.
 */
int getPatternIDFromList(const char *filename) {
	int id = -1;
#ifdef DEBUG_LOGGING
	__android_log_write(ANDROID_LOG_INFO,"AR native","trying to retrieve pattern from list");
#endif
	if (patternIDs == NULL) {
#ifdef DEBUG_LOGGING
		__android_log_write(ANDROID_LOG_INFO,"AR native","list of patterns is null!!");
#endif	
		return -1;
	}
	list_iterator_start(patternIDs);
	while (list_iterator_hasnext(patternIDs)) {
		patternID * currPatt = (patternID *) list_iterator_next(patternIDs);
#ifdef DEBUG_LOGGING
		__android_log_print(ANDROID_LOG_INFO,"AR native","current pattern fiel: %s",currPatt->filename);
#endif
		if (strcmp(filename, currPatt->filename) == 0) {
#ifdef DEBUG_LOGGING
			__android_log_write(ANDROID_LOG_INFO,"AR native","found pattern in list");
#endif
			id = currPatt->id;
			break;
		}
	}
	list_iterator_stop(patternIDs);

#ifdef DEBUG_LOGGING
	if(id==-1)
	__android_log_print(ANDROID_LOG_INFO,"AR native","found no pattern in the list for file %s",filename);
#endif
	return id;
}
Ejemplo n.º 4
0
Archivo: ctx.c Proyecto: velter/OpenSC
static void load_reader_driver_options(sc_context_t *ctx)
{
	struct sc_reader_driver *driver = ctx->reader_driver;
	scconf_block *conf_block = NULL;
	sc_reader_t *reader;
	int max_send_size;
	int max_recv_size;

	conf_block = sc_get_conf_block(ctx, "reader_driver", driver->short_name, 1);

	if (conf_block != NULL) {
		max_send_size = scconf_get_int(conf_block, "max_send_size", -1);
		max_recv_size = scconf_get_int(conf_block, "max_recv_size", -1);
		if (max_send_size >= 0 || max_recv_size >= 0) {
			if (list_iterator_start(&ctx->readers)) {
				reader = list_iterator_next(&ctx->readers);
				while (reader) {
					if (max_send_size >= 0)
						reader->max_send_size = max_send_size;
					if (max_recv_size >= 0)
						reader->max_recv_size = max_recv_size;
					reader = list_iterator_next(&ctx->readers);
				}
				list_iterator_stop(&ctx->readers);
			}
		}
	}
}
Ejemplo n.º 5
0
struct lprov_entry* list_revert(list_t* list)
{
	struct lprov_entry* first = NULL;
	struct lprov_entry* previous = NULL;
	struct lprov_entry* current = NULL;
	struct lconv_entry* entry = NULL;

	if (list == NULL)
		return NULL;

	list_iterator_start(list);
	while (list_iterator_hasnext(list))
	{
		entry = list_iterator_next(list);
		current = malloc(sizeof(struct lprov_entry));
		current->address = entry->address;
		current->label = bstr2cstr(entry->label, '0');
		current->next = NULL;
		if (previous != NULL)
			previous->next = current;
		if (first == NULL)
			first = current;
		previous = current;
	}
	list_iterator_stop(list);

	return first;
}
Ejemplo n.º 6
0
int dbg_lua_handle_lines(lua_State* L)
{
    struct dbg_state* state = dbg_lua_extract_state(L, 1);
    void* ud = dbg_lua_extract_state_ud(L, 1);
    list_t* symbols = state->dbg_lua_get_symbols();
    struct dbg_sym* current = NULL;
    struct dbg_sym_payload_line* payload = NULL;
    int tbl;
    lua_newtable(L);
    if (symbols == NULL)
        return 1;
    tbl = lua_gettop(L);
    list_iterator_start(symbols);
    while (list_iterator_hasnext(symbols))
    {
        current = (struct dbg_sym*)list_iterator_next(symbols);
        if (current->type != DBGFMT_SYMBOL_LINE)
            continue;
        payload = (struct dbg_sym_payload_line*)current->payload;
        if (payload->path == NULL)
            continue;
        lua_newtable(L);
        lua_pushstring(L, payload->path->data);
        lua_setfield(L, -2, "file");
        lua_pushnumber(L, payload->lineno);
        lua_setfield(L, -2, "line");
        lua_pushnumber(L, payload->address);
        lua_setfield(L, -2, "address");
        lua_rawseti(L, tbl, lua_objlen(L, tbl) + 1);
    }
    list_iterator_stop(symbols);
    return 1;
}
Ejemplo n.º 7
0
void free_policy_value(bool runtime, policy_value_t* value)
{
    if ((runtime && !value->runtime) ||
        (!runtime && value->runtime))
        return;
    switch (value->type)
    {
        case NUMBER:
        case TABLE:
        case FIELD:
            break;
        case WORD:
        case VARIABLE:
            bdestroy(value->string);
            break;
        case FUNCTION:
            free_policy_function_call(runtime, value->function);
            break;
        case LIST:
            list_iterator_start(value->list);
            while (list_iterator_hasnext(value->list))
                free_policy_value(runtime, list_iterator_next(value->list));
            list_iterator_stop(value->list);
            list_destroy(value->list);
            free(value->list);
            break;
        default:
            break;
    }
    free(value);
}
Ejemplo n.º 8
0
///
/// Pops a scope from the stack.
///
void ppimpl_pop_scope(state_t* state)
{
    scope_t* scope;
    match_t* match;
    match_t* old;
    bstring name;
    size_t a, i;
    if (list_size(&state->scopes) == 0)
        return;
    scope = list_extract_at(&state->scopes, list_size(&state->scopes) - 1);

    // Delete text if this scope was silenced.
    if (!scope->active)
    {
        list_delete_range(&state->cached_output,
                scope->start_index,
                list_size(&state->cached_output) - 1);
    }

    // Delete handlers that were defined this scope.
    list_iterator_start(&scope->new_handlers);
    while (list_iterator_hasnext(&scope->new_handlers))
    {
        name = list_iterator_next(&scope->new_handlers);

        for (i = 0; i < list_size(&state->handlers); i++)
        {
            old = list_get_at(&state->handlers, i);
            if (biseq(name, old->text.ref))
            {
                // We need remove the old handler.
                // FIXME: Free the old handler.
                list_delete_at(&state->handlers, i);
                i--;
            }
        }

        bdestroy(name);
    }
    list_iterator_stop(&scope->new_handlers);
    list_destroy(&scope->new_handlers);

    // Restore handlers.
    for (a = 0; a < list_size(&scope->old_handlers); a++)
    {
        match = list_get_at(&scope->old_handlers, a);

        // Restore the old handler.
        list_append(&state->handlers, match);
        list_extract_at(&scope->old_handlers, a);
        a--;
    }
    list_destroy(&scope->old_handlers);

    // Free memory.
    free(scope);
}
Ejemplo n.º 9
0
void free_policy_state(bool runtime, policy_state_t* state)
{
    if (runtime)
        return;
    list_iterator_start(&state->variables);
    while (list_iterator_hasnext(&state->variables))
        free_policy_instruction(runtime, list_iterator_next(&state->variables));
    list_iterator_stop(&state->variables);
    list_destroy(&state->variables);
    free(state);
}
Ejemplo n.º 10
0
void wr_rtp_packet_destroy(wr_rtp_packet_t * rtp_packet)
{
    list_iterator_start(&rtp_packet->data_frames);
    while(list_iterator_hasnext(&rtp_packet->data_frames)){
        wr_data_frame_t * frame = (wr_data_frame_t * ) list_iterator_next(&rtp_packet->data_frames);
        free(frame->data);
        free(frame);
    }
    list_iterator_stop(&rtp_packet->data_frames);
    list_destroy(&rtp_packet->data_frames);
}
Ejemplo n.º 11
0
int wr_rtp_packet_copy_with_data(wr_rtp_packet_t * new_packet, wr_rtp_packet_t * old_packet)
{
    wr_rtp_packet_copy(new_packet, old_packet);
    list_init(&new_packet->data_frames);
    list_iterator_start(&old_packet->data_frames);
    while(list_iterator_hasnext(&old_packet->data_frames)){
        wr_data_frame_t * frame = (wr_data_frame_t * ) list_iterator_next(&old_packet->data_frames);
        wr_rtp_packet_add_frame(new_packet, frame->data, frame->size, frame->length_in_ms);
    }
    list_iterator_stop(&old_packet->data_frames);
    return 0;
}
Ejemplo n.º 12
0
list_t* list_clone(list_t* original)
{
	list_t* list = malloc(sizeof(list_t));
	list_init(list);
	list_attributes_copy(list, &lconv_entry_meter, 1);
	list_attributes_comparator(list, &lconv_entry_comparator);
	list_attributes_seeker(list, &lconv_entry_seeker);
	list_iterator_start(original);
	while (list_iterator_hasnext(original))
		list_append(list, list_iterator_next(original));
	list_iterator_stop(original);
	return list;
}
Ejemplo n.º 13
0
void free_policies(bool runtime, policies_t* policies)
{
    if (runtime)
        return;
    list_iterator_start(&policies->policies);
    while (list_iterator_hasnext(&policies->policies))
        free_policy(runtime, list_iterator_next(&policies->policies));
    list_iterator_stop(&policies->policies);
    list_destroy(&policies->policies);
    if (policies->settings != NULL)
        free_policy(runtime, policies->settings);
    free(policies);
}
Ejemplo n.º 14
0
void handle_LIST(char **params, int socket){
  printf("at the top of handle_list, plenty of time to fill the printf buffer and send a message\n");
  char msgbuf[512];
  char* nick;
  char *chanName = params[1];
  channel* chan;
  
  struct ClientData * ourCli = (struct ClientData *)list_seek(&clientList, &socket);
  nick = ourCli->nick;

  //printf("Ahfksadkfhkwelkheiowhrihfksadnfnkahseihiofhiohdsfaskdnfkneifhihdisjfkalsdklfjksdjfij\n");
  //printf("chan null is :%d\n", chanName == NULL);
  //printf("shfksadkfhkwelkheiowhrihfksadnfnkahseihiofhiohdsfaskdnfkneifhihdisjfkalsdklfjksdjfij\n");
  if(chanName != NULL){
    chan = (channel *) findChannel(chanName);

    //if channel doesn't exist
    if(chan == NULL){
      return;
    }

    sprintf(msgbuf, ":%s 322 %s %s %d :%s\r\n", host, nick, chanName, chan->nusers, chan->topic); //add stuff
    sendMessage(msgbuf, socket);
    sprintf(msgbuf, ":%s 323 %s :End of LIST\r\n", host, nick);
    sendMessage(msgbuf, socket);
    
  } else {
  
    pthread_mutex_lock(&chanListLock);
    if(list_iterator_start(&channelList) == 0){
      printf("could not iterate list\n");
      return;
    }
    printf("starting channel iterator\n");
    while(list_iterator_hasnext(&channelList)){
      chan = (channel *) list_iterator_next(&channelList);
      printf("sending channel info\n");
      sprintf(msgbuf, ":%s 322 %s %s %d :%s\r\n", host, nick, chan->name, chan->nusers, chan->topic); //add stuff
      sendMessage(msgbuf, socket);  
    }

    list_iterator_stop(&channelList);
    pthread_mutex_unlock(&chanListLock);

    sprintf(msgbuf, ":%s 323 %s :End of LIST\r\n", host, nick);
    sendMessage(msgbuf, socket);
  }
  
  
  return;
}
Ejemplo n.º 15
0
/* Function to return a list of users connected/in a channel */
void handle_LUSERS(char** params, int socket)
{
  char msgbuf[512];
  int numUsers = 0;
  int numUnregUsers = 0;
  
  pthread_mutex_lock(&listLock);
  struct ClientData *cur;
  struct ClientData *cli;
  char user[500];

  //iterate through the list of users, get the number that are registered and unregistered
  if(list_iterator_start(&clientList) ==0) 
   {
      printf("Error iterating the list.\n");
      return;
   }
   while(list_iterator_hasnext(&clientList))
   {
     cur = (struct ClientData *)list_iterator_next(&clientList);
     if(cur->registered){
       numUsers++;
     } else {
       numUnregUsers++;
     }
   }
   if(list_iterator_stop(&clientList) == 0)
   {
      printf("Error iterating the list.\n");
      return;
   }
   pthread_mutex_unlock(&listLock);

   //get the client we are sending this to
   cli = (struct ClientData *)list_seek(&clientList, &socket);
   strcpy(user, cli->user);

   //send the message
   sprintf(msgbuf, ":%s 251 %s :There are %d users and 0 services on 1 servers\r\n", host, user, numUsers);
  sendMessage(msgbuf, socket);   
  sprintf(msgbuf,":%s 252 %s 0 :operator(s) online\r\n", host, user);
  sendMessage(msgbuf, socket);
  sprintf(msgbuf, ":%s 253 %s %d :unknown connection(s)\r\n",host, user, numUnregUsers);
  sendMessage(msgbuf, socket);
  sprintf(msgbuf,":%s 254 %s 0 :channels formed\r\n",host, user);
  sendMessage(msgbuf, socket);
  sprintf(msgbuf,":%s 255 %s :I have %d clients and 1 servers\r\n",host, user, numUsers+numUnregUsers);
  sendMessage(msgbuf, socket);
  return;
}
Ejemplo n.º 16
0
void procauth_fin() {
    procpid *pp;

    /* free filenames */
    list_iterator_start(&proclist);
    while (list_iterator_hasnext(&proclist)) {
        pp = (procpid *)list_iterator_next(&proclist);
        free(pp->filename);
    }
    list_iterator_stop(&proclist);

    /* destroy the list itself */
    list_destroy(&proclist);
}
Ejemplo n.º 17
0
void free_policy_function_call(bool runtime, policy_function_call_t* call)
{
    if (runtime)
        return;
    if (call->parameters != NULL)
    {
        list_iterator_start(call->parameters);
        while (list_iterator_hasnext(call->parameters))
            free_policy_value(runtime, list_iterator_next(call->parameters));
        list_iterator_stop(call->parameters);
        list_destroy(call->parameters);
        free(call->parameters);
    }
    free(call);
}
Ejemplo n.º 18
0
void* get_thread(int id, bzz_t *lock) {
	bzz_thread_t *found = 0;

	list_t *threads = lock->threads;
	list_iterator_start(threads);

	while(list_iterator_hasnext(threads)) {
		bzz_thread_t *current = list_iterator_next(threads);
		if(current->id == id) {
			found = current;
		}
	}
	list_iterator_stop(threads);

	return found;
}
Ejemplo n.º 19
0
unsigned int num_old_gold_waiting(bzz_t *lock) {
	unsigned int num_old_gold_waiting = 0;
	list_t *waiting_gold_threads = lock->waiting_gold_threads;

	list_iterator_start(waiting_gold_threads);
	// Iterate through waiting gold threads checking for old threads
	while(list_iterator_hasnext(waiting_gold_threads)) {
		bzz_thread_t *current = list_iterator_next(waiting_gold_threads);
		if(is_old(current, lock)) {
			num_old_gold_waiting++;
		}
	}
	list_iterator_stop(waiting_gold_threads);

	return num_old_gold_waiting;
}
Ejemplo n.º 20
0
int procauth_refreshpids() {
    procpid *pp;
    pid_t newpid;
    int changed = 0;

    /* update each process in list with the current pid */
    list_iterator_start(&proclist);
    while (list_iterator_hasnext(&proclist)) {
        pp = (procpid *)list_iterator_next(&proclist);
        newpid  = procauth_getprocpid(pp->filename);
        if (newpid != pp->current_pid) changed++;
        pp->current_pid = newpid;
    }
    list_iterator_stop(&proclist);
    sshguard_log(LOG_DEBUG,"refreshing the list of pids from pidfiles... %d pids changed", changed);

    return changed;
}
Ejemplo n.º 21
0
//TODO release globalref
JNIEXPORT void JNICALL Java_edu_dhbw_andar_ARToolkit_removeObject
(JNIEnv *env, jobject artoolkit, jint objectID) {
    if(list_delete(&objects,&objectID) != 0) {
        //failed to delete -> throw error
        jclass exc = (*env)->FindClass( env, "edu/dhbw/andar/exceptions/AndARException" );
        if ( exc != NULL )
            (*env)->ThrowNew( env, exc, "could not delete object from native array" );
    }
#ifdef DEBUG_LOGGING
    __android_log_write(ANDROID_LOG_INFO,"AR native","array of objects still containing the following elements:");
    list_iterator_start(&objects);        /* starting an iteration "session" */
    while (list_iterator_hasnext(&objects)) { /* tell whether more values available */
        Object* curObject = (Object *)list_iterator_next(&objects);     /* get the next value */
        __android_log_print(ANDROID_LOG_INFO,"AR native","name: %s", curObject->name);
        __android_log_print(ANDROID_LOG_INFO,"AR native","id: %s", curObject->id);
    }
    list_iterator_stop(&objects);
#endif
}
Ejemplo n.º 22
0
/**
 * Sends an asynchronous event to any waiting client
 */
LONG EHSignalEventToClients(void)
{
	LONG rv = SCARD_S_SUCCESS;
	int32_t filedes;

	(void)pthread_mutex_lock(&ClientsWaitingForEvent_lock);

	(void)list_iterator_start(&ClientsWaitingForEvent);
	while (list_iterator_hasnext(&ClientsWaitingForEvent))
	{
        filedes = *(int32_t *)list_iterator_next(&ClientsWaitingForEvent);
		rv = MSGSignalClient(filedes, SCARD_S_SUCCESS);
	}
	(void)list_iterator_stop(&ClientsWaitingForEvent);

	(void)list_clear(&ClientsWaitingForEvent);

	(void)pthread_mutex_unlock(&ClientsWaitingForEvent_lock);

	return rv;
} /* EHSignalEventToClients */
Ejemplo n.º 23
0
///
/// Saves the bin with the specified name to the specified path.
///
/// @param name The name of the bin to save.
/// @param path The path to save the bin to.
///
void bins_save(freed_bstring name, freed_bstring path)
{
	FILE* out;
	struct ldbin* bin = list_seek(&ldbins.bins, name.ref);
	assert(bin != NULL);

	// Open the output file.
	out = fopen(path.ref->data, "wb");

	// Write each byte from the bin.
	list_iterator_start(&bin->words);
	while (list_iterator_hasnext(&bin->words))
		fwrite(list_iterator_next(&bin->words), sizeof(uint16_t), 1, out);
	list_iterator_stop(&bin->words);

	// Close the output file.
	fclose(out);

	// Free strings.
	bautodestroy(name);
	bautodestroy(path);
}
Ejemplo n.º 24
0
void list_prov_destroy(list_t* list)
{
	struct lconv_entry* entry = NULL;
	
	if (list == NULL)
		return;
	
	list_iterator_start(list);
	while (list_iterator_hasnext(list))
	{
		entry = list_iterator_next(list);
		bdestroy(entry->label);
		
		// No need to free entries as list_attributes_copy
		// has the copy_data option set to 1.  We also don't
		// free the bin name, as we don't own that.
	}
	list_iterator_stop(list);
	list_clear(list);
	list_destroy(list);
	free(list);
}
Ejemplo n.º 25
0
void free_policy_instruction(bool runtime, policy_instruction_t* instruction)
{
    if (runtime)
        return;
    if (instruction->value != NULL)
        free_policy_value(runtime, instruction->value);
    if (instruction->for_variable != NULL)
        bdestroy(instruction->for_variable);
    if (instruction->for_start != NULL)
        free_policy_value(runtime, instruction->for_start);
    if (instruction->for_end != NULL)
        free_policy_value(runtime, instruction->for_end);
    if (instruction->for_instructions != NULL)
    {
        list_iterator_start(instruction->for_instructions);
        while (list_iterator_hasnext(instruction->for_instructions))
            free_policy_instruction(runtime, list_iterator_next(instruction->for_instructions));
        list_iterator_stop(instruction->for_instructions);
        list_destroy(instruction->for_instructions);
        free(instruction->for_instructions);
    }
    free(instruction);
}
Ejemplo n.º 26
0
void dbgfmt_free(list_t* symbols)
{
    struct dbg_sym* symbol;

    if (symbols == NULL)
        return;

    list_iterator_start(symbols);
    while (list_iterator_hasnext(symbols))
    {
        symbol = list_iterator_next(symbols);

        // Free payload.
        dbgfmt_free_symbol(symbol);

        // No need to free the symbol, it will be
        // free'd by simclist when we clear / destroy
        // the list.
    }
    list_iterator_stop(symbols);
    list_clear(symbols);
    list_destroy(symbols);
    free(symbols);
}
Ejemplo n.º 27
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Write all new alarms to the diskloop.
 */
void alarm_archive( alarm_context_t* alarm_list, buffer_t* url_str,
                    st_info_t* st_info )
{
    time_t   start_time = 0;
    time_t   end_time   = 0;
    uint8_t  buf_byte   = 0;
    uint16_t buf_word   = 0;
    uint32_t buf_dword  = 0L;
    //uint64_t buf_qword  = 0LL;

    uint16_t  version_type = 0x8000 | FALCON_VERSION;
    uint16_t  alarm_count  = 0;
    char*     retmsg     = NULL;
    buffer_t* alarm_data = NULL;
    alarm_line_t* alarm  = NULL;

    alarm_data = buffer_init();

    if (!alarm_data) {
        if (gDebug)
            fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
        else
            syslog(LOG_ERR, "falcon: unable to allocate space for alarm data\n");
        return;
    }

    // Print each line in the filtered list
    list_iterator_stop(alarm_list);
    list_iterator_start(alarm_list);
    while (list_iterator_hasnext(alarm_list)) 
    {
        alarm = (alarm_line_t*)list_iterator_next(alarm_list);
        if (alarm->sent)
            continue;

        if (!start_time)
          start_time = alarm->timestamp;
        if (!end_time)
          end_time = alarm->timestamp;

        if (gDebug)
            fprintf(stdout, "DEBUG %s, line %d, date %s: %s\n", 
                    __FILE__, __LINE__, __DATE__, alarm->text);

        if ((retmsg = q330LogMsg(alarm->text, st_info->station,
                                 st_info->network, "LOG",
                                 st_info->location)) != NULL)
        { // error trying to log the message
            if (gDebug)
                fprintf(stderr, "falcon: failed to write alarms to log: %s\n", retmsg);
            else
                syslog(LOG_ERR, "falcon: failed to write alarms to log: %s\n", retmsg);
            exit(1);
        }

        if (gDebug)
        {
            fprintf(stdout, "Alarm '%s':\n", alarm->description);
            fprintf(stdout, "    Channel    : %02x\n", (alarm->channel));
            fprintf(stdout, "    Timestamp  : %li\n", (long)(alarm->timestamp));
            fprintf(stdout, "    Event Code : 0x%02x\n", (alarm->event));
            fprintf(stdout, "    Sent       : %s\n", alarm->sent ? "Yes" : "No");
            fprintf(stdout, "    Hash       : 0x%08lx\n", (unsigned long)(alarm->hash));
            fprintf(stdout, "    Text       : %s\n", alarm->text);
        }

        if (start_time > alarm->timestamp)
            start_time = alarm->timestamp;
        if (end_time < alarm->timestamp)
            end_time = alarm->timestamp;

        if (!alarm->description[0])
        {
            if (gDebug)
                fprintf(stderr, "falcon: description code not found\n");
            else
                syslog(LOG_ERR, "falcon: description code not found\n");
            continue;
        }

        // There must be at least enough space for one more alarm.
        // If there isn't, queue this buffer's contents, and reset it.
        // This should prevent us from ever fragmenting alarm data
        // across opaque blockettes.
        if (alarm_count && (alarm_data->length > 400))
        {
            buffer_seek(alarm_data, 2);
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));

            if (gDebug) {
                fprintf(stdout, "falcon: [MID] alarms were found\n");
                fprintf(stdout, "[MID] raw buffer:\n");
                format_data(alarm_data->content, alarm_data->length, 0, 0);
            }

            QueueOpaque(alarm_data->content, (int)alarm_data->length,
                        st_info->station, st_info->network,
                        st_info->alarm_chan, st_info->location,
                        FALCON_IDSTRING);
            alarm_data  = buffer_destroy(alarm_data);
            alarm_count = 0;
            start_time  = alarm->timestamp;
            end_time    = alarm->timestamp;
            alarm_data  = buffer_init();
            if (!alarm_data) {
                if (gDebug)
                    fprintf(stderr, "falcon: unable to allocate space for alarm data\n");
                else
                    syslog(LOG_ERR, "falcon: unable to allocate space for alarm data\n");
                return;
            }
        } // If we've built up a records worth of alarm data

        if (!alarm_count)
        {
            // Write alarm header info
            if (gDebug)
            {
                fprintf(stderr, "falcon: Writing alarm header info.\n");
            }
            buf_word = htons(version_type);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
            // Reserve space for elements that will be assigned just 
            // prior to queueing data
            buf_dword = htonl(start_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_dword = htonl(end_time);
            buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
            buf_word = htons(alarm_count);
            buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        }

        // Add an alarm
        buf_word = htons(alarm->channel);
        buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));
        buf_dword = htonl(alarm->timestamp);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buffer_write(alarm_data, &(alarm->event), sizeof(alarm->event));
        buf_byte = (uint8_t)strlen(alarm->description);
        buffer_write(alarm_data, &buf_byte, sizeof(buf_byte));
        buffer_write(alarm_data, (uint8_t*)(alarm->description), (size_t)buf_byte);
        buffer_terminate(alarm_data);

        alarm_count++;
        alarm->sent = 1;
    }
    list_iterator_stop(alarm_list);

    if (alarm_count && alarm_data && alarm_data->content && alarm_data->length)
    {
        buffer_seek(alarm_data, 2);
        buf_dword = htonl(start_time);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buf_dword = htonl(end_time);
        buffer_write(alarm_data, (uint8_t*)(&buf_dword), sizeof(buf_dword));
        buf_word = htons(alarm_count);
        buffer_write(alarm_data, (uint8_t*)(&buf_word), sizeof(buf_word));

        if (gDebug) {
            fprintf(stderr, "falcon: [END] alarms were found\n");
            fprintf(stderr, "[END] raw buffer:\n");
            format_data(alarm_data->content, alarm_data->length, 0, 0);
        }

        QueueOpaque(alarm_data->content, (int)alarm_data->length,
                    st_info->station, st_info->network,
                    st_info->alarm_chan, st_info->location,
                    FALCON_IDSTRING);
    }
    alarm_data = buffer_destroy(alarm_data);

    // Make sure we limit the accumulation of alarm messages
    while (list_size(alarm_list) > MAX_CONTEXT_ALARMS)
    {
        alarm = list_fetch(alarm_list);
        alarm = alarm_line_destroy(alarm);
    }
}
Ejemplo n.º 28
0
wr_errorcode_t wr_wavfile_filter_start(wr_rtp_filter_t * filter)
{

    SNDFILE * file;
    SF_INFO file_info;
    wr_encoder_t * codec;
    wr_rtp_packet_t rtp_packet;
    int sequence_number = 0;
    int rtp_timestamp = 0;
    struct timeval packet_start_timestamp;
    struct timeval packet_end_timestamp;

    gettimeofday(&packet_start_timestamp, NULL);
    timeval_copy(&packet_end_timestamp, &packet_start_timestamp);

    int rtp_in_frame = iniparser_getpositiveint(wr_options.output_options, "global:rtp_in_frame", 1);
    int frames_count = 0;

    /* open WAV file */
    file = sf_open(wr_options.filename, SFM_READ, &file_info);
    if (!file){
        wr_set_error("cannot open or render sound file");
        return WR_FATAL;
    }
    if (file_info.samplerate != 8000){
        wr_set_error("this tool works only with .wav files in 8kHz, rerecord your signal or resample it (with sox for example)\n");
        return WR_FATAL; 
    }

    list_iterator_start(wr_options.codec_list);
    if (list_iterator_hasnext(wr_options.codec_list)){
        codec = (wr_encoder_t*)list_iterator_next(wr_options.codec_list); 
        wr_rtp_packet_init(&rtp_packet, codec->payload_type, sequence_number, 1, rtp_timestamp, packet_start_timestamp);
    }else{
        wr_set_error("no codec is found");
        return WR_FATAL;
    }

    wr_rtp_filter_notify_observers(filter, TRANSMISSION_START, NULL);

    /* One cycle iteration encode one data frame */
    while(codec){
        int   input_buffer_size = (*codec->get_input_buffer_size)(codec->state);
        int   output_buffer_size = (*codec->get_output_buffer_size)(codec->state); 
        short input_buffer[input_buffer_size];
        char output_buffer[output_buffer_size];

        bzero(input_buffer, input_buffer_size);
        bzero(output_buffer, output_buffer_size);

        input_buffer_size = sf_read_short(file, input_buffer, input_buffer_size);
        if (!input_buffer_size){ /*EOF*/
            if (frames_count){
                wr_rtp_filter_notify_observers(filter, NEW_PACKET, &rtp_packet);
                wr_rtp_packet_destroy(&rtp_packet);
                frames_count = 0;
                sequence_number++;
                timeval_copy(&packet_start_timestamp, &packet_end_timestamp);
                wr_rtp_packet_init(&rtp_packet, codec->payload_type, sequence_number, 0, rtp_timestamp, packet_start_timestamp);
            }
            sf_seek(file, 0, SEEK_SET);
            if (list_iterator_hasnext(wr_options.codec_list)){
                codec = (wr_encoder_t*)list_iterator_next(wr_options.codec_list);
            }else{
                codec = NULL;
                list_iterator_stop(wr_options.codec_list);
            }
            continue;
        }
        output_buffer_size = (*codec->encode)(codec->state, input_buffer, output_buffer);
        wr_rtp_packet_add_frame(&rtp_packet, output_buffer, output_buffer_size,  1000 * input_buffer_size / file_info.samplerate);
        timeval_increment(&packet_end_timestamp, 1e6 * input_buffer_size / file_info.samplerate);
        rtp_timestamp += input_buffer_size;
        frames_count++;
        if (frames_count == rtp_in_frame){
            wr_rtp_filter_notify_observers(filter, NEW_PACKET, &rtp_packet);
            wr_rtp_packet_destroy(&rtp_packet);
            frames_count = 0;
            sequence_number++;
            timeval_copy(&packet_start_timestamp, &packet_end_timestamp);
            wr_rtp_packet_init(&rtp_packet, codec->payload_type, sequence_number, 0, rtp_timestamp, packet_start_timestamp);
        }
    }   
    wr_rtp_filter_notify_observers(filter, TRANSMISSION_END, NULL);
    sf_close(file);
}
Ejemplo n.º 29
0
/* control_loop() is the main STCP loop; it repeatedly waits for one of the
 * following to happen:
 *   - incoming data from the peer
 *   - new data from the application (via mywrite())
 *   - the socket to be closed (via myclose())
 *   - a timeout
 */
static void control_loop(mysocket_t sd, context_t *ctx)
{
    assert(ctx);
    int tcplen;
    char payload[MAXLEN];
    STCPHeader *in_header;
    void *packtosend;
    char *indicpt;
    char tempbuff[WINLEN];
    stcp_event_type_t eventflag;

    while (!ctx->done)
    {
        unsigned int event;

        eventflag = ANY_EVENT;

        /* set timeout if there is any unack'd data; if not, just make it NULL */
        struct timespec timestart;
        struct timespec *timeout;


        if (ctx->send_unack < ctx->send_next) {
            timestart = ((packet_t *)list_get_at(ctx->unackd_packets, 0))->start_time;
            timeout = &timestart;

            /* constructs the timeout with absolute time by adding the RTO */
            timeout->tv_sec += ctx->rto.tv_sec;
            timeout->tv_nsec += ctx->rto.tv_nsec;

            /* ensures no nanosecond overflow */
            if (timeout->tv_nsec >= 1000000000) {
                timeout->tv_sec  += timeout->tv_nsec / 1000000000;
                timeout->tv_nsec = timeout->tv_nsec % 1000000000;
            }
            if (timeout->tv_sec <= time(NULL))
                /*earliest unacked packet has timed out, unless it's currently sitting in buffer */
                eventflag = NETWORK_DATA|TIMEOUT;
        }
        else {
            timeout = NULL;
            DEBUG("No timeout set\n");
        }



        /* see stcp_api.h or stcp_api.c for details of this function */
        /* XXX: you will need to change some of these arguments! */
        event = stcp_wait_for_event(sd, eventflag, timeout);

        /* check whether it was the network, app, or a close request */
        if (event & APP_DATA)
        {
            DEBUG("Application Data\n");
            /* the application has requested that data be sent */
            /* we should send as long as send_wind > 0 */
            int open_window = ctx->send_wind - (ctx->send_next - ctx->send_unack);
            if(open_window > 0)
            {
                int send_size;

                /* only read in as much from app as we can send */
                if(open_window > MAXLEN) send_size = MAXLEN;
                else send_size = open_window;

                void* buffer = (char *) calloc(1, send_size);

                tcplen = stcp_app_recv(sd, buffer, send_size);

                if( tcplen > 0 ) {
                    DEBUG("Application data size: %d\n", tcplen);
                    /*create and send packet*/
                    packtosend = (void *)make_stcp_packet(TH_ACK, ctx->send_next, ctx->recv_next, tcplen);
                    memcpy(packtosend + sizeof(STCPHeader), buffer, tcplen);
                    stcp_network_send(sd, packtosend, sizeof(STCPHeader) + tcplen, NULL);
                    DEBUG("Packet of payload size %d, ack number %d, seq number %d sent to network\n", tcplen, ctx->recv_next, ctx->send_next);
                    packet_t_create(ctx, packtosend, sizeof(STCPHeader) + tcplen); /* now a packet has been pushed onto the unacked queue */
                    /* update window length and send_next */
                    ctx->send_next += tcplen;
                }
                free(buffer);
                buffer = NULL;
                free(packtosend);
                packtosend = NULL;
            }
            else DEBUG("Could not get application data\n");
        }

        if (event & NETWORK_DATA)
        {
            DEBUG("Network Data\n");
            in_header = malloc(sizeof(STCPHeader));

            /* network data transmission */
            int data_size = recv_packet(sd, ctx, payload, MAXLEN, in_header);

            DEBUG("Received net data size is %d\n", data_size);


            /* send ACK as long as it's not past our window, and actually contained data */
            if(data_size > 0 || data_size == -2) { /* -2 indicates it received old data, which we should not send to the application */
                packtosend = (void *)make_stcp_packet(TH_ACK, ctx->send_next, ctx->recv_next, 0);
                stcp_network_send(sd, packtosend, sizeof(STCPHeader), NULL);
                free(packtosend);
                packtosend = NULL;
            }



            /* send payload to application, if it's valid */
            if(data_size > 0) {
                DEBUG("Sent data of size %d to application\n", data_size);
                stcp_app_send(sd, ctx->recv_buffer, data_size);


                /* slide the window over */
                indicpt = strchr(ctx->recv_indicator, '\0');
                data_size = indicpt - ctx->recv_indicator;
                memcpy(tempbuff, ctx->recv_buffer + data_size, WINLEN - data_size);
                memset(ctx->recv_buffer, '\0', WINLEN);
                memcpy(ctx->recv_buffer, tempbuff, WINLEN - data_size);

                /* slide window indicator over */
                memcpy(tempbuff, indicpt, WINLEN - data_size);
                memset(ctx->recv_indicator, '\0', WINLEN);
                memcpy(ctx->recv_indicator, tempbuff, WINLEN - data_size);
            }

            /* deal with connection teardown, if need be */
            if (in_header->th_flags & TH_ACK) {
                /* go from FIN-WAIT1 --> FIN-WAIT2 */
                if(ctx->connection_state == CSTATE_FIN_WAIT1) {
                    DEBUG("State: FIN-WAIT2\n");
                    ctx->connection_state = CSTATE_FIN_WAIT2;
                }
                /* go from LAST-ACK --> CLOSED */
                if(ctx->connection_state == CSTATE_LAST_ACK) {
                    DEBUG("State: CLOSED\n");
                    ctx->connection_state = CSTATE_CLOSED;
                    free(in_header);
                    in_header = NULL;
                    break;
                }
                /* go from CLOSING --> CLOSED */
                if(ctx->connection_state == CSTATE_CLOSING) {
                    DEBUG("State: CLOSED\n");
                    ctx->connection_state = CSTATE_CLOSED;
                    free(in_header);
                    in_header = NULL;
                    break;
                }
            }
            /* branching for the receipt of a FIN packet */
            if (in_header->th_flags & TH_FIN) {
                DEBUG("Received FIN packet\n");
                /* Acknowledge FIN, which counts as a byte */
                ctx->recv_next++;
                packtosend = (void *)make_stcp_packet(TH_ACK, ctx->send_next, ctx->recv_next, 0);
                stcp_network_send(sd, packtosend, sizeof(STCPHeader), NULL);

                /* go into CLOSE-WAIT */
                if (ctx->connection_state == CSTATE_ESTABLISHED) {
                    DEBUG("State: CLOSE_WAIT\n");
                    /* inform app of FIN */
                    stcp_fin_received(sd);

                    ctx->connection_state = CSTATE_CLOSE_WAIT;
                }
                /* go from FIN-WAIT2 --> CLOSED */
                if (ctx->connection_state == CSTATE_FIN_WAIT2) {
                    DEBUG("State: CLOSED\n");
                    ctx->connection_state = CSTATE_CLOSED;
                    free(in_header);
                    in_header = NULL;
                    break;
                }
                /* go from FIN-WAIT1 --> CLOSING */
                if (ctx->connection_state == CSTATE_FIN_WAIT1) {
                    DEBUG("State: CLOSING\n");
                    /* inform app of FIN */
                    stcp_fin_received(sd);

                    ctx->connection_state = CSTATE_CLOSING;
                }
                free(in_header);
                in_header = NULL;
            }
        }

        if (event & APP_CLOSE_REQUESTED)
        {
            DEBUG("Application close requested\n");
            if(ctx->connection_state == CSTATE_ESTABLISHED)
            {
                /* need to send all outstanding data first */
                DEBUG("Sending FIN packet with seq %d, ack %d\n", ctx->send_next, ctx->recv_next);
                STCPHeader *header = make_stcp_packet(TH_FIN, ctx->send_next, ctx->recv_next, 0);
                stcp_network_send(sd, header, sizeof(STCPHeader), NULL);
                ctx->send_unack += 1;
                packet_t_create(ctx, header, sizeof(STCPHeader));
                free(header);
                header = NULL;
                /* go into FIN-WAIT1 */
                ctx->connection_state = CSTATE_FIN_WAIT1;
                DEBUG("State: FIN-WAIT1\n");
            }
            if(ctx->connection_state == CSTATE_CLOSE_WAIT)
            {
                DEBUG("Sending FIN packet with seq %d, ack %d\n", ctx->send_next, ctx->recv_next);
                STCPHeader *header = make_stcp_packet(TH_FIN, ctx->send_next, ctx->recv_next, 0);
                stcp_network_send(sd, header, sizeof(STCPHeader), NULL);
                packet_t_create(ctx, header, sizeof(STCPHeader));
                ctx->send_next += 1;
                free(header);
                header = NULL;
                /* go from CLOSE-WAIT --> LAST-ACK */
                ctx->connection_state = CSTATE_LAST_ACK;
                DEBUG("State: LAST-ACK\n");
            }
        }
        if (event == TIMEOUT)
        {
            int giveup = 0;
            /* TIMEOUT--resend all packets in ctx->unackd_packets */
            packet_t *resendpack;
            list_iterator_start(ctx->unackd_packets);
            while (list_iterator_hasnext(ctx->unackd_packets)) {
                resendpack = (packet_t *)list_iterator_next(ctx->unackd_packets);
                if (resendpack->retry_count > 6) {
                    /*close the connection*/
                    DEBUG("Too many retries");
                    errno = ECONNABORTED;
                    giveup = 1;
                }
                /* increment retries */
                resendpack->retry_count++;
                clock_gettime(CLOCK_REALTIME, &(resendpack->start_time));
                ((STCPHeader *)(resendpack->packet))->th_ack = htonl(ctx->recv_next);
                stcp_network_send(sd, resendpack->packet, resendpack->packet_size, NULL);
                DEBUG("Resent packet with sequence number %d\n", ntohl(((STCPHeader *)(resendpack->packet))->th_seq));
            }
            list_iterator_stop(ctx->unackd_packets);
            if (giveup)
                break;
        }
    }
}
Ejemplo n.º 30
0
void ddbg_precycle_hook(vm_t* vm, uint16_t pos, void* ud)
{
    unsigned int i = 0;
    struct breakpoint* bk;
    uint16_t op, a, b;

    // Handle any symbols that are at this cycle.
    list_t* symbols = ddbg_get_symbols(vm->pc);
    list_iterator_start(symbols);
    while (list_iterator_hasnext(symbols))
        dbg_lua_handle_hook_symbol(&lstate, NULL, bautofree((bstring)list_iterator_next(symbols)));
    list_iterator_stop(symbols);
    list_empty(symbols);
    free(symbols);

    // Handle custom Lua commands.
    dbg_lua_handle_hook(&lstate, NULL, bautofree(bfromcstr("precycle")), pos);

    // Check to see if Lua halted the VM and return if it did.
    if (vm->halted)
        return;

    // Handle breakpoints.
    if (!ignore_next_breakpoint)
    {
        for (i = 0; i < list_size(&breakpoints); i++)
        {
            bk = (struct breakpoint*)list_get_at(&breakpoints, i);

            if (vm->pc == bk->addr)
            {
                vm->halted = true;
                ignore_next_breakpoint = true;
                vm_hook_break(vm); // Required for UI to update correctly.
                if (bk->temporary)
                    list_delete_at(&breakpoints, i--);
                if (!bk->silent)
                    ddbg_disassemble(max_int32((int32_t)vm->pc - 10, 0x0), min_int32((int32_t)vm->pc + 10, 0x10000) - vm->pc);
                printd(LEVEL_DEFAULT, "Breakpoint hit at 0x%04X.\n", bk->addr);
                return;
            }
        }
    }
    ignore_next_breakpoint = false;

    // Handle backtrace.
    op = INSTRUCTION_GET_OP(vm->ram[vm->pc]);
    a = INSTRUCTION_GET_A(vm->ram[vm->pc]);
    b = INSTRUCTION_GET_B(vm->ram[vm->pc]);
    if ((op == OP_SET && b == PC) || (op == OP_NONBASIC && b == NBOP_JSR))
    {
        // FIXME: This doesn't handle every valid value correctly..
        if (a == PUSH_POP)
            list_delete_at(&backtrace, list_size(&backtrace) - 1);
        else if (a == NXT_LIT)
        {
            printd(LEVEL_DEBUG, "jumping literally from 0x%04X to 0x%04X (0x%04X).\n", vm->pc, vm->ram[vm->pc + 1], vm->pc + 1);
            list_append(&backtrace, backtrace_entry_create(vm->pc, vm->ram[vm->pc + 1]));
        }
        else if (a == NXT)
        {
            //list_append(&backtrace, backtrace_entry_create(vm->pc, vm->ram[vm->ram[vm->pc+1]]));
        }
        else
        {
            // Unhandled.
            printd(LEVEL_DEBUG, "warning: unhandled backtrace jump occurred.\n");
        }
    }
}