Permalink
Browse files

Half way done WOOT

  • Loading branch information...
1 parent c5c5e1e commit df1ea637b4231df002527e424a6eb9dd96f482d0 @zoukai957 zoukai957 committed Oct 9, 2012
Showing with 150 additions and 46 deletions.
  1. +85 −0 cs352proxy.c
  2. +12 −13 socket.c
  3. +3 −14 socket.h
  4. +5 −19 tap.c
  5. +45 −0 tap.h
View
@@ -0,0 +1,85 @@
+#include "tap.h"
+#include "socket.h"
+
+int main(int argc, char ** argv){
+ //Determins the port of the remote server and local server
+ //If device is the client the local port is the same as the the remote port
+ //since the local port isn't specified
+ //ie. port on server = port on this device
+ int port;
+ if (argc == 2){
+ port = atoi(argv[1]);
+ } else if (argc == 3){
+ port = atoi(argv[2]);
+ } else {
+ printf("Error: You entered something incorrectly for the arguments");
+ exit(1);
+ }
+
+
+
+
+
+ //opens the tap
+ //exits entire program upon failure to open tap
+ int tapID; //Id / handle of the tap
+ if (argc ==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;
+ if(argc == 2){ //If device is server
+ commSocket = acceptConnection(socketID);
+ } 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
+ if(atoi(argv[3])==0){
+ //Converts from DNS format to IPV4
+ struct hostent *converter;
+ converter = gethostbyname(argv[3]);
+ inet_ntop(converter->h_addrtype,converter->h_name,IP,INET_ADDRSTRLEN);
+ }else{
+ IPReal = argv[3];
+ }
+
+ if(connectToServer(commSocket, argv[s], IPReal) == 0){
+ printf("ERROR: Could not connect to server exiting program");
+ exit(1);
+ }
+ free IP;
+ }
+
+
+
+
+
+ //Begins to Send and receive messages to each other
+
+
+
+
+
+
+
+ //Closes Socket and tap
+ close(tapID);
+ close(socketID);
+ if (argc == 2){
+ close(commSocket);
+ }
+
+
+}
View
@@ -1,12 +1,14 @@
-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*/
+#include "socket.h"
+
+int openSocket(int port) { /*This creates a generic socket, using whatever address the machine wants to give it, on the port given*/
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in myaddr;
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(port);
bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr));
- fcntl(sock, F_SETFF, FNDELAY); /*THIS SOCKET WILL NOW PERFORM ASYNCHRONOUSLY*/
+ //fcntl(sock, F_SETFF, FNDELAY); /*THIS SOCKET WILL NOW PERFORM ASYNCHRONOUSLY*/
return sock; /*returns the FD of the socket*/
}
@@ -15,16 +17,15 @@ int connectToServer(int sock, int port, char * name) { /*creates an outgoing con
memset((char *_)&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
-
- memcpy((void *)&servaddr.sin_addr, name, sizeof(name));
+ servaddr.sin_addr.s_addr = inet_addr(name);
if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
return 0;
}
return 1;
}
-int acceptConnection(int sock, int(* functionPointer)(int)) { /*accepts an inbound connection on the specified socket. 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 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;
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
@@ -36,13 +37,11 @@ int acceptConnection(int sock, int(* functionPointer)(int)) { /*accepts an inbou
exit(1);
}
- //this loop will allow more vms to connect to this computer
- while ((acceptedsocket = accept(sock, (struct sockaddr *)&client_addr, &alen)) < 0) {
-
- //Insert code here
- }
+ acceptedsocket = accept(sock, (struct sockaddr *)&client_addr, &alen);
- perror("accept failed");
- exit(2);
-
+ if(acceptsocket < 0){
+ perror("accept failed");
+ exit(2);
+ }
+ return acceptsocket;
}
View
@@ -4,22 +4,11 @@
#include <stdio.h>
#include <netdb.h>
#include <pthread.h>
+#include <arpa/inet.h>
-int (* functionPointer) (int) = NULL; /*generic function pointer, accepts a socket*/
+typedef int (* functionPointer) (int); /*generic function pointer, accepts a socket*/
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, int(* functionPointer)(int)); /*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.*/
-
-/**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);
+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
@@ -1,20 +1,4 @@
-#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>
+#include "tap.h"
/**************************************************
* allocate_tunnel:
@@ -47,16 +31,18 @@ int allocate_tunnel(char *dev, int flags) {
/**opens the tap connection
* returns the tap handle
* DONT FORGET TO CLOSE TAP using close(tap_fd)*/
-void openTap(){
+int openTap(char * tapName){
char *if_name = (char*)malloc(sizeof(char) * 16);
- strcpy(if_name, "tap0");
+ strcpy(if_name, tapName);
int tap_fd;
if ( (tap_fd = allocate_tunnel(if_name, IFF_TAP | IFF_NO_PI)) < 0 ) {
perror("Opening tap interface failed! \n");
exit(1);
}
+ free(ifname);
+
return tap_fd;
}
View
@@ -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);
+
+/**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,
+ * but the 500 bytes is adjustable
+ * User must remember to free the return
+ * Also assumes tap was already opened*/
+char *readTap(int tap_fd);
+
+/**opens the tap connection
+ * returns the tap handle
+ * DONT FORGET TO CLOSE TAP using close(tap_fd)*/
+int openTap(char * tapName);
+
+/**Writes messages into the tap
+ * 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);
+
+

0 comments on commit df1ea63

Please sign in to comment.