Permalink
Browse files

Joe's portion of the code; Not test since server was down but should …

…work
  • Loading branch information...
1 parent bfc69cc commit 4711efd10993ebc649bfb5212c041355480a60aa @zoukai957 zoukai957 committed Oct 5, 2012
Showing with 57 additions and 0 deletions.
  1. +12 −0 socket.h
  2. +45 −0 tap.c
View
@@ -8,3 +8,15 @@
int openSocket(int port);
int connectToServer(int sock, int port, char * name);
+
+/**Continuously reads from the tap and prints the string it gets from tap
+ * Takes no parameters
+ * Will take up to 500 bytes from tap otherwise segmentation fault,
+ * but the 500 bytes is adjustable
+ * Exits entire program if for some reason program can't open tap*/
+char * read();
+
+/**Writes messages into the tap
+ * There are no parameters
+ Exits entire program if for some reason program can't open tap*/
+char * write(char *msg);
View
45 tap.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <net/if.h>
+#include <arpa/inet.h>
+#include <linux/if_tun.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/select.h>
+#include <sys/time.h>
+
+/**************************************************
+ * allocate_tunnel:
+ * open a tun or tap device and returns the file
+ * descriptor to read/write back to the caller
+ *****************************************/
+int allocate_tunnel(char *dev, int flags) {
+ int fd, error;
+ struct ifreq ifr;
+ char *device_name = "/dev/net/tun";
+ if( (fd = open(device_name , O_RDWR)) < 0 ) {
+ perror("error opening /dev/net/tun");
+ return fd;
+ }
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = flags;
+ if (*dev) {
+ strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+ }
+ if( (error = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
+ perror("ioctl on tap failed");
+ close(fd);
+ return error;
+ }
+
+ strcpy(dev, ifr.ifr_name);
+ return fd;
+}

0 comments on commit 4711efd

Please sign in to comment.