| |
| /****************************************************************************** |
| *(C) Copyright 2015 Marvell International Ltd. |
| * All Rights Reserved |
| ******************************************************************************/ |
| |
| #include <arpa/inet.h> |
| #include <stdbool.h> |
| #include <string.h> |
| #include <include/log.h> |
| #include "chl_util.h" |
| |
| #define INET_PROCF "/proc/net/if_inet6" |
| #define INET_PROCF_FORMAT \ |
| " %2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx %*x %x %x %*x %s" |
| |
| int validate_ip(const char *addr, int family) |
| { |
| unsigned char buf[sizeof(struct in6_addr)]; |
| |
| if (family != AF_INET6 && family != AF_INET) |
| return 1; |
| |
| return inet_pton(family, addr, buf) != 1; |
| } |
| |
| int get_global_ipv6(const char *ifname, char *addr, int *prefix) |
| { |
| FILE *f; |
| int scope, ret = 1; |
| unsigned char ipv6[16]; |
| char dname[16]; |
| |
| f = fopen(INET_PROCF, "r"); |
| if (!f) { |
| CHL_ERR("failed to open %s\n", INET_PROCF); |
| return 1; |
| } |
| |
| while (fscanf(f, INET_PROCF_FORMAT, &ipv6[0], &ipv6[1], |
| &ipv6[2],&ipv6[3], &ipv6[4], &ipv6[5], &ipv6[6], |
| &ipv6[7],&ipv6[8], &ipv6[9], &ipv6[10], &ipv6[11], |
| &ipv6[12], &ipv6[13], &ipv6[14], &ipv6[15], |
| prefix, &scope, dname) != EOF) { |
| |
| if (strcmp(ifname, dname)) |
| continue; |
| |
| if (scope) |
| continue; |
| |
| if (inet_ntop(AF_INET6, ipv6, addr, INET6_ADDRSTRLEN) == NULL) |
| continue; |
| |
| ret = 0; |
| break; |
| } |
| |
| fclose(f); |
| return ret; |
| } |
| |
| bool str_starts_with(const char *str, const char *prefix) |
| { |
| size_t lenpre = strlen(prefix), lenstr = strlen(str); |
| return lenstr < lenpre ? false : strncasecmp(prefix, str, lenpre) == 0; |
| } |
| |
| void cid_to_ccinet(int cid, char *ccinet) |
| { |
| sprintf(ccinet, "ccinet%d", cid - 1); |
| } |
| |
| void cid_to_section(int cid, char *wan) |
| { |
| sprintf(wan, "wan%d", cid - 1); |
| } |
| |
| void cid_to_section6(int cid, char *wan6) |
| { |
| sprintf(wan6, "wan6%d", cid - 1); |
| } |