views:

46

answers:

2

Hello guys,

I am a beginner to networking and I have a few questions regarding networking. 1)How can a process execute code that is sent from a different computer on the network. Generally a process's code segment cannot be changed once its loaded to ensure protection. (Also I can execute some arbitrary code to corrupt the process's memory) 2)Also can a process hear to multiple ports ? And multiple processes can hear to a same port ? For example two https associated with port 80. How to distinguish between the processes and how to ensure protection ? 3)Also I would like to know how listen is implemented in sockets. Are they implemented as software interrupts ?

Any good book recommendations are very much appreciated.

Thanks & Regards,

Mousey.

+2  A: 

Q: How can a process execute code sent from another machine?
A: Generally, this is a bad idea as the security concerns are difficult to fully explore. However, this can be done by saving the network-delivered code to a separate executable and then launching this new program. This can also be done on most systems by just treating the raw bytes received as code; load the bytes into the heap (not the stack!), cast the address to a function pointer, and call it. Again though, this is almost certainly a bad idea.

Q: Can a process listen on multiple ports simultaneously?
A: Yes. By the way, HTTPS is port 443. HTTP is port 80.

Q: Can multiple processes listen on the same port (with the same protocol, on the same address)?
A: No. Other processes might be able to eavesdrop and also receive the packets, but they're not directly bound to the port. In general, only one process can be bound to a given protocol/port/address 3-tuple.

Q: How is blocking while listening on a socket implemented?
A: By the operating system, in its own fashion. Generally a thread is moved into the "blocking" state when it calls accept, read, or poll/select on a non-ready socket, and will not receive CPU time until some data have arrived.

Borealid
If so how can my google chrome firefox able to get http requests at the same time.
mousey
@mousey They are not web servers, so they don't listen on port 80. The other end does.
calmh
To clarify what calmh said above, Chrome and Firefox are listening on (different) random high-numbered ports, not on port 80.
Borealid
@Borealid Thank you very much.
mousey
+1  A: 

1)How can a process execute code that is sent from a different computer on the network. Generally a process's code segment cannot be changed once its loaded to ensure protection.

This has nothing to do with networking. Once you receive the data through a socket, it's in your local memory. What you do after that is OS-specific. For example, on Windows, you can use VirtualProtect to mark pages as executable.

2)Also can a process hear to multiple ports ?

Sure, just create a different socket for each port you want to listen to. Of course, to use them simultaneously, you either need to use non-blocking sockets or run each socket in a separate thread.

3)Also I would like to know how listen is implemented in sockets. Are they implemented as software interrupts ?

This is entirely OS-specific. listen just sets up the socket so that it can accept connections. Any connection requests that arrive after this (this probably happens somewhere in the TCP/IP driver) are put in a queue by the OS. When you later call accept, the OS pulls out the first pending connection from this queue and returns a socket to that.

casablanca
Actually, I don't think `listen` is a blocking call at all. You may be thinking of `accept`.
Borealid
yes listen is not a blocking call. Accept or recv are all blocking
mousey
Oops, fixed. I don't know what I was thinking when I wrote that. :)
casablanca