summaryrefslogtreecommitdiffstats
path: root/03_exercise/cli/client.c
blob: d64dd626cb297650c53bf4adeb42674fc8131cab (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
#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>

#define PORT 9000
#define HOST "127.0.0.1"

#define BUF_SIZE 2048

int cfd;

/* Signal Handler for SIGINT */
void sigintHandler(int sig_num) {
    errno = 0;
    if (fcntl(cfd, F_GETFD) != -1 || errno != EBADF) {
        write(cfd, "exit\n", 6);
        close(cfd);
    }
    printf("Terminating client\n");
    fflush(stdout);
    exit(-1);
}

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

int write_all(int sock, char *buffer, int size) {
    int nwrite, sent = 0;

    while (sent < size) {
        if ((nwrite = write(sock, buffer + sent, size - sent)) < 0) {
            perror("Write");
            exit(1);
        }

        sent += nwrite;
    }

    return sent;
}

void send_file(int server_fd, char *path) {
    char buffer[512];
    int  file_fd;

    if ((file_fd = open(path, O_RDONLY)) == -1) { perror("Open"); }

    FILE *file = fdopen(file_fd, "r");

    fseek(file, 0L, SEEK_END);
    long int sz = ftell(file);
    fseek(file, 0L, SEEK_SET);

    int  length = snprintf(NULL, 0, "%ld", sz);
    char msg[length + 1 + 3];
    sprintf(msg, "<<!%ld", sz);

    write(server_fd, msg, strlen(msg));

    int size;
    while (read(file_fd, buffer, sizeof(buffer)) != 0) {
        size = strlen(buffer) + 1;
        if (write(server_fd, &size, sizeof(size)) < 0) {
            perror("Write");
            exit(1);
        }

        printf("Sent %i bytes\n", write_all(server_fd, buffer, strlen(buffer) + 1));
    }

    fclose(file);
    close(file_fd);
}

int main() {
    struct sockaddr_in addr = {
            .sin_family = AF_INET,
            .sin_port = htons(PORT),
            .sin_addr.s_addr = inet_addr(HOST)
    };
    char               buf[BUF_SIZE];

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

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

    char      *line = NULL;
    size_t    cap   = 0;
    __ssize_t length;

    if (read(cfd, buf, BUF_SIZE) < 0)
        die("Could not receive message");

    printf("%s\n", buf);
    memset(buf, 0, BUF_SIZE);

    while (1) {
        printf("$> ");
        //printf("[C] awaiting command\n");
        if ((length = getline(&line, &cap, stdin)) < 0) {
            fprintf(stderr, "Failed to read from STDIN");
            fflush(stderr);
            exit(-1);
        }

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

        char cmd[4];
        char path[BUF_SIZE - 3];
        memcpy(cmd, line, 3);
        cmd[3] = 0;
        memcpy(path, line + 4, BUF_SIZE - 4);
        path[strlen(path) - 1] = 0;
        printf("cmd: \"%s\"\n", cmd);
        printf("path: \"%s\"\n", path);
        if (strcmp(cmd, "put") == 0) {
            if (strlen(path) == 0) {
                fprintf(stderr, "path missing\n");
            } else {
                printf("got put with %s\n", path);
                send_file(cfd, path);
            }
            free(line);
            line = NULL;
            continue;
        }

        strncpy(buf, line, strlen(line) + 1);

        //printf("[C] pre send: \"%s\"\n", buf);

        if (write(cfd, buf, strlen(line)) < 0)
            die("Could not send message");

        if (strcmp(line, "exit\n") == 0) {
            free(line);
            line = NULL;
            break;
        }

        if (read(cfd, buf, BUF_SIZE) < 0)
            die("Could not receive message");

        printf("%s", buf);
        memset(buf, 0, BUF_SIZE);
        free(line);
        line = NULL;
    }

    close(cfd);
    return 0;
}