aboutsummaryrefslogtreecommitdiff
path: root/source/archived/v1/common.h
blob: c7bb0ddad457b6914c82aceb310e70d48d28997a (plain)
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
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#define PORT 9983
// max buffer size sent over network
// TODO: choose a better size
#define BUF_MAX 256
// max size for a message sent
#define MESSAGE_MAX 256
// max length of author field
#define MESSAGE_AUTHOR_LEN 13
// max length of timestamp field
#define MESSAGE_TIMESTAMP_LEN 9
// current user's name
#define USERNAME "Jef Koek"

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

// To serialize the text that could be arbitrary length the lenght is encoded after the author
// string and before the text.
struct Message {
    u8 author[MESSAGE_AUTHOR_LEN];
    u8 timestamp[MESSAGE_TIMESTAMP_LEN]; // HH:MM:SS
    u16 text_len;                             // length of the text including null terminator
    char *text;
} typedef Message;

// printf without buffering using write syscall, works when using sockets
void writef(char *format, ...);

u16 str_len(char *str);

// save the message msg to file in binary format, returns zero on success, returns 1 if the msg.text
// was empty which should not be allowed.
u8 message_fsave(Message *msg, FILE *f);
// load the message msg from file f, returns zero on success, returns 1 if the msg.text
// was empty which should not be allowed.
u8 message_fload(Message *msg, FILE *f);

// Encode msg and send it to fd
// return -1 if send() returns -1. Otherwise returns number of bytes sent.
// NOTE: this function should not alter the content stored in msg.
u32 message_send(Message *msg, u32 fd);
// Decode data from fd and populate msg with it
// if recv() returns 0 or -1 it will return early and return 0 or -1 accordingly.
// Otherwise returns the number of bytes received
u32 message_receive(Message *msg, u32 fd);

void writef(char *format, ...)
{
    char buf[255 + 1];
    va_list args;
    va_start(args, format);

    vsnprintf(buf, sizeof(buf), format, args);
    va_end(args);

    int n = 0;
    while (*(buf + n) != 0)
        n++;
    write(0, buf, n);
}

// Returns the length of the string plus the null terminator
u16 str_len(char *str)
{
    if (*str == 0)
        return 0;

    u16 i = 0;
    while (str[i])
        i++;

    return i + 1;
}

void str_cpy(char *to, char *from)
{
    while ((*to++ = *from++))
        ;
}

// Save msg to file f
// Returns 0 on success, returns 1 if msg->text is NULL, returns 2 if mfg->len is 0
u8 message_fsave(Message *msg, FILE *f)
{
    if (msg->text == NULL) {
        return 1;
    } else if (msg->text_len == 0)
        return 2;

    fwrite(&msg->timestamp, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN, 1, f);
    fwrite(&msg->author, sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
    fwrite(&msg->text_len, sizeof(msg->text_len), 1, f);
    fwrite(&msg->text, msg->text_len, 1, f);

    return 0;
}

u8 message_fload(Message *msg, FILE *f)
{
    fread(msg, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN + sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
    u16 len;
    fread(&len, sizeof(len), 1, f);
    if (len == 0) {
        // TODO: Error: empty message should not be allowed
        // empty message
        msg->text = NULL;
        return 1;
    }
    char txt[len];
    fgets(txt, len, f);
    memcpy(msg->text, txt, len);

    return 0;
}

u32 message_send(Message *msg, u32 serverfd)
{
    // for protocol see README.md
    u32 buf_len = sizeof(buf_len) + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + msg->text_len;
    char buf[buf_len];
    u32 offset;

    memcpy(buf, &buf_len, sizeof(buf_len));
    offset = sizeof(buf_len);
    memcpy(buf + offset, msg->author, MESSAGE_AUTHOR_LEN);
    offset += MESSAGE_AUTHOR_LEN;
    memcpy(buf + offset, msg->timestamp, MESSAGE_TIMESTAMP_LEN);
    offset += MESSAGE_TIMESTAMP_LEN;
    memcpy(buf + offset, msg->text, msg->text_len);

    u32 n = send(serverfd, &buf, buf_len, 0);
    if (n == -1)
        return n;

    return n;
}

u32 message_receive(Message *msg, u32 clientfd)
{
    // for protocol see README.md
    // must all be of the s
    u32 nrecv = 0, buf_len = 0;
    // limit on what can be received with recv()
    u32 buf_size = 20;
    // temporary buffer to receive message data over a stream
    char recv_buf[BUF_MAX] = {0};

    nrecv = recv(clientfd, recv_buf, buf_size, 0);
    if (nrecv == 0 || nrecv == -1)
        return nrecv;

    memcpy(&buf_len, recv_buf, sizeof(buf_len));

    u32 i = 0;
    while (nrecv < buf_len) {
        // advance the copying by the amounts of bytes received each time
        i = recv(clientfd, recv_buf + nrecv, buf_size, 0);
        if (i == 0 || i == -1)
            return nrecv;
        nrecv += i;
    }

    memcpy(msg, recv_buf + sizeof(buf_len), MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN);
    msg->text = recv_buf + sizeof(buf_len) + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN;
    msg->text_len = buf_len - sizeof(buf_len) - MESSAGE_AUTHOR_LEN - MESSAGE_TIMESTAMP_LEN;

    return nrecv;
}