Skip to content

Commit

Permalink
Deprecated following functions:
Browse files Browse the repository at this point in the history
* bool parse_ipv4addr(char *ipstr, unsigned int *ip)
* char *get_stripv4(unsigned int *ip, bool _static)
* char *get_stripv4_r(unsigned int *ip, char *ipstr)
  • Loading branch information
jacopodl committed Jul 12, 2017
1 parent 4a8f50b commit 5eb9901
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
38 changes: 34 additions & 4 deletions include/ipv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,59 @@ 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`.
* @param __IN__ip Pointer to integer(32bit) contains IPv4.
* @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`.
* @param __IN__ip Pointer to integer(32bit) contains IPv4.
* @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.
Expand Down
34 changes: 34 additions & 0 deletions src/ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5eb9901

Please sign in to comment.