Selecting Specific Protocols
If the third argument of the socket
call is 0
,
socket
selects a default protocol to use with the returned
socket of the type requested. The default protocol is usually correct, and alternate
choices are not usually available. When using raw sockets to communicate directly
with lower-level protocols or lower-level hardware interfaces, set up
de-multiplexing with the protocol argument. For more information, see the
socket
(3C) man page.
Using raw sockets in the Internet family to implement a new
protocol on IP ensures that the socket only receives packets for the
specified protocol. To obtain a particular protocol, determine the
protocol number as defined in the protocol family. For the Internet
family, use one of the library routines that are discussed in Standard Routines, such as
getprotobyname
.
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> ... pp = getprotobyname("newtcp"); s = socket(AF_INET6, SOCK_STREAM, pp->p_proto);
Using getprotobyname
()
results in a socket
s
by using a stream-based connection, but with a
protocol type of newtcp
instead of the default
tcp
.