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.
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.