Esempio n. 1
0
//Update ARP entry for the srcip
void updateARP(char *ip)
{
	ARP_table *entry,*new_entry;
	ip_mac *val;
	if (pthread_rwlock_rdlock(&arp_lock) != 0)
	{
		printf("Can't acquire read ARP lock.\n");
	}
	HASH_FIND_STR(arp_tbl,ip,entry);
	pthread_rwlock_unlock(&arp_lock);

	if(entry == NULL)
	{
		return;
	}
	else
	{
		val = entry->value;
		if(val->valid)
		{
			new_entry = (ARP_table*) malloc(sizeof(ARP_table));
			strcpy(new_entry->key,ip);
			val->timestamp = time(0);
			new_entry->value = val;
			if (pthread_rwlock_wrlock(&arp_lock) != 0)
			{
				printf("Can't acquire read ARP lock.\n");
			}
			HASH_REPLACE_STR(arp_tbl, key, entry, new_entry);
			pthread_rwlock_unlock(&arp_lock);
		}
	}
}
Esempio n. 2
0
int setuFunct( char *funct, char *def, char **args, int argc){
	Ufunct *function = malloc( sizeof(Ufunct));
	ALLOCCHECK( function);
	function->funct = funct;
	function->definition = def;
	function->args = args;
	function->argc = argc;

	Ufunct *temp;
	HASH_REPLACE_STR( userFuncts, funct, function, temp);

	free( temp);
	return 0;
}
Esempio n. 3
0
int setvar( char *var, mpfr_t value){
	Var *variable = malloc( sizeof(Var));
	ALLOCCHECK( variable);
	variable->var = var;
	mpfr_init( variable->value);
	mpfr_set( variable->value, value, MPFR_RNDN);

	Var *temp;
	HASH_REPLACE_STR( variables, var, variable, temp);

	if( temp != NULL)
		mpfr_clear(temp->value);
	free( temp);
	return 0;
}
Esempio n. 4
0
File: tokenizer.c Progetto: hatt/ir
/*
 Map tokens to logical document ID
 */
int tokenize(struct tokenlist **tokens, struct stoplist **stopwords, char *buffer, uint32_t id, int print, int stop) {
  char *word;

  // Separators. Don't split on single quotes.
  while ((word = strsep(&buffer, "\\,.?/:;=-()[]{}\'\" \n\t")) != NULL) {
    struct tokenlist *tok;
    struct token *doc;

    struct tokenlist *newtok;
    struct token *newdoc;

    if (strlen(word) > 0) {
      // this shouldn't be necessary, but for some reason
      // tags are sent to be tokenized by the parser
      if (word[0] == '<') {
        return 1;
      }

      // Convert to lowercase
      for (int i = 0; i < strlen(word); i++) {
        word[i] = tolower(word[i]);
      }

      // Prune stopwords
      if (stop) {
        if (stoplist_find(stopwords, word)) {
          continue;
        }
      }

      // Find our tokenized word, dump into tok table
      HASH_FIND_STR(*tokens, word, tok);
      /* If tmp table is empty, then word hasn't
       * been added to our token list yet. Initialise
       * a new token entry in our token list.
       */
      if (tok == NULL) {
      // Assign token and nested metadata
      // Add local hash table entry to global
        tok = malloc(sizeof(struct tokenlist));
        tok->document = NULL;
        tok->token = word;

        // Initialize nested hash table
        // Set nested hash table fields
        doc = malloc(sizeof(struct token));
        doc->id = id;
        doc->count = 1;

        HASH_ADD_INT(tok->document, id, doc);
        HASH_ADD_STR(*tokens, token, tok);
      } else {
        newtok = malloc(sizeof(struct tokenlist));
        newtok->document = NULL;
        newtok->token = tok->token;

        HASH_FIND_INT(tok->document, &id, doc);
        if (doc == NULL) {
          newdoc = malloc(sizeof(struct token));
          newdoc->id = id;
          newdoc->count = 1;
        } else {
          newdoc = malloc(sizeof(struct token));
          newdoc->id = doc->id;
          newdoc->count = doc->count + 1;
        }

        //printf("Token \"%s\" has count %u in ID %u\n", word, doc->count, doc->id);
        HASH_REPLACE_INT(newtok->document, id, newdoc, doc);
        HASH_REPLACE_STR(*tokens, token, newtok, tok);

        free(doc);
        free(tok);
      }

      if (print) {
        printf("%s\n", word);
      }
    }
  }

  return 0;
}
Esempio n. 5
0
void peer_join(unsigned int ip, short port, unsigned int room){
  struct peer *s;
  int r=0;
  int room_exists = 0;
  for(s=peers; s != NULL; s=(struct peer *)s->hh.next){
    if(s->room == room){
      r = r+1;
      if(r>=MAX_ROOM_SIZE){
        pthread_mutex_lock(&stdout_lock);
        fprintf(stderr, "Peer join failed - room full.\n");
        pthread_mutex_unlock(&stdout_lock);
        send_error(ip, port, 'j', 'f');
        return;
      }
      room_exists = 1;
    }
  }
  if (room_exists==0){
    pthread_mutex_lock(&stdout_lock);
    fprintf(stderr, "Peer join failed - room does not exist.\n");
    pthread_mutex_unlock(&stdout_lock);
    send_error(ip, port, 'j', 'e');
    return;
  }
  
  //setup entry
  struct peer *new_peer;
  new_peer = (struct peer *)malloc(sizeof(struct peer));
  char* ip_and_port_format = (char *)"%d:%d";
  sprintf(new_peer->ip_and_port, ip_and_port_format, ip, port);
  new_peer->room = room;
  new_peer->alive = 1;
  

  HASH_FIND_STR(peers, (new_peer->ip_and_port), s);
  if(s!=NULL && s->room==room){
    pthread_mutex_lock(&stdout_lock);
    fprintf(stderr, "Peer join failed - already in room.\n");
    pthread_mutex_unlock(&stdout_lock);
    send_error(ip, port, 'j', 'a');
    return;
  }
  int old_room_update = -1;
  if(s==NULL){ 
    //peer not found - join
    pthread_mutex_lock(&peers_lock);
    HASH_ADD_STR( peers, ip_and_port, new_peer );
    pthread_mutex_unlock(&peers_lock);
  }else{
    //peer found - switch
    old_room_update = s->room;
    pthread_mutex_lock(&peers_lock);
    HASH_REPLACE_STR( peers, ip_and_port, new_peer, s );
    pthread_mutex_unlock(&peers_lock);
  }
  if(old_room_update!=-1){
    pthread_mutex_lock(&stdout_lock);
    fprintf(stderr, "%s peer switched from %d to %d.\n", new_peer->ip_and_port, old_room_update, room);
    pthread_mutex_unlock(&stdout_lock);
    peer_list(ip, port, room);
    peer_list(0, -1, old_room_update);
  }else{
    pthread_mutex_lock(&stdout_lock);
    fprintf(stderr, "%s joined %d\n", new_peer->ip_and_port, room);
    pthread_mutex_unlock(&stdout_lock);
    peer_list(ip, port, room);
  }
}