简体   繁体   中英

Creating a raw socket - Debian + Codeblocks

I'm currently working on the start phase of my bachelor thesis and my problem is to implement a gateway (aka router). I'm currently trying to open a raw socket so I can shred open an Ethernet packet and check what different kind of fields it contains.

I'm using Codeblocks IDE to write C++ code on a Debian VM.

When try to open the raw socket through the following code I get an error saying:

    Error creating raw socket: Operation not permitted

Code:

    int CreateRawSocket(int protocol_to_sniff)
    {
        int s;

        if((s = socket(PF_PACKET, RAW_SOCK, htons(protocol_to_sniff))) == -1)
        {
            perror("Error creating raw socket");
            exit(-1);
        }

        return s;
    }

    int main()
    {

    int s = CreateRawSocket(ETH_P_ALL);

    }

I've searched around a little but I cannot seem to fix the problem, although I think I know what the problem is. So far as I've figured out it is a problem with permissions, but I have no clue on how to give codeblocks sudo permissions.

Any ideas?

Solved it. It was really simple actually.

I entered:

     sudo codeblocks

and it opened codeblocks with sudo rights, but I had to keep that terminal window running or it would shutdown codeblocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM