-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
237 lines (219 loc) · 4.56 KB
/
client.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
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <ctype.h> // tolower
#define TRUE 1
#define FALSE 0
#define READ_END 0
#define WRITE_END 1
#define BUFF_SIZE 100
#define LENGTH_BYTES 1
#define PROMPT ": "
void printify(const char* str, ...);
void lower(char* str);
void disconnect();
void exit_gracefully();
void exit_handler(int signo);
void sig_conn_closed_handler();
int sock;
int connection_open = FALSE;
int main(int argc, char *argv[])
{
if (signal(SIGINT, exit_handler) == SIG_ERR)
{
perror("signal: SIGINT");
return -1;
}
if (signal(SIGTERM, exit_handler) == SIG_ERR)
{
perror("signal: SIGTERM");
return -1;
}
struct sockaddr_in server;
struct hostent *hp;
server.sin_family = AF_INET;
while(TRUE)
{
printify("");
char input[BUFF_SIZE];
int c;
if ((c = read(STDIN_FILENO, input, BUFF_SIZE)) < 0)
{
perror("Reading from stdin");
}
if (c == 1)
continue;
input[c-1] = '\0';
lower(input);
char* cmd = strtok(input, " ");
if (!strcmp(cmd, "conn") || !strcmp(cmd, "connect"))
{
char* host = strtok(NULL, " ");
char* port = strtok(NULL, " ");
if (!(host && port))
{
printify("Usage: conn[ect] <host> <port>\n");
continue;
}
hp = gethostbyname(host);
if (!hp)
{
printify("%s: unknown host\n", argv[1]);
continue;
}
bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
server.sin_port = htons(atoi(port));
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("opening stream socket");
exit(EXIT_FAILURE);
}
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0)
{
perror("connecting stream socket");
continue;
}
connection_open = TRUE;
printify("Connected.\n");
struct pollfd rfds[2];
rfds[0].fd = STDIN_FILENO;
rfds[0].events = POLLIN;
rfds[1].fd = sock;
rfds[1].events = POLLIN;
while(connection_open)
{
// printify("client waiting for input\n");
int r, w;
if ((r = poll(rfds, 2, -1)) < 0)
{
if (errno == EINTR)
continue;
else
{
perror("poll");
exit_gracefully();
}
}
if (rfds[1].revents & POLLIN)
{
// printify("sock input detected\n");
char buff[BUFF_SIZE];
if ((r = read(sock, buff, BUFF_SIZE)) < 0)
{
perror("sock read");
continue;
}
if (r == 0)
{
// printify("Socket closed.\n");
connection_open = FALSE;
// raise(SIG_CONN_CLOSED);
break;
}
if (write(STDOUT_FILENO, buff, r) < 0)
{
perror("stdout write");
continue;
}
}
else if (rfds[0].revents & POLLIN)
{
// printify("stdin input detected\n");
char buff[BUFF_SIZE];
if ((r = read(STDIN_FILENO, buff, BUFF_SIZE)) < 0)
{
perror("stdin read");
continue;
}
buff[r-1] = '\0';
char tmp[BUFF_SIZE];
sscanf(buff, "%s", tmp);
lower(tmp);
if (!strcmp(tmp, "q") || !strcmp(tmp, "ex") || !strcmp(tmp, "quit") || !strcmp(tmp, "exit"))
{
printify("exit cmd detected\n");
exit_gracefully();
}
if ((w = write(sock, &r, LENGTH_BYTES)) < 0)
{
perror("Writing to socket");
printify("Failed to send command size.\n");
continue;
}
// printify("sent cmd length %d\n", r);
if ((w = write(sock, buff, r)) < 0)
{
perror("Writing to socket");
printify("Failed to send command.\n");
}
if (w == 0)
{
printify("Zero write.\n");
break;
}
// printify("sent cmd %s\n", buff);
}
}
disconnect();
}
else
{
printify("Unknown command.\n");
}
}
}
void disconnect()
{
printify("Disconnected.\n");
shutdown(sock, SHUT_RDWR);
close(sock);
}
void printify(const char* str, ...)
{
char buff[BUFF_SIZE];
va_list args;
va_start(args, str);
write(STDOUT_FILENO, PROMPT, sizeof(PROMPT));
if (write(STDOUT_FILENO, buff, vsprintf(buff, str, args)) == -1)
{
perror("printify: write");
}
fsync(STDOUT_FILENO);
va_end(args);
}
void exit_gracefully()
{
exit_handler(0);
}
void exit_handler(int signo)
{
int len = 4;
write(sock, &len, LENGTH_BYTES);
write(sock, "exit", 4);
shutdown(sock, SHUT_RDWR);
close(sock);
printify("Exiting.\n");
exit(signo);
}
void lower(char* str)
{
char* c;
for(c = str; *c; c++)
*c = tolower(*c);
}
void sig_conn_closed_handler()
{
connection_open = FALSE;
}