-
Notifications
You must be signed in to change notification settings - Fork 1
/
client1.c
167 lines (148 loc) · 3.65 KB
/
client1.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "crc16.h"
//ASCII characters
#define SOH 1
#define STX 2
#define EOT 4
#define ACK 6
#define NAK 21
#define CAN 24
#define SUB 26
enum sendstate {
initial, // send filename
handshake, // wait for C
send_block, // read and send the block
wait_reply, // wait for packet to be ack'd
finish
}; // send eot
#define XMODEM_KEY 0x1021
int main(int argc, char* argv[]) {
int soc, len, err;
unsigned char buf[128]; // payloads
char temp;
struct addrinfo *info, hints;
struct sockaddr_in peer;
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
if ( argc != 4) {
fprintf(stderr, "Usage: %s <hostname> <port> <filename>\n", argv[0]);
exit(1);
}
if ((err = getaddrinfo(argv[1], argv[2], &hints, &info))) {
fprintf (stderr, "%s\n", gai_strerror(err));
exit (1);
}
FILE *fp = fopen(argv[3], "rb");
if (!fp) {
perror("fopen");
exit(2);
}
peer = *(struct sockaddr_in *)(info->ai_addr);
if ((soc = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror ("socket");
exit (1);
}
if (connect(soc, (struct sockaddr *)&peer, sizeof(peer)) == -1) {
perror ("connect");
exit (1);
}
int state = initial;
int current_block, readblock;
while (1) {
if (state == initial) {
write(soc, argv[3], strlen(argv[3]));
write(soc, "\r\n", 2);
current_block = 1;
readblock = 1;
state = handshake;
printf("Client sent filename and entering handshake.\n");
}
if (state == handshake) {
while ((len = read(soc, &temp, 1)) > 0) {
if (temp == 'C') {
printf("Client found C; entering send_block\n");
state = send_block;
break;
}
}
if (len <= 0) {
fprintf(stderr, "Server dropped client\n");
exit(1);
}
}
if (state == send_block) {
if (readblock) {
readblock = 0;
len = fread(buf, 1, 128, fp);
if (len == 0) {
printf("Client got 0 fread; moving to finish.\n");
state = finish;
continue;
}
while (len < 128)
buf[len++] = SUB;
}
unsigned char char_block = current_block;
temp = SOH;
printf("Client sending the block+overhead\n");
write(soc, &temp, 1);
printf("Sending block %d and inverse %d\n", char_block, 255-char_block);
write(soc, &char_block, 1);
char_block = 255 - char_block;
write(soc, &char_block, 1);
write(soc, buf, 128);
unsigned short crc = crc_message(XMODEM_KEY, buf, 128);
printf("Client wants to send crc %x\n", crc);
char_block = crc >> 8;
printf("Client sends crc first byte %x\n", char_block);
write(soc, &char_block, 1);
char_block = crc;
printf("Client sends crc second byte %x\n", char_block);
write(soc, &char_block, 1);
printf("Client moving to wait_reply\n");
state = wait_reply;
}
if (state == wait_reply) {
while ((len = read(soc, &temp, 1)) > 0) {
if (temp == NAK) {
printf("Client received nak; moving back to send_block\n");
state = send_block;
break;
}
if (temp == ACK) {
printf("Client received ack; moving to send_block for next block\n");
state = send_block;
readblock = 1;
current_block++;
if (current_block > 255)
current_block = 0;
break;
}
}
}
if (state == finish) {
temp = EOT;
printf("Client sent eot, waiting for ack\n");
write(soc, &temp, 1);
while ((len = read(soc, &temp, 1)) > 0) {
if (temp == NAK) {
temp = EOT;
write(soc, &temp, 1);
printf("Client received nak to its EOT! Sending new EOT\n");
break;
}
if (temp == ACK) {
printf("Done!\n");
exit(0);
}
}
}
}
return 0;
}