1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 *
5 * Description: Demo example of configuring NewIP address.
6 *
7 * Author: Yang Yanjun <yangyanjun@huawei.com>
8 *
9 * Data: 2022-09-06
10 */
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #include "nip_uapi.h"
19 #include "nip_lib.h"
20
21 /* get ifindex based on the device name
22 * struct ifreq ifr;
23 * struct nip_ifreq ifrn;
24 * ioctl(fd, SIOGIFINDEX, &ifr);
25 * ifr.ifr_ifindex; ===> ifindex
26 */
nip_add_addr(int ifindex,const struct nip_addr * addr,int opt)27 int nip_add_addr(int ifindex, const struct nip_addr *addr, int opt)
28 {
29 int fd, ret;
30 struct nip_ifreq ifrn;
31
32 fd = socket(AF_NINET, SOCK_DGRAM, 0);
33 if (fd < 0)
34 return -1;
35
36 memset(&ifrn, 0, sizeof(ifrn));
37 ifrn.ifrn_addr = *addr;
38 ifrn.ifrn_ifindex = ifindex;
39
40 ret = ioctl(fd, opt, &ifrn);
41 if (ret < 0 && errno != EEXIST) { // ignore File Exists error
42 printf("cfg newip addr fail, ifindex=%d, opt=%d, ret=%d.\n", ifindex, opt, ret);
43 close(fd);
44 return -1;
45 }
46
47 close(fd);
48 return 0;
49 }
50
cmd_help(void)51 void cmd_help(void)
52 {
53 /* nip_addr wlan0 add 01 (在wlan0上配置地址01) */
54 /* nip_addr wlan0 del 01 (在wlan0上删除地址01) */
55 printf("[cmd example] nip_addr <netcard-name> { add | del } <addr>\n");
56 }
57
parse_name(char ** argv,int * ifindex,char * dev)58 int parse_name(char **argv, int *ifindex, char *dev)
59 {
60 int len = strlen(*argv);
61
62 memset(dev, 0, ARRAY_LEN);
63 if (!len || len >= (ARRAY_LEN - 1))
64 return -1;
65 memcpy(dev, *argv, len);
66 dev[len + 1] = '\0';
67
68 if (strncmp(dev, NIC_NAME_CHECK, strlen(NIC_NAME_CHECK))) {
69 printf("unsupport addr cfg cmd-1, cmd=%s\n", dev);
70 cmd_help();
71 return -1;
72 }
73 return nip_get_ifindex(dev, ifindex);
74 }
75
parse_cmd(char ** argv,int * opt)76 int parse_cmd(char **argv, int *opt)
77 {
78 char cmd[ARRAY_LEN];
79 int len = strlen(*argv);
80
81 memset(cmd, 0, ARRAY_LEN);
82 if (!len || len >= (ARRAY_LEN - 1))
83 return -1;
84 memcpy(cmd, *argv, len);
85 cmd[len + 1] = '\0';
86
87 if (!strncmp(cmd, CMD_ADD, strlen(CMD_ADD))) {
88 *opt = SIOCSIFADDR;
89 } else if (!strncmp(cmd, CMD_DEL, strlen(CMD_DEL))) {
90 *opt = SIOCDIFADDR;
91 } else {
92 printf("unsupport addr cfg cmd-2, cmd=%s\n", cmd);
93 cmd_help();
94 return -1;
95 }
96 return 0;
97 }
98
main(int argc,char ** argv_input)99 int main(int argc, char **argv_input)
100 {
101 char dev[ARRAY_LEN];
102 int ret;
103 int opt;
104 int ifindex = 0;
105 char **argv = argv_input;
106 struct nip_addr addr = {0};
107
108 if (argc != DEMO_INPUT_3) {
109 printf("unsupport addr cfg input, argc=%d\n", argc);
110 cmd_help();
111 return -1;
112 }
113
114 /* 配置参数1解析: <netcard-name> */
115 argv++;
116 ret = parse_name(argv, &ifindex, dev);
117 if (ret != 0)
118 return -1;
119
120 /* 配置参数2解析: { add | del } */
121 argv++;
122 ret = parse_cmd(argv, &opt);
123 if (ret != 0)
124 return -1;
125
126 /* 配置参数3解析: <addr> */
127 argv++;
128 if (nip_get_addr(argv, &addr)) {
129 printf("unsupport addr cfg cmd-3\n");
130 cmd_help();
131 return 1;
132 }
133
134 ret = nip_add_addr(ifindex, &addr, opt);
135 if (ret != 0)
136 return -1;
137
138 printf("%s (ifindex=%d) cfg addr success\n", dev, ifindex);
139 return 0;
140 }
141
142