A simple program for generating packages and sending packages

Source: Internet
Author: User
Tags socket error htons

Steps:

  • 1. Pack Mr

 

Define struct
Define struct Variables
Assign values to struct Variables
# Define flowcount 30

Struct netflow_header
Baotou Structure
{

Unsigned short version;

Unsigned short count;

Unsigned int sysuptime;

Unsigned int unix_seconds;

Unsigned int unix_nanoseconds;

Unsigned int flow_sequence_number;

Unsigned char engine_type;

Unsigned char engine_id;

Unsigned short reserved;

};
Struct
Netflow_entry
Payload Structure
{

Unsigned int src_ip;

Unsigned int dst_ip;

Unsigned int next_hop_ip;

Unsigned short input_interface_index;

Unsigned short output_interface_index;

Unsigned int packets;

Unsigned int bytes;

Unsigned int start_time;

Unsigned int end_time;

Unsigned short src_port;

Unsigned short dst_port;

Unsigned char pad;

Unsigned char flag;

Unsigned char proto;

Unsigned char TOS;

Unsigned short src_as;

Unsigned short dst_as;

Unsigned char src_netmask_len;

Unsigned char dst_netmask_len;

Unsigned short padding;

};

Struct
Netflow_pkt
Overall Package Structure
{

Struct netflow_header
HDR; Baotou


Struct netflow_entry entry [30];
Payload

} Pkts;

  • 2. Resend package

Establish socket,
Establish connection
Issue the previously generated package

Unsigned int gettickcount ()
{
Struct TMS
TM;
Return
(Unsigned INT) times (& TM );
}

Unsigned int rand_long ()
{

Unsigned int ip_addr, IP1, ip2, IP3, ip4;

IP1 = rand () % 254 + 1;

Ip2 = rand () % 254 + 1;

IP3 = rand () % 254 + 1;

Ip4 = rand () % 254 + 1;


Ip_addr =
(IP1 <24) + (ip2 <16) + (IP3 <8) + ip4;


Return ip_addr;

}

Create_packet ()
Generate a package
{

Int I = 0;

Int J = 0;

Int K = 0;

Unsigned int currentt;

Int counts = flowcount;

Int numbytes;

Socklen_t socklen = sizeof (servaddr );


Pkts. HDR. Version =
Htons (5 );


Pkts. HDR. Count
= Htons (counts );


Currentt =
Gettickcount ()
;

Pkts. HDR. sysuptime = htons (currentt-2000 );


Pkts. HDR. unix_seconds = htons (currentt/1000 );

Pkts. HDR. unix_nanoseconds = htons (currentt-Pkts. HDR. unix_seconds
* 1000 );

Pkts. HDR. engine_id = (unsigned char) 11;

Pkts. HDR. engine_type = (unsigned char) 1;

Pkts. HDR. Reserved = (unsigned char) 1;


For (I = 0; I <counts; I ++)

{

Pkts. Entry [I]. src_ip = htons (rand_long ());

Pkts. Entry [I]. dst_ip = htons (rand_long ());

Pkts. Entry [I]. next_hop_ip = htons (rand_long ());


Pkts. Entry [I]. input_interface_index = htons (RAND () % 3 + 1 );

Pkts. Entry [I]. output_interface_index = htons (RAND () % 3 + 1 );


Pkts. Entry [I]. packets = htons (RAND () % 1000 + 5 );

Pkts. Entry [I]. bytes = htons (RAND () % 2048 + 48 );


Pkts. Entry [I]. start_time = htons (currentt-1500 );

Pkts. Entry [I]. end_time = htons (currentt-100 );


Pkts. Entry [I]. src_port =
Htons (RAND () % 65533 + 1 );

Pkts. Entry [I]. dst_port =
Htons (RAND () % 65533 + 1 );


Pkts. Entry [I]. Pad = htons (1 );

Pkts. Entry [I]. Flag = htons (RAND () % 8129 );


If (counts % 2 = 0)

Pkts. Entry [I]. proto = (unsigned char) 1; // LFC
Changed

Else

If (counts % 3 = 0)

Pkts. Entry [I]. proto = (unsigned char) 6;

Else

Pkts. Entry [I]. proto = (unsigned char) 17;


Pkts. Entry [I]. TOS = (unsigned char) 1;


Pkts. Entry [I]. src_as = htons (RAND () % 100 + 1 );

Pkts. Entry [I]. dst_as = htons (RAND () % 100 + 1 );


Pkts. Entry [I]. src_netmask_len = htons (24 );

Pkts. Entry [I]. dst_netmask_len = htons (24 );


Pkts. Entry [I]. Padding = htons (0 );


}

}

Int clifd;

Struct sockaddr_in servaddr, cliaddr;

Send_packet (char * server_addr_string, unsigned int
Server_port) packet sending Function
{


If (clifd = socket (af_inet, sock_dgram, 0) <
0)

{

Printf ("create socket error! /N ");

Exit (1 );

}


Bzero (& cliaddr, sizeof (cliaddr ));

Cliaddr. sin_family = af_inet;

Cliaddr. sin_addr.s_addr = htons (inaddr_any );

Cliaddr. sin_port = htons (0 );


Bzero (& servaddr, sizeof (servaddr ));

Servaddr. sin_family = af_inet;

Inet_aton (server_addr_string, & servaddr. sin_addr );


Servaddr. sin_port = htons (server_port );

If (numbytes = sendto (clifd, (char
*) & Pkts, 24 + 48 * flowcoun
T, 0, (struct sockaddr
*) & Servaddr, socklen) =-1 ){

Printf ("error send! /N ");

Printf ("Wait error: % s/n", strerror (errno ));

Exit (1 );

}

}

Int
Main ()
Main Program
{
Create_packet ();
Pack Mr
Send_packet (str_dst_ip, dst_port); resend the packet
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.