通过socket从网口发送二层报文

代码模拟二层lldp报文,从socket接口发送出去,这是协议报文

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <malloc.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>    

#define INTERFACE_NAME  "eth0"


int main(void)
{

    static struct sockaddr_ll  sll_g;

    int sockfd;

    // unsigned char lldp_mac[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x0E};
    struct ifreq *ifr = malloc (sizeof (struct ifreq));
    sockfd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
    if (0 > sockfd) {
        perror("socket error");
        exit(EXIT_FAILURE);
    }
    memset (&sll_g, 0, sizeof (struct sockaddr_ll));
    memset (ifr, 0, sizeof (struct ifreq)); 
    sll_g.sll_family   = PF_PACKET;
    sll_g.sll_protocol = htons (ETH_P_ALL);
    sll_g.sll_halen    = 6;
    sll_g.sll_ifindex  = if_nametoindex (INTERFACE_NAME);
    strcpy (ifr->ifr_name, INTERFACE_NAME);
    if (bind (sockfd, (struct sockaddr *) &sll_g, sizeof (struct sockaddr_ll)) < 0)
    {
        close (sockfd);
        return 0;
    }
    ioctl (sockfd, SIOCGIFFLAGS, ifr);
    ifr->ifr_flags |=  IFF_ALLMULTI; /* allmulti on (verified via ifconfig) */
    // ifr->ifr_flags |=  IFF_PROMISC; /*promisc on (verified via ifconfig) */
    ioctl (sockfd, SIOCSIFFLAGS, ifr);
    if (ioctl (sockfd, SIOCGIFHWADDR, ifr) == -1)
    {
        close (sockfd);
        return 0;
    }
    char send_msg[105] =  {0x01 ,0x80, 0xc2, 0x00, 0x00, 0x0e, 0x90, 0xe2, 0xfc, 0x00, 0x3b, 0x4f, 0x88, 0xcc, 0x02, 0x07,
                           0x04, 0x90, 0xe2, 0xfc, 0x00, 0x3b, 0x4f, 0x04, 0x04, 0x07, 0x47, 0x45, 0x31, 0x06, 0x02, 0x00, 
                           0x0a, 0x08, 0x05, 0x70, 0x6f, 0x72, 0x74, 0x31, 0x0a, 0x05, 0x54, 0x39, 0x30, 0x30, 0x34, 0x0c,
                           0x22, 0x54, 0x4f, 0x50, 0x2d, 0x54, 0x45, 0x4b, 0x20, 0x49, 0x6e, 0x64, 0x75, 0x73, 0x74, 0x72,
                           0x69, 0x61, 0x6c, 0x20, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x53, 0x77, 0x69,
                           0x74, 0x63, 0x68, 0x0e, 0x04, 0x00, 0xff, 0x00, 0xff, 0x10, 0x0c, 0x05, 0x01, 0xc0, 0xa8, 0x01,
                           0xe0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    free (ifr);
    int msg_len = 105;
    for ( ; ; ) {
    int len = sendto(sockfd, send_msg, msg_len, 0 , (struct sockaddr *)&sll_g, sizeof(sll_g));
        if(len == -1)  {
            perror("sendto");
        }
        sleep(10);
    }

    close(sockfd);
    exit(EXIT_SUCCESS);
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中使用socket发送报文有多种方式,其中最常用的是TCP和UDP协议。 使用TCP协议发送报文的流程如下: 1. 创建一个socket对象,指定使用TCP协议。 2. 使用socket对象的connect()方法连接到目标主机和端口号。 3. 使用socket对象的send()方法发送报文。 4. 使用socket对象的recv()方法接收服务器返回的数据。 5. 关闭socket连接。 示例代码如下: ```python import socket # 创建一个socket对象,指定使用TCP协议 client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 连接到目标主机和端口号 server_address = ('127.0.0.1', 8888) client_socket.connect(server_address) # 发送报文 message = 'Hello, World!' client_socket.send(message.encode()) # 接收服务器返回的数据 data = client_socket.recv(1024).decode() print('Received:', data) # 关闭socket连接 client_socket.close() ``` 使用UDP协议发送报文的流程如下: 1. 创建一个socket对象,指定使用UDP协议。 2. 使用socket对象的sendto()方法发送报文。 3. 使用socket对象的recvfrom()方法接收服务器返回的数据。 4. 关闭socket连接。 示例代码如下: ```python import socket # 创建一个socket对象,指定使用UDP协议 client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 发送报文 message = 'Hello, World!' server_address = ('127.0.0.1', 8888) client_socket.sendto(message.encode(), server_address) # 接收服务器返回的数据 data, address = client_socket.recvfrom(1024) print('Received:', data.decode()) # 关闭socket连接 client_socket.close() ``` 需要注意的是,UDP协议是无连接的,因此在发送报文时需要指定目标主机和端口号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值