]> git.ipfire.org Git - thirdparty/iw.git/blob - vendor.c
iw: add and fix -Wsign-compare
[thirdparty/iw.git] / vendor.c
1 #include <errno.h>
2 #include <string.h>
3
4 #include <netlink/genl/genl.h>
5 #include <netlink/genl/family.h>
6 #include <netlink/genl/ctrl.h>
7 #include <netlink/msg.h>
8 #include <netlink/attr.h>
9
10 #include "nl80211.h"
11 #include "iw.h"
12
13 SECTION(vendor);
14
15 static int read_file(FILE *file, char *buf, size_t size)
16 {
17 size_t count = 0;
18 int data;
19
20 while ((data = fgetc(file)) != EOF) {
21 if (count >= size)
22 return -EINVAL;
23 buf[count] = data;
24 count++;
25 }
26
27 return count;
28 }
29
30 static int read_hex(unsigned int argc, char **argv, char *buf, size_t size)
31 {
32 unsigned int i, data;
33 int res;
34
35 if (argc > size)
36 return -EINVAL;
37
38 for (i = 0; i < argc; i++) {
39 res = sscanf(argv[i], "0x%x", &data);
40 if (res != 1 || data > 0xff)
41 return -EINVAL;
42 buf[i] = data;
43 }
44
45 return argc;
46 }
47
48 static int handle_vendor(struct nl80211_state *state,
49 struct nl_msg *msg, int argc, char **argv,
50 enum id_input id)
51 {
52 unsigned int oui;
53 unsigned int subcmd;
54 char buf[2048] = {};
55 int res, count = 0;
56 FILE *file = NULL;
57
58 if (argc < 3)
59 return 1;
60
61 res = sscanf(argv[0], "0x%x", &oui);
62 if (res != 1) {
63 printf("Vendor command must start with 0x\n");
64 return 2;
65 }
66
67 res = sscanf(argv[1], "0x%x", &subcmd);
68 if (res != 1) {
69 printf("Sub command must start with 0x\n");
70 return 2;
71 }
72
73 if (!strcmp(argv[2], "-"))
74 file = stdin;
75 else
76 file = fopen(argv[2], "r");
77
78 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, oui);
79 NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd);
80
81 if (file) {
82 count = read_file(file, buf, sizeof(buf));
83 fclose(file);
84 } else
85 count = read_hex(argc - 2, &argv[2], buf, sizeof(buf));
86
87 if (count < 0)
88 return -EINVAL;
89
90 if (count > 0)
91 NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, count, buf);
92
93 return 0;
94
95 nla_put_failure:
96 return -ENOBUFS;
97 }
98
99 COMMAND(vendor, send, "<oui> <subcmd> <filename|-|hex data>", NL80211_CMD_VENDOR, 0, CIB_NETDEV, handle_vendor, "");