Skip to content

Commit

Permalink
Check OS.
Browse files Browse the repository at this point in the history
  • Loading branch information
prodrigestivill committed Feb 15, 2008
1 parent bcbeefe commit 5824b20
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 50 deletions.
14 changes: 13 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ AC_SUBST(VPMN_CFLAGS)
AC_SUBST(VPMN_LIBS)



dnl Check and set OS

#AC_CANONICAL_HOST

case $host_os in
*linux*)
AC_DEFINE(HAVE_LINUX, 1, [Linux])
[ rm -f src/tundev.c; ln -sf linux/tundev.c src/tundev.c ]
;;
*)
AC_MSG_ERROR("Unknown operating system.")
;;
esac

AC_OUTPUT([
Makefile
Expand Down
5 changes: 3 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ vpmnd_SOURCES = \
udpsrvthread.h \
udpsrvdtls.c \
udpsrvdtls.h \
tundev.c \
tundevthread.c \
tundevthread.h \
config.h \
config.c \
tundev.h
tundev.h \
tundev.c \
tunsrv.c

vpmnd_LDFLAGS = \
-lpthread
Expand Down
47 changes: 7 additions & 40 deletions src/tundev.c → src/linux/tundev.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "config.h"
#include "debug.h"
#include "tundevthread.h"

int tundev_fd = -1;

Expand All @@ -59,7 +58,7 @@ tundev_initdev ()
}

memset (&ifr, 0, sizeof (ifr));
ifr.ifr_flags = IFF_TUN; // | IFF_NO_PI;
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (iface_len > 0)
strncpy (ifr.ifr_name, iface, IFNAMSIZ);

Expand Down Expand Up @@ -126,42 +125,10 @@ tundev_write (const void *buf, int count)
return -1;
}

void
tundev ()
int
tundev_read (void *buf, int count)
{
int rc, th;
struct tundevthread_t tundevthreads[num_tundevthreads];

for (th = 0; th < num_tundevthreads; th++)
{
if ((rc = tundevthread_create (&(tundevthreads[th]))))
{
log_error ("Thread %d creation failed: %d\n", th, rc);
break;
}
}
while (1)
{
for (th = 0; th < num_tundevthreads; th++)
{
if (pthread_mutex_trylock (&tundevthreads[th].thread_mutex) == 0)
{
pthread_mutex_lock (&tundevthreads[th].cond_mutex);
tundevthreads[th].buffer_len =
read (tundev_fd, tundevthreads[th].buffer,
sizeof (tundevthreads[th].buffer));
if (tundevthreads[th].buffer_len > 0)
{
pthread_cond_signal (&tundevthreads[th].cond);
}
else
{
pthread_mutex_unlock (&tundevthreads[th].thread_mutex);
log_error ("Error reading form interface.\n");
}
pthread_mutex_unlock (&tundevthreads[th].cond_mutex);
break;
}
}
}
if (tundev_fd >= 0)
return read (tundev_fd, buf, count);
return -1;
}
2 changes: 1 addition & 1 deletion src/tundev.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@

int tundev_initdev ();
int tundev_write (const void *buf, int count);
void tundev ();
int tundev_read (void *buf, int count);

#endif /* _TUNDEV_H */
69 changes: 69 additions & 0 deletions src/tunsrv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/***************************************************************************
* tunsrv.c
*
* Tue Feb 12 09:51:17 2008
* Copyright 2008 Pau Rodriguez-Estivill
* <prodrigestivill@gmail.com>
****************************************************************************/

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/

#include <pthread.h>
#include "config.h"
#include "debug.h"
#include "tundev.h"
#include "tundevthread.h"

void
tundev ()
{
int rc, th;
struct tundevthread_t tundevthreads[num_tundevthreads];

for (th = 0; th < num_tundevthreads; th++)
{
if ((rc = tundevthread_create (&(tundevthreads[th]))))
{
log_error ("Thread %d creation failed: %d\n", th, rc);
break;
}
}
while (1)
{
for (th = 0; th < num_tundevthreads; th++)
{
if (pthread_mutex_trylock (&tundevthreads[th].thread_mutex) == 0)
{
pthread_mutex_lock (&tundevthreads[th].cond_mutex);
tundevthreads[th].buffer_len =
tundev_read (tundevthreads[th].buffer,
sizeof (tundevthreads[th].buffer));
if (tundevthreads[th].buffer_len > 0)
{
pthread_cond_signal (&tundevthreads[th].cond);
}
else
{
pthread_mutex_unlock (&tundevthreads[th].thread_mutex);
log_error ("Error reading form interface.\n");
}
pthread_mutex_unlock (&tundevthreads[th].cond_mutex);
break;
}
}
}
}
8 changes: 5 additions & 3 deletions src/udpsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ udpsrv_init ()
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = htonl (INADDR_ANY);
bind_addr.sin_port = htons (port_udp);
if (bind (udpsrv_fd, (struct sockaddr *) &bind_addr, sizeof (bind_addr)) != 0)
if (bind (udpsrv_fd, (struct sockaddr *) &bind_addr, sizeof (bind_addr)) !=
0)
{
log_error ("Bind error\n");
return -1;
Expand All @@ -56,10 +57,11 @@ udpsrv_init ()
}

int
udpsrv_write (const void *buf, int count)
udpsrv_sendto (const void *buf, size_t len, const struct sockaddr *to,
socklen_t tolen)
{
if (udpsrv_fd >= 0)
return write (udpsrv_fd, buf, count);
return sendto (udpsrv_fd, buf, len, 0, to, tolen);
return -1;
}

Expand Down
5 changes: 3 additions & 2 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ bin_PROGRAMS = \

tundevtest_SOURCES = \
tundevtest.c\
../src/tundev.c \
../src/tundevthread.c \
../src/tundevthread.h \
../src/debug.c \
../src/debug.h \
../src/config.h \
../src/config.c \
../src/tundev.h
../src/tundev.h \
../src/tundev.c \
../src/tunsrv.c

tundevtest_LDFLAGS = \
-lpthread
Expand Down
5 changes: 4 additions & 1 deletion test/tundevtest.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "../src/config.h"
#include "../src/debug.h"
#include "../src/tundev.h"
void tunsrv ();

int
main ()
Expand All @@ -9,6 +12,6 @@ main ()
log_error ("Could not create the interface.\n");
return -1;
}
tundev ();
tunsrv ();
return 0;
}

0 comments on commit 5824b20

Please sign in to comment.