Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of FLAG in socket send and recv

While searching in the Linux manual page, what I have found about the format of send and recv in socket is like below:

For send,

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

For recv,

ssize_t recv(int sockfd, void *buf, size_t len, int flags);

But I am not sure what they are trying to tell about int flags. In one sample code I have found the value of flag as 0 (zero). What it means? Also what is the meaning of the line below in the man page?

"The flags argument is the bitwise OR of zero or more of the following flags."

Then the list of flags:

MSG_CONFIRM
MSG_DONTROUTE
.
.
.
etc.
like image 282
user3751012 Avatar asked Jun 26 '14 12:06

user3751012


3 Answers

if int flags are equal to 0, it means that no flags are specified. these are optional.
to answer about ORing flags - it is a mechanism that allow you to specify more than one flag - MSG_CONFIRM | MSG_DONTWAIT specify two flags.

OR gate:          AND gate:

a b  out          a b  out
0 0  0            0 0  0
0 1  1            0 1  0
1 0  1            1 0  0
1 1  1            1 1  1

what i understand, is that by ORing flags you set specific bits to 1 in an int variable.
later in the code, by ANDing that variable with specific flags you know whether flag was set or not.

if you had specified MSG_DONTWAIT flag, the code: flags & MSG_DONTWAIT will return 1, so you know that flag was set.

let's have a look how MSG_DONTWAIT is defined.

enum
  {
    ...
    MSG_DONTWAIT    = 0x40, /* Nonblocking IO.  */
#define MSG_DONTWAIT    MSG_DONTWAIT
    ...
  };

the hex notation 0x40 means that only 7th bit is set to 1.

below i present an example of bitwise operations from socket.c. there's a check whether O_NONBLOCK flag was set when socket file descriptor was created. if so, then set on current flags variable 7th bit to 1, which was defined as MSG_DONTWAIT.

if (sock->file->f_flags & O_NONBLOCK)
    flags |= MSG_DONTWAIT;

a nice reference about bitwise operations: http://teaching.idallen.com/cst8214/08w/notes/bit_operations.txt

like image 112
macfij Avatar answered Oct 26 '22 02:10

macfij


Flags allow to pass extra behavior. The default value (0) will lead to the default behavior. In most of the simple cases that's what you want.
If you want the network subsystem to behave in a specific way you can pass on flag value or several values by Oring them: For example If you want to have the 'MSG_DONTWAIT' and the 'MSG_MORE' behaviour (as described by the man page) you can use MSG_DONTWAIT | MSG_MORE.

like image 3
Nicolas Defranoux Avatar answered Oct 26 '22 01:10

Nicolas Defranoux


Here's a good explanation, with an example.

The 0 flag allows you to use a regular recv(), with a standard behavior. If you want to use a custom recv(), you need to separate your flags (thoses who're listed in the man page) with the OR operator, as it's stated here :

"The flags argument is the bitwise OR of zero or more of the following flags."

Just like that :

recv(sockfd, buf, buflen, FLAG | FLAG | FLAG);
like image 2
Plk Avatar answered Oct 26 '22 01:10

Plk