aboutsummaryrefslogtreecommitdiff
path: root/archived/v1/send.c
blob: 27e9793571bc93c9e204945232b758bab093bc4d (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
// minimal client implementation
#include "common.h"
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

u32 serverfd;

// NOTE: Errno could be unset and contain an error for a previous command
void debug_panic(const char *msg)
{
    writef("%s errno: %d\n", msg, errno);
    raise(SIGINT);
}

// get current time in timestamp string
void timestamp(u8 timestamp[MESSAGE_TIMESTAMP_LEN])
{
    time_t now;
    struct tm *ltime;
    time(&now);
    ltime = localtime(&now);
    strftime((char*)timestamp, MESSAGE_TIMESTAMP_LEN, "%H:%M:%S", ltime);
}

int main(int argc, char **argv)
{

    if (argc < 2) {
        printf("usage: send <msg>\n");
        return 1;
    }

    Message input = {
        .author    = "Friendship",
    };
    input.text = argv[1];
    input.text_len  = str_len(input.text);

    serverfd = socket(AF_INET, SOCK_STREAM, 0);
    if (serverfd == -1)
        debug_panic("Error while getting socket.");

    const struct sockaddr_in address = {
        AF_INET,
        htons(PORT),
        {0},
    };

    if (connect(serverfd, (struct sockaddr *)&address, sizeof(address)))
        debug_panic("Error while connecting.");

    printf("input.len: %d\n", input.text_len);
    timestamp(input.timestamp);

    message_send(&input, serverfd);

    return 0;
}