-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsock352lib.c
More file actions
717 lines (610 loc) · 17.6 KB
/
sock352lib.c
File metadata and controls
717 lines (610 loc) · 17.6 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* CS352 Socket Library Implementation
* Project 1, Part 1
*
* This library implements 352 RDPv1 - the 352 Reliable Data Protocol version 1
*
* There are 9 library functions defined for part 1.
* CS352 RDP uses UDP as the underlying transport protocol.
* Later versions will add port spaces, concurrency, and security functions.
Mushaheed Kapadia (msk154)
Megan Murray (msm230)
*/
#include "sock352.h"
#include "socket352.c"
#include <errno.h>
#include <fcntl.h>
/*
* All the current (active) connections
*/
socket352_t *sockets;
/*
* A temp socket to hold information
*/
socket352_t *temp;
/*
* sock352_init
*
* initializes the port (use default port of 27182 for this project)
* called by both client and server sides
*/
int sock352_init(int udp_port)
{
/*
* Check to see if port is valid
*/
if(udp_port < 0){
printf("Failed to set the upd_port in sock352_init()\n");
return SOCK352_FAILURE;
}
/*
* Initialize temp socket
*/
temp = (socket352_t *)calloc(1, sizeof(socket352_t));
initSocket(temp);
/*
* Set the port values
*/
if(udp_port == 0){
temp->local_port = SOCK352_DEFAULT_UDP_PORT;
temp->remote_port = SOCK352_DEFAULT_UDP_PORT;
}
else{
temp->local_port = temp->remote_port = udp_port;
}
return SOCK352_SUCCESS;
}
/*
* sock352_init2
*
* initializes the local and remote port if they are set
*
*/
int sock352_init2(int remote_port, int local_port)
{
/*
* Check to see if valid ports were given
*/
if(local_port < 0 || remote_port < 0){
printf("Failed to set the remote or local port in sock352_init2()\n");
return SOCK352_FAILURE;
}
/*
* Intialize the temp socket
*/
temp = (socket352_t *)calloc(1, sizeof(socket352_t));
initSocket(temp);
/*
* Set the remote port
*/
if(remote_port == 0) temp->remote_port = SOCK352_DEFAULT_UDP_PORT;
else temp->remote_port = remote_port;
/*
* Set the local port
*/
if(local_port == 0) temp->local_port = SOCK352_DEFAULT_UDP_PORT;
else temp->local_port = local_port;
return SOCK352_SUCCESS;
}
/*
* sock352_init3()
*
* initializes local and remote ports,
* sets up environment variables
*/
int sock352_init3(int remote_port, int local_port, char *env_p[]){
/*
* Init the socket same as sock325_init2()
*/
if(sock352_init2(remote_port, local_port) == SOCK352_FAILURE){
return SOCK352_FAILURE;
}
/*
* Set the environment variables ????
* what do we do with the env_p ????
*/
return SOCK352_SUCCESS;
}
/*
* sock352_socket
* creates a socket and returns an fd
* called by both client and server sides
*
@param : domain --> address family type
@param : type --> type of socket (stream, write, etc.)
@param : protocol --> TCP/UDP ?
@return : socket file descriptor -- kind of like an index or a hash for the file descriptor
*/
int sock352_socket(int domain, int type, int protocol)
{
/*
* Check to see if inputs are valid -- Don't really need them after this...
*/
if(domain != PF_CS352) {
printf("Invalid domain in sock352_socket()\n");
return SOCK352_FAILURE;
}
if(type != SOCK_STREAM) {
printf("Invalid type in sock352_socket()\n");
return SOCK352_FAILURE;
}
if(protocol != 0) {
printf("Invalid protocol in sock352_socket()\n");
return SOCK352_FAILURE;
}
/*
* Create an actual udp socket
*/
int sock_fd = 0;
if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
printf("Failed to create socket in sock352_socket(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
temp->sock_fd = sock_fd;
/*
* Add the socket to the socket list
*/
int sfd = addSocket(&sockets, temp);
}
/*
* sock352_bind
* ???
* Bind has to take in the server socket info and put it into the socket that we created, so tha t we can use it again later on in connect when we're setting up the actual UDP socket, since the connect() function only takes in a spot for the struct sockaddr_in *client and not the server.
*
* used by server side only
*/
int sock352_bind(int fd, sockaddr_sock352_t *addr, socklen_t len)
{
/*
* Get the socket from the connections
*/
socket352_t *socket;
if((socket = findSocket(&sockets, fd)) == NULL){
printf("Invalid fd in sock352_bind()\n");
return SOCK352_FAILURE;
}
/*
* Set up the sockaddr for the bind (local side)
*/
socket->local = (struct sockaddr_in *)calloc(1, sizeof(struct sockaddr_in));
socket->local->sin_family = AF_INET;
socket->local->sin_addr.s_addr = addr->sin_addr.s_addr;
socket->local->sin_port = htons(socket->local_port);
/*
* Actually bind
*/
if((bind(socket->sock_fd, (struct sockaddr *)socket->local, sizeof(struct sockaddr_in))) < 0){
printf("Unable to bind socket to address in sock352_bind() %s\n", strerror(errno));
return SOCK352_FAILURE;
}
return SOCK352_SUCCESS;
}
/*
* sock352_connect
*
* called only from client
*/
int sock352_connect(int fd, sockaddr_sock352_t *dest, socklen_t len)
{
/*
* Get our socket from the hash table
*/
socket352_t *socket;
if((socket = findSocket(&sockets, fd)) == NULL){
printf("Invalid fd in sock352_connect()\n");
return SOCK352_FAILURE;
}
/*
* Create the destination sockaddr_in
*/
socket->other = (struct sockaddr_in *)calloc(1, sizeof(struct sockaddr_in));
socket->other->sin_family = AF_INET;
socket->other->sin_addr.s_addr = dest->sin_addr.s_addr;
socket->other->sin_port = htons(socket->remote_port);
/*
* Create the packet to send
*/
packet_t packet;
packet.header.version = SOCK352_VER_1;
packet.header.header_len = htons((uint16_t)sizeof(sock352_pkt_hdr_t));
packet.header.flags = SOCK352_SYN;
packet.header.sequence_no = getSeqNumber(socket);
packet.header.window = sizeof(packet.data);
printf("header len: %d\n", packet.header.header_len);
/*
* Send the packet to the destination
*/
if((sendto(socket->sock_fd, &(packet.header), sizeof(sock352_pkt_hdr_t), 0, (struct sockaddr *)socket->other, sizeof(struct sockaddr_in))) < 0){
printf("Failed to send SYN packet in sock352_connect(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Change the connection state
*/
socket->state = SYN_SENT;
/*
* Wait for packet to arrive from server
*/
socklen_t sockaddr_size = sizeof(struct sockaddr_in);
printf("Waiting for packet from server...\n");
if((recvfrom(socket->sock_fd, &(packet.header), sizeof(packet_t), 0, (struct sockaddr *)socket->other, &sockaddr_size)) < 0){
printf("Failed to read packet from server in sock352_connect(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Got packet from the server.. make sure SYN|ACK is set
if(packet->header.flags != (SOCK352_SYN | SOCK352_ACK)){
printf("Invalid packet received from server\n");
return SOCK352_FAILURE;
}
*/
printf("packet->header_len: %d\n", packet.header.header_len);
/*
* Update packet header to be sent
*/
packet.header.flags = SOCK352_ACK;
packet.header.ack_no = packet.header.sequence_no + 1;
packet.header.sequence_no = getSeqNumber(socket);
/*
* Set connection as established
*/
socket->state = ESTABLISHED;
/*
* Set the updated ACK packet to the server
*/
if((sendto(socket->sock_fd, &(packet.header), sizeof(sock352_pkt_hdr_t), 0, (struct sockaddr *)socket->other, sizeof(struct sockaddr_in))) < 0){
printf("Failed to send ACK packet in sock352_connect(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Create the transmit and receive lists
socket->unack_packets = (packet_t *)calloc(1, sizeof(packet_t));
socket->recv_packets = (packet_t *)calloc(1, sizeof(packet_t));
*/
/*
* Free stuff ( the packet )
*/
//free(packet);
return SOCK352_SUCCESS;
}
/*
* sock352_listen
*
* converts socket to listening socket where incoming connnections from clients
* will be accepted
* used by server side only
*/
int sock352_listen(int fd, int n)
{
/*
* Get the local socket
*/
socket352_t *socket;
if((socket = findSocket(&sockets, fd)) == NULL){
printf("Bad socket fd in sock352_listen()\n");
return SOCK352_FAILURE;
}
/*
* Allocate space for the connections
*/
socket->n_connections = n;
socket->connections = (int *)calloc(n, sizeof(int));
/*
* Change the socket to LISTEN state
*/
socket->state = LISTEN;
return SOCK352_SUCCESS;
}
/*
* sock352_accept
*
* puts server to sleep waiting for a connection to arrive
* return value is a new fd, the connected fd.
* addr is the address of the client that just connected
* called only by server side
*/
int sock352_accept(int _fd, sockaddr_sock352_t *addr, int *len)
{
/*
* Get our socket from the hash table
*/
socket352_t *socket352;
if((socket352 = findSocket(&sockets, _fd)) == NULL){
printf("Failed to find the socket in sock352_accept()\n");
return SOCK352_FAILURE;
}
/*
* Initalize the other sockaddr
*/
socket352->other = (struct sockaddr_in *)calloc(1, sizeof(struct sockaddr_in));
/*
* Create a packet struct to hold the incoming packet
*/
packet_t *packet = (packet_t *)calloc(1, sizeof(packet_t));
/*
* Create a socklen_t to size
*/
socklen_t sockaddr_size = sizeof(struct sockaddr_in);
/*
* Wait for an incoming SYN packet header
*/
printf("Waiting for client SYN packet...\n");
if((recvfrom(socket352->sock_fd, &(packet->header), sizeof(sock352_pkt_hdr_t), 0, (struct sockaddr *)socket352->other, &sockaddr_size)) < 0){
printf("Failed to read from socket in sock352_accept(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
printf("packet->header.header_len: %u\n", packet->header.header_len);
/*
* Got first packet from client.. Check if SYN bit is set
*/
if(packet->header.flags != SOCK352_SYN){
printf("Invalid packet received in sock352_accept()\n");
return SOCK352_FAILURE;
}
/*
* Change the socket state
*/
socket352->state = SYN_RECEIVED;
/*
* Update packet to be sent
*/
packet->header.flags = SOCK352_SYN | SOCK352_ACK;
packet->header.ack_no = packet->header.sequence_no;
packet->header.sequence_no = getSeqNumber(socket352);
packet->header.window = sizeof(packet->data);
/*
* Send the updated packet
*/
if((sendto(socket352->sock_fd, &(packet->header), sizeof(sock352_pkt_hdr_t), 0, (struct sockaddr *)socket352->other, sockaddr_size)) < 0){
printf("Failed to send SYN|ACK packet in sock352_accept(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Change the state to established
*/
socket352->state = ESTABLISHED;
/*
* Get ACK packet
*/
printf("Waiting for ACK packet from client\n");
if((recvfrom(socket352->sock_fd, packet, sizeof(packet_t), 0, (struct sockaddr *) socket352->other, &sockaddr_size)) < 0){
printf("Failed to received ACK packet in sock352_accept(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
if(packet->header.flags != SOCK352_ACK){
printf("Invalid ACK packet in sock352_accept()\n");
return SOCK352_FAILURE;
}*/
/*
* Create the connection for the client structure
*/
socket352_t *client = (socket352_t *)calloc(1, sizeof(socket352_t));
client->other = (struct sockaddr_in *)calloc(1, sizeof(struct sockaddr_in));
//addSocket(&sockets, client);
addClient(socket352, client);
/*
* Free stuff
*/
free(packet);
return socket352->fd;
}
/* sock352_close
*
* Closes the specified socket connection
* releases any memory and general cleanup may be needed
* called by both client and server
*/
int sock352_close(int fd)
{
printf("closing... \n");
/*
* Get the socket
*/
socket352_t *socket;
if((socket=findSocket(&sockets, fd)) == NULL){
printf("Unable to find socket in sock352_close()\n");
return SOCK352_FAILURE;
}
/*
* Create the fin packet
*/
packet_t *fin_packet = (packet_t *)calloc(1, sizeof(packet_t));
fin_packet->header.version = SOCK352_VER_1;
fin_packet->header.header_len = (uint16_t)sizeof(sock352_pkt_hdr_t);
fin_packet->header.payload_len = 0;
fin_packet->header.flags = SOCK352_FIN;
if(sendto(socket->sock_fd, fin_packet, sizeof(packet_t), 0, (struct sockaddr *)socket->other, sizeof(struct sockaddr_in)) < 0){
printf("Failed to send packet in sock352_close(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
socket->state = FIN_WAIT_1;
/*
* Get a packet from the other side
*/
packet_t *r_packet = (packet_t *)calloc(1, sizeof(packet_t));
if(recvfrom(socket->sock_fd, r_packet, sizeof(packet_t), 0, NULL, NULL) < 0){
printf("Failed to receive fin packet in sock352_close(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
socket->state = LAST_ACK;
/*
* Create the ack packet
*/
packet_t *ack_packet = (packet_t *)calloc(1, sizeof(packet_t));
ack_packet->header.version = SOCK352_VER_1;
ack_packet->header.header_len = (uint16_t)sizeof(sock352_pkt_hdr_t);
ack_packet->header.payload_len = 0;
ack_packet->header.flags = SOCK352_ACK;
/*
* Send the ack packet
*/
if(sendto(socket->sock_fd, ack_packet, sizeof(packet_t), 0, (struct sockaddr *)socket->other, sizeof(struct sockaddr_in)) < 0){
printf("Failed to send ack packet in sock352_close(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Get ACK packet from the other side
*/
if(recvfrom(socket->sock_fd, r_packet, sizeof(packet_t), 0, NULL, NULL) < 0){
printf("Failed to receive ack packet in sock352_close(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
socket->state = CLOSED;
/*
* Close socket
*/
close(socket->sock_fd);
printf("closed socket\n");
/*
* Free things
*/
//free(fin_packet);
//free(r_packet);
//free(ack_packet);
//free(socket);
//free(sockets);
return SOCK352_SUCCESS;
}
/*
* read and write should be pretty straight forward
* do these last
* used by both client and server
*/
/*
* read to the buffer from teh fd
* @param: fd - the fd to read from
* @param: buf - the buf to read to
* @param: count - the max number of bytes to read in
* @return: the number of bytes we read from the fd
*/
int sock352_read(int fd, void *buf, int count)
{
printf("in sock352_read()\n");
/*
* Get the socket
*/
socket352_t *socket;
if((socket = findSocket(&sockets, fd)) == NULL){
printf("Failed to load the socket in sock352_read(): %d\n", fd);
return SOCK352_FAILURE;
}
/*
* Create the packet to read to
*/
packet_t r_packet;
/*
* Create the ack packet to respond with
*/
packet_t *ack_packet = (packet_t *)calloc(1, sizeof(packet_t));
ack_packet->header.version = SOCK352_VER_1;
ack_packet->header.header_len = (uint16_t)sizeof(sock352_pkt_hdr_t);
ack_packet->header.sequence_no = getSeqNumber(socket);
ack_packet->header.flags = SOCK352_ACK;
/*
* Read a packet
*/
int bytes_read = 0;
if((bytes_read = recvfrom(socket->sock_fd, &r_packet, sizeof(packet_t), 0, NULL, NULL)) < 0){
printf("Failed to receive packet in sock352_read(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Set up ack no
*/
ack_packet->header.ack_no = r_packet.header.sequence_no;
/*
* Send ack packet
*/
if((sendto(socket->sock_fd, ack_packet, sizeof(packet_t), 0, (struct sockaddr *)socket->other, sizeof(struct sockaddr_in))) < 0){
printf("Failed to send ack packet in sock352_read(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Copy the information over into the buffer
*/
memcpy(buf, r_packet.data, count);
/*
* Free stuff
*/
free(ack_packet);
//free(r_packet);
printf("payload size: %d\n", ntohs(bytes_read));
return ntohs(r_packet.header.payload_len);
}
/*
* write to the buffer to the fd
* @param: fd - fd to write to
* @param: buf - the buffer to write
* @param: count - the number of bytes that we're writing
* @return: the number of bytes written to the fd
*
* --> write one packet at a time
* --> wait for ack
* --> return
*/
int sock352_write(int fd, void *buf, int count)
{
printf("in sock352_write()\n");
/*
* Get the socket from the connection
*/
socket352_t *socket;
if((socket = findSocket(&sockets, fd)) == NULL){
printf("Failed to find the socket in socket352_write()\n");
return SOCK352_FAILURE;
}
/*
* Create and set up the send packet struct to be sent
*/
packet_t *packet = (packet_t *)calloc(1, sizeof(packet_t));
memcpy(packet->data, buf, count); /* copy the data from the buf */
/*
* Create and set up the send packet header
*/
packet->header.version = SOCK352_VER_1;
packet->header.header_len = (uint16_t)sizeof(sock352_pkt_hdr_t);
packet->header.sequence_no = getSeqNumber(socket);
packet->header.payload_len = htons(count);
printf("payload size: %d\ncount: %d\n", packet->header.payload_len, count);
/*
* Create the recv packet
*/
packet_t *r_packet = (packet_t *)calloc(1, sizeof(packet_t));
int bytes_written;
int packet_sent = 0;
while(packet_sent == 0){
/*
* Send the packet
*/
if((bytes_written = sendto(socket->sock_fd, packet, sizeof(*packet), 0, (struct sockaddr *)socket->other, sizeof(struct sockaddr_in))) < 0){
printf("Failed to write to packet in sock352_write(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
/*
* Receive the ack packet
*/
int bytes_read;
if((bytes_read = recvfrom(socket->sock_fd, r_packet, sizeof(packet_t), 0, NULL, NULL)) < 0){
printf("Failed to read ack packet in sock352_read(): %s\n", strerror(errno));
return SOCK352_FAILURE;
}
packet_sent = 1; /* say we sent the packet */
/*
* Confirm the ack number and ack flags
*/
if(r_packet->header.ack_no != packet->header.sequence_no || r_packet->header.flags != SOCK352_ACK){
printf("Invalid packet received in sock352_write()\n");
return SOCK352_FAILURE;
}
}
/*
* Free things
*/
free(packet);
free(r_packet);
/*
* Everything Okay.
*/
return count;
}