Skip to content

Commit

Permalink
refactor: change return types of IP and MAC validation functions to b…
Browse files Browse the repository at this point in the history
…ool and add is_file_exist function

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Dec 11, 2024
1 parent 79bc62f commit 22c70f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
20 changes: 14 additions & 6 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,29 @@ save_pid_file(const char *pf)
return;
}

// true: 1; false: 0
int
bool
is_valid_ip(const char *ip)
{
if (!ip) {
return 0;
return false;
}
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ip, &(sa.sin_addr));
return result != 0;
}

int
bool
is_valid_ip6(const char *ip)
{
if (!ip) {
return 0;
return false;
}
struct sockaddr_in6 sa;
int result = inet_pton(AF_INET6, ip, &(sa.sin6_addr));
return result != 0;
}

int
bool
is_valid_mac(const char *mac)
{
int i = 0;
Expand Down Expand Up @@ -354,3 +353,12 @@ get_cpu_usage()

return percent_usage;
}

bool
is_file_exist(const char *file)
{
struct stat buf;
if(stat(file, &buf) == 0)
return true;
return false;
}
11 changes: 6 additions & 5 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#ifndef _UTIL_H_
#define _UTIL_H_

#include <sys/types.h>
#include <sys/socket.h>
#include <stdbool.h>

/** @brief Initialize the ICMP socket */
int init_icmp_socket(void);
Expand All @@ -24,12 +23,14 @@ void icmp6_ping(const char *);
/** @brief Save pid of this wifidog in pid file */
void save_pid_file(const char *);

int is_valid_ip(const char *);
bool is_valid_ip(const char *);

int is_valid_ip6(const char *);
bool is_valid_ip6(const char *);

int is_valid_mac(const char *);
bool is_valid_mac(const char *);

float get_cpu_usage();

bool is_file_exist(const char *file);

#endif /* _UTIL_H_ */

0 comments on commit 22c70f8

Please sign in to comment.