Thread: The option is unknown at the level indicated.

  1. #1
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42

    The option is unknown at the level indicated.

    Does my system not support this constant?
    code:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <errno.h>
    #include <stdlib.h>
    
    int main()
    {
    int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
    	if(sock == -1)
    	   printf("Error\n");
    
        char *name;
    	name="ccmni0";
    	if( (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, name, 6)) < 0)
    	   printf("SO_BINDTODEVICE error\n");
    	char *arr[7];
    	int ss = 7;
    	ret = getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &arr, &ss);
    	if(ret==0)
    	{
    		printf("SO_BINDTODEVICE: %s\n", arr);
    	}
    	else
    	{
    		if(errno==EBADF)
                printf("The argument sockfd is not a valid file descriptor.\n");
    		else if(errno==EFAULT)
    		    printf("The address pointed to by optval is not in a valid part of.the process address space.  For getsockopt(), this error may also be returned if optlen is not in a valid part of the process address space.\n");
    		else if(errno==EINVAL)
    		    printf("optlen invalid in setsockopt().  In some cases this error.can also occur for an invalid value in optval (e.g., for the IP_ADD_MEMBERSHIP option described in ip(7)).\n");
    		else if(errno==ENOPROTOOPT)
    		    printf("The option is unknown at the level indicated.\n");
    		else if(errno==ENOTSOCK)
    		    printf("The file descriptor sockfd does not refer to a socket.\n");
    }
    I write the program from the phone and therefore my internet interface is called ccmni0. Why can not I access the variable A and write the value there with the setsockopt command can I? This example is working and taken from the documentation, you can check at yourself, there is nothing to correct it.
    The computer is just a dumb machine, people are foolish by the turbidity, but a person is better better as he is not a programmed object but an endless subject.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by DevZero View Post
    This example is working and taken from the documentation, you can check at yourself, there is nothing to correct it.
    Please stop helping this mindless retard.

  3. #3
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by algorism View Post
    Please stop helping this mindless retard.
    Where did you get rid of retard? Better help and explain it in clear language to me and explain to me why the manuals are written in such an incomprehensible language that no one can understand

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The first question is why aren't you just using bind()?

    Next, the manual page for socket() says this
    SO_BINDTODEVICE
    Bind this socket to a particular device like "eth0", as specified in the passed interface name. If the name is an empty string or the option length is zero, the socket device binding is removed. The passed option is a variable-length null-terminated interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an interface, only packets received from that particular interface are processed by the socket. Note that this only works for some socket types, particularly AF_INET sockets. It is not supported for packet sockets (use normal bind(2) there).
    Which then begs the question as to where you got your example using AF_LOCAL ?

    Even the tiniest amount of effort on your part would have found a lot of further reading material on the subject.
    c - bind vs SO_BINDTODEVICE socket - Stack Overflow
    c - Problems with SO_BINDTODEVICE Linux socket option - Stack Overflow
    c++ - When should I call the SO_BINDTODEVICE socket option for a STREAM socket - Stack Overflow

    In particular, you might need (as I did when I tried it), to have 'root' privilege when trying to do setsockopt.

    Lastly, what's all this?
    Code:
        char *arr[7];
        int ss = 7;
    This is nothing like what getsockopt requires.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <net/if.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
      int sock = socket(AF_INET, SOCK_STREAM, 0); //!! not AF_LOCAL
      if (sock == -1)
        perror("socket()");
    
      char *name;
      name = "eth0";
      if ((setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) < 0)
        perror("setsockopt(SO_BINDTODEVICE)");
      char arr[IFNAMSIZ] = { 0 };
      int ss = IFNAMSIZ;
      int ret = getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, arr, &ss);
      if (ret == 0) {
        printf("SO_BINDTODEVICE: %s\n", arr);
      } else {
        perror("getsockopt(SO_BINDTODEVICE)");
      }
    }
    
    $ ./a.out 
    setsockopt(SO_BINDTODEVICE): Operation not permitted
    SO_BINDTODEVICE: 
    
    $ sudo ./a.out 
    SO_BINDTODEVICE: eth0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-20-2017, 04:57 PM
  2. Switching between user-level and kernel-level threads
    By Javed Iqbal in forum Tech Board
    Replies: 9
    Last Post: 12-16-2014, 09:16 AM
  3. y or n option?
    By Munkey01 in forum C++ Programming
    Replies: 11
    Last Post: 12-24-2002, 10:41 AM
  4. VS.NET .SLN Option
    By nTanker in forum C# Programming
    Replies: 0
    Last Post: 09-25-2002, 01:44 PM

Tags for this Thread