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 8273307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:25:52+00:00 2026-06-08T07:25:52+00:00

I am trying to build a program that can send TCP packets, but when

  • 0

I am trying to build a program that can send TCP packets, but when I compiled my code to test the send function it just doesn’t run the way it should. When given arguments it skips to the getchar and exits the program as if the function wasn’t called at all. Now I am not particularly experienced and this is probably a stupid question, but I haven’t been able to find a solution elsewhere. My guess is I’m calling it wrong somehow and that maybe the send_tcp function can’t be inside of the forgepacket function.

The following is my code (not finished at all but should be good enough for a little test run) and the function giving me problems is forgepacket.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#define VERSION "1.0"
#define PCKT_LEN 8192

/* Prototypes */    

void forgepacket (unsigned int, unsigned int, unsigned short, unsigned short);  
void usage(); 

/* Checksum */
unsigned short checksum (unsigned short *pac, int len)
{ 
    unsigned long sum;
    for (sum = 0; len > 0; len--)
            sum += *pac++;
    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    return(~sum);
}               

in_addr_t sip;
in_addr_t dip;
int sport;
int dport;                  

int main(int argc,char **argv)
{
int c;

/* Are we in root? */   
if(geteuid() !=0)
    {
        printf("Root access is required to run this program.\n\n");
        exit(0);        
    }   

while (1)
    {
        static struct option long_options[] =
            {
                /* Options */
            {"send",       no_argument,       0, 's'}, /* args s, r and f have no function yet */
            {"recieve",    no_argument,       0, 'r'},
            {"file",       required_argument, 0, 'f'},
            {"destip",     required_argument, 0, 'i'},
            {"destport",   required_argument, 0, 'p'},
            {"sourceip",   required_argument, 0, 'o'},
            {"sourceport", required_argument, 0, 't'},
            {0, 0, 0, 0}
            };

           int option_index = 0;

           c = getopt_long (argc, argv, "srf:d:i:p:o:t:",
                        long_options, &option_index);

                      /* Detect the end of the options. */
           if (c == -1)
             break;

           switch (c)
             {
             case 0:
               /* If this option set a flag, do nothing else now. */
               if (long_options[option_index].flag != 0)
             break;
               printf ("option %s", long_options[option_index].name);
               if (optarg)
             printf (" with arg %s", optarg);
               printf ("\n");
               break;

             case 's':
               puts ("option -s\n");
               break;

             case 'r':
               puts ("option -r\n");
               break;

             case 'f':
               printf ("option -f with value `%s'\n", optarg);
               break;

             case 'i':
               dip = inet_addr(optarg);
               break;

             case 'p':
               dport = htons(atoi(optarg)); 
               /* Add handling of bad/non number input here */
               break;

             case 'o': 
               sip = inet_addr(optarg);
               break;

             case 't': 
               sport = htons(atoi(optarg));
               break;

             case '?':
               /* Error message printed */
               break;

             default:
               abort ();
             }
         }

           /* Print any remaining command line arguments (not options). */
        if (optind < argc)
    {
        printf ("non-option ARGV-elements: ");
        while (optind < argc)
        printf ("%s ", argv[optind++]);
        putchar ('\n');
    }

            /* check if all mandatory options are set and for unknown arguments */
            /* This REALLY needs changing... */
     if (dip, sip, dport, sport == 0)
        {
            usage();
            return (-1);
        }


    forgepacket(dip, sip, dport, sport);

    getchar ();
    exit (0);

}

void forgepacket(unsigned int sourceip, unsigned int destip, unsigned short sourceport,
                 unsigned short destport)
{           
    /* IP header structure */
    struct ipheader {

        unsigned char      iph_ihl:5, 
                           iph_ver:4;
        unsigned char      iph_tos;
        unsigned short int iph_len;
        unsigned short int iph_id;
        unsigned char      iph_flags;
        unsigned short int iph_offset;
        unsigned char      iph_ttl;
        unsigned char      iph_protocol;
        unsigned short int iph_chksum;
        unsigned int       iph_sourceip;
        unsigned int       iph_destip;

        };

    /* TCP header structure */      
    struct tcpheader {

        unsigned short int tcph_sourceport;
        unsigned short int tcph_destport;
        unsigned int       tcph_seqnum;
        unsigned int       tcph_acknum;
        unsigned char      tcph_reserved:4, tcph_offset:4;
        unsigned int

            tcp_res1:4,       
            tcph_hlen:4,      
            tcph_fin:1,       
            tcph_syn:1,       
            tcph_rst:1,       
            tcph_psh:1,       
            tcph_ack:1,       
            tcph_urg:1,       
            tcph_res2:2;

        unsigned short int tcph_win;
        unsigned short int tcph_chksum;
        unsigned short int tcph_urgptr;

        };

    int send_tcp()
        {
            int sock, one = 1;
            char buffer[PCKT_LEN];
            struct sockaddr_in sin, din;
            const int *val = &one;

            sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
            if (sock < 0)
                {
                    printf("\nError: socket()\n\n");
                    exit (-1);
                }
            else
                    printf ("\nsocket() - Using SOCK_RAW and TCP protocol is OK.\n\n");

            /* Size of the headers */           
            struct ipheader *ip = (struct ipheader *) buffer;
            struct tcpheader *tcp = (struct tcpheader *) (buffer + sizeof (struct ipheader));
            memset (buffer, 0, PCKT_LEN);

            /* IP attributes */
            ip->iph_ihl = 5;
            ip->iph_ver = 4;
            ip->iph_tos = 16;
            ip->iph_len = sizeof(struct ipheader) + sizeof(struct tcpheader);
            ip->iph_id = htons(54321);
            ip->iph_offset = 0;
            ip->iph_ttl = 64;
            ip->iph_protocol = 6;
            ip->iph_chksum = 0; 

            ip->iph_sourceip = sip;
            ip->iph_destip = dip;

            /* TCP attributes */
            tcp->tcph_sourceport = sport;
            tcp->tcph_destport = dport;

            tcp->tcph_seqnum = htonl(1);
            tcp->tcph_acknum = 0;
            tcp->tcph_offset = 5;
            tcp->tcph_syn = 1;
            tcp->tcph_ack = 0;
            tcp->tcph_win = htons(32767);
            tcp->tcph_chksum = 0; 
            tcp->tcph_urgptr = 0;

            ip->iph_chksum = checksum ((unsigned short *) buffer, (sizeof (struct ipheader )+ sizeof (struct tcpheader)));

            /* Address family */ 
            sin.sin_family = AF_INET;
            din.sin_family = AF_INET;

            /* Source port */
            sin.sin_port = sport;
            din.sin_port = dport;

            /* Source IP */
            sin.sin_addr.s_addr = sip;
            din.sin_addr.s_addr = dip;      

            /* Tell the Kernel we're building our own packet */
            if ((setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof (one))) < 0)
                {
                    printf("\nError: Can't set socketoptions\n\n");
                    return (-1);
                }

            /* Send */
            if (sendto(sock, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                {
                    printf("\nError: Can't send packet\n\n");
                    return (-1);
                }

            else
                    printf("Packet sent to %d", dip); 

            close(sock);
        }           
}

void usage() 

{
 /* This is the user manual */  

    printf("\nTCP project %s\n\n", VERSION);

    printf("Usage:  -s -f <file> -i <destip> -p <destport> -o <sourceip> -t <sourceport>\n\n"); 

    printf("-s, --send,         Puts program in send mode\n");
    printf("-r, --recieve,      Puts program in recieve mode\n"); 
    printf("-f, --file,         Specify file containing steganographic message\n");         
    printf("-i, --destip,       Destination IP address\n"); 
    printf("-p, --destport,     Destination port\n"); 
    printf("-o, --sourceip          Source IP address\n"); 
    printf("-t, --sourceport        Source port\n"); 

}   
  • 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-06-08T07:25:54+00:00Added an answer on June 8, 2026 at 7:25 am

    It appears the function definition of send_tcp() is within the function definition of forgepacket() (I didn’t know this was even possible??), but there is no call to it. Replicate the arrangement you have for forgepacket() for send_tcp():

    /* Prototype. */
    int send_tcp(); /* add whatever parameters are required. */
    

    and move definition of send_tcp() outside of forgepacket() and add a call to send_tcp() within forgepacket().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to build a simple test program that uses atomic operations, I get
I am trying to build a program that accepts an array of integers as
I'm trying to build a small program that hosts vst effects and I would
I am currently trying to build a program where I have a recursive function
I'm trying to build a ScrolledWindow that you can draw on using the mouse,
I am trying to build a test program in c++ to automate testing for
I am trying to build a program that uses libusb and I get a
Helllo! I'm trying to build a program that calculates the best score from a
I'm trying to build a class that stores program settings as a std::map. Since
I'm trying to build a progressbar class that can have an arbitrary number of

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.