Created
December 17, 2018 02:29
-
-
Save qiaoxu123/6ef5a73451ef8cbd8bbd0d36e8fe9224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <linux/socket.h> | |
#define ETH_P_WSMP 0x88DC | |
int main(){ | |
/* DSMP Socket Address */ | |
struct sockaddr_dsmp{ | |
__kernel_sa_family_t dsmp_family; | |
unsigned int dsmp_psid; | |
}; | |
/* DSMP Socket Address */ | |
struct sockaddr_dsmp addr; | |
/* Create PF_DSMP socket */ | |
int sockfd = socket(PF_DSMP,SOCK_DGRAM,0); | |
/* Set DSMP Socket Address */ | |
memset(&addr,0,sizeof(struct sockaddr_dsmp)); | |
addr.dsmp_family = AF_DSMP; | |
addr.dsmp_psid = 0x0; | |
char buf[40]; | |
/* Bind by PSID */ | |
bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_dsmp)); | |
while(1){ | |
/* Receive DSMP Packets */ | |
int nread = recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL); | |
if(nread) | |
printf("nread = %d\n",nread); | |
break; | |
} | |
return 0; | |
/* //创建套接字 | |
int sock = socket(AF_INET, SOCK_STREAM, 0); | |
//向服务器(特定的IP和端口)发起请求 | |
struct sockaddr_in serv_addr; | |
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充 | |
serv_addr.sin_family = AF_INET; //使用IPv4地址 | |
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址 | |
serv_addr.sin_port = htons(1234); //端口 | |
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); | |
//读取服务器传回的数据 | |
char buffer[40]; | |
read(sock, buffer, sizeof(buffer)-1); | |
printf("Message form server: %s\n", buffer); | |
//关闭套接字 | |
close(sock); | |
return 0; */ | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <linux/if_ether.h> | |
int main(){ | |
/* Open PF_PACKET Socket for EtherType ETHER_P_DSMP */ | |
int sockfd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_DSMP)); | |
/*Construct Ethernet Header*/ | |
struct ether_header *eh = (struct ether_header *)buf; | |
eh->ether_type = htons(ETH_P_DSMP); | |
memcpy(eh->ether_shost,src_mac,ETH_ALEN); | |
memcpy(eh->ether_dhost,dst_mac,ETH_ALEN); | |
int tx_len += sizeof(struct ether_header); | |
/* Construct sockaddr_ii */ | |
sockaddr_ll socket_address; | |
socket_address.sll_ifindex = ifindex; | |
socket_address.sll_halen = ETH_ALEN; | |
memset(socket_address.sll_addr,OxFF,ETH_ALEN); | |
/* File DSMP packet as playload */ | |
while(1){ | |
/* Send DSMP packets */ | |
sendto(sockfd,buf,tx_len,0,(struct sockaddr*)&socket_address,sizeof(struct sockaddr_ii)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment