diff options
-rw-r--r-- | src/plugins/ietf/ietf_nat.c | 6 | ||||
-rw-r--r-- | src/plugins/sys_util.h | 40 | ||||
-rw-r--r-- | src/scvpp/inc/scvpp/comm.h | 2 | ||||
-rw-r--r-- | src/scvpp/src/comm.c | 28 |
4 files changed, 68 insertions, 8 deletions
diff --git a/src/plugins/ietf/ietf_nat.c b/src/plugins/ietf/ietf_nat.c index 9b2890e..9613b52 100644 --- a/src/plugins/ietf/ietf_nat.c +++ b/src/plugins/ietf/ietf_nat.c @@ -93,7 +93,7 @@ static int parse_instance_policy_external_ip_address_pool( if (sr_xpath_node_name_eq(val->xpath, "pool-id")) { SRP_LOG_WRN("%s not supported.", val->xpath); } else if(sr_xpath_node_name_eq(val->xpath, "external-ip-pool")) { - rc = prefix2address(tmp_str, val->data.string_val, &prefix); + rc = prefix2ip4(tmp_str, val->data.string_val, &prefix); if (0 != rc) { SRP_LOG_ERR_MSG("Error translate"); return SR_ERR_INVAL_ARG; @@ -336,7 +336,7 @@ static int parse_instance_mapping_table_mapping_entry( return SR_ERR_INVAL_ARG; } - rc = prefix2address(tmp_str, val->data.string_val, NULL); + rc = prefix2ip4(tmp_str, val->data.string_val, NULL); if (0 != rc) { SRP_LOG_ERR_MSG("Error translate"); return SR_ERR_INVAL_ARG; @@ -357,7 +357,7 @@ static int parse_instance_mapping_table_mapping_entry( return SR_ERR_INVAL_ARG; } - rc = prefix2address(tmp_str, val->data.string_val, NULL); + rc = prefix2ip4(tmp_str, val->data.string_val, NULL); if (0 != rc) { SRP_LOG_ERR_MSG("Error translate"); return SR_ERR_INVAL_ARG; diff --git a/src/plugins/sys_util.h b/src/plugins/sys_util.h index 692a012..3f85537 100644 --- a/src/plugins/sys_util.h +++ b/src/plugins/sys_util.h @@ -89,13 +89,12 @@ static inline int ip_prefix_split(const char* ip_prefix) * @brief Get IPv4 host address from IPv4 prefix. * * @param[out] dst Host IPv4 address. - * @param[in] src IPv4 Prefix. - * @param[in] length dst buffer length. + * @param[in] src IPv4 prefix. * @param[out] prefix Get Prefix length, optional value. Can be NULL. * @return -1 when failure, 0 on success. */ static inline int -prefix2address(char *dst, const char *src, uint8_t *prefix_length) +prefix2ip4(char *dst, const char *src, uint8_t *prefix_length) { if (!src || !dst) return -1; @@ -104,10 +103,41 @@ prefix2address(char *dst, const char *src, uint8_t *prefix_length) if (!p) return -1; // '/' not found - if ((p - src + 1) > VPP_IP4_ADDRESS_STRING_LEN) //+ 1 needed for \0 + size_t size = p - src; + if ((size + 1) > VPP_IP4_ADDRESS_STRING_LEN) //+ 1 needed for \0 return -1; - strncpy(dst, src, VPP_IP4_ADDRESS_STRING_LEN); + strncpy(dst, src, size); + + if (!prefix_length) + *prefix_length = atoi(++p); + + return 0; +} + +/** + * @brief Get IPv6 host address from IPv6 prefix. + * + * @param[out] dst Host IPv6 address. + * @param[in] src IPv6 prefix. + * @param[out] prefix Get Prefix length, optional value. Can be NULL. + * @return -1 when failure, 0 on success. + */ +static inline int +prefix2ip6(char *dst, const char *src, uint8_t *prefix_length) +{ + if (!src || !dst) + return -1; + + char *p = strchr(src, '/'); + if (!p) + return -1; // '/' not found + + size_t size = p - src; + if ((size + 1) > VPP_IP6_ADDRESS_STRING_LEN) //+ 1 needed for \0 + return -1; + + strncpy(dst, src, size); if (!prefix_length) *prefix_length = atoi(++p); diff --git a/src/scvpp/inc/scvpp/comm.h b/src/scvpp/inc/scvpp/comm.h index 8d2b2e2..3e6122e 100644 --- a/src/scvpp/inc/scvpp/comm.h +++ b/src/scvpp/inc/scvpp/comm.h @@ -195,6 +195,8 @@ api_name##_all_cb(vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_las int sc_aton(const char *cp, u8 * buf, size_t length); char * sc_ntoa(const u8 * buf); +int sc_pton(int af, const char *cp, u8 * buf); +char * sc_ntop(int af, const u8 * buf, char *addr); /** * @brief Function converts the u8 array from network byte order to host byte order. diff --git a/src/scvpp/src/comm.c b/src/scvpp/src/comm.c index a065b58..0169d0a 100644 --- a/src/scvpp/src/comm.c +++ b/src/scvpp/src/comm.c @@ -99,6 +99,34 @@ char* sc_ntoa(const u8 * buf) return inet_ntoa(addr); } +int sc_pton(int af, const char *cp, u8 * buf) +{ + ARG_CHECK2(false, cp, buf); + + int ret = inet_pton(af, cp, buf); + + if (0 == ret) + return -EINVAL; + + return 0; +} + +char* sc_ntop(int af, const u8 * buf, char *addr) +{ + ARG_CHECK(NULL, buf); + ARG_CHECK(NULL, addr); + + socklen_t size = 0; + if (af == AF_INET) + size = INET_ADDRSTRLEN; + else if (af == AF_INET6) + size = INET6_ADDRSTRLEN; + else + return NULL; + + return inet_ntop(af, (void*)buf, addr, size); +} + /** * @brief Function converts the u8 array from network byte order to host byte order. * |