-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplenc.c
325 lines (268 loc) · 7.23 KB
/
simplenc.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <pthread.h>
int handle_session(int session, int client);
int init_connect(char *host,char *port,int udp);
int read_socket(int sockfd);
int server_listen(char *host, char* port, int udp);
int send_stdin(int sockfd);
void *read_socket_client(void* arg);
void *send_stdin_server(void *arg);
int handle_session(int sockfd, int client)
{
// pthread_t thread_id;
if (client) {
// if(pthread_create(&thread_id, NULL, read_socket_client, (void*)&sockfd)!=0){
// fprintf(stderr, "error in handle session pthread create");
// exit(1);
// }
send_stdin(sockfd);
}else {
// if (pthread_create(&thread_id, NULL, send_stdin_server, (void*)&sockfd) != 0) {
// fprintf(stderr, "error in handle session pthread create");
// exit(1);
// }
read_socket(sockfd);
}
return 1;
}
void *read_socket_client(void* arg)
{
int sockfd = (int)arg;
char data[2048];
int len;
printf("hello fromr ead socket client thread");
while(1){
memset(&data,0, sizeof(data));
len = read(sockfd, data, sizeof(data) -1);
if (!len){
close(sockfd);
exit(0);
}
printf("%s", data);
}
}
int init_connect(char *host,char *port,int udp){
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
if (udp) {
hints.ai_socktype = SOCK_DGRAM; // for UDP
}else {
hints.ai_socktype = SOCK_STREAM; // for TCP
}
if ( getaddrinfo(host,port, &hints, &servinfo) !=0) {
fprintf(stderr, "internal error in TCP get addr info\n");
exit(1);
}
// loop through all results and connect to thd first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "internal error looped off end of list");
exit(2);
}
handle_session(sockfd, 1);
return 1;
}
void *send_stdin_server(void *arg){
// arg is the socket to send on
int sockfd = (int)arg;
char buf[2048];
char* input;
printf("hello from send stdin server thread");
while(1) {
memset(&buf, 0, sizeof(buf));
input = fgets(buf, sizeof(buf), stdin);
if (!input) {
close(sockfd);
exit(0);
}
write(sockfd, buf, sizeof(buf));
}
}
int read_socket(int sockfd){
char data[2048];
int len;
while(1){
memset(&data,0, sizeof(data));
len = read(sockfd, data, sizeof(data) -1);
if (!len){
close(sockfd);
exit(0);
}
fprintf(stdout,"%s", data);
}
return 1;
}
int send_stdin(int sockfd){
// arg is the socket to send on
char buf[2048];
char* input;
while(1) {
memset(&buf, 0, sizeof(buf));
input = fgets(buf, sizeof(buf), stdin);
if (!input) {
close(sockfd);
exit(0);
}
write(sockfd, buf, sizeof(buf));
}
}
int server_listen(char *host, char* port, int udp)
{
const char* portname= port ;
struct addrinfo hints;
memset(&hints,0,sizeof(hints));
const char* hostname;
if (host)
hostname= host; /* wildcard */
else {
hostname = 0;
hints.ai_flags=AI_PASSIVE|AI_ADDRCONFIG;
}
hints.ai_family = AF_INET;
hints.ai_protocol=0;
if(udp)
hints.ai_socktype = SOCK_DGRAM;
else
hints.ai_socktype = SOCK_STREAM;
struct addrinfo* res=0;
int err=getaddrinfo(hostname,portname,&hints,&res);
if (err!=0) {
fprintf(stderr, "internal error in server listen get addrinfo");
exit(1);
}
int server_fd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
if (server_fd==-1) {
fprintf(stderr, "internal error in server listen socket");
exit(1);
}
if (bind(server_fd,res->ai_addr,res->ai_addrlen)==-1) {
fprintf(stderr, "internal error in server listen bind");
exit(1);
}
freeaddrinfo(res);
if(!udp) {
if (listen(server_fd, SOMAXCONN)) {
fprintf(stderr, "internal error in server listen listen call");
exit(1);
}
int session_fd = accept(server_fd, 0, 0);
if (session_fd == -1) {
fprintf(stderr, "internal error in server listen accept call");
exit(1);
} else {
handle_session(session_fd, 0);
close(session_fd);
exit(0);
}
}
handle_session(server_fd, 0);
close(server_fd);
exit(0);
}
int main(int argc, char *argv[]) {
int c, listen = 0, udp = 0;
char *port = NULL;
char *hostname = NULL;
char usage[] = "invalid or missing options\n usage: snc [-l] [-u] [hostname] port";
while ((c = getopt(argc, argv, "lu")) != -1) {
switch (c) {
case 'l':
listen = 1;
break;
case 'u':
udp = 1;
break;
}
}
if ((argc > 5) || (argc < 2)){
printf(usage);
exit(1);
}
if ((listen && udp && argc ==4) ||(listen && !udp && argc ==3) ) {
hostname = "localhost";
port = argv[argc - 1];
}
else if((listen && udp && argc==5) ||(listen && !udp && argc ==4)){
hostname = argv[argc-2];
port = argv[argc-1];
}
int server = 0, client=0;
port = argv[argc-1];
hostname = argv[argc-2];
if (listen)
server = server_listen(hostname, port, udp);
else {
client = init_connect(hostname, port, udp);
}
}
// create connection for not listening
// listen for connections for listening
// handle conetion -- for echoing
// int port = strtol(argv[argc-1]);
//
// }
// if(!listen){
// if((udp && argc !=4) || (!udp && argc !=3)){
// fprintf(stderr, usage);
// exit(1);
// }
// }
//}
// int listen_fd, comm_fd, sock_fd;
//struct sockaddr_in servaddr;
//struct hostent *server;
//char sendline[1064];
//if (udp) {
//if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
//fprintf(stderr, "internal error in udp init server socket");
//exit(1);
//}
//} else {
//if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
//fprintf(stderr, "internal error in udp init server socket");
//exit(1);
//}
//}
//
//memset(&servaddr, 0, sizeof(servaddr));
//servaddr.sin_family = AF_INET;
//
//int handle_session(int sockfd){
// char data[2048];
// char buf[2048];
// size_t len;
// while(1){
// len = read(sockfd, data, 2048);
// data[len] = '\0';
// fprintf(stdout,"%s", data);
//
// memset(&buf,0, sizeof(buf));
// ssize_t bytes = read(0,buf,2048);
//
// if (bytes==0){
// close(sockfd);
// exit(0);
// }
//
// send(sockfd, buf, strlen(buf) +1,0);
// }
//}