4

I'm following a piece of code, where the author who uses the predefined struct sockaddr_in writes host_addr.sin_family = AF_INET; is trying to set up a mini webserver in Linux(Ubuntu). But sin_family does not appear anywhere in the definition of struct sockaddr_in. Does __SOCKADDR_COMMON (sin_) , which is included in the definition, have a special meaning ?

In the webserver's source file he includes <sys/socket.h>, <netinet/in.h>, <arpa/inet.h> and a predefined library that uses Linux libraries.

I found

#define __SOCKADDR_COMMON(sa prefix) \
sa_family_t sa_prefix##family

in one of linux's library file that he used.

I do not understand this definition. A piece by piece explanation would be nice.

And also why does he have to set the family of the socket address, if he defined the protocol of the socket through the parameter?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Search for __SOCKADDR_COMMON as a #define in a library you're using. Also, tell us which includes are in that C file? It's either an IDE / Library macro, or something in what you're using. Either way we can guess, but don't have enough information without you telling us which library you're talking about. – George Stocker Dec 31 '15 at 00:56
  • Found the line #define __SOCKADDR_COMMON(sa prefix) \ sa_family_t sa_prefix##family I've wrote to denote the character, everything else is in there. – shooting-squirrel Dec 31 '15 at 01:03
  • And also why does he have to set the family of the socket address, if he defined the protocol of the socket through the parameter? – shooting-squirrel Dec 31 '15 at 01:09
  • I don't know. What library is it? Probably as a default? I have no idea. – George Stocker Dec 31 '15 at 01:10
  • Why did you put it on hold? Aren't these the macros and structures used by anyone in Linux? – shooting-squirrel Dec 31 '15 at 01:12
  • He tries to set up a server, uses the libraries , , and a predefined one that uses linux libraries. – shooting-squirrel Dec 31 '15 at 01:16
  • You never mention in your question that it's linux, nor do you even give a code snippet that reproduces the issue you're seeing. – George Stocker Dec 31 '15 at 01:22
  • Outside of those issues, taking what you've just learned, you can restructure your question and we'll be able to re-open it. – George Stocker Dec 31 '15 at 01:23
  • Does this answer your question? [Where is "sin\_family" in struct sockaddr\_in - /usr/include/netinet/in.h?](https://stackoverflow.com/questions/27307412/where-is-sin-family-in-struct-sockaddr-in-usr-include-netinet-in-h) – user2462027 Jan 29 '20 at 07:34

1 Answers1

5

On linux, struct sockaddr_in looks like this;

struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;         /* Port number.  */
    struct in_addr sin_addr;        /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
               __SOCKADDR_COMMON_SIZE -
               sizeof (in_port_t) -
               sizeof (struct in_addr)];
  };

__SOCKADDR_COMMON is a normal C macro, that's defined like this:

#define __SOCKADDR_COMMON(sa_prefix) \
sa_family_t sa_prefix##family

It simply concatenates, with the ## preprocessor operation, the passed in argument to form the variable name.

When you expand that usage in struct sockaddr_in , you get this:

sa_family_t sin_family

So __SOCKADDR_COMMON (sin_); will simply be replaced with sa_family_t sin_family;

Also, the struct sockaddr_in is documented in the ip(7) man page, and this documents the member variables that's available to you, regardless of how the actual implementation look like.

nos
  • 223,662
  • 58
  • 417
  • 506
  • Thanks a lot for the clarification. Is the following syntax a generic one #define MACRO_NAME(sa_prefix) \ vartype sa_prefix##argument? Where does this syntax come from? Meaning how does the compiler know this? What's the purpose of the backslash? And also why does he have to set the family of the socket address, if he defined the protocol of the socket through the parameter? Does every webserver software source code contain a piece of code that sets the address family of the socket? – shooting-squirrel Jan 01 '16 at 18:27
  • This is normal syntax that's part of the C language (in particular the preprocessor part), The backslash means that the macro definition [continues on the next line](http://stackoverflow.com/questions/10419530/multi-line-preprocessor-macros). You have to specify the family since various socket calls are generic and can deal with different kinds of sockets, protocols and structs. There are different `struct sockaddr_XXX` types for e.g. IPv4, Bluetooth, etc. all they have in common is the 1. member that tells what kind of struct it is. Yes, every web server would need to do that. – nos Jan 02 '16 at 02:46