Ejemplo n.º 1
0
bool_t set_saved_race_results( char *player,
			       char *event,
			       char *cup,
			       char *race,
			       difficulty_level_t d,
			       scalar_t time,
			       int herring,
			       int score )
{
    hash_table_t player_table;
    hash_table_t event_table;
    hash_table_t cup_table;
    hash_table_t race_table;
    save_info_t *this_save;

    player_table = results_save_table[d];

    if ( !get_hash_entry( player_table, player, (hash_entry_t*)&event_table ) )
    {
	event_table = create_hash_table();
	add_hash_entry( player_table, player, (hash_entry_t)event_table );
    }

    if ( !get_hash_entry( event_table, event, (hash_entry_t*)&cup_table ) ) {
	cup_table = create_hash_table();
	add_hash_entry( event_table, event, (hash_entry_t)cup_table );
    }

    if ( !get_hash_entry( cup_table, cup, (hash_entry_t*)&race_table ) ) {
	race_table = create_hash_table();
	add_hash_entry( cup_table, cup, (hash_entry_t)race_table );
    }

    if ( !get_hash_entry( race_table, race, (hash_entry_t*)&this_save ) ) {
	this_save = (save_info_t*)malloc(sizeof(save_info_t));
	memset( this_save, 0, sizeof(save_info_t) );
	add_hash_entry( race_table, race, (hash_entry_t)this_save );
	this_save->data_type = RACE_RESULTS; 
    }

    check_assertion( this_save->data_type == RACE_RESULTS,
		     "Invalid data type" );

    strcpy( this_save->data.results.event, event );
    strcpy( this_save->data.results.cup, cup );
    strcpy( this_save->data.results.race, race );
    this_save->data.results.difficulty = d;
    this_save->data.results.time = time;
    this_save->data.results.herring = herring;
    this_save->data.results.score = score;

#ifdef __APPLE__
    // Write as soon as possible
    write_saved_games();
#endif

    return True;
}
Ejemplo n.º 2
0
bool_t bind_font( char *binding, char *fontname, scalar_t size, 
		  colour_t colour )
{
    font_node_t *fontnode;
    font_t *font;

    print_debug(DEBUG_FONT, "Binding %s to font name: %s", 
		binding, fontname);

    if (!get_font( fontname, &fontnode)) {
	check_assertion(0, "Attempt to bind to unloaded font");
	return False;
    }

    if (get_hash_entry(binding_table, binding, (hash_entry_t*)(&font))) {
	font->node->ref_count--;
    } else {
	font = (font_t*)malloc(sizeof(font_t));
	add_hash_entry(binding_table, binding, (hash_entry_t)font);
    }

    font->node = fontnode;
    font->size = size * mHeight / 320;
    font->colour = colour;

    font->node->ref_count += 1;

    return True;
}
Ejemplo n.º 3
0
/* called with note_lock held */
uint do_hash(struct dentry * dentry, struct vfsmount * vfsmnt, struct dentry * root, struct vfsmount * rootmnt)
{
	struct qstr * dname;
	uint value = -1;
	uint firsthash;
	uint incr;
	uint parent = 0;
	struct op_hash_index *entry;

	if (!wind_dentries(dentry, vfsmnt, root, rootmnt))
		goto out;

	/* unwind and hash */

	while ((dname = pop_dname())) {
		/* if N is prime, value in [0-N[ and incr = max(1, value) then
		 * iteration: value = (value + incr) % N covers the range [0-N[
		 * in N iterations */
		incr = firsthash = value = name_hash(dname->name, dname->len, parent);
		if (incr == 0)
			incr = 1;

	retry:
		entry = &hash_map[value];
		/* existing entry ? */
		if (streq(get_from_pool(entry->name), dname->name)
			&& entry->parent == parent)
			goto next;

		/* new entry ? */
		if (entry->parent == -1) {
			if (add_hash_entry(entry, parent, dname->name, dname->len))
				goto fullpool;
			goto next;
		}

		/* nope, find another place in the table */
		value = (value + incr) % OP_HASH_MAP_NR;

		if (value == firsthash)
			goto fulltable;

		goto retry;
	next:
		parent = value;
	}

out:
	op_dname_top = 0;
	return value;
fullpool:
	printk(KERN_ERR "oprofile: string pool exhausted.\n");
	value = -1;
	goto out;
fulltable:
	printk(KERN_ERR "oprofile: component hash table full :(\n");
	value = -1;
	goto out;
}
Ejemplo n.º 4
0
bool_t set_last_completed_cup( char* player, char* event, 
			       difficulty_level_t d, char* cup )
{
    hash_table_t event_table;
    save_info_t *this_save;
    difficulty_level_t level;
    int i;

    if ( !get_hash_entry( progress_save_table, player, (hash_entry_t*)&event_table ) ) {
	event_table = create_hash_table();
	add_hash_entry( progress_save_table, player, event_table );
    }

    if ( !get_hash_entry( event_table, event, (hash_entry_t*)&this_save ) ) {
	this_save = (save_info_t*)malloc(sizeof(save_info_t)*
					 DIFFICULTY_NUM_LEVELS);
	memset( this_save, 0, sizeof(save_info_t) * DIFFICULTY_NUM_LEVELS );

	for( i=0; i<DIFFICULTY_NUM_LEVELS; i++ ) {
	    level = (difficulty_level_t)i;
	    strcpy( this_save[level].data.event.event, event );
	    this_save[level].data.event.difficulty = d;

	    if ( level != d ) {
		this_save[level].data_type = INVALID_DATA;
	    } else {
		this_save[level].data_type = EVENT_INFO;
	    }
	}
	add_hash_entry( event_table, event, this_save );
    }

    this_save[d].data_type = EVENT_INFO;
    strcpy( this_save[d].data.event.cup, cup );
    return True;
}
Ejemplo n.º 5
0
bool_t set_high_score( char* event, char* cup, char* player, int score )
{
    hash_table_t *cup_table;
    score_info_t *this_score;

    if ( !get_hash_entry( score_table, event, (hash_entry_t*)&cup_table ) ) {
	cup_table = (hash_table_t*)malloc(sizeof(hash_table_t));
	*cup_table = create_hash_table();
	add_hash_entry( score_table, event, cup_table );
    }

    if ( !get_hash_entry( *cup_table, cup, (hash_entry_t*)&this_score ) ) {
	this_score = (score_info_t*)malloc(sizeof(score_info_t));
	strcpy(this_score->event.event, event);
	strcpy(this_score->event.cup, cup);
	this_score->event.difficulty = (difficulty_level_t)0;
	this_score->data_type = (score_data_t)0;
	add_hash_entry( *cup_table, cup, this_score );
    }

    this_score->score = score;
    strcpy( this_score->player_name, player );
    return True;
}
Ejemplo n.º 6
0
/*! 
  Associate the sounds in the _name_ array with the
  sound context sound_context.  If more than one sound is specified,
  then a sound will be chosen randomly from the list each time the
  sound is played.  If num_sounds == 0, then the entry for the sound
  context is deleted.

  \pre     \c sound_context != NULL
  \arg \c  sound_context sound context to bind sounds to
  \arg \c  names list of sound names to bind to context
  \arg \c  num_sounds number of elements in \c names

  \return  none
  \author  jfpatry
  \date    Created:  2000-08-13
  \date    Modified: 2000-08-14
*/
void bind_sounds_to_context( char *sound_context, char **names, int num_sounds )
{
    int i;
    sound_context_data_t *data;

    check_assertion( initialized_, "audio module not initialized" );

    if ( get_hash_entry( sound_contexts_, sound_context, 
			 (hash_entry_t*) &data ) )
    {
	/* Entry for this context already exists; decrement ref_cts &
	   delete arrays */

	for (i=0; i<data->num_sounds; i++) {
	    decr_sound_data_ref_ctr( data->sound_names[i] );
	    free( data->sound_names[i] );
	}

	free( data->sound_names );
	free( data->chunks );
	del_hash_entry( sound_contexts_, sound_context, NULL );
    } else {
	data = (sound_context_data_t*)malloc( sizeof(sound_context_data_t) );
    }

    if ( num_sounds == 0 ) {
	/* delete the context */
	free( data );
	return;
    }

    check_assertion( num_sounds > 0, "num_sounds isn't > 0 " );

    data->num_sounds = num_sounds;
    data->sound_names = (char**)malloc( sizeof(char*)*num_sounds );
    data->chunks = (Mix_Chunk**)malloc( sizeof(Mix_Chunk*)*num_sounds );
    data->loop_count = 0;
    data->channel = 0;
    data->volume = 128;

    for (i=0; i<num_sounds; i++) {
	data->sound_names[i] = string_copy( names[i] );
	incr_sound_data_ref_ctr( names[i] );
	data->chunks[i] = NULL;
    }
    add_hash_entry( sound_contexts_, sound_context, (hash_entry_t)data );
}
Ejemplo n.º 7
0
/*! 
  Associate the music with name _name_ to the music context
  _music_context_.  If _name_ is NULL or the null string, the data for
  the music context will be deleted.

  \pre     \c music_context != \c NULL
  \arg \c  music_context music context to bind sounds to
  \arg \c  name name of music to bind to context
  \arg \c  loop number of loops to play music for (-1 to loop forever)

  \return  none
  \author  jfpatry
  \date    Created:  2000-08-13
  \date Modified: 2000-08-14 */
void bind_music_to_context( char *music_context, char *name, int loop )
{
    music_context_data_t *data;

    check_assertion( initialized_, "audio module not initialized" );

    if ( get_hash_entry( music_contexts_, music_context, 
			 (hash_entry_t*) &data ) )
    {
	/* Entry for this context already exists; decrement ref count &
	   delete string */

	/* check if music is playing, stop if it is */
	if ( current_music_name_ != NULL && 
	     strcmp( current_music_name_, data->music_name ) == 0 )
	{

	    Mix_HaltMusic();

	    current_music_name_ = NULL;
	    current_music_data_ = NULL;
	    check_assertion( get_music_playing_status( data->music_name ),
			     "inconsistent music playing status info" );
	    set_music_playing_status( data->music_name, False );
	}

	decr_music_data_ref_ctr( data->music_name );
	free( data->music_name );
	del_hash_entry( music_contexts_, music_context, NULL );
    } else {
	data = (music_context_data_t*)malloc( sizeof(music_context_data_t) );
    }

    if ( name == NULL || name[0]=='\0' ) {
	/* delete the context */
	free( data );
	return;
    }

    data->music_name = string_copy( name );
    incr_music_data_ref_ctr( name );
    data->music = NULL;
    data->loop = loop;
    add_hash_entry( music_contexts_, music_context, (hash_entry_t)data );
}
Ejemplo n.º 8
0
/*! 
  Adds a mouse up callback for the specified widget.
  \pre     widget != NULL
  \arg \c  widget the widget to receive mouse up events
  \arg \c  cb the callback function (NULL to remove callback)

  \return  None
  \author  jfpatry
  \date    Created:  2000-09-16
  \date    Modified: 2000-09-16
*/
void ui_add_mouse_up_callback( void* widget, mouse_button_event_cb_t cb ) 
{
    char *key;
    hash_table_t table;
    ui_callback_data_t *cb_data;

    key = generate_key_from_pointer( widget );
    table = mouse_up_cbs;

    if ( get_hash_entry( table, key, NULL) ) {
	del_hash_entry( table, key, (hash_entry_t*)&cb_data );
	free(cb_data);
    }

    if ( cb != NULL ) {
	add_hash_entry( table, key, generate_cb_data( widget, (void*) cb ) );
    }
}
Ejemplo n.º 9
0
//External interface using a hash table to speedup the process
funcPointerT findFunctionUsingDlopen(const char* function_name, const char* lib_name)
{
  funcPointerT result =0;
 
#if USE_UTHASH
 result = find_hash_entry(function_name); 
 if (result)
    return result;
 else  
 {
   result = findWithDlopen(function_name,lib_name);
   add_hash_entry(function_name,result);
 }
#else 
  result = findWithDlopen(function_name,lib_name);
#endif 
  return result;
}
Ejemplo n.º 10
0
bool_t load_font( char *fontname, char *filename, char *texname )
{
    font_node_t *fontnode;
    tex_font_metrics_t *tfm;
    texture_node_t *tex;

    print_debug(DEBUG_FONT, "Loading font %s from file: %s", 
		fontname, filename);
    if ( initialized == False ) {
        check_assertion( 0, "font module not initialized" );
    } 

    if ( ! get_texture( texname, &tex ) ) {
	print_warning( IMPORTANT_WARNING, 
		       "Texture `%s' does not exist", texname );
	return False;
    }

    tfm = load_tex_font_metrics( filename );

    if ( tfm == NULL ) {
    	print_warning( IMPORTANT_WARNING, 
		       "couldn't load font file %s", filename );
    	return False;
    }


    if (get_hash_entry( font_table, fontname, (hash_entry_t*)&fontnode )) { 
	print_debug( DEBUG_FONT, "Font %s already exists, deleting...", 
		     fontname );
	delete_tex_font_metrics( fontnode->tfm );
	fontnode->tex->ref_count -= 1;
    } else {
	fontnode = (font_node_t*)malloc(sizeof(font_node_t));
	fontnode->ref_count = 0;
	add_hash_entry( font_table, fontname, (hash_entry_t)fontnode ); 
    }

    fontnode->tfm = tfm;
    fontnode->tex = tex;
    tex->ref_count += 1;

    return True;
} 
Ejemplo n.º 11
0
bool_t bind_texture( char *binding, char *texname )
{
    texture_node_t *tex, *oldtex;

    print_debug(DEBUG_TEXTURE, "Binding %s to texture name: %s", 
		binding, texname);
    if (!get_texture( texname, &tex)) {
	check_assertion(0, "Attempt to bind to unloaded texture");
	return False;
    }

    if (get_hash_entry(binding_table, binding, (hash_entry_t*)(&oldtex))) {
	oldtex->ref_count--;
	if (!del_hash_entry(binding_table, binding, NULL)) {
	    check_assertion(0, "Cannot delete known texture");
	    return False;
	}
    }

    add_hash_entry(binding_table, binding, (hash_entry_t)tex);
    tex->ref_count++;

    return True;
}
Ejemplo n.º 12
0
bool_t load_texture( char *texname, char *filename, int repeatable )
{
    IMAGE *texImage;
    texture_node_t *tex;
    int max_texture_size;


    print_debug(DEBUG_TEXTURE, "Loading texture %s from file: %s", 
		texname, filename);
    if ( initialized == False ) {
        check_assertion( 0, "texture module not initialized" );
    } 

    texImage = ImageLoad( filename );

    if ( texImage == NULL ) {
    	print_warning( IMPORTANT_WARNING, 
		       "couldn't load image %s", filename );
    	return False;
    }

    if (get_hash_entry( texture_table, texname, (hash_entry_t*)&tex )) { 
	print_debug(DEBUG_TEXTURE, "Found texture %s with id: %d", 
		    texname, tex->texture_id);
        glDeleteTextures( 1, &(tex->texture_id) );
    } else {
        tex = (texture_node_t*)malloc(sizeof(texture_node_t));

	check_assertion( tex != NULL, "out of memory" );

	tex->ref_count = 0;
	add_hash_entry( texture_table, texname, (hash_entry_t)tex ); 
    }
 
    tex->repeatable = repeatable;
    glGenTextures( 1, &(tex->texture_id) );
    glBindTexture( GL_TEXTURE_2D, tex->texture_id );

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);


    if ( repeatable ) {
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    } else {
#ifndef __APPLE__DISABLED__
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
#endif
    }
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
                     get_min_filter() );

    /* Check if we need to scale image */
    glGetIntegerv( GL_MAX_TEXTURE_SIZE, &max_texture_size );
    if ( texImage->sizeX > max_texture_size ||
	 texImage->sizeY > max_texture_size ) 
    {
#ifdef __APPLE__DISABLED__
        abort(); //We don't support that yet
#else
	char *newdata = (char*)malloc( texImage->sizeZ *
				       max_texture_size *
				       max_texture_size );

	check_assertion( newdata != NULL, "out of memory" );

	print_debug( DEBUG_TEXTURE, "Texture `%s' too large -- scaling to "
		     "maximum allowed size",
		     filename );


	/* In the case of large- or small-aspect ratio textures, this
           could end up using *more* space... oh well. */
	gluScaleImage( texImage->sizeZ == 3 ? GL_RGB : GL_RGBA,
		       texImage->sizeX, texImage->sizeY, 
		       GL_UNSIGNED_BYTE,
		       texImage->data,
		       max_texture_size, max_texture_size, 
		       GL_UNSIGNED_BYTE,
		       newdata );

	free( texImage->data );
	texImage->data = (unsigned char*) newdata;
	texImage->sizeX = max_texture_size;
	texImage->sizeY = max_texture_size;
#endif
    }

#ifndef __APPLE__
    gluBuild2DMipmaps( GL_TEXTURE_2D, texImage->sizeZ, texImage->sizeX,
		       texImage->sizeY, texImage->sizeZ == 3 ? GL_RGB : GL_RGBA, 
		       GL_UNSIGNED_BYTE, texImage->data );
#else
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    
    glTexImage2D( GL_TEXTURE_2D, 0, texImage->sizeZ == 3 ? GL_RGB : GL_RGBA, texImage->sizeX,
		       texImage->sizeY == 255 ? 256 : texImage->sizeY /* Work around for tree.png */,
               0, texImage->sizeZ == 3 ? GL_RGB : GL_RGBA,
		       GL_UNSIGNED_BYTE, texImage->data );
#endif
    free( texImage->data );
    free( texImage );
	TRDebugLog("%dx%d %d (%d)\n", texImage->sizeX, texImage->sizeY, texImage->sizeZ, glGetError());
    return True;
} 
Ejemplo n.º 13
0
GHashTable*
imgur_get_record (const gchar* record_name)
{
	gchar *path = get_path ();
	GKeyFile *keyfile = get_keyfile (path);
	GHashTable *result = g_hash_table_new (g_str_hash, g_str_equal);
	gchar **keys, **cursor;
	gchar *local_thumbnail = NULL;
	if (!keyfile)
	{
		g_free (path);
		return result;
	}

	keys = g_key_file_get_keys (keyfile,
		record_name,
		NULL, NULL);

	if (!keys)
	{
		g_free (path);
		g_key_file_free (keyfile);
		return result;
	}

	for (cursor = keys; *cursor; cursor++)
	{
		gchar *value = g_key_file_get_string (keyfile,
				record_name,
				*cursor,
				NULL);

		add_hash_entry (result,
			*cursor,
			value);
	}

	local_thumbnail = get_local_thumbnail (path,
		keyfile,
		record_name);

	if (g_file_test (local_thumbnail,
		G_FILE_TEST_IS_REGULAR))
	{
		add_hash_entry (result,
			"local_thumbnail",
			local_thumbnail);
	}
	g_free (local_thumbnail);

	/* Since we took it out before storage: */
	add_hash_entry (result,
		"image_hash",
		record_name);

	/* FIXME: free the keyfile? */
	g_strfreev (keys);
	g_free (path);

	return result;
}
Ejemplo n.º 14
0
void do_check_areas(CHAR_DATA * ch, char * argument)
{
    hash_table * room_hash;
    hash_table * obj_hash;
    hash_table * mob_hash;
    int          min_vnum, max_vnum;
    int          new_vnum,old_vnum;
    char          buffer[MAX_INPUT_LENGTH];
    FILE       *  out_file;

    AREA_DATA * CurArea;
    BUILD_DATA_LIST * pList;
    ROOM_INDEX_DATA * pRoomIndex;
    OBJ_INDEX_DATA  * pObjIndex;

    /* Create hash tables for rooms, mobiles, objects */

    room_hash=create_hash_table(MAX_KEY_HASH);
    obj_hash=create_hash_table(MAX_KEY_HASH);
    mob_hash=create_hash_table(MAX_KEY_HASH);

    out_file=fopen("area_changes.txt","a");

    for (CurArea=first_area; CurArea != NULL; CurArea=CurArea->next)
    {
        min_vnum=CurArea->min_vnum;
        max_vnum=CurArea->max_vnum;
        fprintf(out_file,"%s:\n",CurArea->name);

        /* Go through rooms */
        for (pList=CurArea->first_area_room; pList != NULL; pList=pList->next)
        {
            pRoomIndex=pList->data;
            if (pRoomIndex->vnum < min_vnum || pRoomIndex->vnum > max_vnum)
            {
                old_vnum=pRoomIndex->vnum;
                /* Find a free slot */
                for (new_vnum=min_vnum; new_vnum <= max_vnum; new_vnum++)
                    if (get_room_index(new_vnum)==NULL)
                        break;

                if (new_vnum > max_vnum)
                {
                    sprintf(buffer,"Not enough vnums in area %s\n\r",CurArea->name);
                    send_to_char(buffer,ch);
                }
                else
                {
                    fprintf(out_file,"Room: [%5i] -> [%5i]\n",old_vnum,new_vnum);
                    /* Delete from room hashing table, and put new vnum in. */
                    add_hash_entry(room_hash,old_vnum,(void *) new_vnum);
                    swap_global_hash('R',pRoomIndex,old_vnum,new_vnum);
                    pRoomIndex->vnum=new_vnum;
                    area_modified(CurArea);

                }
            }
        }

        /* Go through objs */
        for (pList=CurArea->first_area_object; pList != NULL; pList=pList->next)
        {
            pObjIndex=pList->data;
            if (pObjIndex->vnum < min_vnum || pObjIndex->vnum > max_vnum)
            {
                old_vnum=pObjIndex->vnum;
                /* Find a free slot */
                for (new_vnum=min_vnum; new_vnum <= max_vnum; new_vnum++)
                    if (get_obj_index(new_vnum)==NULL)
                        break;

                if (new_vnum > max_vnum)
                {
                    sprintf(buffer,"Not enough vnums in area %s\n\r",CurArea->name);
                    send_to_char(buffer,ch);
                }
                else
                {
                    fprintf(out_file,"Obj: [%5i] -> [%5i] %s\n",old_vnum,new_vnum,pObjIndex->short_descr);
                    /* Delete from obj hashing table, and put new vnum in. */
                    add_hash_entry(obj_hash,old_vnum,(void *) new_vnum);
                    swap_global_hash('O',pObjIndex,old_vnum,new_vnum);
                    pObjIndex->vnum=new_vnum;
                    area_modified(CurArea);

                }
            }
        }
    }

    fclose(out_file);
    return;
}
Ejemplo n.º 15
0
Archivo: ground.c Proyecto: tinypie/TCT
/* 
 * load ground truth file. The file should have the session type in comment.
 * We will check that, if it's voilated, then refuse to work.
 *
 */
int load_ground_truth()
{
	FILE *fp;
	char buffer[MAXLINE];
	char *p, *sptr = NULL;
	int type = 0;
	struct class_info *entry;

	/* open the ground truth file */
	if ((fp=fopen(tct_opts.ground_truth, "r")) == NULL) {
		err_sys("open file ground truth error");
	}

	while (fgets(buffer, MAXLINE, fp) != NULL) {
		/* skip blank line */
		if (buffer[0] == '\n') 
			continue;

		/* the sesion type is in the comment line */
		if (buffer[0] == '#') {
			if (strstr(buffer, "session type") != NULL) {
				if ((p=strstr(buffer, "flow")) != NULL) {	/* flow and biflow */
					if (*(p-1) == 'i' && *(p-2) == 'b') {	/* biflow */
						type = SESSION_TYPE_BFLOW;
					} else {
						type = SESSION_TYPE_FLOW;
					}
				} else {
					type = SESSION_TYPE_PKT;
				}

				if (type != tct_opts.work_mode) {
					fclose(fp);
					err_quit("ground truth has incorrect session type");
				}
			}
			continue;
		}

		entry = (struct class_info *) malloc(sizeof(struct class_info));

		entry->next = NULL;

		/* get src ip */
		p = strtok_r(buffer, "\t", &sptr);
		get_address(&(entry->addr.a_addr), p);

		/* get src port */
		p = strtok_r(NULL, "\t", &sptr);
		entry->addr.a_port = htons(atoi(p));

		/* get dst ip */
		p = strtok_r(NULL, "\t", &sptr);
		get_address((&entry->addr.b_addr), p);

		/* get dst port */
		p = strtok_r(NULL, "\t", &sptr);
		entry->addr.b_port = htons(atoi(p));

		/* get protocal */
		p = strtok_r(NULL, "\t", &sptr);
		entry->addr.l4proto = atoi(p);

		/* get timestamp (start time) */
		p = strtok_r(NULL, "\t", &sptr);
		get_time(&entry->time, p);
#if 0
		/* skip endtime and total pkts  and root app*/
		for (i = 0; i < 3; i++) {
			strtok_r(NULL, "\t", &sptr);
		}
#endif

		p = strtok_r(NULL, "\t", &sptr);
		char *tmp;
		if ((tmp = strchr(p, '\n')) != NULL)
			*tmp = '\0';

#if 0
		strcpy(entry->name, p);
#endif

		entry->app_id = get_id(p);

		add_hash_entry(entry);
	}

	fclose(fp);
	return 0;
}
Ejemplo n.º 16
0
void write_saved_games( void )
{
    hash_search_t player_scan_ptr;
    hash_table_t event_table;
    hash_search_t event_scan_ptr;
    hash_table_t cup_table;
    hash_search_t cup_scan_ptr;
    hash_table_t race_table;
    hash_search_t race_scan_ptr;
    hash_search_t save_scan_ptr;
    char *player_name;
    char *event_name;
    FILE* save_stream;
    char save_file[BUFF_LEN];
    save_info_t *this_save;
    difficulty_level_t level;
    int i;
    hash_table_t player_table;

    /* Create list of players */
    player_table = create_hash_table();

    begin_hash_scan( progress_save_table, &player_scan_ptr );
    while ( next_hash_entry( player_scan_ptr, &player_name, NULL ) ) 
    {
	if ( !get_hash_entry( player_table, player_name, NULL ) ) {
	    add_hash_entry( player_table, player_name, "" );
	}
    }
    end_hash_scan( player_scan_ptr );

    for (i=0; i<DIFFICULTY_NUM_LEVELS; i++) {
	begin_hash_scan( results_save_table[i], &player_scan_ptr );
	while ( next_hash_entry( player_scan_ptr, &player_name, NULL ) ) 
	{
	    if ( !get_hash_entry( player_table, player_name, NULL ) ) {
		add_hash_entry( player_table, player_name, "" );
	    }
	}
	end_hash_scan( player_scan_ptr );
    }


    /* truncate and initialize all save files */
    begin_hash_scan( player_table, &player_scan_ptr );
    while ( next_hash_entry( player_scan_ptr, &player_name, NULL )  )
    {
	truncate_and_init_save_file( player_name );
    }
    end_hash_scan( player_scan_ptr );


    /* Don't need player list anymore */
    del_hash_table( player_table );
    player_table = NULL;


    begin_hash_scan( progress_save_table, &player_scan_ptr );
    while ( next_hash_entry( player_scan_ptr, &player_name, 
			     (hash_entry_t*)&event_table ) ) 
    {
	if ( get_save_game_file_name( save_file, player_name, 
				      sizeof(save_file) ) == 0 ) 
	{
	    save_stream = fopen( save_file, "ab" );

	    if ( save_stream == NULL ) {
		print_warning( IMPORTANT_WARNING,
			       "Couldn't open `%s' for writing: %s",
			       save_file, 
			       strerror( errno ) );
	    } else {
		begin_hash_scan( event_table, &save_scan_ptr );

		while ( next_hash_entry( save_scan_ptr, &event_name, 
					 (hash_entry_t*)&this_save ) ) 
		{
		    for( i=0; i<DIFFICULTY_NUM_LEVELS; i++ ) {
			level = (difficulty_level_t)i;
			if ( this_save[level].data_type >= 0 ) {
			    fwrite( &(this_save[level]), 
				    sizeof(this_save[level]), 
				    1, 
				    save_stream );
			}
		    }
		}
		end_hash_scan( save_scan_ptr );

		if ( fclose( save_stream ) != 0 ) {
		    perror( "fclose" );
		}
		save_stream = NULL;
	    }
	}
    }
    end_hash_scan( player_scan_ptr );

    for (i=0; i<DIFFICULTY_NUM_LEVELS; i++) {
	begin_hash_scan( results_save_table[i], &player_scan_ptr );
	while ( next_hash_entry( player_scan_ptr, &player_name, 
				 (hash_entry_t*)&event_table ) ) 
	{
	    if ( get_save_game_file_name( save_file, player_name, 
					  sizeof(save_file) ) == 0 ) 
	    {
		save_stream = fopen( save_file, "ab" );

		if ( save_stream == NULL ) {
		    print_warning( IMPORTANT_WARNING,
				   "Couldn't open `%s' for writing: %s",
				   save_file, 
				   strerror( errno ) );
		} else {
		    begin_hash_scan( event_table, &event_scan_ptr );
		    while ( next_hash_entry( event_scan_ptr, NULL, 
					     (hash_entry_t*)&cup_table ) ) 
		    {
			begin_hash_scan( cup_table, &cup_scan_ptr );
			while ( next_hash_entry( cup_scan_ptr, NULL,
						 (hash_entry_t*)&race_table ) )
			{
			    begin_hash_scan( race_table, &race_scan_ptr );
			    while ( next_hash_entry( race_scan_ptr, NULL,
						 (hash_entry_t*)&this_save ) )
			    {
				fwrite( this_save, 
					sizeof(save_info_t), 
					1, 
					save_stream );
				
			    }
			    end_hash_scan( race_scan_ptr );
			}
			end_hash_scan( cup_scan_ptr );
		    }
		    end_hash_scan( event_scan_ptr );

		    if ( fclose( save_stream ) != 0 ) {
			perror( "fclose" );
		    }
		    save_stream = NULL;
		}
	    }
	}
	end_hash_scan( player_scan_ptr );
    }
}
Ejemplo n.º 17
0
void add_vnum(int vnum)
{
 hash_entry * entry;
 hash_entry * range, * temp;
 int start_vnum;
 int end_vnum;
 int cnt;

 /* Adds a vnum, adding to beggining or ends */
 
 start_vnum=vnum;
 end_vnum=vnum;
 
 for ( ; ; )
 {
  cnt=0;
 
  if ( (range=get_hash_entry(start_vnum-1)) != NULL)
  {
   if (range->offset > 0)
    printf("ERROR in ranges vnum: %i range start: %i range end: %i",
           start_vnum, range->vnum, range->vnum+range->offset);
   else
   {
    start_vnum=range->vnum+range->offset;
    if (   range->offset != 0
        && (temp=get_hash_entry(start_vnum)) != NULL)
       del_hash_entry(temp);
    del_hash_entry(range);
    cnt=1;
   }
  }
  
  if ( (range=get_hash_entry(end_vnum+1)) != NULL)
  {
   if (range->offset < 0)
    printf("ERROR in ranges vnum: %i range start: %i range end: %i",
           end_vnum, range->vnum+range->offset, range->vnum);
   else
   {
    end_vnum=range->vnum+range->offset;
    if (   range->offset != 0
        && (temp=get_hash_entry(start_vnum)) != NULL)
       del_hash_entry(temp);
    del_hash_entry(range);
    cnt=1;
   }
  }
  
  if (!cnt)
   break;
 }
           
 if (start_vnum == end_vnum)
 {
  entry=malloc(sizeof(hash_entry));
  entry->vnum=start_vnum;
  entry->offset=0;
  add_hash_entry(entry);
 }
 else
 {
  entry=malloc(sizeof(hash_entry));
  entry->vnum=start_vnum;
  entry->offset=end_vnum-start_vnum;
  add_hash_entry(entry);
  
  entry=malloc(sizeof(hash_entry));
  entry->vnum=end_vnum;
  entry->offset=start_vnum-end_vnum;
  add_hash_entry(entry);
 }
} 
Ejemplo n.º 18
0
void do_check_areas(CHAR_DATA * ch, char * argument)
{
 hash_table * room_hash;
 hash_table * obj_hash;
 hash_table * mob_hash;
 int           min_vnum,max_vnum;
 int           new_vnum,old_vnum;
 char          buffer[MAX_INPUT_LENGTH];
 FILE       *  out_file;
 int           a;
 RESET_DATA *  pReset;
 
 AREA_DATA * CurArea;
 BUILD_DATA_LIST * pList;
 ROOM_INDEX_DATA * pRoomIndex;
 OBJ_INDEX_DATA  * pObjIndex;
 MOB_INDEX_DATA  * pMobIndex;
 
 /* Create hash tables for rooms, mobiles, objects */

 room_hash=create_hash_table(MAX_KEY_HASH);
 obj_hash=create_hash_table(MAX_KEY_HASH);
 mob_hash=create_hash_table(MAX_KEY_HASH);
 
 out_file=fopen("area_changes.txt","a");
 
 for (CurArea=first_area; CurArea != NULL; CurArea=CurArea->next)
 {
  min_vnum=CurArea->min_vnum;
  max_vnum=CurArea->max_vnum;
  fprintf(out_file,"%s:\n",CurArea->name);
  
  /* Go through rooms */
  for (pList=CurArea->first_area_room; pList != NULL; pList=pList->next)
  {
   pRoomIndex=pList->data;
   if (pRoomIndex->vnum < min_vnum || pRoomIndex->vnum > max_vnum)
   {
    old_vnum=pRoomIndex->vnum;
    /* Find a free slot */
    for (new_vnum=min_vnum; new_vnum <= max_vnum; new_vnum++)
     if (get_room_index(new_vnum)==NULL)
      break;
   
    if (new_vnum > max_vnum)
    {
     sprintf(buffer,"Not enough vnums in area %s\n\r",CurArea->name);
     send_to_char(buffer,ch);
    }
    else
    {
     fprintf(out_file,"Room: [%5i] -> [%5i] %s\n",old_vnum,new_vnum,pRoomIndex->name);    
     /* Delete from room hashing table, and put new vnum in. */
     add_hash_entry(room_hash,old_vnum,(void *) new_vnum);
     swap_global_hash('R',pRoomIndex,old_vnum,new_vnum);
     pRoomIndex->vnum=new_vnum;
     area_modified(CurArea);
         
    }
   }
  }
  
  /* Go through objs */
  for (pList=CurArea->first_area_object; pList != NULL; pList=pList->next)
  {
   pObjIndex=pList->data;
   if (pObjIndex->vnum < min_vnum || pObjIndex->vnum > max_vnum)
   {
    old_vnum=pObjIndex->vnum;
    /* Find a free slot */
    for (new_vnum=min_vnum; new_vnum <= max_vnum; new_vnum++)
     if (get_obj_index(new_vnum)==NULL)
      break;
   
    if (new_vnum > max_vnum)
    {
     sprintf(buffer,"Not enough vnums in area %s\n\r",CurArea->name);
     send_to_char(buffer,ch);
    }
    else
    {
     fprintf(out_file,"Obj: [%5i] -> [%5i] %s\n",old_vnum,new_vnum,pObjIndex->short_descr);    
     /* Delete from obj hashing table, and put new vnum in. */
     add_hash_entry(obj_hash,old_vnum,(void *) new_vnum);
     swap_global_hash('O',pObjIndex,old_vnum,new_vnum);
     pObjIndex->vnum=new_vnum;
     area_modified(CurArea);
     
    }
   }
  }
  
  /* Go through mobs */
  for (pList=CurArea->first_area_mobile; pList != NULL; pList=pList->next)
  {
   pMobIndex=pList->data;
   if (pMobIndex->vnum < min_vnum || pMobIndex->vnum > max_vnum)
   {
    old_vnum=pMobIndex->vnum;
    /* Find a free slot */
    for (new_vnum=min_vnum; new_vnum <= max_vnum; new_vnum++)
     if (get_mob_index(new_vnum)==NULL)
      break;
   
    if (new_vnum > max_vnum)
    {
     sprintf(buffer,"Not enough vnums in area %s\n\r",CurArea->name);
     send_to_char(buffer,ch);
    }
    else
    {
     fprintf(out_file,"Mob: [%5i] -> [%5i] %s\n",old_vnum,new_vnum,pMobIndex->short_descr);    
     /* Delete from mob hashing table, and put new vnum in. */
     add_hash_entry(mob_hash,old_vnum,(void *) new_vnum);
     swap_global_hash('M',pMobIndex,old_vnum,new_vnum);
     pMobIndex->vnum=new_vnum;
     area_modified(CurArea);
     /* Check for shops */
     if (pMobIndex->pShop != NULL)
      pMobIndex->pShop->keeper=new_vnum;
     
    }
   }
  }
 }
 
 fclose(out_file);
 
 /* Now go through all things referencing the changes 
  * 
  *      Resets     
  *      exit->key
  *      
  */
  
 for (CurArea=first_area; CurArea != NULL; CurArea=CurArea->next)
 {
  for (pList=CurArea->first_area_room; pList != NULL; pList=pList->next)
  {
   /* Check keys and exits*/
   pRoomIndex=pList->data;
   
   for (a=0; a<=5; a++)
   {
    if (   pRoomIndex->exit[a] != NULL )
    {
     if (  pRoomIndex->exit[a]->key != 0
          && (new_vnum=(int) get_hash_entry(obj_hash,pRoomIndex->exit[a]->key)) != 0)
        {
         pRoomIndex->exit[a]->key = new_vnum;
         area_modified(CurArea);
        }
     if ( (new_vnum=(int) get_hash_entry(room_hash,pRoomIndex->exit[a]->vnum)) != 0)
        {
         pRoomIndex->exit[a]->vnum = new_vnum;
         area_modified(CurArea);
        }
    }
        
   }
  }
  
  /* Scan through resets */
  for (pReset=CurArea->first_reset; pReset != NULL; pReset=pReset->next)
  {
   switch (pReset->command)
   {
     case 'M':
       if ( (new_vnum=(int) get_hash_entry(mob_hash,pReset->arg1)) != 0)
       {
        area_modified(CurArea);
        pReset->arg1=new_vnum;
       }
       if ( (new_vnum=(int) get_hash_entry(room_hash,pReset->arg3)) != 0)
       {
        area_modified(CurArea);
        pReset->arg3=new_vnum;
       }
       break;
       
     case 'O':
       if ( (new_vnum=(int) get_hash_entry(obj_hash,pReset->arg1)) != 0)
       {
        area_modified(CurArea);
        pReset->arg1=new_vnum;
       }
       if ( (new_vnum=(int) get_hash_entry(room_hash,pReset->arg3)) != 0)
       {
        area_modified(CurArea);
        pReset->arg3=new_vnum;
       }
       break;
        
     case 'P':
       if ( (new_vnum=(int) get_hash_entry(obj_hash,pReset->arg1)) != 0)
       {
        area_modified(CurArea);
        pReset->arg1=new_vnum;
       }
       if ( (new_vnum=(int) get_hash_entry(obj_hash,pReset->arg3)) != 0)
       {
        area_modified(CurArea);
        pReset->arg3=new_vnum;
       }
       break;
       
     case 'G':
     case 'E':
       if ( (new_vnum=(int) get_hash_entry(obj_hash,pReset->arg1)) != 0)
       {
        area_modified(CurArea);
        pReset->arg1=new_vnum;
       }
       break;
       
     case 'D':
     case 'R':
       if ( (new_vnum=(int) get_hash_entry(room_hash,pReset->arg1)) != 0)
       {
        area_modified(CurArea);
        pReset->arg1=new_vnum;
       }
       break;
   }
     
  }
 }
  
 /* FINISHED */
  
 delete_hash_table(room_hash);
 delete_hash_table(obj_hash);
 delete_hash_table(mob_hash);

 return;  
}
Ejemplo n.º 19
0
int
IpVerify::Verify( DCpermission perm, const condor_sockaddr& addr, const char * user, MyString *allow_reason, MyString *deny_reason )
{
	perm_mask_t mask;
	in6_addr sin6_addr;
	const char *thehost;
    const char * who = user;
	MyString peer_description; // we build this up as we go along (DNS etc.)

	if( !did_init ) {
		Init();
	}
	/*
	 * Be Warned:  careful about parameter "sin" being NULL.  It could be, in
	 * which case we should return FALSE (unless perm is ALLOW)
	 *
	 */

	switch ( perm ) {

	case ALLOW:
		return USER_AUTH_SUCCESS;
		break;

	default:
		break;
	}

	sin6_addr = addr.to_ipv6_address();
	mask = 0;	// must initialize to zero because we logical-or bits into this

    if (who == NULL || *who == '\0') {
        who = TotallyWild;
    }

	if ( perm >= LAST_PERM || !PermTypeArray[perm] ) {
		EXCEPT("IpVerify::Verify: called with unknown permission %d\n",perm);
	}


		// see if a authorization hole has been dyamically punched (via
		// PunchHole) for this perm / user / IP
		// Note that the permission hierarchy is dealt with in
		// PunchHole(), by punching a hole for all implied levels.
		// Therefore, if there is a hole or an implied hole, we will
		// always find it here before we get into the subsequent code
		// which recursively calls Verify() to traverse the hierarchy.
		// This is important, because we do not want holes to find
		// there way into the authorization cache.
		//
	if ( PunchedHoleArray[perm] != NULL ) {
		HolePunchTable_t* hpt = PunchedHoleArray[perm];
		MyString ip_str_buf = addr.to_ip_string();
		const char* ip_str = ip_str_buf.Value();
		MyString id_with_ip;
		MyString id;
		int count;
		if ( who != TotallyWild ) {
			id_with_ip.sprintf("%s/%s", who, ip_str);
			id = who;
			if ( hpt->lookup(id, count) != -1 )	{
				if( allow_reason ) {
					allow_reason->sprintf(
						"%s authorization has been made automatic for %s",
						PermString(perm), id.Value() );
				}
				return USER_AUTH_SUCCESS;
			}
			if ( hpt->lookup(id_with_ip, count) != -1 ) {
				if( allow_reason ) {
					allow_reason->sprintf(
						"%s authorization has been made automatic for %s",
						PermString(perm), id_with_ip.Value() );
				}
				return USER_AUTH_SUCCESS;
			}
		}
		id = ip_str;
		if ( hpt->lookup(id, count) != -1 ) {
			if( allow_reason ) {
				allow_reason->sprintf(
					"%s authorization has been made automatic for %s",
					PermString(perm), id.Value() );
			}
			return USER_AUTH_SUCCESS;
		}
	}

	if ( PermTypeArray[perm]->behavior == USERVERIFY_ALLOW ) {
			// allow if no HOSTALLOW_* or HOSTDENY_* restrictions 
			// specified.
		if( allow_reason ) {
			allow_reason->sprintf(
				"%s authorization policy allows access by anyone",
				PermString(perm));
		}
		return USER_AUTH_SUCCESS;
	}
		
	if ( PermTypeArray[perm]->behavior == USERVERIFY_DENY ) {
			// deny
		if( deny_reason ) {
			deny_reason->sprintf(
				"%s authorization policy denies all access",
				PermString(perm));
		}
		return USER_AUTH_FAILURE;
	}
		
	if( LookupCachedVerifyResult(perm,sin6_addr,who,mask) ) {
		if( deny_reason && (mask&deny_mask(perm)) ) {
			deny_reason->sprintf(
				"cached result for %s; see first case for the full reason",
				PermString(perm));
		}
		else if( allow_reason && (mask&allow_mask(perm)) ) {
			allow_reason->sprintf(
				"cached result for %s; see first case for the full reason",
				PermString(perm));
		}
	}
	else {
		mask = 0;

			// if the deny bit is already set, skip further DENY analysis
		perm_mask_t const deny_resolved = deny_mask(perm);
			// if the allow or deny bit is already set,
			// skip further ALLOW analysis
		perm_mask_t const allow_resolved = allow_mask(perm)|deny_mask(perm);

			// check for matching subnets in ip/mask style
		char ipstr[INET6_ADDRSTRLEN] = { 0, };
   		addr.to_ip_string(ipstr, INET6_ADDRSTRLEN);

		peer_description = addr.to_ip_string();

		if ( !(mask&deny_resolved) && lookup_user_ip_deny(perm,who,ipstr)) {
			mask |= deny_mask(perm);
			if( deny_reason ) {
				deny_reason->sprintf(
					"%s authorization policy denies IP address %s",
					PermString(perm), addr.to_ip_string().Value() );
			}
		}

		if ( !(mask&allow_resolved) && lookup_user_ip_allow(perm,who,ipstr)) {
			mask |= allow_mask(perm);
			if( allow_reason ) {
				allow_reason->sprintf(
					"%s authorization policy allows IP address %s",
					PermString(perm), addr.to_ip_string().Value() );
			}
		}


		std::vector<MyString> hostnames;
		// now scan through hostname strings
		if( !(mask&allow_resolved) || !(mask&deny_resolved) ) {
			hostnames = get_hostname_with_alias(addr);
		}

		for (unsigned int i = 0; i < hostnames.size(); ++i) {
			thehost = hostnames[i].Value();
			peer_description.append_to_list(thehost);

			if ( !(mask&deny_resolved) && lookup_user_host_deny(perm,who,thehost) ) {
				mask |= deny_mask(perm);
				if( deny_reason ) {
					deny_reason->sprintf(
						"%s authorization policy denies hostname %s",
						PermString(perm), thehost );
				}
			}

			if ( !(mask&allow_resolved) && lookup_user_host_allow(perm,who,thehost) ) {
				mask |= allow_mask(perm);
				if( allow_reason ) {
					allow_reason->sprintf(
						"%s authorization policy allows hostname %s",
						PermString(perm), thehost );
				}
			}
		}
			// if we found something via our hostname or subnet mactching, we now have 
			// a mask, and we should add it into our table so we need not
			// do a gethostbyaddr() next time.  if we still do not have a mask
			// (perhaps because this host doesn't appear in any list), create one
			// and then add to the table.
			// But first, check our parent permission levels in the
			// authorization heirarchy.
			// DAEMON and ADMINISTRATOR imply WRITE.
			// WRITE, NEGOTIATOR, and CONFIG_PERM imply READ.
		bool determined_by_parent = false;
		if ( mask == 0 ) {
			if ( PermTypeArray[perm]->behavior == USERVERIFY_ONLY_DENIES ) {
				dprintf(D_SECURITY,"IPVERIFY: %s at %s not matched to deny list, so allowing.\n",who, addr.to_sinful().Value());
				if( allow_reason ) {
					allow_reason->sprintf(
						"%s authorization policy does not deny, so allowing",
						PermString(perm));
				}

				mask |= allow_mask(perm);
			} else {
				DCpermissionHierarchy hierarchy( perm );
				DCpermission const *parent_perms =
					hierarchy.getPermsIAmDirectlyImpliedBy();
				bool parent_allowed = false;
				for( ; *parent_perms != LAST_PERM; parent_perms++ ) {
					if( Verify( *parent_perms, addr, user, allow_reason, NULL ) == USER_AUTH_SUCCESS ) {
						determined_by_parent = true;
						parent_allowed = true;
						dprintf(D_SECURITY,"IPVERIFY: allowing %s at %s for %s because %s is allowed\n",who, addr.to_sinful().Value(),PermString(perm),PermString(*parent_perms));
						if( allow_reason ) {
							MyString tmp = *allow_reason;
							allow_reason->sprintf(
								"%s is implied by %s; %s",
								PermString(perm),
								PermString(*parent_perms),
								tmp.Value());
						}
						break;
					}
				}
				if( parent_allowed ) {
					mask |= allow_mask(perm);
				}
				else {
					mask |= deny_mask(perm);

					if( !determined_by_parent && deny_reason ) {
							// We don't just allow anyone, and this request
							// did not match any of the entries we do allow.
							// In case the reason we didn't match is
							// because of a typo or a DNS problem, record
							// all the hostnames we searched for.
						deny_reason->sprintf(
							"%s authorization policy contains no matching "
							"ALLOW entry for this request"
							"; identifiers used for this host: %s, hostname size = %lu, "
							"original ip address = %s",
							PermString(perm),
							peer_description.Value(),
							(unsigned long)hostnames.size(),
							ipstr);
					}
				}
			}
		}

		if( !determined_by_parent && (mask&allow_mask(perm)) ) {
			// In case we are allowing because of not matching a DENY
			// entry that the user expected us to match (e.g. because
			// of typo or DNS problem), record all the hostnames we
			// searched for.
			if( allow_reason && !peer_description.IsEmpty() ) {
				allow_reason->sprintf_cat(
					"; identifiers used for this remote host: %s",
					peer_description.Value());
			}
		}

			// finally, add the mask we computed into the table with this IP addr
			add_hash_entry(sin6_addr, who, mask);			
	}  // end of if find_match is FALSE

		// decode the mask and return True or False to the user.
	if ( mask & deny_mask(perm) ) {
		return USER_AUTH_FAILURE;
	}

	if ( mask & allow_mask(perm) ) {
		return USER_AUTH_SUCCESS;
	}

	return USER_AUTH_FAILURE;
}
Ejemplo n.º 20
0
void count_flow (const struct ip * ip,uint16_t iplen,uint32_t tspkt) {
	flow tmp, tmp_peer;
	flow * flowrec=NULL;

	uint16_t hsize,psize,sslsize;
	uint32_t seq;
	struct tcphdr *t;
	int direction=0;

	t=(struct tcphdr *)(((uint8_t*)ip) + ip->ip_hl*4);


	hsize=ip->ip_hl*4+t->doff*4;
	psize=ntohs(ip->ip_len)-hsize;

	seq=ntohl(t->seq);


	if (psize>0) {
	//	If this packet is a data packet, we look for its connection in the hashtable
		/* Set the key to the source/destination address pair. */
		tmp.src.s_addr = ip->ip_src.s_addr;
		tmp.dst.s_addr = ip->ip_dst.s_addr;
		tmp.sport = ntohs (t->source);
		tmp.dport = ntohs (t->dest);

		// Peer
		tmp_peer.src.s_addr = tmp.dst.s_addr;
		tmp_peer.dst.s_addr = tmp.src.s_addr;
		tmp_peer.sport = tmp.dport;
		tmp_peer.dport = tmp.sport;

		direction=0;
		if (flowrec = (flow *) find_hash_entry (flow_hash, &tmp)) {
			direction=1;
		} else if (flowrec = (flow *) find_hash_entry (flow_hash, &tmp_peer)) {
			direction=2;
		}
		

		if (direction>0 && flowrec->label==LABEL_NONE) {
		//	Connection exists, is classifiable (SYN seen) and in sequence
			
			//First we reorder the list of connection (connections with most recent packets at the end of the list)
			if (flowrec->next) {
				//Else connection is already the last in the last so no modification
				//Connection cannot be the first we have a special empty pointer for the head of the list (first_flow)
				flowrec->prev->next=flowrec->next;
				flowrec->next->prev=flowrec->prev;
				active_flows->next=flowrec;
				flowrec->prev=active_flows;
				flowrec->next=NULL;
				active_flows=flowrec;
			}
			flowrec->ts_latest=tspkt;

			if (direction==1) {
				if (flowrec->data1==0) {
					//First packet with data
					flowrec->data1=1;
					flowrec->nextseq=seq+psize;
				} else if (flowrec->nextseq!=seq) {
					//Out of sequence packet, we remove the entry for this connection
					flowrec->label=LABEL_OUTOFSEQ;
					if (memory>=OPT_NOSTORE) {
						print_flow(flowrec);
						clear_hash_entry(flow_hash,flowrec);
						return ;
					}
				} else {
					flowrec->nextseq+=psize;
				}
			}
			if (direction==2) {
				if (flowrec->data2==0) {
					//First packet in reverse direction, initialize sequence number
					flowrec->data2=1;
					flowrec->nextseqpeer=seq+psize;
				} else if (flowrec->nextseqpeer!=seq) {
					flowrec->label=LABEL_OUTOFSEQ;
					if (memory>=OPT_NOSTORE) {
						print_flow(flowrec);
						clear_hash_entry(flow_hash,flowrec);
						return ;
					}
				} else {
					flowrec->nextseqpeer+=psize;
				}
			}
	
			if (!flowrec->isSSL) {
				assert(flowrec->datapkts<pktlimit);
				flowrec->pkt_sizes[flowrec->datapkts]=(direction==1) ? psize : -psize;
				flowrec->datapkts+=1;
				analyze(flowrec);
			}
			
			//If packet is SSL we parse it
			if (flowrec->isSSL) {
				sslsize=0;
				if (!flowrec->SSLdata) {
					sslsize=sslpayload(((uint8_t*)t)+t->doff*4,psize,iplen-hsize);
					if (sslsize>0) flowrec->SSLdata=1;
				} else sslsize=psize-5; 	//5: length of SSL header

				if (flowrec->SSLdata) {
					assert(flowrec->datapkts<pktlimit);
					flowrec->pkt_sizes[flowrec->datapkts]=(direction==1) ? clearsize(sslsize) : -clearsize(sslsize);
					flowrec->datapkts+=1;
					analyze(flowrec);
				}
			}
		}
		// Else: Connection is not classifiable: ignored
	} else if (t->syn==1 && t->ack==0) {
		// We are only intersted in SYN packets when there is no data

		flowrec = (flow *) malloc (sizeof (flow));
		if (flowrec == NULL) {
			fprintf (stderr, "Can't malloc new connection\n");
			abort ();
		}

		flowrec->pkt_sizes=(int16_t *)malloc(pktlimit*sizeof(int16_t));
		if (flowrec->pkt_sizes == NULL) {
			fprintf (stderr, "Can't malloc new connection\n");
			abort ();
		}
		flowrec->label=LABEL_NONE;

		bzero(flowrec->pkt_sizes,pktlimit*sizeof(int16_t));
		
		flowrec->src.s_addr=ip->ip_src.s_addr;
		flowrec->dst.s_addr=ip->ip_dst.s_addr;
		flowrec->sport=ntohs (t->source);
		flowrec->dport=ntohs (t->dest);

		flowrec->nextseq=0;
		flowrec->nextseqpeer=0;
		flowrec->data1=0;
		flowrec->data2=0;

		flowrec->isSSL=0;
		flowrec->SSLdata=0;

		flowrec->datapkts=0; 
		flowrec->ts_latest=tspkt;

		flowrec->prev = active_flows;
		flowrec->next = NULL;

		active_flows->next = flowrec;
      		active_flows = flowrec;

      		nbflows++;

      		add_hash_entry (flow_hash, flowrec);
	} else if (memory>=OPT_GARBAGE && (t->rst || t->fin)) {
	// We remove connections in which we see RST or FIN

		/* Set the key to the source/destination address pair. */
		tmp.src.s_addr = ip->ip_src.s_addr;
		tmp.dst.s_addr = ip->ip_dst.s_addr;
		tmp.sport = ntohs (t->source);
		tmp.dport = ntohs (t->dest);

		// Peer
		tmp_peer.src.s_addr = tmp.dst.s_addr;
		tmp_peer.dst.s_addr = tmp.src.s_addr;
		tmp_peer.sport = tmp.dport;
		tmp_peer.dport = tmp.sport;
		
		flowrec = (flow *) find_hash_entry (flow_hash, &tmp);
		if (!flowrec) flowrec = (flow *) find_hash_entry (flow_hash, &tmp_peer);
		if (flowrec) {
			print_flow(flowrec);
			clear_hash_entry(flow_hash,flowrec);
			return ;
		}
	}
	flowrec;
}