From da8a41390ef13b2623d354d97991f0ab4919ae09 Mon Sep 17 00:00:00 2001 From: coolsnowwolf Date: Sat, 5 Jan 2019 23:14:39 +0800 Subject: [PATCH] ppp: add shellsync ppp support --- package/network/services/ppp/Makefile | 8 +- package/network/services/ppp/files/ppp.sh | 10 +- .../ppp/patches/001-honor-ldflags.patch | 6 +- .../services/ppp/patches/203-opt_flags.patch | 11 + .../ppp/patches/511-pptp_cflags.patch | 11 + .../services/ppp/patches/511-syncppp.patch | 214 ++++++++++++++++++ package/network/services/shellsync/Makefile | 34 +++ .../services/shellsync/src/shellsync.c | 106 +++++++++ 8 files changed, 392 insertions(+), 8 deletions(-) create mode 100644 package/network/services/ppp/patches/511-pptp_cflags.patch create mode 100644 package/network/services/ppp/patches/511-syncppp.patch create mode 100644 package/network/services/shellsync/Makefile create mode 100644 package/network/services/shellsync/src/shellsync.c diff --git a/package/network/services/ppp/Makefile b/package/network/services/ppp/Makefile index beeaa53c22..0c81c46a89 100644 --- a/package/network/services/ppp/Makefile +++ b/package/network/services/ppp/Makefile @@ -10,7 +10,7 @@ include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=ppp PKG_VERSION:=2.4.7 -PKG_RELEASE:=12 +PKG_RELEASE:=13 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=https://download.samba.org/pub/ppp/ @@ -36,7 +36,7 @@ endef define Package/ppp $(call Package/ppp/Default) - DEPENDS:=+kmod-ppp + DEPENDS:=+kmod-ppp +libpthread +shellsync TITLE:=PPP daemon VARIANT:=default endef @@ -189,8 +189,8 @@ $(call Build/Configure/Default,, \ $(PKG_BUILD_DIR)/pppd/plugins/pppoatm/linux/ endef -TARGET_CFLAGS += -ffunction-sections -fdata-sections -TARGET_LDFLAGS += -Wl,--gc-sections +TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto +TARGET_LDFLAGS += -Wl,--gc-sections -flto -fuse-linker-plugin MAKE_FLAGS += COPTS="$(TARGET_CFLAGS)" \ PRECOMPILED_FILTER=1 \ diff --git a/package/network/services/ppp/files/ppp.sh b/package/network/services/ppp/files/ppp.sh index 99d5e4939f..b56b83c72d 100755 --- a/package/network/services/ppp/files/ppp.sh +++ b/package/network/services/ppp/files/ppp.sh @@ -225,7 +225,15 @@ proto_pppoe_setup() { json_get_var service service json_get_var host_uniq host_uniq - ppp_generic_setup "$config" \ +#By òåò壺²¢·¢²¦ºÅͬ²½µÄÇ°ÆÚ×¼±¸ + syncppp_option="" + [ "$(uci get syncdial.config.enabled)" == "1" ] && { + ppp_if_cnt=$(cat /etc/config/network | grep -c "proto 'pppoe'") + syncppp_option="syncppp $ppp_if_cnt" + shellsync $ppp_if_cnt 10 + } + ppp_generic_setup "$config" \ + $syncppp_option \ plugin rp-pppoe.so \ ${ac:+rp_pppoe_ac "$ac"} \ ${service:+rp_pppoe_service "$service"} \ diff --git a/package/network/services/ppp/patches/001-honor-ldflags.patch b/package/network/services/ppp/patches/001-honor-ldflags.patch index 1328811537..fae66a5ec7 100644 --- a/package/network/services/ppp/patches/001-honor-ldflags.patch +++ b/package/network/services/ppp/patches/001-honor-ldflags.patch @@ -5,15 +5,15 @@ radius.so: radius.o libradiusclient.a - $(CC) -o radius.so -shared radius.o libradiusclient.a -+ $(CC) $(COPTS) -o radius.so -shared radius.o libradiusclient.a ++ $(CC) $(CFLAGS) -o radius.so -shared radius.o libradiusclient.a radattr.so: radattr.o - $(CC) -o radattr.so -shared radattr.o -+ $(CC) $(COPTS) -o radattr.so -shared radattr.o ++ $(CC) $(CFLAGS) -o radattr.so -shared radattr.o radrealms.so: radrealms.o - $(CC) -o radrealms.so -shared radrealms.o -+ $(CC) $(COPTS) -o radrealms.so -shared radrealms.o ++ $(CC) $(CFLAGS) -o radrealms.so -shared radrealms.o CLIENTOBJS = avpair.o buildreq.o config.o dict.o ip_util.o \ clientid.o sendserver.o lock.o util.o md5.o diff --git a/package/network/services/ppp/patches/203-opt_flags.patch b/package/network/services/ppp/patches/203-opt_flags.patch index d0eb918299..069b8585f3 100644 --- a/package/network/services/ppp/patches/203-opt_flags.patch +++ b/package/network/services/ppp/patches/203-opt_flags.patch @@ -30,3 +30,14 @@ Signed-off-by: Jo-Philipp Wich OBJS = pppdump.o bsd-comp.o deflate.o zlib.o INSTALL= install +--- a/pppd/plugins/rp-pppoe/Makefile.linux ++++ b/pppd/plugins/rp-pppoe/Makefile.linux +@@ -39,7 +39,7 @@ debug.o: debug.c + $(CC) $(CFLAGS) -c -o debug.o debug.c + + rp-pppoe.so: plugin.o discovery.o if.o common.o +- $(CC) $(CFLAGS) -o rp-pppoe.so -shared plugin.o discovery.o if.o common.o ++ $(CC) $(CFLAGS) -fPIC -o rp-pppoe.so -shared plugin.o discovery.o if.o common.o + + install: all + $(INSTALL) -d -m 755 $(LIBDIR) diff --git a/package/network/services/ppp/patches/511-pptp_cflags.patch b/package/network/services/ppp/patches/511-pptp_cflags.patch new file mode 100644 index 0000000000..548bf41c1f --- /dev/null +++ b/package/network/services/ppp/patches/511-pptp_cflags.patch @@ -0,0 +1,11 @@ +--- a/pppd/plugins/pptp/Makefile.linux ++++ b/pppd/plugins/pptp/Makefile.linux +@@ -20,7 +20,7 @@ all: pptp.so + $(CC) $(CFLAGS) -c -o $@ $< + + pptp.so: dirutil.o orckit_quirks.o pptp.o pptp_callmgr.o pptp_ctrl.o pptp_quirks.o util.o vector.o +- $(CC) -o pptp.so -shared dirutil.o orckit_quirks.o pptp.o pptp_callmgr.o pptp_ctrl.o pptp_quirks.o util.o vector.o ++ $(CC) -fPIC -o pptp.so -shared dirutil.o orckit_quirks.o pptp.o pptp_callmgr.o pptp_ctrl.o pptp_quirks.o util.o vector.o + + install: all + $(INSTALL) -d -m 755 $(LIBDIR) diff --git a/package/network/services/ppp/patches/511-syncppp.patch b/package/network/services/ppp/patches/511-syncppp.patch new file mode 100644 index 0000000000..fe58b8cc81 --- /dev/null +++ b/package/network/services/ppp/patches/511-syncppp.patch @@ -0,0 +1,214 @@ +diff -Naur ppp-2.4.5.0/pppd/chap-new.c ppp-2.4.5/pppd/chap-new.c +--- ppp-2.4.5.0/pppd/chap-new.c 2012-04-18 15:44:46.000000000 +0800 ++++ ppp-2.4.5/pppd/chap-new.c 2012-04-19 20:24:28.000000000 +0800 +@@ -37,6 +37,8 @@ + #include "chap-new.h" + #include "chap-md5.h" + ++#include "syncppp.h" ++ + #ifdef CHAPMS + #include "chap_ms.h" + #define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5) +@@ -481,6 +483,19 @@ + p[2] = len >> 8; + p[3] = len; + ++ if (npppd > 1) { ++ if (syncppp(npppd) < 0) { ++ error("syncppp sync fail"); ++ sem_unlink(SEM_COUNT_NAME); ++ sem_unlink(SEM_BLOCK_NAME); ++ } else { ++ info("syncppp sync succeeded"); ++ } ++ } else { ++ info("syncppp not active"); ++ } ++ ++ + output(0, response, PPP_HDRLEN + len); + } + +diff -Naur ppp-2.4.5.0/pppd/Makefile.linux ppp-2.4.5/pppd/Makefile.linux +--- ppp-2.4.5.0/pppd/Makefile.linux 2012-04-18 15:44:46.000000000 +0800 ++++ ppp-2.4.5/pppd/Makefile.linux 2012-04-19 20:01:05.000000000 +0800 +@@ -13,16 +13,16 @@ + + PPPDSRCS = main.c magic.c fsm.c lcp.c ipcp.c upap.c chap-new.c md5.c ccp.c \ + ecp.c ipxcp.c auth.c options.c sys-linux.c md4.c chap_ms.c \ +- demand.c utils.c tty.c eap.c chap-md5.c session.c ++ demand.c utils.c tty.c eap.c chap-md5.c session.c syncppp.c + + HEADERS = ccp.h session.h chap-new.h ecp.h fsm.h ipcp.h \ + ipxcp.h lcp.h magic.h md5.h patchlevel.h pathnames.h pppd.h \ +- upap.h eap.h ++ upap.h eap.h syncppp.h + + MANPAGES = pppd.8 + PPPDOBJS = main.o magic.o fsm.o lcp.o ipcp.o upap.o chap-new.o md5.o ccp.o \ + ecp.o auth.o options.o demand.o utils.o sys-linux.o ipxcp.o tty.o \ +- eap.o chap-md5.o session.o ++ eap.o chap-md5.o session.o syncppp.o + + # + # include dependencies if present +@@ -33,7 +33,7 @@ + # CC = gcc + # + COPTS = -O2 -pipe -Wall -g +-LIBS = ++LIBS = -lpthread + + # Uncomment the next 2 lines to include support for Microsoft's + # MS-CHAP authentication protocol. Also, edit plugins/radius/Makefile.linux. +diff -Naur ppp-2.4.5.0/pppd/options.c ppp-2.4.5/pppd/options.c +--- ppp-2.4.5.0/pppd/options.c 2012-04-18 15:44:46.000000000 +0800 ++++ ppp-2.4.5/pppd/options.c 2012-04-19 20:24:49.000000000 +0800 +@@ -126,6 +126,7 @@ + bool dryrun; /* print out option values and exit */ + char *domain; /* domain name set by domain option */ + int child_wait = 5; /* # seconds to wait for children at exit */ ++int npppd = 0; /* synchronize between multiple pppd */ + struct userenv *userenv_list; /* user environment variables */ + + #ifdef MAXOCTETS +@@ -311,6 +312,10 @@ + "Set pathname of ipv6-down script", + OPT_PRIV|OPT_STATIC, NULL, MAXPATHLEN }, + ++ { "syncppp", o_int, &npppd, ++ "sync among multiple pppd when sending chap/pap respond", OPT_PRIO }, ++ ++ + #ifdef HAVE_MULTILINK + { "multilink", o_bool, &multilink, + "Enable multilink operation", OPT_PRIO | 1 }, +diff -Naur ppp-2.4.5.0/pppd/pppd.h ppp-2.4.5/pppd/pppd.h +--- ppp-2.4.5.0/pppd/pppd.h 2012-04-18 15:44:46.000000000 +0800 ++++ ppp-2.4.5/pppd/pppd.h 2012-04-19 20:25:02.000000000 +0800 +@@ -327,6 +327,7 @@ + extern bool dump_options; /* print out option values */ + extern bool dryrun; /* check everything, print options, exit */ + extern int child_wait; /* # seconds to wait for children at end */ ++extern int npppd; /* synchronize between multiple pppd */ + + #ifdef MAXOCTETS + extern unsigned int maxoctets; /* Maximum octetes per session (in bytes) */ +diff -Naur ppp-2.4.5.0/pppd/syncppp.c ppp-2.4.5/pppd/syncppp.c +--- ppp-2.4.5.0/pppd/syncppp.c 1970-01-01 08:00:00.000000000 +0800 ++++ ppp-2.4.5/pppd/syncppp.c 2012-04-18 15:46:59.000000000 +0800 +@@ -0,0 +1,75 @@ ++#include ++#include ++#include ++#include ++#include ++#include ++#include "pppd.h" ++#include "syncppp.h" ++ ++int syncppp(int nproc) ++{ ++ int flags; ++ int value; ++ sem_t *block; ++ sem_t *count; ++ struct timespec ts; ++ ++ if (nproc <= 1) { ++ error("syncppp: number of pppd should be larger than 1"); ++ return -1; ++ } ++ ++ if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { ++ error("clock_gettime error"); ++ return -1; ++ } ++ ts.tv_sec += SYNCPPP_TIMEOUT; ++ ++ ++ flags = O_RDWR | O_CREAT; ++ block = sem_open(SEM_BLOCK_NAME, flags, 0644, 0); ++ count = sem_open(SEM_COUNT_NAME, flags, 0644, 0); ++ if (block == SEM_FAILED || count == SEM_FAILED) { ++ error("syncppp: sem_open failed"); ++ return -1; ++ } ++ ++ if (sem_post(count) < 0) { ++ error("syncppp: sem_post failed"); ++ return -1; ++ } ++ if (sem_getvalue(count, &value) < 0) { ++ error("syncppp: sem_getvalue failed"); ++ return -1; ++ } ++ info("%d pppd have arrived, waiting for the left %d", value, nproc-value); ++ if (value >= nproc) { ++ while (nproc-1 > 0) { ++ if (sem_post(block) < 0) { ++ error("syncppp: sem_post failed"); ++ return -1; ++ } ++ nproc--; ++ } ++ } else { ++ if (sem_timedwait(block, &ts) < 0) { ++ if (errno == ETIMEDOUT) { ++ error("syncppp: sem_timewait time out"); ++ } else { ++ error("syncppp: sem_timewait error"); ++ } ++ return -1; ++ } ++ ++ } ++ ++ sem_close(count); ++ sem_close(block); ++ ++ sem_unlink(SEM_COUNT_NAME); ++ sem_unlink(SEM_BLOCK_NAME); ++ ++ return 0; ++} ++ +diff -Naur ppp-2.4.5.0/pppd/syncppp.h ppp-2.4.5/pppd/syncppp.h +--- ppp-2.4.5.0/pppd/syncppp.h 1970-01-01 08:00:00.000000000 +0800 ++++ ppp-2.4.5/pppd/syncppp.h 2012-04-18 15:46:59.000000000 +0800 +@@ -0,0 +1,3 @@ ++#define SEM_BLOCK_NAME "block" ++#define SEM_COUNT_NAME "count" ++#define SYNCPPP_TIMEOUT 5 +diff -Naur ppp-2.4.5.0/pppd/upap.c ppp-2.4.5/pppd/upap.c +--- ppp-2.4.5.0/pppd/upap.c 2012-04-18 15:44:46.000000000 +0800 ++++ ppp-2.4.5/pppd/upap.c 2012-04-19 20:26:22.000000000 +0800 +@@ -52,6 +52,8 @@ + #include "pppd.h" + #include "upap.h" + ++#include "syncppp.h" ++ + static const char rcsid[] = RCSID; + + static bool hide_password = 1; +@@ -568,6 +570,18 @@ + PUTCHAR(u->us_passwdlen, outp); + BCOPY(u->us_passwd, outp, u->us_passwdlen); + ++ if (npppd > 1) { ++ if (syncppp(npppd) < 0) { ++ error("syncppp sync fail"); ++ sem_unlink(SEM_COUNT_NAME); ++ sem_unlink(SEM_BLOCK_NAME); ++ } else { ++ info("syncppp sync succeeded"); ++ } ++ } else { ++ info("syncppp not active"); ++ } ++ + output(u->us_unit, outpacket_buf, outlen + PPP_HDRLEN); + + TIMEOUT(upap_timeout, u, u->us_timeouttime); diff --git a/package/network/services/shellsync/Makefile b/package/network/services/shellsync/Makefile new file mode 100644 index 0000000000..6dce14b35c --- /dev/null +++ b/package/network/services/shellsync/Makefile @@ -0,0 +1,34 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=shellsync +PKG_VERSION:=0.2 +PKG_RELEASE:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/shellsync + CATEGORY:=Utilities + TITLE:=shellsync + DEPENDS:=+libpthread +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Package/shellsync/description +A tool to sync different shell scripts.Based on syncppp patch by morfast. +endef + +define Build/Compile + $(TARGET_CROSS)gcc -pthread -o $(PKG_BUILD_DIR)/shellsync $(PKG_BUILD_DIR)/shellsync.c +endef + +define Package/shellsync/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/shellsync $(1)/usr/bin +endef + + +$(eval $(call BuildPackage,shellsync)) diff --git a/package/network/services/shellsync/src/shellsync.c b/package/network/services/shellsync/src/shellsync.c new file mode 100644 index 0000000000..4aef1e9ba2 --- /dev/null +++ b/package/network/services/shellsync/src/shellsync.c @@ -0,0 +1,106 @@ +/************************************************************************* + > File Name: shellsync.c + > Author: GuoGuo + > Mail: gch981213@gmail.com + > Created Time: 2014å¹´11月06æ—¥ 星期四 19æ—¶15分30秒 + ************************************************************************/ +#include +#include +#include +#include +#include +#include +#define SEM_BLOCK_NAME "SYNCSHELL_block" +#define SEM_COUNT_NAME "SYNCSHELL_count" +int wait_timeout; +int sync_wait(int nproc) +{ + int flags; + int value; + sem_t *block; + sem_t *count; + struct timespec ts; + + if (nproc <= 1) { + printf("sync_wait: number of processes should be larger than 1\n"); + return -1; + } + + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + printf("clock_gettime error\n"); + return -1; + } + ts.tv_sec += wait_timeout; + + + flags = O_RDWR | O_CREAT; + block = sem_open(SEM_BLOCK_NAME, flags, 0644, 0); + count = sem_open(SEM_COUNT_NAME, flags, 0644, 0); + if (block == SEM_FAILED || count == SEM_FAILED) { + printf("sync_wait: sem_open failed\n"); + return -1; + } + + if (sem_post(count) < 0) { + printf("sync_wait: sem_post failed\n"); + return -1; + } + if (sem_getvalue(count, &value) < 0) { + printf("sync_wait: sem_getvalue failed\n"); + return -1; + } + printf("%d processes have arrived, waiting for the left %d\n", value, nproc-value); + if (value >= nproc) { + while (nproc-1 > 0) { + if (sem_post(block) < 0) { + printf("sync_wait: sem_post failed\n"); + return -1; + } + nproc--; + } + } else { + if (sem_timedwait(block, &ts) < 0) { + if (errno == ETIMEDOUT) { + printf("sync_wait: sem_timewait time out\n"); + } else { + printf("sync_wait: sem_timewait error\n"); + } + return -1; + } + + } + + sem_close(count); + sem_close(block); + + sem_unlink(SEM_COUNT_NAME); + sem_unlink(SEM_BLOCK_NAME); + + return 0; +} + +int main(int argc,char *argv[]) +{ + if(argc!=3) + { + printf("Usage: shellsync \n"); + } + else + { + int proc_num; + sscanf(argv[1],"%d",&proc_num); + sscanf(argv[2],"%d",&wait_timeout); + if(sync_wait(proc_num)<0) + { + printf("Processes sync failed.\n"); + sem_unlink(SEM_COUNT_NAME); + sem_unlink(SEM_BLOCK_NAME); + return 1; + } + else + { + printf("Processes sync succeed.\n"); + return 0; + } + } +}