
-----------------------------------
Justin_
Thu Mar 16, 2006 3:27 am

linux raw packets (c)
-----------------------------------
Hi, I don't know how to get this code to compile in FC4.  I'm using GCC, I'm not sure what includes I need.



#define P 2123		/* lets flood the sendmail port */

unsigned short		/* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
  unsigned long sum;
  for (sum = 0; nwords > 0; nwords--)
    sum += *buf++;
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  return ~sum;
}

int 
main (void)
{
  int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);	/* open raw socket */
  char datagram

-----------------------------------
Andy
Thu Mar 16, 2006 8:34 am


-----------------------------------
I didnt have time to go through your entire code, but why are you declaring your main as void, and still returning 0? and why are there brackets around this?


  {                                /* lets do it the ugly way.. */
    int one = 1;
    const int *val = &one;
    if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
      printf ("Warning: Cannot set HDRINCL!\n");
  }

-----------------------------------
Justin_
Thu Mar 16, 2006 1:05 pm


-----------------------------------
its not my code, i'm reading a raw packet tutorial so it was simply copy and paste.  I figured the code would be correct...  

Oh well, thanks Andy.

-----------------------------------
wtd
Thu Mar 16, 2006 1:15 pm


-----------------------------------
As far as I can tell, it is correct.  It's just tremendously ugly.  But then, it is C.  ;)

Is there some reason you couldn't do this in a language with a higher level networking library?

-----------------------------------
Justin_
Thu Mar 16, 2006 1:29 pm


-----------------------------------
Yes, I am learning raw sockets.  I want to know every detail that goes into a packet.  I have a decent non-programming understanding as it is, but now I want to put that understanding into some application via programming.  

the line: 

Int main (void) 

You say its correct.  I don't understand it, and it appears neither did Andy.  But I do see that main is returning an int.  

And about compiling it, I've never compiled anything in linux before so I need major help there.

-----------------------------------
wtd
Thu Mar 16, 2006 1:36 pm


-----------------------------------
int main(void)

Declares a function named main that returns int, and has a void parameter list.

I suggest you read "man gcc".

-----------------------------------
Justin_
Thu Mar 16, 2006 1:52 pm


-----------------------------------
Could int main (void) be written int main() then?

I've done a fair share of reading in my day and the only thing it taught me is much more than I needed to know.  It would be more pleasurable for me if I could get a straight cut to the chase, but I understand if you don't want to help.  I'm just going to have to post my question at other forums   :)

-----------------------------------
wtd
Thu Mar 16, 2006 1:57 pm


-----------------------------------
Could int main (void) be written int main() then?

Yes.

I've done a fair share of reading in my day and the only thing it taught me is much more than I needed to know.  It would be more pleasurable for me if I could get a straight cut to the chase, but I understand if you don't want to help.  I'm just going to have to post my question at other forums   :)

I could write a huge long tutorial on how to use all of GCC's options, but then it'd be as long as the manpage, and probably not as well written.

And honestly, the subject doesn't interest me that much.

The basic compilation:

gcc -o output_file source_file1.c source_file2.c source_fileN.c

And if you need custom include paths, use the -I (capital i) option.  If you need to include a library, use the -l (lowercase L) option.

-----------------------------------
Justin_
Thu Mar 16, 2006 2:02 pm


-----------------------------------
yeah that's pretty much all i needed.  But, what includes do I need for a program like this in linux.  

P.S. I tried using a -I in windows because one of my header files was in a different directory, I gave it an absolute path and it didn't work.  The syntax I used was like this:

-If:\Documents and Settings\  

is it possible to give -I an absolute path?

-----------------------------------
wtd
Thu Mar 16, 2006 2:07 pm


-----------------------------------
GCC may be interacting poorly with the way Windows handles filesystems.  *nix-y environments have a single root, but Windows has multiple roots.  This is a fairly fundamental schism.  I would suggest sticking to Linux for now so that you can focus on the networking voodoo, rather than unbreaking Windows.

-----------------------------------
wtd
Thu Mar 16, 2006 2:10 pm


-----------------------------------
You may wish to try:

-lsocket

-----------------------------------
Justin_
Thu Mar 16, 2006 2:21 pm


-----------------------------------
Yeah I just noticed the file system issues with -I on windows.  It loses you when you put a space.  

In linux i tried to compile the above code like this: 

gcc rawSocket.c -lsocket


But a number of things came up undeclared.

-----------------------------------
md
Thu Mar 16, 2006 2:25 pm


-----------------------------------
I'm curious as to why you'd want to learn this stuff anyways. Unless your writing a IP stack or implementing a new protocol this level of code is nearly useless. 

The reason the socket libraries exist is to make dealing with sockets easy ;)

-----------------------------------
wtd
Thu Mar 16, 2006 2:44 pm


-----------------------------------
Yeah I just noticed the file system issues with -I on windows.  It loses you when you put a space.

Surround paths with spaces with quotes.

-----------------------------------
Justin_
Thu Mar 16, 2006 6:45 pm


-----------------------------------

I'm curious as to why you'd want to learn this stuff anyways.


Because I'm curious as to how sockets work.  



Surround paths with spaces with quotes.

  

Right, thanks.  

So I guess you don't know the includes it needs?  And here's another question for you.  In windows ctrl-c = copy.  Is there a keyboard short cut like this for linux?  I'm guessing no, because it depends on the current thread that's running.

-----------------------------------
md
Thu Mar 16, 2006 6:50 pm


-----------------------------------
So I guess you don't know the includes it needs?  And here's another question for you.  In windows ctrl-c = copy.  Is there a keyboard short cut like this for linux?  I'm guessing no, because it depends on the current thread that's running.

What editor are you using? There is almost certainly copy functionality; though the keys might be different.

-----------------------------------
Justin_
Thu Mar 16, 2006 8:33 pm


-----------------------------------
emacs, but I was really asking for a universal copy shortcut, for shell and everything.  I didn't think there was one.  When I think of linux I think of the most user unfriendly environment imaginable.  The one good thing about commercial software is it adheres to standards.

-----------------------------------
wtd
Thu Mar 16, 2006 8:37 pm


-----------------------------------
So, why use emacs?  Just edit in something like gedit and then use the shell to compile.  You can have your cake and eat it too.

-----------------------------------
Justin_
Fri Mar 17, 2006 3:05 am


-----------------------------------
I don't understand what I'd be gaining if I edited in Gedit but thanks anyway.

-----------------------------------
wtd
Fri Mar 17, 2006 3:08 am


-----------------------------------
A decent GUI environment?  The keyboard shortcuts you're used to?

-----------------------------------
Justin_
Fri Mar 17, 2006 4:16 am


-----------------------------------
What keyboard shortcuts?  I specifically asked if there were any.  Wow.

-----------------------------------
wtd
Fri Mar 17, 2006 4:41 am


-----------------------------------
Gedit is a GUI text editor.  It's standard issue with recent versions of the Gnome desktop environment, and from screenshots, it appears you're using that.

Remember, the GUI and the command-line are not entirely separate.  They both deal with the same filesystem.

-----------------------------------
Justin_
Fri Mar 17, 2006 4:15 pm


-----------------------------------
I know this.  I'm sorry you're confused what my question actually is.  

My questions that remain unanswered are: 

What headers do I need to compile that raw socket code example in linux?

And on the side: 

In linux, are their keyboard shortcuts for copy and paste like ctrl-c in windows?

-----------------------------------
wtd
Fri Mar 17, 2006 4:18 pm


-----------------------------------
There weren't include directives in this code you downloaded?  Because before it seemed you were asking about which libraries you needed to link against, and now it seems you're asking about headers.  These are very different things.

-----------------------------------
md
Fri Mar 17, 2006 4:29 pm


-----------------------------------
In linux, are their keyboard shortcuts for copy and paste like ctrl-c in windows?

As I tried to point out it depends on what program you are using. If your using something like gedit then the keys are crtl-c and crtl-v just like windows. If your using something else try reading the man pages or using google.

-----------------------------------
Justin_
Fri Mar 17, 2006 5:35 pm


-----------------------------------
That sucks Cornflake.  Truly, I mean imagine having to learn new hotkeys for every single program just because people can't devise one standard.

Well before I phrased the question "what includes do I need"  and I figured that was explicit enough considering include is the keyword for including headers.  

For example:  

#include   

what do I include for this program in linux?

-----------------------------------
md
Sat Mar 18, 2006 10:39 am


-----------------------------------
That sucks Cornflake.  Truly, I mean imagine having to learn new hotkeys for every single program just because people can't devise one standard.

Well before I phrased the question "what includes do I need"  and I figured that was explicit enough considering include is the keyword for including headers.  

For example:  

#include   

what do I include for this program in linux?

No, it makes sense when you look at the differences between linux and windows. On linux there are many many character/console based programs, and in those programs ctrl+whatever is almost always an important escape sequence that can't be over-written, or is better overwritten with something more inline with what hte original escape sequence is supposed to do. For instance ctrl-c causes most programs to exit. Making it instead copy text would just confuse habitual linux users. When you use graphical tools like gedit or any of the other X based editors copy and paste are almost always the same as their windows counter-parts as escape sequences aren't as integral in a graphical enviroment.

If you don't want to take the time to learn the commands for emacs then use something else. Complaining because you are lazy seems pretty poor to me.

As for the include I would imagine that whatever tutorials you were using would tell you. Perhaps reading them instead of just swiping the code might be a good idea. 'Course it's really not that hard to guess it's something like sockets.h

-----------------------------------
Justin_
Sat Mar 18, 2006 1:22 pm


-----------------------------------
lol, you win, i'll stop posting here.  It's getting to be a bad experience for me with all the bull shit.
