• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 Huawei Device Co., Ltd.
4  *
5  * Description: Provide NewIP universal library.
6  *
7  * Author: Yang Yanjun <yangyanjun@huawei.com>
8  *
9  * Data: 2022-07-18
10  */
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <linux/if.h>  /* struct ifreq depend */
17 
18 #include "nip_uapi.h"
19 #include "nip_lib.h"
20 
21 #define ADDR_STR_LEN 2
22 #define STR_FMT_1    55
23 #define STR_FMT_2    87
24 
nip_get_ifindex(const char * ifname,int * ifindex)25 int32_t nip_get_ifindex(const char *ifname, int *ifindex)
26 {
27 	int fd;
28 	struct ifreq ifr;
29 
30 	memset(&ifr, 0, sizeof(ifr));
31 	strcpy(ifr.ifr_name, ifname);
32 	fd = socket(AF_NINET, SOCK_DGRAM, 0);
33 	if (fd < 0) {
34 		printf("creat socket fail, ifname=%s\n", ifname);
35 		return -1;
36 	}
37 	if ((ioctl(fd, SIOCGIFINDEX, &ifr)) < 0) {
38 		printf("get ifindex fail, ifname=%s\n", ifname);
39 		close(fd);
40 		return -1;
41 	}
42 	close(fd);
43 
44 	printf("%s ifindex=%u\n", ifname, ifr.ifr_ifindex);
45 	*ifindex = ifr.ifr_ifindex;
46 	return 0;
47 }
48 
nip_addr_fmt(char * addr_str,struct nip_addr * sap,int addrlen_input)49 int nip_addr_fmt(char *addr_str, struct nip_addr *sap, int addrlen_input)
50 {
51 	unsigned char first_byte;
52 	int addrlen, i;
53 
54 	memset(sap, 0, sizeof(struct nip_addr));
55 	for (i = 0; i < INDEX_MAX; i++) {
56 		if (addr_str[i] == 0)
57 			break;
58 
59 		/* 0 ~ 9 = 48 ~ 57,  '0'构造成 0  = 48 - 48 */
60 		if (addr_str[i] >= '0' && addr_str[i] <= '9') {
61 			addr_str[i] = addr_str[i] - '0';
62 		/* A ~ F = 65 ~ 70,  'A'构造成 10 = 65 - 55 */
63 		} else if (addr_str[i] >= 'A' && addr_str[i] <= 'F') {
64 			addr_str[i] = addr_str[i] - STR_FMT_1;
65 		/* a ~ f = 97 ~ 102, 'a'构造成 10 = 97 - 87 */
66 		} else if (addr_str[i] >= 'a' && addr_str[i] <= 'f') {
67 			addr_str[i] = addr_str[i] - STR_FMT_2;
68 		} else {
69 			printf("Newip addr error: uaddr[%d]=%c\n", i, addr_str[i]);
70 			return 1;
71 		}
72 	}
73 
74 	first_byte = addr_str[0] << NIP_ADDR_LEN_4;
75 	first_byte += addr_str[1];
76 	if (first_byte <= ADDR_FIRST_DC)
77 		addrlen = NIP_ADDR_LEN_1;
78 	else if (first_byte <= ADDR_FIRST_F0 || first_byte == ADDR_FIRST_FF)
79 		addrlen = NIP_ADDR_LEN_2;
80 	else if (first_byte == ADDR_FIRST_F1)
81 		addrlen = NIP_ADDR_LEN_3;
82 	else if (first_byte == ADDR_FIRST_F2)
83 		addrlen = NIP_ADDR_LEN_5;
84 	else if (first_byte == ADDR_FIRST_F3)
85 		addrlen = NIP_ADDR_LEN_7;
86 	else if (first_byte == ADDR_FIRST_FE)
87 		addrlen = NIP_ADDR_LEN_8;
88 	else
89 		addrlen = 0;
90 
91 	if (addrlen_input != addrlen) {
92 		printf("Newip addr error, first_byte=0x%x\n", first_byte);
93 		return 1;
94 	}
95 
96 	sap->bitlen = addrlen * NIP_ADDR_LEN_8;
97 	printf("*************************************************\n");
98 	printf("Newip addr len=%d\n", addrlen);
99 	for (i = 0; i < addrlen; i++) {
100 		sap->NIP_ADDR_FIELD8[i] = addr_str[i * INDEX_2] << INDEX_4;
101 		sap->NIP_ADDR_FIELD8[i] += addr_str[i * INDEX_2 + 1];
102 		printf("%02x ", sap->NIP_ADDR_FIELD8[i]);
103 	}
104 	printf("\n*************************************************\n\n");
105 
106 	return 0;
107 }
108 
nip_get_addr(char ** args,struct nip_addr * addr)109 int nip_get_addr(char **args, struct nip_addr *addr)
110 {
111 	unsigned int len;
112 	char *sp = *args;
113 	int addrlen_input = 0;
114 	__u8 addr_str[INDEX_MAX] = {0};
115 
116 	while (*sp != '\0') {
117 		addrlen_input += 1;
118 		sp++;
119 	}
120 
121 	if (addrlen_input % ADDR_STR_LEN != 0) {
122 		printf("NewIP addr str-len invalid, addrlen_input=%d\n", addrlen_input);
123 		return -1;
124 	}
125 
126 	len = strlen(*args);
127 	if (!len || len >= (INDEX_MAX - 1))
128 		return -1;
129 	memcpy(addr_str, *args, len);
130 	addr_str[len + 1] = '\0';
131 
132 	return nip_addr_fmt(addr_str, addr, addrlen_input / ADDR_STR_LEN);
133 }
134 
135