eth_linux.c 3.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013

	This program 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 3 of the License, or
	(at your option) any later version.

	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

 */

/*
	Ethernet device access for Linux.
 */

Jeroen Vreeken's avatar
Jeroen Vreeken committed
23
24
25
#include <ec/eth.h>
#include <ec/ec_int.h>
#include <log/log.h>
26
27
28
29
30
31
32
33

#include <net/if.h>
#include <arpa/inet.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdio.h>
34
#include <errno.h>
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

static struct sockaddr_ll sockaddr_ll;

int eth_timeout_set(int ec_sock, struct timeval *tx, struct timeval *rx)
{
	setsockopt(ec_sock, SOL_SOCKET, SO_SNDTIMEO, tx, sizeof(*tx));
	setsockopt(ec_sock, SOL_SOCKET, SO_RCVTIMEO, rx, sizeof(*rx));

	return 0;
}


int eth_open(char *ifname)
{
	int ec_sock;
	struct ifreq ifreq;
	int ret;
	struct timeval timeout;
	
	ec_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ETHERCAT));
	if (ec_sock < 0)
		return ec_sock;
	
	strncpy(ifreq.ifr_name, ifname, IFNAMSIZ);
	ret = ioctl(ec_sock, SIOCGIFINDEX, &ifreq);
	if (ret < 0) {
61
		log_send(LOG_T_ERROR, "ioctl SIOCGIFINDEX: %s", strerror(errno));
62
63
		goto err_socket;
	}
64
65
	log_send(LOG_T_DEBUG, "Interface %s index nr found: %d", 
	    ifname, ifreq.ifr_ifindex);
66
67
68
69
70
71
72

	sockaddr_ll.sll_family = AF_PACKET;
	sockaddr_ll.sll_protocol = htons(ETH_P_ETHERCAT);
	sockaddr_ll.sll_ifindex = ifreq.ifr_ifindex;

	ret=bind(ec_sock, (struct sockaddr*)&sockaddr_ll, sizeof(sockaddr_ll));
	if (ret < 0) {
73
		log_send(LOG_T_ERROR, "bind(): %s", strerror(errno));
74
75
		goto err_socket;
	}
76
77
	log_send(LOG_T_DEBUG, "Bind to interface and protocol 0x%04x done",
	    ETH_P_ETHERCAT);
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


	sockaddr_ll.sll_addr[0] = 0xff;
	sockaddr_ll.sll_addr[1] = 0xff;
	sockaddr_ll.sll_addr[2] = 0xff;
	sockaddr_ll.sll_addr[3] = 0xff;
	sockaddr_ll.sll_addr[4] = 0xff;
	sockaddr_ll.sll_addr[5] = 0xff;
	sockaddr_ll.sll_halen = ETH_ALEN;

	/* Using select: set to non-blocking (1)
	   Using SO_SNDTIMEO: set to blocking (0) */
	ioctl(ec_sock, FIONBIO, &(int){0});

	timeout.tv_sec = 0;
	timeout.tv_usec = EC_DGRAM_TIMEOUT;
	setsockopt(ec_sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
	timeout.tv_sec = 0;
	timeout.tv_usec = EC_DGRAM_TIMEOUT;
	setsockopt(ec_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

	return ec_sock;

err_socket:
	close(ec_sock);
	return -1;
}

int eth_close(int ec_sock)
{
	return close(ec_sock);
}

int eth_recv(int ec_sock, void *frame, int max_len)
{
	return recv(ec_sock, frame, max_len, 0);
}

int eth_send(int ec_sock, void *frame, int len)
{
	return sendto(ec_sock, frame, len, 
	    MSG_DONTWAIT, 
	    (struct sockaddr *)&sockaddr_ll, sizeof(sockaddr_ll));
}