-
Notifications
You must be signed in to change notification settings - Fork 2
/
rs.c
193 lines (167 loc) · 4.46 KB
/
rs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Last Update:2018-08-13 11:28:12
/**
* @file rawsocket.c
* @brief
* @author luxueqian
* @version 0.1.00
* @date 2018-07-11
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/msg.h>
#include "rs.h"
static uint8_t rmt_mac[6]={0x7F,0xFF,0xFF,0xFF,0xFF,0xFF}; //远端mac地址
#define max_package_len (RS_MAX_PACKAGE_LEN)
#define package_header_len (sizeof(struct ethhdr))
int RSCreate(char *ethname, int type, RSContext *ctx)
{
int sockfd;
int if_index; //网卡索引
struct ifreq *req; //网卡操作
struct sockaddr_ll *device; //链路层地址结构
struct ethhdr *whdr = NULL;
memset(ctx, 0x0, sizeof(RSContext));
ctx->type = type;
ctx->buflen = max_package_len + sizeof(struct ethhdr);
ctx->tx_buf = (uint8_t*) malloc(ctx->buflen);
if(ctx->tx_buf == NULL)
{
printf("memory not enough !!\n");
goto failed;
}
memset(ctx->tx_buf, 0x0, ctx->buflen);
whdr = (struct ethhdr*)ctx->tx_buf;
req = &ctx->req;
device = &ctx->device;
//1:建立raw socket
if ((ctx->sfd = socket(AF_PACKET, SOCK_RAW, htons(type)))== -1)
{
//printf("raw sokcet set up fail !!\n");
goto failed;
}
sockfd = ctx->sfd;
//2:得到本机网口信息
strcpy( req->ifr_name, ethname );
if ( ioctl( sockfd, SIOCGIFFLAGS, req ) < 0 )
{
printf( "failed to do ioctl!" );
goto failed;
}
if ( ioctl(sockfd, SIOCGIFHWADDR, req) < 0 )
{
printf( "failed to do netif ioctl!!%m\n" );
goto failed;
}
memcpy( ctx->loc_mac, req->ifr_hwaddr.sa_data, 6 ); //得到本地网卡地址
if ( ioctl( sockfd, SIOCGIFINDEX, req ) < 0 )
{
printf( "failed to get IF hw address index!!\n" );
goto failed;
}
if_index = req->ifr_ifindex; //得到本地网卡索引
//设置发送device结构体
memset(device,0,sizeof(struct sockaddr_ll));
device->sll_ifindex = if_index;
device->sll_family = AF_PACKET;
memcpy(device->sll_addr, ctx->loc_mac, 6);
device->sll_halen = htons(6);
device->sll_protocol = htons(type);
if( bind(sockfd, (struct sockaddr*)device, sizeof(struct sockaddr_ll)) < 0)
{
printf("Socket Bind error.\n");
goto failed;
}
//4:初始化发送包
memcpy(whdr->h_dest, rmt_mac, 6);
memcpy(whdr->h_source, ctx->loc_mac, 6);
whdr->h_proto = htons(type);
return 0;
failed:
if(ctx)
{
if (ctx->sfd > 0)
close(ctx->sfd);
if(ctx->tx_buf != NULL)
free(ctx->tx_buf);
memset(ctx, 0x0, sizeof(RSContext));
}
return -1;
}
int RSRelease(RSContext *ctx)
{
if(ctx)
{
if (ctx->sfd > 0)
close(ctx->sfd);
if(ctx->tx_buf != NULL)
free(ctx->tx_buf);
memset(ctx, 0x0, sizeof(RSContext));
}
return 0;
}
int RSWrite(RSContext *ctx, uint8_t*p_data, int len)
{
int tx_len = 0;
if(!ctx)
return -1;
if(len > max_package_len)
return -1;
memcpy(ctx->tx_buf + sizeof(struct ethhdr), p_data, len);
int pkg_len = len + sizeof(struct ethhdr);
tx_len = sendto (ctx->sfd, ctx->tx_buf, pkg_len, 0, (struct sockaddr *) &ctx->device, sizeof (ctx->device));
if(tx_len != pkg_len)
return -1;
return 0;
}
static int checkpack(RSContext *ctx, struct ethhdr *eth)
{
if(ntohs(eth->h_proto) == ctx->type)
return 1;
else
return 0;
}
int RSRead(RSContext *ctx, uint8_t*p_buf, int *len)
{
int rx_len = 0;
uint8_t pdata[max_package_len+package_header_len];
rx_len = recvfrom(ctx->sfd, pdata, max_package_len+package_header_len, 0, NULL, NULL);
if(rx_len<=package_header_len || (checkpack(ctx, (struct ethhdr *)pdata) == 0))
return -1;
rx_len -= package_header_len;
if (rx_len < 0)
return -1;
if(rx_len < *len)
*len = rx_len;
memcpy(p_buf, pdata+package_header_len, *len);
return 0;
}
/*
* return value:
* == 0: timeout
* > 0: get msg
* < 0: failed
*/
int RSSelect(RSContext *ctx, uint32_t timeout_ms)
{
int maxfd = 0;
struct timeval tv;
fd_set rfds;
if(!ctx)
return -1;
FD_ZERO(&rfds);
FD_SET(ctx->sfd, &rfds);
maxfd = ctx->sfd + 1;
tv.tv_sec = timeout_ms/1000;
tv.tv_usec = (timeout_ms%1000) * 1000;
int ret = select(maxfd, &rfds, NULL, NULL, &tv);
return ret;
}