Skip to content

Commit

Permalink
use master/worker to avoid unsolved coredump error
Browse files Browse the repository at this point in the history
  • Loading branch information
puxxustc committed Jan 19, 2016
1 parent ba50f86 commit f4c41e2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
os:
- linux
- osx
language: c
compiler:
- gcc
- clang
before_script:
- "mkdir -p m4"
- "autoreconf -if"
script:
- "git clone https://github.com/sustrik/libmill.git"
- "pushd libmill"
- "./autogen.sh"
- "./configure"
- "make"
- "popd"
- "autoreconf -if"
- "export CPPFLAGS=-I$(pwd)/libmill"
- "export LDFLAGS=-L$(pwd)/libmill"
- "./configure --prefix=/usr --sysconfdir=/etc"
- "make"
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AC_CHECK_LIB([mill], [go], [AC_SUBST(LIB_MILL)])

# Checks for header files.
AC_HEADER_ASSERT
AC_CHECK_HEADERS([fcntl.h libmill.h limits.h netdb.h pwd.h stddef.h stdint.h stdio.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/stat.h sys/time.h unistd.h])
AC_CHECK_HEADERS([fcntl.h libmill.h limits.h netdb.h pwd.h stddef.h stdint.h stdio.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/stat.h sys/time.h sys/wait.h unistd.h])

# Check build target
case "$host" in
Expand Down
41 changes: 39 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "conf.h"
#include "log.h"
#include "utils.h"
#include "vpn.h"

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

// role == 0 -> master, role == 1 -> worker
static int role;

static void signal_cb(int signo)
{
(void)signo;
vpn_stop();
if (role == 1)
{
vpn_stop();
}
}

int main(int argc, char **argv)
Expand Down Expand Up @@ -73,5 +82,33 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

return vpn_run();
while (1)
{
int cpid = fork();
if (cpid < 0)
{
LOG("failed to spawn worker process");
}
else if (cpid == 0)
{
// worker
role = 1;
vpn_run();
return 0;
}
else
{
// master
int status;
wait(&status);
if (WIFEXITED(status))
{
return 0;
}
else
{
LOG("worker terminated unexpectedly, respawn woker");
}
}
}
}
6 changes: 2 additions & 4 deletions src/vpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ coroutine static void udp_worker(int port, int timeout)
}

// 写入到 tun 设备
assert(pbuf.len < sizeof(pbuf.payload));
n = tun_write(tun, pbuf.payload, pbuf.len);
if (n < 0)
{
Expand Down Expand Up @@ -465,7 +464,6 @@ static int encapsulate(pbuf_t *pbuf)

// 加密
ssize_t n = PAYLOAD_OFFSET + pbuf->len + pbuf->padding;
assert(n < (ssize_t)sizeof(pbuf->payload));
crypto_encrypt(pbuf);

return (int)n;
Expand All @@ -476,7 +474,7 @@ static int encapsulate(pbuf_t *pbuf)
static int decapsulate(pbuf_t *pbuf, int n)
{
assert(pbuf != NULL);
assert(n < (int)sizeof(pbuf->payload));
assert(n <= (int)conf->mtu + PAYLOAD_OFFSET);

// 解密
int invalid = crypto_decrypt(pbuf, n);
Expand Down Expand Up @@ -511,7 +509,7 @@ static int decapsulate(pbuf_t *pbuf, int n)
// 暂不处理 ack flag
}

assert(pbuf->len < sizeof(pbuf->payload));
assert(pbuf->len <= conf->mtu);
return (int)(pbuf->len);
}

Expand Down

0 comments on commit f4c41e2

Please sign in to comment.