summaryrefslogtreecommitdiffstats
path: root/03_exercise/cli/client.c
blob: 67731f4dded9c994c8a03ce7e578701b87137e69 (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdbool.h>

#define PORT 9000
#define HOST "127.0.0.1"

#define BUF_SIZE 2048

#define BREAK    1
#define CONTINUE 2

char *get_current_dir_name(void);

int server_sock;
pid_t pid;

/* Signal Handler for SIGINT */
void sigintHandler(int sig_num);

static inline void die(char const *msg);

int write_all(int target_sock, char *buffer, size_t size);

void send_file(char *path);

int parse(char *buffer, ssize_t length);

void read_stdin();

_Noreturn void read_server_sock();

int main() {
    setvbuf(stdout, NULL, _IONBF, 0);

    struct sockaddr_in addr = {
            .sin_family = AF_INET,
            .sin_port = htons(PORT),
            .sin_addr.s_addr = inet_addr(HOST)
    };

    if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        die("Could not open socket");

    if (connect(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
        die("Could not connect to socket");

    signal(SIGINT, &sigintHandler);

    pid = fork();

    if (pid == 0) {
        read_stdin();
    } else {
        read_server_sock();
    }

    close(server_sock);
    kill(pid, SIGKILL);

    return 0;
}

void receive_file(int recv_sock, char *path, int size) {
    char buffer[BUF_SIZE];
    int  file_fd;
    int  received;

    if ((file_fd = open(path, (O_WRONLY | O_CREAT | O_TRUNC), 0644)) == -1) { perror("Open"); }

    if (size == 0) {
        // just touch
        close(file_fd);
        return;
    }

    received = 0;

    /* Read all data */
    while (received < size) {
        int nread;
        if ((nread = read(recv_sock, buffer, BUF_SIZE)) > 0) {
            if (write_all(file_fd, buffer, nread) != nread) {
                break;
            }
            received += nread;
        }
    }

    if (received == size) {
        printf("successfully got remote file \"%s/%s\"", get_current_dir_name(), path);
        printf("\n");
    } else {
        printf("error getting remote file \"%s/%s\"", get_current_dir_name(), path);
        printf("\n");
    }

    close(file_fd);
}

bool check_get(char *buffer) {
    // "<<!bytecount!name"
    char cmd[4];

    memcpy(cmd, buffer, 3);
    cmd[3] = 0;

    if (strcmp(cmd, "<<!") == 0) {
        //printf("[c]: \"%s\"\n", buffer);
        int    offset    = 3;
        int    i         = offset;
        char   rest[BUF_SIZE - 3];
        size_t file_size;
        for (; i < BUF_SIZE; ++i) {
            if (buffer[i] == '!')
                break;
            rest[i - offset] = buffer[i];
        }
        rest[i - offset] = 0;
        file_size = atoi(rest);

        offset           = ++i;
        for (; i < BUF_SIZE; ++i) {
            if (buffer[i] == '!')
                break;
            rest[i - offset] = buffer[i];
        }
        rest[i - offset] = 0;
        char path[strlen(rest) + 1];
        memcpy(path, rest, strlen(rest));
        path[strlen(rest)]     = 0;
        path[strlen(rest) - 1] = 0;

        receive_file(server_sock, path, file_size);

        return true;
    }
    return false;
}

void read_server_sock() {
    char server_buffer[BUF_SIZE];

    while (1) {
        ssize_t length;
        if ((length = read(server_sock, server_buffer, BUF_SIZE)) > 0) {
            server_buffer[length] = '\0';

            // get issued, as it seems
            if (check_get(server_buffer)) {
                printf("$> ");
                fflush(stdout);
                continue;
            }

            printf("%s", server_buffer);
            if (length == 1 && server_buffer[length - 1] == '\n') {
                printf("$> ");
                fflush(stdout);
            }
        }
    }
}

void read_stdin() {
    char stdin_buffer[BUF_SIZE];

    bool done = false;
    while (!done) {
        ssize_t length;
        memset(stdin_buffer, 0, BUF_SIZE);
        if ((length = read(STDIN_FILENO, stdin_buffer, BUF_SIZE)) > 0) {
            switch (parse(stdin_buffer, length)) {
                case CONTINUE:
                    continue;
                case BREAK:
                    done = true;
                    break;
                default:
                    break;
            }
        }
    }
}

void sigintHandler(int sig_num) {
    errno = 0;
    if (fcntl(server_sock, F_GETFD) != -1 || errno != EBADF) {
        sig_num = write(server_sock, "exit\n", 6);
        (void) sig_num;
        close(server_sock);
    }
    errno = 0;
    die("Terminating client\n");
}

static inline void die(char const *msg) {
    perror(msg);
    kill(pid, SIGKILL);
    exit(-1);
}

int write_all(int target_sock, char *buffer, size_t size) {
    int sent = 0;

    while (sent < size) {
        int nwrite;
        if ((nwrite = write(target_sock, buffer + sent, size - sent)) < 0) {
            die("Write");
        }

        sent += nwrite;
    }

    return sent;
}

void send_file(char *path) {
    printf("Trying to send \"%s\" to remote...\n", path);
    char buffer[BUF_SIZE];
    memset(buffer, 0, BUF_SIZE);
    int tmp_fd, file_fd;

    if ((tmp_fd = open(path, O_RDONLY)) == -1) {
        perror("Open");
        return;
    }
    FILE *file = fdopen(tmp_fd, "r");
    fseek(file, 0L, SEEK_END);
    long int sz = ftell(file);
    fseek(file, 0L, SEEK_SET);
    fclose(file);
    close(tmp_fd);
    if ((file_fd = open(path, O_RDONLY)) == -1) {
        perror("Open");
        return;
    }

    int length = snprintf(NULL, 0, "%ld", sz);
    sprintf(buffer, "<<!%ld!", sz);
    sprintf(&(buffer[3 + length + 1]), "%s!\n", path);

    printf("[c] \"%s\"\n", buffer);

    if (write(server_sock, buffer, strlen(buffer)) != strlen(buffer)) {
        perror("write header");
        return;
    }
    memset(buffer, 0, BUF_SIZE);

    errno = 0;
    size_t count;
    while ((count = read(file_fd, buffer, sizeof(buffer))) > 0) {
        printf("Sent %i bytes\n", write_all(server_sock, buffer, count));
    }

    if (errno)
        perror("wad");

    close(file_fd);

    printf("done.\n");
}

int parse(char *buffer, ssize_t length) {
    char cmd[4];
    char path[BUF_SIZE - 3];
    memcpy(cmd, buffer, 3);
    cmd[3] = 0;
    memcpy(path, buffer + 4, BUF_SIZE - 4);
    path[strlen(path) - 1] = 0;
    if (strcmp(cmd, "put") == 0) {
        if (strlen(path) == 0) {
            fprintf(stderr, "path missing\n");
        } else {
            send_file(path);
        }
        return CONTINUE;
    }

    buffer[length] = '\0';

    if (strspn(buffer, " \n\t") == strlen(buffer)) {
        // skip empty lines - empty being just spaces or tabs
        return CONTINUE;
    }

    if (write(server_sock, buffer, strlen(buffer)) < 0)
        die("Could not send message");

    // TODO: exit should only close the connection (cleanly), NOT terminate the server
    if (strcmp(buffer, "exit\n") == 0) {
        return BREAK;
    }

    return 0;
}