Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of https://github.com/jobrien929/cs352proxy

  • Loading branch information...
commit 5c553030a7ffe05c87e647c7799f5da349ba06f8 2 parents 69acbfc + aacff56
@zoukai957 zoukai957 authored
Showing with 81 additions and 5 deletions.
  1. +27 −3 socket.c
  2. +27 −0 socket.c~
  3. +5 −2 socket.h
  4. +22 −0 socket.h~
View
30 socket.c
@@ -1,5 +1,5 @@
-int openSocket(int port) {
+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 sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in myaddr;
memset((char *)&myaddr, 0, sizeof(myaddr));
@@ -8,10 +8,10 @@ int openSocket(int port) {
myaddr.sin_port = htons(port);
bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr));
fcntl(sock, F_SETFF, FNDELAY); /*THIS SOCKET WILL NOW PERFORM ASYNCHRONOUSLY*/
- return sock;
+ return sock; /*returns the FD of the socket*/
}
-int connectToServer(int sock, int port, char * name) {
+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));
servaddr.sin_family = AF_INET;
@@ -24,4 +24,28 @@ int connectToServer(int sock, int port, char * name) {
}
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.*/
+ socklen_t alen;
+ struct sockaddr_in my_addr;
+ struct sockaddr_in client_addr;
+ int sockoptval = 1;
+ int acceptedsocket;
+
+ if (listen(svc, 10) < 0) {
+ perror("listen failed");
+ return -1;
+ }
+ while (1) {
+ while ((acceptedsocket = accept(sock, (struct sockaddr *)&client_addr, &alen)) < 0) {
+ if ((errno != ECHILD) && (errno != ERESTART) && (errno != EINTR)) {
+ perror("accept failed");
+ return -2;
+ }
+ /*INSERT NEW THREAD HERE FOR THE SOCKET TO TALK TO - USE ACCEPTEDSOCKET*/
+ /*JOHN FINISH THIS OUT, USE THE FUNCTIONPOINTER PASSED INTO ACCEPTCONNECTION TO SPAWN THE THREAD*/
+ }
+ }
+ return 0;
+}
View
27 socket.c~
@@ -0,0 +1,27 @@
+
+int openSocket(int port) {
+ 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*/
+ return sock;
+}
+
+int connectToServer(int sock, int port, char * name) {
+ struct sockaddr_in servaddr;
+ memset((char *_)&servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = htons(port);
+
+ memcpy((void *)&servaddr.sin_addr, name, sizeof(name));
+
+ if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
+ return 0;
+ }
+ return 1;
+}
+
View
7 socket.h
@@ -3,11 +3,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
+#include <pthread.h>
-int openSocket(int port);
-int connectToServer(int sock, int port, char * name);
+int (* functionPointer) (int) = NULL; /*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
View
22 socket.h~
@@ -0,0 +1,22 @@
+#include <sys/socket.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <netdb.h>
+
+
+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);
Please sign in to comment.
Something went wrong with that request. Please try again.