Permalink
Browse files

Fixed all compilation error along with other changes. I also added mu…

…texes but I believe we will still need select(). Also when testing this code I think we should make a copy(aka fork) of these files and make changes on the copy. That way there aren't three people trying to edit and update the same document. We can combine all of our changes when we meet on Friday
  • Loading branch information...
1 parent ae6d9eb commit e42dc389eeefa3d0958876fca36408f03cf8b3ee @zoukai957 zoukai957 committed Oct 10, 2012
Showing with 64 additions and 46 deletions.
  1. +16 −20 cs352proxy.c
  2. +8 −6 socket.c
  3. +1 −1 socket.h
  4. +30 −15 tap.c
  5. +9 −4 tap.h
View
36 cs352proxy.c
@@ -26,28 +26,25 @@ int main(int argc, char ** argv){
//exits entire program upon failure to open tap
int tapID; //Id / handle of the tap
if (argc ==2){
- tapID = openTap(argv[2];
+ tapID = openTap(argv[2]);
}else{
tapID = openTap(argv[3]);
}
- //opens a socket
- int socketID = openSocket(port); //ID / handle of the socket
-
//if device is server then it waits for connection and accepts it
//else it establishes a connection
- int commSocket = socketID;
+ int commSocket;
if(argc == 2){ //If device is server
- commSocket = acceptConnection(socketID);
+ commSocket = acceptConnection(port);
} else{ //If device is client
//Checks if the 3rd argument is in DNS format or IPV4
char *IP = malloc(sizeof(char)*500);
- char *IPReal = IP
+ char *IPReal = IP;
if(atoi(argv[3])==0){
//Converts from DNS format to IPV4
struct hostent *converter;
@@ -56,46 +53,45 @@ int main(int argc, char ** argv){
}else{
IPReal = argv[3];
}
-
- if(connectToServer(commSocket, argv[s], IPReal) == 0){
+ commSocket = socket(AF_INET, SOCK_STREAM, 0);
+ if(connectToServer(commSocket, port, IPReal) == 0){
printf("ERROR: Could not connect to server exiting program");
exit(1);
}
- free IP;
+ free(IP);
}
+ //creates the mutexes
+ pthread_mutex_init(&socketMu, NULL);
+ pthread_mutex_init(&tapMu, NULL);
+
//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);
+ pthread_create(&threadtapRID, NULL,(void *)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);
+ pthread_create(&threadsockRID, NULL,(void *)writeTap,args);
int rt;
- pthread_join(threadID,rt)
+ pthread_join(threadtapRID,(void *)(&rt));
if(rt==1){
printf("There was an error from reading from tap");
}
int wt;
- pthread_join(thread2ID,wt)
+ pthread_join(threadsockRID,(void *)(&wt));
if(wt==1){
printf("There was an error from writing to tap");
}
//Closes Socket and tap
close(tapID);
- close(socketID);
- if (argc == 2){
- close(commSocket);
- }
+ close(commSocket);
}
View
14 socket.c
@@ -14,7 +14,7 @@ int openSocket(int port) { /*This creates a generic socket, using whatever addre
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*/
struct sockaddr_in servaddr;
- memset((char *_)&servaddr, 0, sizeof(servaddr));
+ memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = inet_addr(name);
@@ -25,19 +25,21 @@ int connectToServer(int sock, int port, char * name) { /*creates an outgoing con
return 1;
}
-int acceptConnection(int sock) { //accepts an inbound connection on the specified socket. This accepts ONLY 1 connection request and returns the socket which does the communication on succes, exits the entire program upon failure
- socklen_t alen;
+int acceptConnection(int port) { //accepts an inbound connection on the specified socket. This accepts ONLY 1 connection request and returns the socket which does the communication on succes, exits the entire program upon failure
+ socklen_t alen = sizeof(struct sockaddr_in);
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
- int sockoptval = 1;
- int acceptedsocket;
+ int svc;
+ int acceptsocket;
+
+ svc = openSocket(port);
if (listen(svc, 10) < 0) {
perror("listen failed");
exit(1);
}
- acceptedsocket = accept(sock, (struct sockaddr *)&client_addr, &alen);
+ acceptsocket = accept(svc, (struct sockaddr *)&client_addr, &alen);
if(acceptsocket < 0){
perror("accept failed");
View
2 socket.h
@@ -11,5 +11,5 @@ 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.*/
+int acceptConnection(int port); /*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
45 tap.c
@@ -1,6 +1,8 @@
#include "tap.h"
#include "socket.h"
+
+
/**************************************************
* allocate_tunnel:
* open a tun or tap device and returns the file
@@ -42,7 +44,7 @@ int openTap(char * tapName){
exit(1);
}
- free(ifname);
+ free(if_name);
return tap_fd;
}
@@ -61,24 +63,30 @@ int readTap(int tap_fd, int socketID){
while(1){
//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);
+ FD_SET(tap_fd, &readfds);
+ select(tap_fd+1, &readfds, NULL, NULL, NULL);
+
+ //reads from tap
+ pthread_mutex_lock(&tapMu); //Locks tap for only reading from tap
+ short nbits = read(tap_fd, d.data,2044);
+ pthread_mutex_unlock(&tapMu); //Unlocks tap
- unit32_t nbits = 0; //Number of bytes read from the tap
- if((nbits = read(tap_fd, d.data,500)) < 0){
+ if(nbits < 0){
printf("Error: Could not read from tap\n");
close(tap_fd);
return 1;
}
//Headers for file
//type:
- unit32_t vlanID = %0xABCD;
- d.type = htonl(vlanID);
+ short vlanID = 0xABCD;
+ d.type = htons(vlanID);
//length: (this is taken in bytes)
- d.length = htonl(nbits);
+ d.length = htons(nbits);
//Sends data over the socket
+ pthread_mutex_lock(&socketMu); //Locks socket for only write
send(socketID, &d, d.length, 0);
+ pthread_mutex_unlock(&socketMu); //unlocks socket
}
@@ -90,30 +98,37 @@ int readTap(int tap_fd, int socketID){
* 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(int socketID, int tap_fd){
+int 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);
+ FD_SET(tap_fd, &readfds);
+ select(tap_fd+1, &readfds, NULL, NULL, NULL);
//recieves msg from socket
+ pthread_mutex_lock(&socketMu); //locks socket for read
recv(socketID, msg, 500, 0);
+ pthread_mutex_unlock(&socketMu); //unlocks socket
//Strips header from msg
- int length = ntohl(msg.length);
+ int length = ntohs(msg->length);
char strippedMsg[500];
- memcpy(strippedMsg, msg.data, length);
+ memcpy(strippedMsg, msg->data, length);
- if(write(tap_fd, msg.data, length) < 0){
+ //Sends data through tap
+ pthread_mutex_lock(&tapMu); //locks tap for write
+ int writeError = write(tap_fd, msg->data, length);
+ pthread_mutex_unlock(&tapMu); //unlocks tap
+
+ if( writeError< 0){
printf("Error: Could not write to tap\n");
return 1;
}
- )
+ }
return 0;
View
13 tap.h
@@ -18,6 +18,11 @@
#include <netinet/in.h>
#include <unistd.h>
+pthread_t threadtapRID; //Thread from reading from tap
+pthread_t threadsockRID; //Thread for reading from socket
+pthread_mutex_t socketMu; //lock for socket
+pthread_mutex_t tapMu; //lock for tap
+
/**************************************************
* allocate_tunnel:
* open a tun or tap device and returns the file
@@ -26,9 +31,9 @@
int allocate_tunnel(char *dev, int flags);
typedef struct _datagram{
- unit32_t type;
- unit32_t length;
- char data[500];
+ short type;
+ short length;
+ char data[2044];
} datagram;
/**Continuously reads from the tap and sends what it read into the socket
@@ -49,7 +54,7 @@ int openTap(char * tapName);
* 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(int socketID, int tap_fd);
+int writeTap(int socketID, int tap_fd);

0 comments on commit e42dc38

Please sign in to comment.