6 * tst-packet.c - send and receive CAN frames via PF_PACKET sockets
8 * Remark: The sending of CAN frames via PF_PACKET does not work for
9 * virtual CAN devices (vcan) as the packet type is not set to
10 * PACKET_LOOPBACK by the packet socket, so you'll never get something
11 * back (btw the outgoing vcan packet counters are updated correctly).
13 * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of Volkswagen nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * Alternatively, provided that this notice is retained in full, this
29 * software may be distributed under the terms of the GNU General
30 * Public License ("GPL") version 2, in which case the provisions of the
31 * GPL apply INSTEAD OF those given above.
33 * The provided data structures and external interfaces from this code
34 * are not restricted to be used by modules with a GPL compatible license.
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
49 * Send feedback to <socketcan-users@lists.berlios.de>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
62 #include <netinet/in.h> /* for htons() */
64 #include <linux/if_packet.h>
65 #include <linux/if_ether.h> /* for ETH_P_CAN */
66 #include <linux/can.h> /* for struct can_frame */
68 int main(int argc, char **argv)
71 struct can_frame frame;
73 static struct ifreq ifr;
74 static struct sockaddr_ll sll;
75 char *ifname = "vcan2";
78 int send_one_frame = 0;
80 while ((opt = getopt(argc, argv, "i:s")) != -1) {
92 fprintf(stderr, "Unknown option %c\n", opt);
97 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_CAN));
103 if (strcmp(ifname, "any") == 0)
106 strcpy(ifr.ifr_name, ifname);
107 ioctl(s, SIOCGIFINDEX, &ifr);
108 ifindex = ifr.ifr_ifindex;
111 sll.sll_family = AF_PACKET;
112 sll.sll_ifindex = ifindex;
113 sll.sll_protocol = htons(ETH_P_CAN);
115 if (bind(s, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
122 frame.can_id = 0x123;
124 frame.data[0] = 0x11;
125 frame.data[1] = 0x22;
127 nbytes = write(s, &frame, sizeof(struct can_frame));
132 if (nbytes != sizeof(struct can_frame)) {
140 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
143 } else if (nbytes < sizeof(struct can_frame)) {
144 fprintf(stderr, "read: incomplete CAN frame\n");
147 if (frame.can_id & CAN_EFF_FLAG)
148 printf("%8X ", frame.can_id & CAN_EFF_MASK);
150 printf("%3X ", frame.can_id & CAN_SFF_MASK);
152 printf("[%d] ", frame.can_dlc);
154 for (i = 0; i < frame.can_dlc; i++) {
155 printf("%02X ", frame.data[i]);
157 if (frame.can_id & CAN_RTR_FLAG)
158 printf("remote request");