Send an Ethernet data frame on linux

Source: Internet
Author: User
Send an Ethernet data frame on the linux platform-general Linux technology-Linux technology and application information. The following is a detailed description. # Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# DefineIFALIASZ256
Typedef char INT8;

# Define VSTRONG_PROTOCOL 0x0807
# Define DEBUGMSG (cond, printf_exp)
Int main (int argc, char ** argv ){
Int I, send_flag = 0;
Struct sockaddr_ll stTagAddr;
Char szBuff [68];
Memset (& stTagAddr, 0, sizeof (stTagAddr ));
StTagAddr. sll_family = PF_PACKET; // enter AF_PACKET, which is no longer processed by the protocol layer.
StTagAddr. sll_protocol = htons (VSTRONG_PROTOCOL );
Int ret;
Int ERR;
Struct ifreq req;
Int sd;
Sd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_IP); // This sd is used to obtain the index of eth0. When it is finished, close d = socket (PF_INET, SOCK_DGRAM, 0) AF_INET, SOCK_DGRAM, 0 SOCK_PACKET
Strncpy (req. ifr_name, "eth0", 4); // obtain the index using the device name
Ret = ioctl (sd, SIOCGIFFLAGS, & req );

// Close (sd );
If (ret =-1)
{
DEBUGMSG (1, ("Level: % d [Searcher main] Get eth0 index err \ n", ERR ));
}
StTagAddr. sll_ifindex = req. ifr_ifindex; // the index of the eth0 Nic, which is very important for the system to send data to the NIC.
StTagAddr. sll_pkttype = PACKET_OUTGOING; // The type of the Flag package is the outgoing package.
StTagAddr. sll_halen = 6; // The target MAC address length is 6.

// Enter the target MAC address
StTagAddr. sll_addr [0] = 0xff;
StTagAddr. sll_addr [1] = 0xff;
StTagAddr. sll_addr [2] = 0xff;
StTagAddr. sll_addr [3] = 0xff;
StTagAddr. sll_addr [4] = 0xff;
StTagAddr. sll_addr [5] = 0xff;

// Fill in the frame header and content

Memset (szBuff, 1, 68 );

For (I = 0; I <68; I ++)
Printf ("% d \ n", szBuff );
Send_flag = sendto (sd, (INT8 *) szBuff, sizeof (szBuff), 0, (const struct sockaddr *) & stTagAddr, sizeof (stTagAddr ));
}
This is my changed program. send_flag returns-1.
Related Article

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.