How do I create RAW TCP/IP packets in C++?

后端 未结 11 1352
有刺的猬
有刺的猬 2020-12-24 06:59

I\'m a beginning C++ programmer / network admin, but I figure I can learn how to do this if someone points me in the right direction. Most of the tutorials are demonstrated

相关标签:
11条回答
  • 2020-12-24 08:02

    I've been developing libtins for the past year. It's a high level C++ packet crafting and sniffing library.

    Unless you want to reinvent the wheel and implement every protocol's internals, I'd recommend you to use some higher level library which already does that for you.

    0 讨论(0)
  • 2020-12-24 08:04

    For starters, it would be helpful if you clarify what you mean by "raw". Traditionally this means that you want to craft all of the layer 4 header (TCP/UDP/ICMP... header), and perhaps some of the IP header on your own. For this you will need more than beej's tutorial which has been mentioned my many here already. In this case you want raw IP sockets obtained using the SOCK_RAW argument in your call to socket (see http://mixter.void.ru/rawip.html for some basics).

    If what you really want is just to be able to establish a TCP connection to some remote host such as port 80 on stackoverflow.com, then you probably don't need "raw" sockets and beej's guide will serve you well.

    For an authoritative reference on the subject, you should really pick up Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition) by Stevens et al.

    0 讨论(0)
  • 2020-12-24 08:05

    Read Unix Network Programming by Richard Stevens. It's a must. It explains how it all works, gives you code, and even gives you helper methods. You might want to check out some of his other books. Advanced Programming In The Unix Enviernment is a must for lower level programming in Unix is general. I don't even do stuff on the Unix stack anymore, and the stuf from these books still helps how I code.

    0 讨论(0)
  • 2020-12-24 08:05

    Libpcap will let you craft complete packets (layer2 through layer 7) and send them out over the wire. As fun as that sounds, there's some caveats with it. You need to create all the appropriate headers and do all the checksumming yourself. Libnet can help with that, though.

    If you want to get out of the C++ programming pool, there is scapy for python. It makes it trivial to craft and transmit TCP/IP packets.

    0 讨论(0)
  • 2020-12-24 08:06

    For TCP client side:

    Use gethostbyname to lookup dns name to IP, it will return a hostent structure. Let's call this returned value host.

    hostent *host = gethostbyname(HOSTNAME_CSTR);
    

    Fill the socket address structure:

    sockaddr_in sock;
    sock.sin_family = AF_INET;
    sock.sin_port = htons(REMOTE_PORT);
    sock.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
    

    Create a socket and call connect:

    s = socket(AF_INET, SOCK_STREAM, 0); 
    connect(s, (struct sockaddr *)&sock, sizeof(sock))
    

    For TCP server side:

    Setup a socket

    Bind your address to that socket using bind.

    Start listening on that socket with listen

    Call accept to get a connected client. <-- at this point you spawn a new thread to handle the connection while you make another call to accept to get the next connected client.

    General communication:

    Use send and recv to read and write between the client and server.

    Source code example of BSD sockets:

    You can find some good example code of this at wikipedia.

    Further reading:

    I highly recommend this book and this online tutorial:

    alt text

    4:

    0 讨论(0)
提交回复
热议问题