29 #include <linux/version.h>
30 #include <linux/if_ether.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
40 #include <sys/ioctl.h>
41 #include <net/ethernet.h>
42 #include <netpacket/packet.h>
63 mac = (
char *) malloc(6);
64 mac[0] = strtoul(string1, &string2, 16);
65 for(i = 1; i <= 5; i++){
66 mac[i] = strtoul(string2 + 1, &string2, 16);
78 static void die(
const char *msg)
84 static unsigned char gethex(
const char *s,
char **end){
86 while(isspace(*s)) s++;
88 return strtoul(s, end, 16);
92 unsigned char *hex = malloc((strlen(s) +1) / 3);
95 *p =
gethex(s, (
char **) &s);
109 int main(
int argc,
char *argv[])
118 src = (
char *) malloc(6);
124 dest = (
char *) malloc(6);
142 static struct option long_options[] = {
144 {
"src", required_argument, 0,
's'},
146 {
"dst", required_argument, 0,
'd'},
148 {
"type", required_argument, 0,
't'},
150 {
"raw", required_argument, 0,
'r'},
152 {
"interface", required_argument, 0,
'i'},
156 int option_index = 0;
158 c = getopt_long(argc, argv,
"s:d:t:r:i:", long_options, &option_index);
167 if(long_options[option_index].flag != 0)
169 printf(
"option %s", long_options[option_index].name);
171 printf(
" with arg %s", optarg);
176 printf(
"option -s with value `%s'\n", optarg);
189 printf(
"option -d with value `%s'\n", optarg);
198 memcpy(dest,
mac, 6);
202 printf(
"option -t with value `%s'\n", optarg);
209 if(strcmp(optarg,
"ipv4") == 0){
215 }
else if(strcmp(optarg,
"ipv6") == 0){
221 }
else if(strcmp(optarg,
"can") == 0){
226 }
else if(strcmp(optarg,
"canfd") == 0){
235 }
else if(strcmp(optarg,
"none") == 0){
243 }
else if(strncmp(optarg,
"0x", 2) == 0){
244 type = strtoul(optarg + 2, &optarg, 16);
246 type = strtoul(optarg, &optarg, 16);
251 printf(
"option -r with value `%s'\n", optarg);
252 raw_index = optind - 1;
256 printf(
"option -i with value `%s'\n", optarg);
257 name_index = optind - 1;
271 printf(
"source: %hhX", *src);
272 printf(
":%hhX", *(src + 1));
273 printf(
":%hhX", *(src + 2));
274 printf(
":%hhX", *(src + 3));
275 printf(
":%hhX", *(src + 4));
276 printf(
":%hhX\n", *(src + 5));
277 printf(
"destination: %hhX", *dest);
278 printf(
":%hhX", *(dest + 1));
279 printf(
":%hhX", *(dest + 2));
280 printf(
":%hhX", *(dest + 3));
281 printf(
":%hhX", *(dest + 4));
282 printf(
":%hhX\n", *(dest + 5));
283 printf(
"type: %hX \n", type);
288 int fd = socket(AF_PACKET, SOCK_DGRAM, htons(type));
295 die(strerror(errno));
302 if(name_index == -1){
305 die(
"name/index number of the network interface required");
308 size_t if_name_len = strlen(argv[name_index]);
309 if(if_name_len <
sizeof(ifr.ifr_name)){
310 memcpy(ifr.ifr_name, argv[name_index], if_name_len);
311 ifr.ifr_name[if_name_len] = 0;
313 printf(
"sizeof(ifr.ifr_name): %d",
sizeof(ifr.ifr_name));
314 printf(
" sollte groesser sein als if_name_len: %d", if_name_len);
317 die(
"interface name is too long");
319 if(ioctl(fd, SIOCGIFINDEX, &ifr) == -1){
322 die(strerror(errno));
324 int ifindex = ifr.ifr_ifindex;
329 struct sockaddr_ll addr = {0};
330 addr.sll_family = AF_PACKET;
331 addr.sll_ifindex = ifindex;
332 addr.sll_halen = ETHER_ADDR_LEN;
333 addr.sll_protocol = htons(type);
334 memcpy(addr.sll_addr, dest, ETHER_ADDR_LEN);
343 die(
"No data to be sent");
348 unsigned char *data = NULL;
349 int length = strlen(argv[raw_index]);
351 int data_length = ((strlen(argv[raw_index]) + 1) / 3);
365 die(
"The raw data is too long for a can frame");
368 memset(frame.data, 0, 8);
371 memcpy(frame.data, data, data_length);
376 if(sendto(fd, &frame,
sizeof(
struct can_frame), 0, (
struct sockaddr*)&addr,
sizeof(addr)) == -1){
379 die(strerror(errno));
381 }
else if(type == 0x000D){
382 printf(
"Ja, wir haben einen canfd\n");
391 die(
"The raw data is too long for a canfd frame");
397 frame.
len = data_length;
398 memcpy(frame.data, data, data_length);
399 printf(
"Alles kopiert und so. Wird gleich gesendet.\n");
404 if(sendto(fd, &frame,
sizeof(
struct canfd_frame), 0, (
struct sockaddr*)&addr,
sizeof(addr)) == -1){
407 die(strerror(errno));
409 printf(
"Jap, gesendet.\n");
412 if(sendto(fd, data, data_length, 0, (
struct sockaddr*)&addr,
sizeof(addr)) == -1){
415 die(strerror(errno));