Bentley4
Bentley4

Reputation: 11038

Why does python specify IPv4 or IPv6 with SOCK_STREAM or SOCK_DGRAM when it also accepts `proto` in the instantiation of socket objects?

Why does python specify IPv4 or IPv6 with SOCK_STREAM or SOCK_DGRAM when it also accepts protocol number arguments in the instantiation of the socket object (nrs 4 and 41 ,see list of protocol numbers)? (as proto in socket.socket([family[, type[, proto]]])) Doesn't that make the family argument useless?

Upvotes: 1

Views: 615

Answers (2)

FatalError
FatalError

Reputation: 54591

proto in this context refers to the Layer 4 (L4) protocol to use. Most likely this will be IPPROTO_TCP (TCP) or IPPROTO_UDP (UDP). In practice it's unlikely you will need to pass these parameters explicitly very often, as for IP generally SOCK_STREAM implies TCP and SOCK_DGRAM implies UDP.

It could come in handy for instance, however, if say you wanted to implement ICMP support in your program using raw sockets. Then you'd pass SOCK_RAW and IPPROTO_ICMP. Also, keep in mind this is based on the Berkeley sockets API which is designed to handle much more than just IP networking (take a look at all the other AF_* from the socket module to get an idea of what it supports).

In contrast, IPv4/IPv6 is a Layer 3 (L3) protocol and works independently from TCP/UDP.

EDIT:

As to why you see IPv4/IPv6 encapsulation in that list, it's to support protocols like IP in IP or 6in4. It's not likely you will deal directly with these mechanisms in your program.

Upvotes: 1

Foon
Foon

Reputation: 6468

Nope. Number 4 and 41 in protocol list you linked to clearly state "IPv4 encapsulation" and "IPv6 encapsulation" You would use IPv4 with IPV6 encapsulation for example to generate packet which are then de-capsulated (unless that's not actually a word) upstream e.g. at an IPv6 over IPv4 tunnel

Upvotes: 1

Related Questions