Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:37:46+00:00 2026-05-24T13:37:46+00:00

I want to know how to generate a packet in c. Supposed we have

  • 0

I want to know how to generate a packet in c. Supposed we have a type as follows:

struct ipheader {
    int version;
    int hdLen;
    int tos;
    int totLen;
    int id;
    ......
    int dstIp;
}

And we have a ipheader type:

struct ipheader ip;

//init the ip
.....

How can I generate a packet(just ip header part) from “ip”. Could anyone show me how?

I want to know to generate packets with information such as mac address, ip header, tcp header, payload. Then I can use the “pcap_sendpacket” function to send the packets I have generated. Could someone give me a little example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:37:47+00:00Added an answer on May 24, 2026 at 1:37 pm

    You can create custom ip packet as below. The below code snippet also makes the custom TCP part which is inside the ip packet. Also the custom function checksum generates the checksum for the packet. You can use rawsocket to send this packet directly to the network. I think the code is easy and self explanatory. Let me know if you have any queries.

    #include <netinet/ip.h>
    #include <netinet/tcp.h>
    
    
    ****************************************
    ipv4 packet structure
    ****************************************
    struct ipv4_packet {
        struct iphdr iph;
        struct tcphdr tcph;
        void *data;
    };
    
    
    ****************************************
    snippet of the IPV4 packet making code
    ****************************************
    struct iphdr ip_head;
    struct tcphdr tcp_head;
    struct sockaddr_in target;
    char packet[2048];
    int i;
    
    struct tcp_pseudo /*the tcp pseudo header*/
    {
        __u32 src_addr;
        __u32 dst_addr;
        __u8 dummy;
        __u8 proto;
        __u16 length;
    } pseudohead;
    
    struct help_checksum /*struct for checksum calculation*/
    {
        struct tcp_pseudo pshd;
        struct tcphdr tcphd;
        char tcpdata[1024];
    } tcp_chk_construct;
    
    /*Prepare IP header*/
    ip_head.ihl = 5; /*headerlength with no options*/
    ip_head.version = 4;
    ip_head.tos = 0;
    ip_head.tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + len);
    ip_head.id = htons(31337 + (rand() % 100));
    ip_head.frag_off = 0;
    ip_head.ttl = 255;
    ip_head.protocol = IPPROTO_TCP;
    ip_head.check = 0; /*Fill in later*/
    ip_head.saddr = htonl(src);
    ip_head.daddr = htonl(dst);
    ip_head.check = in_cksum((unsigned short *) &ip_head, sizeof(struct iphdr));
    
    /*Prepare TCP header*/
    tcp_head.source = htons(src_p);
    tcp_head.dest = htons(dst_p);
    tcp_head.seq = htonl(seq);
    tcp_head.ack_seq = htonl(ack);
    tcp_head.doff = 5;
    
    /* set or reset ack, fin or syn flags as needed */
    tcp_head.ack = 0;
    tcp_head.syn = 0;
    tcp_head.fin = 0;
    
    tcp_head.res1 = 0;
    tcp_head.urg = 0;
    tcp_head.psh = 0;
    tcp_head.rst = 0;
    tcp_head.res2 = 0;
    
    tcp_head.window = htons(0x7c00);
    tcp_head.check = 0; /*Fill in later*/
    tcp_head.urg_ptr = 0;
    
    /*Assemble structure for checksum calculation and calculate checksum*/
    pseudohead.src_addr = ip_head.saddr;
    pseudohead.dst_addr = ip_head.daddr;
    pseudohead.dummy = 0;
    pseudohead.proto = ip_head.protocol;
    pseudohead.length = htons(sizeof(struct tcphdr) + len);
    
    tcp_chk_construct.pshd = pseudohead;
    tcp_chk_construct.tcphd = tcp_head;
    memcpy(tcp_chk_construct.tcpdata, buffer, len);
    
    tcp_head.check = in_cksum((unsigned short *) &tcp_chk_construct,
            sizeof(struct tcp_pseudo) + sizeof(struct tcphdr) + len);
    
    /*Assemble packet*/
    memcpy(packet, (char *) &ip_head, sizeof(ip_head));
    memcpy(packet + sizeof(ip_head), (char *) &tcp_head, sizeof(tcp_head));
    memcpy(packet + sizeof(ip_head) + sizeof(tcp_head), buffer, len);
    
    /*Send packet*/
    target.sin_family = AF_INET;
    target.sin_addr.s_addr = ip_head.daddr;
    target.sin_port = tcp_head.dest;
    i = sendto(sfd, packet, sizeof(struct iphdr) + sizeof(struct tcphdr) + len,
            0, (struct sockaddr *) &target, sizeof(struct sockaddr_in));
    if (i < 0)
        return (-1); /*Error*/
    else
        return (i); /*Return number of bytes sent*/
    
    
    ****************************************        
    FUNCTION FOR CHECKSUM   
    ****************************************
    /* function to calculate the checksum for the packet */
    unsigned short in_cksum(unsigned short *ptr, int nbytes) {
    
        register long sum; /* assumes long == 32 bits */
        u_short oddbyte;
        register u_short answer; /* assumes u_short == 16 bits */
        /*
         * the algorithm is simple, using a 32-bit accumulator (sum),
         * we add sequential 16-bit words to it, and at the end, fold back
         * all the carry bits from the top 16 bits into the lower 16 bits.
         */
        sum = 0;
        while (nbytes > 1) {
            sum += *ptr++;
            nbytes -= 2;
        }
    
        /* mop up an odd byte, if necessary */
        if (nbytes == 1) {
            oddbyte = 0; /* make sure top half is zero */
            *((u_char *) &oddbyte) = *(u_char *) ptr; /* one byte only */
            sum += oddbyte;
        }
    
        /*
         * Add back carry outs from top 16 bits to low 16 bits.
         */
        sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 */
        sum += (sum >> 16); /* add carry */
        answer = ~sum; /* ones-complement, then truncate to 16 bits */
        return (answer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to know how to generate a url in a servlet. I have
I want to know is it possible to generate .raw file format using C++.
I want to know if I can use datas inside configuration profile generate by
I want to know that does apple's Push Notification Server generate new device token
I have an android app it displays web view.I want know the app idle
want to know why String behaves like value type while using ==. String s1
I want to know how to generate all words using java from specified characters
I have a java program and want to generate javadoc for classes/interfaces. However, I
I have ASP.NET project and I want to know what is better to use.
i used rows.xml() to generate html output. i want to know how to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.