diff --git a/CMakeLists.txt b/CMakeLists.txt index 74ee908..38821d9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 2.8) project(LibSpark) set (VERSION_MAJOR 2) -set(VERSION_MINOR 2) -set (VERSION_PATCH 1) +set(VERSION_MINOR 3) +set(VERSION_PATCH 0) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --std=gnu11 --pedantic") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --std=gnu11 --pedantic -DUSE_DEPRECATED") set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") set(INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include") diff --git a/include/ipv4.h b/include/ipv4.h index 5eeb911..082df58 100755 --- a/include/ipv4.h +++ b/include/ipv4.h @@ -135,13 +135,15 @@ bool get_device_ipv4(char *iface_name, struct netaddr_ip *ip); */ bool get_device_netmask(char *iface_name, struct netaddr_ip *netmask); +#ifdef USE_DEPRECATED + /** * @brief Parse string contains a ipv4 address in the form `000.000.000.000`. * @param ipstr String contains ipv4 address in the form `000.000.000.000`. - * @param __OUT__ip Pointer to netaddr_ip structure. + * @param __OUT__ip Pointer to int(32bit). * @return Function returns true if the address has been converted, false otherwise. */ -bool parse_ipv4addr(char *ipstr, unsigned int *ip); +bool parse_ipv4addr(char *ipstr, unsigned int *ip) __attribute__((deprecated)); /** * @brief Obtains ipv4 address in the form `000.000.000.000`. @@ -149,7 +151,7 @@ bool parse_ipv4addr(char *ipstr, unsigned int *ip); * @param _static Not allocate new memory, the result will be saved in internal static bufer. * @return Function returns string contains ip address. */ -char *get_stripv4(unsigned int *ip, bool _static); +char *get_stripv4(unsigned int *ip, bool _static) __attribute__((deprecated)); /** * @brief Obtains ipv4 address in the form `000.000.000.000`. @@ -157,7 +159,35 @@ char *get_stripv4(unsigned int *ip, bool _static); * @param __OUT__ipstr Pointer to string of dimension IPV4STRLEN. * @return Function returns string contains ip address. */ -char *get_stripv4_r(unsigned int *ip, char *ipstr); +char *get_stripv4_r(unsigned int *ip, char *ipstr) __attribute__((deprecated)); + +#else + +/** + * @brief Parse string contains a ipv4 address in the form `000.000.000.000`. + * @param ipstr String contains ipv4 address in the form `000.000.000.000`. + * @param __OUT__ip Pointer to netaddr_ip structure. + * @return Function returns true if the address has been converted, false otherwise. + */ +bool parse_ipv4addr(char *ipstr, struct netaddr_ip *ip); + +/** + * @brief Obtains ipv4 address in the form `000.000.000.000`. + * @param __IN__ip Pointer to netaddr_ip structure. + * @param _static Not allocate new memory, the result will be saved in internal static bufer. + * @return Function returns string contains ip address. + */ +char *get_stripv4(struct netaddr_ip *ip, bool _static); + +/** + * @brief Obtains ipv4 address in the form `000.000.000.000`. + * @param __IN__ip Pointer to netaddr_ip structure. + * @param __OUT__ipstr Pointer to string of dimension IPV4STRLEN. + * @return Function returns string contains ip address. + */ +char *get_stripv4_r(struct netaddr_ip *ip, char *ipstr); + +#endif /** * @brief Built a new IPv4 packet. diff --git a/src/ipv4.c b/src/ipv4.c index 7538a6f..0e90ba8 100755 --- a/src/ipv4.c +++ b/src/ipv4.c @@ -99,6 +99,8 @@ bool get_device_netmask(char *iface_name, struct netaddr_ip *netmask) { return ret; } +#ifdef USE_DEPRECATED + bool parse_ipv4addr(char *ipstr, unsigned int *ip) { unsigned int ipaddr[IPV4ADDRSIZE]; if (strlen(ipstr) >= IPV4STRLEN) @@ -127,6 +129,38 @@ inline char *get_stripv4_r(unsigned int *ip, char *ipstr) { return ipstr; } +#else + +bool parse_ipv4addr(char *ipstr, struct netaddr_ip *ip) { + unsigned int ipaddr[IPV4ADDRSIZE]; + if (strlen(ipstr) >= IPV4STRLEN) + return false; + if (sscanf(ipstr, "%u.%u.%u.%u", ipaddr, ipaddr + 1, ipaddr + 2, ipaddr + 3) != 4) + return false; + if (ipaddr[0] > 255 || ipaddr[1] > 255 || ipaddr[2] > 255 || ipaddr[3] > 255) + return false; + if (ip != NULL) + ip->ip = (ipaddr[3] << 24 | ipaddr[2] << 16 | ipaddr[1] << 8 | ipaddr[0]); + return true; +} + +char *get_stripv4(struct netaddr_ip *ip, bool _static) { + static char static_buf[IPV4STRLEN]; + char *ipstr = static_buf; + if (!_static) { + if ((ipstr = (char *) malloc(IPV4STRLEN)) == NULL) + return NULL; + } + return get_stripv4_r(ip, ipstr); +} + +inline char *get_stripv4_r(struct netaddr_ip *ip, char *ipstr) { + sprintf(ipstr, "%u.%u.%u.%u", (ip->ip) & 0xFF, (ip->ip) >> 8 & 0xFF, (ip->ip) >> 16 & 0xFF, (ip->ip) >> 24 & 0xFF); + return ipstr; +} + +#endif + struct Ipv4Header *build_ipv4_packet(struct netaddr_ip *src, struct netaddr_ip *dst, unsigned char ihl, unsigned short id, unsigned char ttl, unsigned char proto, unsigned short paysize, unsigned char *payload) {