Thread: Problems with setsockopt function (linux)

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Problems with setsockopt function (linux)

    I'm trying to research more about the code I've seen a few days ago,it's include using a function called SetSockOpt with the swich
    Code:
    PT_SO_SET_REPLACE
    and in the api level
    Code:
    SOL_IP
    .
    Unfortunately, I could not find any documentation on what exactly this switch should do (and it may be the source of the problem)

    So i tried to run the code setsockopt with the flag IPT_SO_SET_REPLACE but i keep getting the wired error from errno
    Code:
    Protocol not available
    this is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sched.h>
    #include <linux/sched.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <linux/netlink.h>
    #include <unistd.h>
    #include <sys/ptrace.h>
    #include <netinet/in.h>
    #include <net/if.h>
    #include <linux/netfilter_ipv4/ip_tables.h>
    #include <fcntl.h>
    
    
    int netfilter_setsockopt(void *p) {
        int sock;
        int ret;
        void *data;
        size_t size;
        struct ipt_replace *repl;
    
    
        sock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
    
    
        if (sock == -1) {
                perror("socket");
                return -1;
        }
    
    
        size = sizeof(struct ipt_replace);
    
    
        data = malloc(size);
    
    
        if (data == NULL) {
            perror("malloc");
            return -1;
        }
    
    
        memset(data, 0, size);
    
    
        repl = (struct ipt_replace *) data;
    
    
        repl->num_counters = 0x1;
        repl->size = 0xffffffff;
        repl->valid_hooks = 0x1;
        repl->num_entries = 0x1;
           
            //this function alwaise return -1
        ret = setsockopt(sock, SOL_IP, IPT_SO_SET_REPLACE, (void *) data, size);
    
    
        printf("done %d\n", ret);
            
            //errno say "Protocol not available"
            perror("error cose: ");
    
    
        return 0;
    }
    
    
    int main(void) {
        void *stack;
        int ret;
    
    
        ret = unshare(CLONE_NEWUSER);
    
    
        if (ret == -1) {
            perror("unshare");
            return -1;
        }
    
    
        stack = (void *) malloc(65536);
    
    
        if (stack == NULL) {
            perror("malloc");
            return -1;
        }
    
    
        clone(netfilter_setsockopt, stack + 65536, CLONE_NEWNET, NULL);
    
    
        sleep(1);
    
    
        return 0;
    }
    * i compile the code with
    Code:
    gcc -m32 code.c -o code
    and try also with
    Code:
    gcc code.c -o code
    with both of them i get a few warning (nothing series) but the code got compiled ,and in both the error is the same
    * I'm runing linux 32 bit kernel version 4.4.0-21-generic (ubunto 16.04)

    What could be the problem?
    How can i fix it and make the code work?
    Where i can find more reading materials on setsockopt api level and swiches?

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > with both of them i get a few warning (nothing series) but the code got compiled
    And yet, it doesn't work.
    Perhaps you should post your error messages and let us judge the seriousness of them.

    Your use of void pointers all over the place is highly suspect.

    Why are you complicating things by using clone (badly)? Get the basics working first.

    Is this you as well?
    c - setsockopt IPT_SO_SET_REPLACE flag return error (linux) - Stack Overflow

    How To Ask Questions The Smart Way
    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.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    This is the error i got (i'm on
    Code:
     ubuntu 4.4.0-21 32 bit
    ):
    Code:
    warning: implicit declaration of function 'unshare' [-Wimplicit-function-declaration]  ret=unshare(CLONE_NEWUSER);
    warning: implicit declaration of function 'clone' [-Wimplicit-function-declaration]     clone(netfilter_setsockopt,stack+65536,CLONE_NEWNET,NULL);
    i want to point out that the code compiled the problem is that it return error (when i'm runing the program ) on setsockopt function .

    Yes , thank you for the How to asc q. link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setsockopt issues
    By Elkvis in forum Linux Programming
    Replies: 3
    Last Post: 06-22-2011, 02:04 PM
  2. Linux problems
    By Dark_Phoenix in forum Tech Board
    Replies: 1
    Last Post: 03-22-2008, 05:30 AM
  3. Problems with modems under linux
    By kawk in forum Tech Board
    Replies: 1
    Last Post: 10-23-2006, 03:30 AM
  4. Linking problems in Linux
    By joeprogrammer in forum Tech Board
    Replies: 11
    Last Post: 08-10-2006, 10:40 PM
  5. is this the right way to setsockopt()?
    By redemption in forum C Programming
    Replies: 0
    Last Post: 09-09-2002, 05:49 AM

Tags for this Thread