0

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.

anbocode
  • 53
  • 6
  • 1
    It is not generic - it is different for each protocol. What is the error you get? – user253751 May 20 '22 at 09:36
  • 1
    I'm surprised this is an allowed way to create sockets. I've never heard of an ICMP DGRAM socket. Normally RAW sockets are used for ICMP – user253751 May 20 '22 at 10:21
  • @user253751 I have created PING and Traceroute programmes completly using raw sockets. Both works correctly. But since there are other options (like IPPROTO_ICMP, IPPROTO_RAW), I wanted to experiment with these. – anbocode May 20 '22 at 10:26

2 Answers2

1

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.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • ***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.*** This is what I am looking for.Not only protocol but also type of the sockets. By the way I am using Linux system. – anbocode May 20 '22 at 10:29
  • "By the way I am using Linux" - that's not a clarification, it's an entirely different question! I've answer the question you asked, and if you instead want to ask "what address family and protocol combinations are supported on Linux", then you should do so explicitly. Either edit your question or post a new one. – Useless May 20 '22 at 12:28
1

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.

user253751
  • 57,427
  • 7
  • 48
  • 90