LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2010, 12:30 AM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165
Blog Entries: 1

Rep: Reputation: 22
Help hashing ip sockets


Hello again,

I have a very bad attempt at hashing the components of an tcp session to assign/locate the session in a hash table bucket. I am pretty sure that it has a very high collision rate and when there are a very large number of tcp sessions my application is having to search a long linked list to find the session within the bucket.

All the hashing functions I have found take a single string input where I need to input several integers and hash them into a single result. My guess is that any real hashing function is going to produce better results than what I am currently doing.

Code:
/*
 * Calculates the hash of a session provided the IP addresses, and ports.
 */

static inline __u16
sessionhash(__u32 largerIP, __u16 largerIPPort, 
__u32 smallerIP, __u16 smallerIPPort){

        __u16 hash1 = 0, hash2 = 0, hash3 = 0, hash4 = 0, hash = 0;

        hash1 = (largerIP ^ smallerIP) >> 16;
        hash2 = (largerIP ^ smallerIP);
        hash3 = hash1 ^ hash2;
        hash4 = largerIPPort ^ smallerIPPort;
        hash = hash3 ^ hash4;
        return hash;
 }
Thanks.

Last edited by yaplej; 11-10-2010 at 12:50 AM.
 
Old 11-10-2010, 06:19 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Check out the download section here: http://www.partow.net/programming/hashfunctions/ (I just found it with google, but the writeup has plenty of references..)

A reasonable hit for hasing ip addresses is here: http://www.derkeiler.com/Newsgroups/.../msg00397.html

and you could look up CRC, which is a common (set of?) checksum algorithm which could be used for hashing
 
1 members found this post helpful.
Old 11-11-2010, 08:19 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I should have added, concatenating all the input data before hashing it is a reasonable way to get a combined hash. If you hash the component parts and XOR the hashes, that works unless your hash function works better for some output bit positions than others. (E.g., it wouldn't work well if the hash function makes more "random" high order bits than low order bits, or vice versa.) XORing the hashes of the components also has the property that swapping the contents of two of the components doesn't affect the final hash, which is iffy.

Last edited by aluser; 11-11-2010 at 08:24 PM. Reason: addendum
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dc++ client with tth hashing EmOuBi Linux - Software 3 11-30-2008 07:47 PM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
/etc/shadow - passwords not hashing erics_acvw Linux - Security 1 10-31-2006 03:08 AM
linux password hashing indienick Programming 5 05-18-2006 02:12 PM
Linux and MD5 hashing GAVollink Programming 0 06-04-2003 01:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration