-
Notifications
You must be signed in to change notification settings - Fork 5
/
cb_ethernet.h
347 lines (310 loc) · 11.6 KB
/
cb_ethernet.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Excelfore Communication Base Library
* Copyright (C) 2019 Excelfore Corporation (https://excelfore.com)
*
* This file is part of Excelfore-combase.
*
* Excelfore-combase is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Excelfore-combase is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Excelfore-combase. If not, see
* <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
*/
/**
* @defgroup network network functions binding
* @{
* @file cb_ethernet.h
* @copyright Copyright (C) 2019 Excelfore Corporation
* @author Shiro Ninomiya ([email protected])
*
* @brief bindings to posix network functions
*/
#ifndef __CB_ETHERNET_H_
#define __CB_ETHERNET_H_
#ifdef CB_ETHERNET_NON_POSIX_H
/* non-posix platforms need to support necessary POSIX compatible
* functions and types which are defined as CB_* macros below.
* And provide them in a header file defined as CB_SOCKET_NON_POSIX_H */
#include CB_ETHERNET_NON_POSIX_H
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <ifaddrs.h>
#include <fcntl.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#define CB_SOCKET_T int
#define CB_SOCKET_VALID(x) ((x)>=0)
#define CB_SOCKET_INVALID_VALUE -1
#define CB_ETHHDR_T struct ethhdr
#define CB_SOCKLEN_T socklen_t
#define CB_SOCKADDR_T struct sockaddr
#define CB_SOCKADDR_LL_T struct sockaddr_ll
#define CB_SOCKADDR_IN_T struct sockaddr_in
#define CB_SOCKADDR_IN6_T struct sockaddr_in6
#define CB_SOCKADDR_STORAGE_T struct sockaddr_storage
#define CB_IN_ADDR_T struct in_addr
#define CB_IN6_ADDR_T struct in6_addr
#define CB_IFREQ_T struct ifreq
#define CB_OPEN open
#define CB_CLOSE close
#define CB_SOCKET socket
#define CB_IF_NAMETOINDEX if_nametoindex
#define CB_SOCK_BIND bind
#define CB_SOCK_IOCTL ioctl
#define CB_SOCK_CLOSE close
#define CB_SETSOCKOPT setsockopt
#define CB_SOCK_SENDTO sendto
#define CB_SOCK_RECVFROM recvfrom
#define CB_SOCK_RECVMSG recvmsg
#define CB_SOCK_WRITE write
#define CB_SOCK_CONNECT connect
#define CB_FCNTL fcntl
#define CB_FD_SET_T fd_set
#define CB_FDSET FD_SET
#define CB_FDCLR FD_CLR
#define CB_FDISSET FD_ISSET
#define CB_FDZERO FD_ZERO
#endif // CB_ETHERNET_NON_POSIX_H
/* virtual eth ports and ptp devices are used in the ovip mode.
* common suffix should be added after these prefixes */
/**
*@brief prefix of virtual network device, which supports
* raw ethernet packet over udp
*/
#define CB_VIRTUAL_ETHDEV_PREFIX "cbeth"
/**
*@brief prefix of virtual ptp device.
* the suffix must be common with the virtual network device.
*/
#define CB_VIRTUAL_PTPDEV_PREFIX "cbptp"
/**
*@brief virtual MAC address of the virtual network device.
* the lower 2 bytes are calculated from the suffix
*/
#define CB_VIRTUAL_ETHDEV_MACU32 {0x02,0x01,0x45,0x10}
/* the following definitions have some variations for platforms */
#ifndef H_SOURCE
#define H_SOURCE h_source
#endif
#ifndef H_DEST
#define H_DEST h_dest
#endif
#ifndef H_PROTO
#define H_PROTO h_proto
#endif
/************************************************************
* definitions to handle PTP messages
************************************************************/
/**
*@brief forms ptp header msgtype.
*/
#define PTP_HEAD_MSGTYPE(x) ((*(uint8_t *) ((uint8_t *)(x) + 0)) & 0x0F)
/**
* @brief macro which is used to form ptp header sequence id.
*/
#define PTP_HEAD_SEQID(x) (ntohs(*(uint16_t *) ((uint8_t *)(x) + 30)))
/**
* @brief macro used to form ptp header domain number.
*/
#define PTP_HEAD_DOMAIN_NUMBER(x) (*(uint8_t *)((uint8_t *)(x) + 4))
/** @brief maximum character number of ptp device name */
#define MAX_PTPDEV_NAME 32
/**
* @brief ptpdevice name.
*/
typedef char ptpdevname_t[MAX_PTPDEV_NAME];
/************************************************************
* definitions to handle raw socket
************************************************************/
/**
* @brief this enumeration defines permission for raw socket.
* @verbatim for example @endverbatim
* CB_RAWSOCK_RDWR for read and write.
* CB_RAWSOCK_RDONLY for read only.
* CB_RAWSOCK_WRONLY for write only.
*/
typedef enum {
CB_RAWSOCK_RDWR = 0,
CB_RAWSOCK_RDONLY,
CB_RAWSOCK_WRONLY,
} cb_rawsock_rw_t;
/**
* @brief parameters to open the over IP mode raw socket, the values are in host order
*/
typedef struct cb_rawsock_ovip_para {
uint32_t laddr; //!< local IP address
uint16_t lport; //!< local IP port
uint32_t daddr; //!< destination IP address
uint16_t dport; //!< destination IP port
} cb_rawsock_ovip_para_t;
typedef enum {
CB_SOCK_MODE_OVIP = -1,
CB_SOCK_MODE_NORMAL,
} cb_sock_mode_t;
/**
* @brief raw socket parameters.to open or create raw socket this structure must be filled.
*/
typedef struct cb_rawsock_paras{
const char *dev; //!< ethernet device name
uint16_t proto; //!< protocol value like ETH_P_1588
uint16_t vlan_proto; //!< protocol value in VLAN tag, not used in non-tagged
int priority; //!< PCP priority value in VLAN tag
cb_rawsock_rw_t rw_type; //!< one of RAWSOCK_WRONLY, RAWSOCK_RDONLY, RAWSOCK_RDWR
cb_sock_mode_t sock_mode; //!< -1:raw socket over udp, 0:normal,
cb_rawsock_ovip_para_t *ovipp; //!< over-udp mode parameter
} cb_rawsock_paras_t;
/**
* @brief network device name.
*/
typedef char netdevname_t[IFNAMSIZ];
/************************************************************
* functions
************************************************************/
/**
* @brief get mac address from device name like 'eth0'
* @param sfd if sfd!=-1, pre-opened socket is used to get the mac.
* if sfd==-1, a newly opened udp socket is used to get the mac Address.
* @param dev ethernet device name like 'eth0'
* @param bmac refernce to buffer which is used to store mac address
* of ethernet device.
* @return 0 on success, -1 on error
*/
int cb_get_mac_bydev(CB_SOCKET_T sfd, const char *dev, ub_macaddr_t bmac);
/**
* @brief get ip address from device name like 'eth0'
* @param sfd if sfd!=-1, pre-opened socket is used to get the mac.
* if sfd==-1, a newly opened udp socket is used to get the MAC Address.
* @param dev ethenert device name like 'eth0'
* @param inp reference to 'CB_IN_ADDR_T' where IP address is saved
* @return 0 on success, -1 on error
*/
int cb_get_ip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp);
/**
* @brief get broadcast ip address from device name like 'eth0'
* @param sfd if sfd!=-1, pre-opened socket is used to get the mac.
* if sfd==-1, a newly opened udp socket is used.
* @param dev ethenert device name like 'eth0'
* @param inp reference to 'CB_IN_ADDR_T' where IP address is saved
* @return 0 on success, -1 on error
*/
int cb_get_brdip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp);
/**
* @brief generic raw ethernet open
* @param llrawp cb_rawsock_paras_t -> raw socket open parameters,
* this parameter is passed as refernce to cb_rawsock_paras_t.
* @param fd return a descriptor of opened socket
* @param addr return sockaddr information which is used to open the socket
* @param mtusize MTU size including ETH header size.
* if *mtusize>default size, try to resize MTU size.
* @param bmac the mac address of 'dev' is returned in 'bmac'
* @return 0 on success, -1 on error
* @note for general this function support the both of 'avtp raw' and
* 'general raw', for 'nos'(no OS or primitive OS), this is for
* 'avtp raw' and nos_rawe_socket_open is for 'general raw'
* @note before calling to this function, llrawp must be filled.
* @see @c cb_rawsock_paras_t
*/
int cb_rawsock_open(cb_rawsock_paras_t *llrawp, CB_SOCKET_T *fd, CB_SOCKADDR_LL_T *addr,
int *mtusize, ub_macaddr_t bmac);
/**
* @brief close the socket opened by cb_rawsock_open
* @param fd descriptor of the opened socket
* @return 0 on success, -1 on error.
*/
int cb_rawsock_close(CB_SOCKET_T fd);
/**
* @brief set socket priority
* @param fd descriptor of the opened socket
* @param priority priority number
* @return 0 on success, -1 on error.
*/
int cb_sock_set_priority(CB_SOCKET_T fd, int priority);
/**
* @brief expand mtusize
* @param fd descriptor of the opened socket
* @param mtusize new mtusize, return a new mtu size in *mtusize
* @return 0 on success, -1 on error.
*/
int cb_expand_mtusize(CB_SOCKET_T fd, const char *dev, int *mtusize);
/**
* @brief set the promiscuous mode on the socket
* @param sfd descriptor of the socket
* @param dev ethernet device name like eth0
* @param enable true:enable, false:disable
* @return 0 on success, -1 on error.
*/
int cb_set_promiscuous_mode(CB_SOCKET_T sfd, const char *dev, bool enable);
/**
* @brief register/deregister multicast address to receive
* @param fd descriptor of the socket
* @param dev ethernet device name like eth0
* @param mcastmac multicast address
* @param del 0:register 1:deregister
* @return 0 on success, -1 on error.
*/
int cb_reg_multicast_address(CB_SOCKET_T fd, const char *dev,
const unsigned char *mcastmac, int del);
/**
* @brief get all network devices name availble in systems.
* @param maxdevnum number of devices presents in systems.
* @param netdevs reference to netdevname_t which stores net devices name.
* @return index number of network devices presents on system.
*/
int cb_get_all_netdevs(int maxdevnum, netdevname_t *netdevs);
/**
* @brief get ethtool link state from device name like 'eth0'
* @param cfd if cfd!=-1, pre-opened socket is used to get the ethtool info.
* if cfd==-1, a newly opened udp socket is used.
* @param dev ethernet device name like 'eth0'
* @param linkstate to store link state value (0:down, 1:up)
* @return 0 on success, -1 on error
* @note Linux platform supports this function. Other platform must suport
* in the outside of this layer.
*/
int cb_get_ethtool_linkstate(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate);
/**
* @brief get ethtool info(speed and duplex) from device name like 'eth0'
* @param cfd if cfd!=-1, pre-opened socket is used to get the ethtool info.
* if cfd==-1, a newly opened udp socket is used.
* @param dev ethenert device name like 'eth0'
* @param speed to store speed value(0:unknow, 10:10Mbps, 100:100Mbps, 1000:1Gbps)
* @param duplex to store duplex value(0:unknow, 1:Full, 2:Half)
* @return 0 on success, -1 on error
* @note Linux platform supports this function. Other platform must suport
* in the outside of this layer.
*/
int cb_get_ethtool_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex);
/**
* @brief find network device name from ptp device name
* @return 0 on success, -1 on error
* @param ptpdev ptpdevice name(either format of '/dev/ptp0' or 'ptp0')
* @param netdev the result of network device name, must have enough space
* @note Linux platform supports this function. Other platform must suport
* in the outside of this layer.
*/
int cb_get_netdev_from_ptpdev(char *ptpdev, char *netdev);
/**
* @brief find ptp device name from network device name
* @return 0 on success, -1 on error
* @param netdev network device name (like 'eth0')
* @param ptpdev the result of ptpdevice name(like 'ptp0', no preceding '/dev/'), \n
* must have enough space.
* @note Linux platform supports this function. Other platform must suport
* in the outside of this layer.
*/
int cb_get_ptpdev_from_netdev(char *netdev, char *ptpdev);
#endif
/** @}*/