Skip to content

Commit e77c663

Browse files
refactor: Split file by functionality
1 parent d9bf016 commit e77c663

31 files changed

Lines changed: 2296 additions & 1353 deletions

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
sudo apt-get install -y clang-format
2727
- name: Run clang-format
2828
run: |
29-
clang-format --dry-run --Werror src/*.c
29+
clang-format --dry-run --Werror src/*.c include/*.h
3030
3131
Build:
3232
name: Build
@@ -130,9 +130,9 @@ jobs:
130130
VERSION="${{ inputs.version }}"
131131
132132
if [ -n "$VERSION" ]; then
133-
make STATIC=1 CROSS_PREFIX="$CROSS_PREFIX" VERSION="$VERSION"
133+
make -j$(nproc) CFLAGS="-Werror" STATIC=1 CROSS_PREFIX="$CROSS_PREFIX" VERSION="$VERSION"
134134
else
135-
make STATIC=1 CROSS_PREFIX="$CROSS_PREFIX"
135+
make -j$(nproc) CFLAGS="-Werror" STATIC=1 CROSS_PREFIX="$CROSS_PREFIX"
136136
fi
137137
- name: Upload ${{ matrix.name }}
138138
uses: actions/upload-artifact@v4

Makefile

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ CROSS_PREFIX :=
22
CC=$(CROSS_PREFIX)gcc
33
STRIP=$(CROSS_PREFIX)strip
44

5-
override CFLAGS+=-O3 -std=c99 -pedantic -Wall -Wextra
5+
PREFIX=/usr/local
6+
BINDIR=$(PREFIX)/bin
7+
SRCDIR=src
8+
INCLUDEDIR=include
9+
BUILDDIR=build
10+
SRCS := $(wildcard $(SRCDIR)/*.c)
11+
OBJS := $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRCS))
12+
13+
override CFLAGS+=-O3 -std=c99 -I$(INCLUDEDIR) -pedantic -Wall -Wextra
614
override LDFLAGS+=-lnetfilter_queue -lnfnetlink -lmnl
715

816
ifdef VERSION
9-
override CFLAGS += -DVERSION=\"$(VERSION)\"
17+
override CFLAGS += -DVERSION=\"$(VERSION)\"
1018
endif
1119

12-
PREFIX=/usr/local
13-
BINDIR=$(PREFIX)/bin
14-
BUILDDIR=build
15-
1620
FAKEHTTP=$(BUILDDIR)/fakehttp
1721

1822
ifeq ($(STATIC), 1)
@@ -24,9 +28,17 @@ all: $(FAKEHTTP)
2428
clean:
2529
$(RM) -r $(BUILDDIR)
2630

27-
$(FAKEHTTP): src/fakehttp.c
31+
$(BUILDDIR):
2832
mkdir -p $(BUILDDIR)
29-
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
33+
34+
$(BUILDDIR)/%.d: $(SRCDIR)/%.c | $(BUILDDIR)
35+
$(CC) $(CFLAGS) -MM -MT $(@:.d=.o) $< -MF $@
36+
37+
$(BUILDDIR)/%.o: $(SRCDIR)/%.c | $(BUILDDIR)
38+
$(CC) $(CFLAGS) -c $< -o $@
39+
40+
$(FAKEHTTP): $(OBJS) $(MKS)
41+
$(CC) $(OBJS) -o $@ $(LDFLAGS)
3042
$(STRIP) $@
3143

3244
install: all
@@ -37,3 +49,7 @@ uninstall:
3749
$(RM) $(DESTDIR)$(BINDIR)/fakehttp
3850

3951
.PHONY: all clean install uninstall
52+
53+
ifneq ($(MAKECMDGOALS),clean)
54+
-include $(OBJS:.o=.d)
55+
endif

include/globvar.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* globvar.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_GLOBVAR_H
21+
#define FH_GLOBVAR_H
22+
23+
#include <stdint.h>
24+
#include <stdio.h>
25+
26+
struct fh_context {
27+
int exit;
28+
int sockfd;
29+
FILE *logfp;
30+
31+
/* -d */ int daemon;
32+
/* -h */ const char *hostname;
33+
/* -i */ const char *iface;
34+
/* -k */ int killproc;
35+
/* -m */ uint32_t fwmark;
36+
/* -n */ uint32_t nfqnum;
37+
/* -r */ int repeat;
38+
/* -s */ int silent;
39+
/* -t */ uint8_t ttl;
40+
/* -w */ const char *logpath;
41+
/* -x */ uint32_t fwmask;
42+
/* -z */ int use_iptables;
43+
};
44+
45+
extern struct fh_context g_ctx;
46+
47+
#endif /* FH_GLOBVAR_H */

include/ipv4ipt.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* ipv4ipt.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_IPV4IPT_H
21+
#define FH_IPV4IPT_H
22+
23+
int fh_ipt4_flush(int auto_create);
24+
25+
int fh_ipt4_add(void);
26+
27+
#endif /* FH_IPV4IPT_H */

include/ipv4nft.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* ipv4nft.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_IPV4NFT_H
21+
#define FH_IPV4NFT_H
22+
23+
int fh_nft4_flush(int auto_create);
24+
25+
int fh_nft4_add(void);
26+
27+
#endif /* FH_IPV4NFT_H */

include/ipv4pkt.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* ipv4pkt.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_IPV4PKT_H
21+
#define FH_IPV4PKT_H
22+
23+
#include <stdint.h>
24+
#include <stdlib.h>
25+
26+
int fh_pkt4_make(char *buffer, size_t buffer_size, uint32_t saddr_be,
27+
uint32_t daddr_be, uint16_t sport_be, uint16_t dport_be,
28+
uint32_t seq_be, uint32_t ackseq_be, int psh,
29+
char *tcp_payload, size_t tcp_payload_size);
30+
31+
#endif /* FH_IPV4PKT_H */

include/ipv6ipt.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* ipv6ipt.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_IPV6IPT_H
21+
#define FH_IPV6IPT_H
22+
23+
/* TODO: NOT IMPLEMENTED */
24+
int fh_ipt6_flush(int auto_create);
25+
26+
/* TODO: NOT IMPLEMENTED */
27+
int fh_ipt6_add(void);
28+
29+
#endif /* FH_IPV6IPT_H */

include/ipv6nft.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* ipv6nft.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_IPV6NFT_H
21+
#define FH_IPV6NFT_H
22+
23+
/* TODO: NOT IMPLEMENTED */
24+
int fh_nft6_flush(int auto_create);
25+
26+
/* TODO: NOT IMPLEMENTED */
27+
int fh_nft6_add(void);
28+
29+
#endif /* FH_IPV6NFT_H */

include/ipv6pkt.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* ipv6pkt.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_IPV6PKT_H
21+
#define FH_IPV6PKT_H
22+
23+
#include <stdint.h>
24+
#include <stdlib.h>
25+
26+
/* TODO: NOT IMPLEMENTED */
27+
int fh_pkt6_make(char *buffer, size_t buffer_size, uint8_t *saddr_be,
28+
uint8_t *daddr_be, uint16_t sport_be, uint16_t dport_be,
29+
uint32_t seq_be, uint32_t ackseq_be, int psh,
30+
char *tcp_payload, size_t tcp_payload_size);
31+
32+
#endif /* FH_IPV6PKT_H */

include/logging.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* logging.h - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
3+
*
4+
* Copyright (C) 2025 MikeWang000000
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef FH_LOGGING_H
21+
#define FH_LOGGING_H
22+
23+
#define E(...) fh_logger(__func__, __FILE__, __LINE__, __VA_ARGS__)
24+
#define E_RAW(...) fh_logger_raw(__VA_ARGS__)
25+
#define E_INFO(...) \
26+
if (!g_ctx.silent) { \
27+
E(__VA_ARGS__); \
28+
}
29+
30+
int fh_logger_setup(void);
31+
32+
void fh_logger_cleanup(void);
33+
34+
void fh_logger(const char *funcname, const char *filename, unsigned long line,
35+
const char *fmt, ...);
36+
37+
void fh_logger_raw(const char *fmt, ...);
38+
39+
#endif /* FH_LOGGING_H */

0 commit comments

Comments
 (0)