5

I am currently working on a programming assignment. The assignment is to implement a client,network emulator, and server. The client passes packets to a network emulator, and the network emulator passes to the server. Vice-versa applies as well. The prerequisite for the assignment is that I may only use raw sockets. So I will create my own IP and UDP headers. I have tested my packets with wireshark. They are all correct and in the proper format(it reads them properly).

Another requirement is that the emulator, client and server all have specific ports they must be bound to. Now, I do not understand how to bind a raw socket to a specific port. All my raw sockets receive all traffic on the host address they are bound to. According to man pages, and everywhere else on the internet, including "Unix Network Programming" by Richard Stevens, this is how they are supposed to work. My teacher has not responded to any of my emails and I probably will not be able to ask him until Tuesday.I see two options in front of me. First I can use libpcap to filter from a specific device and then output to my raw socket. I feel this is way out of scope for our assignment though. Or I can filter them after I receive them from the socket. This apparently has a lot of overhead because all the packets are being copied/moved through the kernel. At least, that is my understanding(please feel free to correct me if i'm wrong).

So my question is: Is their an option or something I can set for this? Where the raw socket will bind to a port? Have I missed something obvious?

Thank you for your time.

--

user614885
  • 141
  • 2
  • 2
  • 9

2 Answers2

5

The man page for raw(7) says:

A raw socket can be bound to a specific local address using the bind(2) call. If it isn't bound all packets with the specified IP protocol are received. In addition a RAW socket can be bound to a specific network device using SO_BINDTODEVICE; see socket(7).

Edit: You cannot bind a raw socket to a specific port because "port" is a concept in TCP and UDP, not IP. Look at the header diagrams for those three protocols and it should become obvious: you are working at a lower level, where the concept of port is not known.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • So, to make sure I am interpreting this correctly, I am unable to bind to a specific port? If that is the case, how would you implement the "filter"? Libpcap to a specific device and filter the ports or since this is only for an assignment just filter them out after the socket receives them? Or do you perhaps have a better way to implement in mind? Thank you for your response and time by the way. – user614885 Feb 14 '11 at 02:48
  • See the edit in my answer. I think you are on the right track. – John Zwinck Feb 14 '11 at 02:58
  • A "port" is a construct of the transport protocol. From the network level (IP), packets are addressed to addresses. It is the job of the higher level, transport protocols to implement the "port" semantics. I'd imagine you have to decode the received packets yourself, to determine which "port" it is addressed to and filter accordingly. – Santa Feb 14 '11 at 02:59
  • and Santa, thank you for the help. I'm going to go ahead and do it in the program with a comparison of the port field. I had another question as well. I'm new to the board and I was wondering do I mark this question answered or something? – user614885 Feb 14 '11 at 18:43
2

I would think you're expected to filter the packets in your software. It sounds like the exercise is to learn what the different components of the IP stack do by recreating a simplified piece of it in user space. Normally in the kernel, the IP code would process all packets, verify the IP headers, reassemble fragments, and check the protocol field. If the protocol field is 17 (udp), then it passes it to the UDP code (every UDP packet). It's up to the UDP code to then validate the UDP header and determine if any applications are interested in them based on the destination port.

I imagine your project is expected to more or less mimic this process. Obviously none of it will be as efficient as doing it in the kernel, but since the assignment is to write (part of) an IP stack in user-space, I'd guess efficiency isn't the point of the exercise.

eater
  • 2,697
  • 1
  • 21
  • 24