Home > Blockchain >  How to create a socket as per the specific need by selecting correct socket family, type and protoco
How to create a socket as per the specific need by selecting correct socket family, type and protoco

Time:05-21

I am doing some network programming stuff with python. I know that sockets can be created like below...

For TCP sockets

socket.socket(
            family = socket.AF_INET,
            type   = socket.SOCK_STREAM,
            proto  = socket.IPPROTO_IP,
            fileno = None)

socket.socket(
            family = socket.AF_INET,
            type   = socket.SOCK_STREAM,
            proto  = socket.IPPROTO_TCP,
            fileno = None)

For UDP sockets

socket.socket(
            family = socket.AF_INET,
            type   = socket.SOCK_DGRAM,
            proto  = socket.IPPROTO_IP,
            fileno = None)

socket.socket(
            family = socket.AF_INET,
            type   = socket.SOCK_DGRAM,
            proto  = socket.IPPROTO_TCP,
            fileno = None)

For raw sockets to capture all packets...

socket.socket(
        family = socket.AF_PACKET,
        type   = socket.SOCK_RAW,
        proto  = socket.htons(0x0003),
        fileno = None
    )

Mostly I have been doing TCP, UDP and raw sockets.Apart from TCP and UDP there are many other protocols and Python socket library has support for these other protocols as well. But when I tried to do some works with some other protocols, things are not always correct. Sometimes while creating a socket, everything looks correct but gives error.I have many confusions on which combination of family, type and protocol will fit together correctly while creating a socket. I am consciously not giving any specific code sample here to better explain my problem, instead I am looking for the networking and protocol related logic and knowledge which is required to properly create a socket.

Please kindly share some views and comments on this topic.

CodePudding user response:

The documentation says:

The Python interface is a straightforward transliteration of the Unix system call and library interface for sockets ...

So you should read the documentation for your system's underlying socket library.

For example, my local Linux socket(3P) manpage says:

If the protocol argument is non-zero, it shall specify a protocol that is supported by the address family. [...] The protocols supported by the system are implementation-defined.

You haven't told us what your underlying system is, but that's where you should start looking.

CodePudding user response:

You can't mix and match parameters. Perhaps when they designed the socket API back in 1983, they were thinking you could, but it didn't work out that way.

You have to look at the specific type of socket you want to create, and then use the parameters it says.

I've never heard of a DGRAM ICMP socket and the documentation for icmp doesn't say you can create one. For ICMP you need to use SOCK_RAW, which gives you a raw IP socket.

It seems that DGRAM ICMP is an option in Linux, but it needs special setup to make it work, and it's not well documented: ICMP sockets (linux) and apparently, it doesn't work for you. It is a different type of socket and it will behave differently to a raw socket. It should not be expected to work the same.

  • Related