1

Question regarding creating the socket file for the AF_INET.

For AF_UNIX talking within the same host, we create socket file example /tmp/name_of_the_socket_file

How about AF_INET, does it requires socket file to be created?

AFAIK, I don't need to create socket file for AF_INET and I can communicate with simple client and server model.

My colleague told me I need to create socket file for the AF_INET with IP address as the file name, I don't understand that someone can clarify me?

  • What do you mean by "create a socket file" ? Are you talking about compiled code or scripts? Or anything else? Do you have code examples to discuss as a basis? – user86969 Feb 28 '18 at 13:36
3

My colleague told me I need to create socket file for the AF_INET

Your colleague is wrong. Taking a look at manpages of bind(2) you can see that different socket types have rules:

The rules used in name binding vary between address families. Consult the manual entries in Section 7 for detailed information. For AF_INET, see ip(7); for AF_INET6, see ipv6(7); for AF_UNIX, see packet(7); for AF_X25, see x25(7); and for AF_NETLINK, see netlink(7).

You will see at ip(7) that there is no invocation involving AF_INET that can create a file. There is also this great IBM paper about this socket structure and some history about it. Legacy structure form(BSD 4.4/Unix 98):

struct sockaddr_in {
  uint8_t         sin_len;
  sa_family_t     sin_family;
  u_short         sin_port;
  struct in_addr  sin_addr;
  char            sin_zero[8];
};

Current struct:

struct sockaddr_in {
    sa_family_t    sin_family; /* address family: AF_INET */
    in_port_t      sin_port;   /* port in network byte order */
    struct in_addr sin_addr;   /* internet address */
};

/* Internet address. */
struct in_addr {
    uint32_t       s_addr;     /* address in network byte order */
};

See, no mention of file creation whatsoever.

Quoting other part of ip(7) manpages about how this kind of socket(AF_INET) works:

When a process wants to receive new incoming packets or connections, it should bind a socket to a local interface address using bind(2). In this case, only one IP socket may be bound to any given local (address, port) pair. When INADDR_ANY is specified in the bind call, the socket will be bound to all local interfaces. When listen(2) is called on an unbound socket, the socket is automatically bound to a random free port with the local address set to INADDR_ANY. When connect(2) is called on an unbound socket, the socket is automatically bound to a random free port or to a usable shared port with the local address set to INADDR_ANY.

--snip--

Address format

An IP socket address is defined as a combination of an IP interface address and a 16-bit port number. The basic IP protocol does not supply port numbers, they are implemented by higher level protocols like udp(7) and tcp(7). On raw sockets sin_port is set to the IP protocol.

But if you look at unix(7) manpages you will see some examples of AF_UNIX socket type and its basic scructure. There is even a field called sun_path that is the path for socket files(see Related Stuff below):

#define UNIX_PATH_MAX    108

struct sockaddr_un {
  sa_family_t sun_family;               /* AF_UNIX */
  char        sun_path[UNIX_PATH_MAX];  /* pathname */
};

tl,dr: AF_UNIX is for sockets(and they use files) while AF_INET is for binding to ip addresses, and creating communications on its various forms(unicast, multicast, broadcast...).

Related Stuff:

  • Thanks, @nwildner, but my colleague is not convinced what I said. Now I have the correct info and it clears my confusion. – danglingpointer Feb 28 '18 at 13:56
  • 1
    @LethalProgrammer , Well, is up to him now to show you any software at github that uses AF_INET to create local files ;) It also does not makes sense in my mind why i would create a FILE with an IP adress on its name, if i can use the localhost address or 127.0.0.1 to deal with internal connections using AF_INET if it is totally required by any sort of "internal software development law" :) – user34720 Feb 28 '18 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.