Thread: how do firewalls work ? c socket question

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    how do firewalls work ? c socket question

    my whole motivation for c programming is because i want to be able to make handy things like netcat and nmap etc .. im really not trying to be a hacker or anything gay like that. I just find that stuff interesting.

    but just doing the basic socket stuff im doing at the minute i was wondering how you would go about programming a firewall.

    I mean the only way i could think would be .. well i dont even know, do you have to create a socket on every port and listen for traffic? that sounds stupid so i doubt its that.

    i just dont understand how it can monitor every port at the same time and report on it

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Actually you can get below the port level.
    Programming with pcap
    Worth doing if you are interested in networking. This is how packet sniffers such as "wireshark" work:
    http://en.wikipedia.org/wiki/Packet_analyzer
    It is not hard to write a basic sniffer in C, 200-300 lines.

    I do not know much about firewalls, but I have heard that some of them work this way and some of them do not.
    Last edited by MK27; 04-18-2010 at 06:47 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    thanks for the info mk.

    Hey my other thread the code is quite long, i have simplified it here wondering if you can have a look, its very basic. I cant work out what the problem is though

    i have a function

    i pass it two variables makeConnection("127.0.0.1", 3000)

    get the error cannot preform socket action on non socket. its really frustrating me now.

    Code:
    int makeConnection(char *ip, int port){
    	int s;
    	struct sockaddr_in connectto;
    	if(s = socket(AF_INET,SOCK_STREAM,0) == -1) 
    			bail("Socket Error"); 
    
    	memset(&connectto,0,sizeof(connectto));
    	connectto.sin_family = AF_INET;
    	connectto.sin_addr.s_addr = inet_addr(ip);
    	connectto.sin_port = htons(port);
    	
    	if (connect(s,(struct sockaddr *)&connectto,sizeof(connectto)) < 0) 
    		bail("Connection Error");
    
    	//we have connected.
    	printf("We have connected.\n\n");
    
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's the same mistake you made before:
    Code:
    	if(s = socket(AF_INET,SOCK_STREAM,0) == -1) 
    			bail("Socket Error");
    Without some parentheses around the assignment, this assigns to s the truth of the comparison "socket(AF_INET,SOCK_STREAM,0) == -1". So if it does not equal -1, which it won't if it succeeds, s will be assigned false (0), because the socket call != -1.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    im stupid... lol thanks again for your help i appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket declaration just doesn't work
    By sean in forum C# Programming
    Replies: 3
    Last Post: 06-19-2004, 12:10 PM
  2. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  3. Firewalls - how do they work?
    By nickname_changed in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-14-2003, 02:56 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. Question about socket programming
    By Bill in forum C Programming
    Replies: 3
    Last Post: 09-05-2001, 03:49 AM