gct
gct

Reputation: 14563

Querying maximum socket send buffer size in C?

I know I can cat /proc/sys/net/core/wmem_max to get the maximum size for SO_SNDBUF on a socket, but is there an easy way to query that value in C without going through the kludgy-feeling steps of opening the file, reading, and converting to an integer?

Upvotes: 1

Views: 1927

Answers (2)

nos
nos

Reputation: 229058

To get the value of the net.ipv4.tcp_wmem sysctl, you need to parse it out of the /proc file representing that sysctl (there really is no better way on Linux, and the sysctl system call has long since been deprecated.)

Something like:

unsigned long wmem_min,wmem_default,wmem_max;
FILE *f = fopen("/proc/sys/net/ipv4/tcp_wmem", "r");
if(f == NULL)
   fail();
if(fscanf(f, "%lu %lu %lu", &wmem_min,&wmem_default,&wmem_max) != 3)
  fail();

fclose(f);
 ... use wmem_max

For a particular socket, you can get the current remaining buffer with

   

  socklen_t optlen;
  int send_buf, rc;
  optlen = sizeof(send_buf);
  //if getsockopt is successful, send_buf will hold the buffer size
  rc = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &send_buf, &optlen);

Upvotes: 3

Sean Flynn
Sean Flynn

Reputation: 81

Couldn't you invoke the sysctl command on the shell (use system() or popen/pclose()) to get this information...at least avoids opening a file but may be equivalent in overall ugliness:

system("sysctl -n net.ipv4.tcp_wmem");

Upvotes: 0

Related Questions