-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcproxy2.c
More file actions
209 lines (174 loc) · 6.24 KB
/
cproxy2.c
File metadata and controls
209 lines (174 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
CSC 425 Project/ Milestone 2
cproxy.c -- Connects to the server proxy and listens for a telnet connection
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#define MAX_PENDING 5
#define BUFFER_SIZE 6144
void set_socket_opts(int socket) {
int enable = 1;
if(setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
fprintf(stderr, "Error while attempting to set SO_REUSEADDR!\n");
exit(errno);
}
if(setsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) {
fprintf(stderr, "Error while attempting to set SO_REUSEPORT!\n");
exit(errno);
}
}
void connect_to_server(int socket, char *server_hostname, int server_port, int *connection) {
/* Connect to the server on specified port */
struct hostent *hp = gethostbyname(server_hostname);
if(!hp) {
fprintf(stderr, "ERROR: Unknown host! (lolwut)\n");
exit(errno);
}
// Create client_addr data structure and copy over address
struct sockaddr_in client_addr;
bzero((char *)&client_addr, sizeof(client_addr));
client_addr.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&client_addr.sin_addr, hp->h_length);
client_addr.sin_port = htons(server_port);
// Open connection to the server
*connection = connect(socket, (struct sockaddr *)&client_addr, sizeof(client_addr));
if(*connection == -1){
fprintf(stderr, "ERROR: Connecting to sproxy failed!\n");
exit(errno);
}
}
int main(int argc, char *argv[]) {
if(argc < 4) {
fprintf(stderr, "Usage: ./cproxy listen_port server_ip server_port\n");
exit(1);
}
int listen_port = atoi(argv[1]);
char *server_hostname = argv[2];
int server_port = atoi(argv[3]);
// Connnect to server
int server_sock = socket(PF_INET, SOCK_STREAM, 0);
if(server_sock == -1) {
fprintf(stderr, "ERROR: Could not create socket for telnet!\n");
close(server_sock);
exit(errno);
}
set_socket_opts(server_sock);
int telnet_conn = -1;
connect_to_server(server_sock, server_hostname, server_port, &telnet_conn);
// Setup the socket to listen for cproxy
int listen_sock = socket(PF_INET, SOCK_STREAM, 0);
if(listen_sock == -1) {
fprintf(stderr, "ERROR: Could not create socket for cproxy!\n");
close(server_sock);
close(listen_sock);
exit(errno);
}
set_socket_opts(listen_sock);
// Setup the sockaddr for the listen socket
struct sockaddr_in listen_addr;
listen_addr.sin_family = AF_INET; //assign the address family of the server
listen_addr.sin_port = htons(listen_port);
listen_addr.sin_addr.s_addr = INADDR_ANY; //assign the server's ip
//Bind the socket to an address using bind() system call
int bindfd = bind(listen_sock, (struct sockaddr *)&listen_addr, sizeof(listen_addr));
if(bindfd == -1){
fprintf(stderr, "Error: Binding cproxy listen socket failed\n");
close(listen_sock);
close(server_sock);
exit(errno);
}
//Listen for connections with listen()
int listen_rt = listen(listen_sock, MAX_PENDING);
if(listen_rt == -1){
fprintf(stderr, "Error: Listen call failed\n");
close(listen_sock);
close(server_sock);
exit(errno);
}
// Accept client connection
socklen_t len;
int client_connection = accept(listen_sock, (struct sockaddr *)&listen_addr, &len);
if(client_connection < 0){
fprintf(stderr, "Error: connection accept failed\n");
close(listen_sock);
close(server_sock);
close(client_connection);
exit(errno);
}
// Set up our descriptor set for select
fd_set socket_fds;
// Set up the arguments
int max_fd = (server_sock > client_connection) ? server_sock : client_connection;
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 500000;
int rv;
// Create the buffer
char buf[BUFFER_SIZE];
// Length of the payload recieved
int payload_length = -1;
// Actually forward the data
while(true) {
FD_ZERO(&socket_fds);
FD_SET(server_sock, &socket_fds);
FD_SET(client_connection, &socket_fds);
rv = select(max_fd + 1, &socket_fds, NULL, NULL, NULL);
// Determine the value of rv
if(rv == -1) {
// This means that an error occured
fprintf(stderr, "ERROR: Issue while selecting socket\n");
close(server_sock);
close(client_connection);
close(listen_sock);
exit(errno);
} else if(rv == 0) {
// Timeout: This is not an issue in our program
} else {
// Determine which socket (or both) has data waiting
if(FD_ISSET(server_sock, &socket_fds)) {
// Recieve from the server
payload_length = read(server_sock, &buf, BUFFER_SIZE);
if(payload_length <= 0) {
close(client_connection);
close(listen_sock);
close(server_sock);
break;
}
// Write to the client
send(client_connection, (void *) buf, payload_length, 0);
}
if(FD_ISSET(client_connection, &socket_fds)) {
// Recieve from the client
payload_length = read(client_connection, &buf, BUFFER_SIZE);
if(payload_length <= 0) {
close(client_connection);
close(listen_sock);
close(server_sock);
break;
}
if(strcmp(buf, "exit") == 0) {
close(client_connection);
close(listen_sock);
close(server_sock);
break;
}
// Write to the telnet connection (client)
send(server_sock, (void *) buf, payload_length, 0);
}
}
}
// Close our connections
close(client_connection);
close(listen_sock);
close(server_sock);
}