格式化风格

This commit is contained in:
aixiao 2022-09-29 14:45:29 +08:00
parent 526aac6765
commit 78ffb8d017
6 changed files with 90 additions and 118 deletions

View File

@ -15,8 +15,7 @@ int x_get_rule(const char *chain, struct xtc_handle *handle, char *ipv4)
inet_ntop(AF_INET, &(entry->ip.dst), addr, sizeof(addr));
//printf("%s\n", addr);
//printf("%s\n", t);
if (0 == strcmp(ipv4, addr) && 0 == strcmp(t, "DROP") && 0 == strcmp(entry_match->u.user.name, "tcp"))
{
if (0 == strcmp(ipv4, addr) && 0 == strcmp(t, "DROP") && 0 == strcmp(entry_match->u.user.name, "tcp")) {
r = 0;
break;
}
@ -35,8 +34,7 @@ int show_all_rule(char *ipv4)
handle = iptc_init("filter");
for (chain = iptc_first_chain(handle); chain; chain = iptc_next_chain(handle))
{
for (chain = iptc_first_chain(handle); chain; chain = iptc_next_chain(handle)) {
if (chain != NULL && handle != NULL) {
iptc_get_policy(chain, &counter, handle);
}
@ -99,8 +97,7 @@ struct ipt_entry_match *get_match(const char *sports, const char *dports, unsign
return match;
}
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src,
unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append)
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src, unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append)
{
struct xtc_handle *handle;
struct ipt_entry *chain_entry;
@ -200,7 +197,6 @@ int iptc_add_rule(const char *table, const char *chain, int protocol, const char
return 1;
}
if (entry_match)
free(entry_match);
free(entry_target);

View File

@ -18,7 +18,6 @@
/* Dest port. */
#define NFC_IP_DST_PT 0x0400
#ifndef IPT_MIN_ALIGN
#define IPT_MIN_ALIGN (__alignof__(struct ipt_entry))
#endif
@ -30,9 +29,7 @@
#define IPTC_TARGET_SIZE XT_ALIGN(sizeof(struct ipt_entry_target))
#define IPTC_FULL_SIZE IPTC_ENTRY_SIZE + IPTC_MATCH_SIZE + IPTC_TARGET_SIZE
int show_all_rule(char *ipv4);
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src,
unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append);
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src, unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append);
#endif

32
rhost.c
View File

@ -58,7 +58,6 @@ static char *GET_PUBLIC_IP(char *URL)
//对于同一次阻塞的curl_easy_perform而言在写完获取的数据之前会多次调用 WriteMemoryCallback
res = curl_easy_perform(curl_handle);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
@ -91,14 +90,12 @@ int dingding_warning(char *illegal_ip, char *public_ip, conf *conf)
char temp[64];
char jsonObj[BUFFER];
memset(jsonObj, 0, BUFFER);
memset(temp, 0, 64);
strcpy(temp, public_ip);
temp[strlen(public_ip) - 1] = '\0';
if ((fp = fopen("libcurl_ding.log", "wt+")) == NULL)
{
if ((fp = fopen("libcurl_ding.log", "wt+")) == NULL) {
return 1;
}
@ -107,8 +104,7 @@ int dingding_warning(char *illegal_ip, char *public_ip, conf *conf)
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl == NULL)
{
if (curl == NULL) {
return 1;
}
@ -157,7 +153,6 @@ int mail_warning(char *illegal_ip, char *public_ip, conf *conf)
char text[BUFFER];
char temp[64];
memset(buff, 0, BUFFER);
memset(text, 0, BUFFER);
memset(temp, 0, 64);
@ -166,13 +161,11 @@ int mail_warning(char *illegal_ip, char *public_ip, conf *conf)
temp[strlen(public_ip) - 1] = '\0';
sprintf(text, "echo \"主机:%s, 禁止%s访问\" | mail -s \"System ban IP\" %s", temp, illegal_ip, conf->RECV_MAIL);
if (NULL == (fp = popen(text, "r")))
{
if (NULL == (fp = popen(text, "r"))) {
perror("popen text");
}
while (fgets(buff, BUFFER, fp) != NULL)
{
while (fgets(buff, BUFFER, fp) != NULL) {
buff[strlen(buff) - 1] = '\0';
}
@ -193,14 +186,12 @@ int QQ_mail_warning(char *illegal_ip, char *public_ip, conf *conf)
memset(text, 0, BUFFER);
memset(temp, 0, 32);
strcpy(temp, public_ip);
temp[strlen(public_ip) - 1] = '\0';
sprintf(text, "主机:%s, 禁止%s访问", temp, illegal_ip);
sprintf(string, QQMAIL, conf->RECV_MAIL, text);
return system(string);
}
@ -273,7 +264,6 @@ int rule(conf *conf)
strcat(command, "\"");
strcat(command, awk);
if ((fp = popen(command, "r")) == NULL) // 执行命令
{
perror("popen command");
@ -350,22 +340,18 @@ int main(int argc, char *argv[], char **env)
read_conf("rhost.conf", conf);
//ptintf_conf(conf);
// 新版本获取公网IP
public_ip = GET_PUBLIC_IP("http://ip.sb");
//printf("%s", public_ip);
signal(SIGCHLD, sig_child); // 创建捕捉子进程退出信号
if (0 == strcmp(conf->DAEMON, "on"))
{
if (0 == strcmp(conf->DAEMON, "on")) {
goto goto_daemon;
}
if (argv[1] != NULL && 0 == strcmp(argv[1], "-d"))
{
if (argv[1] != NULL && 0 == strcmp(argv[1], "-d")) {
goto_daemon:
if (daemon(1, 1)) // 守护进程
{
@ -378,17 +364,13 @@ goto_daemon:
sleep(conf->TIME);
}
}
else
{
} else {
rule(conf);
}
free_conf(conf);
free(conf);
free(public_ip);
return 0;
}

View File

@ -13,7 +13,6 @@
#include <sys/types.h>
#include <arpa/inet.h>
#define CENTOS_SYSTEM 1
#define DEBISN_SYSTEM 2
#define UNKNOWN_SYSTEM 3
@ -33,10 +32,8 @@
#define QQMAIL "email -r %s -s \"System ban IP\" -t \"%s\""
extern void read_conf(char *filename, conf * configure);
extern void free_conf(conf * conf);
extern void ptintf_conf(conf * conf);
#endif