Linux Socket Programming by Example - Warren Gay

< BACKCONTINUE >
159231153036212041242100244042145096184016146223183074028121223008110134178007184111152111192

Retrieving the Socket Type (SO_TYPE)

Some socket options can only be retrieved. The SO_TYPE is one such example. This option allows a subroutine, which is passed a socket (as a file descriptor), to determine what kind of socket it is dealing with.

Listing 12.3 shows an example program that determines the type of the socket s.

Example 12.3. gettype.cóGetting SO_TYPE Value of SOL_SOCKET Level Option
 <$nopage>
001 1:   /* gettype.c:
002 2:    *
003 3:    * Get SO_TYPE Option:
004 4:    */
005 5:   #include <stdio.h>
006 6:   #include <unistd.h>
007 7:   #include <stdlib.h>
008 8:   #include <errno.h>
009 9:   #include <string.h>
010 10:  #include <sys/types.h>
011 11:  #include <sys/socket.h>
012 12:  #include <assert.h>
013 13:
014 14:  /*
015 15:   * This function reports the error and
016 16:   * exits back to the shell:
017 17:   */
018 18:  static void
019 19:  bail(const char *on_what) {
020 20:      if ( errno != 0 ) {
021 21:          fputs(strerror(errno),stderr);
022 22:          fputs(": ",stderr);
023 23:      }
024 24:      fputs(on_what,stderr);
025 25:      fputc('\n',stderr);
026 26:      exit(1);
027 27:  }
028 28:
029 29:  int
030 30:  main(int argc,char **argv) {
031 31:      int z;
032 32:      int s = -1;                /* Socket */
033 33:      int so_type = -1;    /* Socket type */
034 34:      socklen_t optlen;  /* Option length */
035 35:
036 36:      /*
037 37:       * Create a TCP/IP socket to use:
038 38:       */
039 39:      s = socket(PF_INET,SOCK_STREAM,0);
040 40:      if ( s == -1 )
041 41:          bail("socket(2)");
042 42:
043 43:      /*
044 44:       * Get socket option SO_SNDBUF:
045 45:       */
046 46:      optlen = sizeof so_type;
047 47:      z = getsockopt(s,SOL_SOCKET,SO_TYPE,
048 48:          &so_type,&optlen);
049 49:      if ( z )
050 50:          bail("getsockopt(s,SOL_SOCKET,"
051 51:              "SO_TYPE)");
052 52:
053 53:      assert(optlen == sizeof so_type);
054 54:
055 55:      /*
056 56:       * Report the buffer sizes: <$nopage>
057 				 <$nopage>
058 				 <$nopage>
059 				 <$nopage>
060 57:       */
061 58:      printf("Socket s : %d\n",s);
062 59:      printf(" SO_TYPE : %d\n",so_type);
063 60:      printf(" SOCK_STREAM = %d\n",
064 61:          SOCK_STREAM);
065 62:
066 63:      close(s);
067 64:      return 0;
068 65:  } <$nopage>
069 			

The salient points of the program are as follows:

  1. Variable so_type is declared as an integer to receive the socket type in line 33.

  2. The socket of type SOCK_STREAM is created in line 39.

  3. The option SO_TYPE is fetched into variable so_type in lines 46 to 53.

  4. The socket s is reported in line 58, whereas its socket type in variable so_type is reported in line 59.

  5. The value of C macro SOCK_STREAM is reported in lines 60 and 61 for comparison purposes.

The following output shows an example compile and execution session for the program:

					
$ make gettype
gcc -c  -D_GNU_SOURCE -Wall -Wreturn-type gettype.c
gcc gettype.o -o gettype
$ ./gettype
Socket s : 3
 SO_TYPE : 1
 SOCK_STREAM = 1
$

				

From this output, you can see that socket number 3 was reported to be of type 1 in the following output line. Note that the C macro SOCK_STREAM is the value of 1, also, confirming that the option value is correct. Just for fun, you might want to modify the program to try the value of SOCK_DGRAM in the socket(2) function call and see whether the reported value changes.

< BACKCONTINUE >

Index terms contained in this section

listings
      retrieving socket type (SO_TYPE) 2nd
retrieving
     socket options
            SO_TYPE 2nd 3rd
SO_TYPE (socket type)
      retrieving 2nd 3rd
sockets
     options
            SO_TYPE 2nd 3rd
types
     socket
            retrieving 2nd 3rd