Permalink
Browse files

Code is completed but has not been tested in any way or any documenta…

…tion been done in any way if you have time please try to do either of these thank you
  • Loading branch information...
1 parent df1ea63 commit 720081e7a8077cc1b6a050401e2ecb8295f435f5 @zoukai957 zoukai957 committed Oct 10, 2012
Showing with 94 additions and 28 deletions.
  1. +26 −10 cs352proxy.c
  2. +1 −0 socket.h
  3. +56 −17 tap.c
  4. +11 −1 tap.h
View
@@ -1,5 +1,7 @@
#include "tap.h"
#include "socket.h"
+#include <pthread.h>
+#include <semaphore.h>
int main(int argc, char ** argv){
//Determins the port of the remote server and local server
@@ -62,17 +64,31 @@ int main(int argc, char ** argv){
free IP;
}
+ //thread for reading from tap and sending msg through socket
+ int argt[2];
+ argt[0]=tapID;
+ argt[1]=commSocket;
+ pthread_t threadID;
+ pthread_create(threadID, NULL,readTap,argt);
+
+ //thread for reading from socket and writing to tap
+ int args[2];
+ args[0]=commSocket;
+ args[1]=tapID;
+ pthread_t thread2ID;
+ pthread_create(thread2ID, NULL,writeTap,args);
+
+ int rt;
+ pthread_join(threadID,rt)
+ if(rt==1){
+ printf("There was an error from reading from tap");
+ }
-
-
-
- //Begins to Send and receive messages to each other
-
-
-
-
-
-
+ int wt;
+ pthread_join(thread2ID,wt)
+ if(wt==1){
+ printf("There was an error from writing to tap");
+ }
//Closes Socket and tap
close(tapID);
View
@@ -12,3 +12,4 @@ typedef int (* functionPointer) (int); /*generic function pointer, accepts a soc
int openSocket(int port); /*This creates a generic socket, that performs asynchronously, using whatever address the machine wants to give it, on the port given*/
int connectToServer(int sock, int port, char * name); /*creates an outgoing connection, using the given socket, the port of the away machine, and the IP address (e.g. 198.162.2.2) of the remote machine. Returns 0 on failure, 1 on success*/
int acceptConnection(int sock); /*accepts an inbound connection on the specified socket, with a function for it to call upon accept. This function should not return unless broken from, or gives an error (such as a fail listen or fail accept). Upon accepting the new connection, a new socket is created to work with.*/
+
View
73 tap.c
@@ -1,4 +1,5 @@
#include "tap.h"
+#include "socket.h"
/**************************************************
* allocate_tunnel:
@@ -46,36 +47,74 @@ int openTap(char * tapName){
return tap_fd;
}
-/**Continuously reads from the tap and returns the string it gets from tap
+/**Continuously reads from the tap and sends what it read into the socket
+ * Only returns if something goes wrong otherwise loops forever
* Takes the handle of the tap as a parameter for tap_fd
* Will take up to 500 bytes from tap otherwise segmentation fault,
* but the 500 bytes is adjustable
- * User must remember to free the return
* Also assumes tap was already opened*/
-char *readTap(int tap_fd){
- //Malloc for the string that is taken from the tap (takes up to 500 bytes)
- char *tapEntry = (char*)malloc(sizeof(char) * 500);
+int readTap(int tap_fd, int socketID){
+ datagram d;
+ fd_set readfds;
+
while(1){
- if(read(tap_fd, tapEntry,500) < 0){
+ //Waits till tap is ready to be read from
+ FD_ZERO(&readfds);
+ FD_SET(tap_fd, readfds);
+ select(tap_fd+1, &readfs, NULL, NULL, NULL);
+
+ unit32_t nbits = 0; //Number of bytes read from the tap
+ if((nbits = read(tap_fd, d.data,500)) < 0){
printf("Error: Could not read from tap\n");
- exit(1);
+ close(tap_fd);
+ return 1;
}
+ //Headers for file
+ //type:
+ unit32_t vlanID = %0xABCD;
+ d.type = htonl(vlanID);
+ //length: (this is taken in bytes)
+ d.length = htonl(nbits);
+
+ //Sends data over the socket
+ send(socketID, &d, d.length, 0);
}
-
- return tapEntry
+
+
+ return 0;
}
/**Writes messages into the tap
- * msg = message that needs to be written to the tap
+ * socketID = socket to wait for message
* tap_fd = the handle of the tap
+ * Returns a 1 upon error and returns a 0 upon success
Assumes the tap was already open*/
-void writeTap(char *msg, int tap_fd){
-
-
- if(write(tap_fd, msg, sizeof(msg)) < 0){
- printf("Error: Could not read from tap\n");
- exit(1);
- }
+void writeTap(int socketID, int tap_fd){
+ fd_set readfds;
+ datagram *msg =(datagram *)malloc(sizeof(datagram));
+
+ while(1){
+ //waits to receive message from socket
+ FD_ZERO(&readfds);
+ FD_SET(tap_fd, readfds);
+ select(tap_fd+1, &readfs, NULL, NULL, NULL);
+
+ //recieves msg from socket
+ recv(socketID, msg, 500, 0);
+
+ //Strips header from msg
+ int length = ntohl(msg.length);
+
+ char strippedMsg[500];
+ memcpy(strippedMsg, msg.data, length);
+
+ if(write(tap_fd, msg.data, sizeof(msg)) < 0){
+ printf("Error: Could not write to tap\n");
+ return 1;
+ }
+ )
+
+ return 0;
}
View
12 tap.h
@@ -15,6 +15,8 @@
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
+#include <netinet/in.h>
+#include <unistd.h>
/**************************************************
* allocate_tunnel:
@@ -23,6 +25,12 @@
*****************************************/
int allocate_tunnel(char *dev, int flags);
+typedef struct _datagram{
+ unit32_t type;
+ unit32_t length;
+ char data[500];
+} datagram;
+
/**Continuously reads from the tap and returns the string it gets from tap
* Takes the handle of the tap as a parameter for tap_fd
* Will take up to 500 bytes from tap otherwise segmentation fault,
@@ -40,6 +48,8 @@ int openTap(char * tapName);
* msg = message that needs to be written to the tap
* tap_fd = the handle of the tap
Assumes the tap was already open*/
-void writeTap(char *msg, int tap_fd);
+void writeTap(datagram msg, int tap_fd);
+
+

0 comments on commit 720081e

Please sign in to comment.