summaryrefslogtreecommitdiffstats
path: root/parser.c
blob: b21de66f6e6b3f5822f737413a992920789393f3 (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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
//  parser.c
//  mcc@exe
//
//  Created by Finn Behrens on 19.02.21.
//

#include <stdlib.h>
#include <string.h>
#include <event2/bufferevent.h>
#include <json.h>

#include "parser.h"
#include "world.h"

int mc_send_package(client_t *client, mc_package_header_t *header)
{
    int err;
    uint8_t *buffer;
 
    err = mc_package_serialize(header, &buffer);
    if (err < 0) {
        return err;
    }
    
    if (bufferevent_write(client->buf_ev, buffer, err) < 0) {
        return -EBADF;
    }
    
    free(buffer);
    
    return err;
}

json_object *create_status_json(client_t *client)
{
    struct json_object *root, *tmp_root, *tmp, *sample, *sample_one;

    root = json_object_new_object();
    
    // version
    tmp_root = json_object_new_object();
    tmp = json_object_new_string("20w07a");
    json_object_object_add(tmp_root, "name", tmp);
    tmp = json_object_new_int(PROTOCOL_VERSION);
    json_object_object_add(tmp_root, "protocol", tmp);
    json_object_object_add(root, "version", tmp_root);
    
    // players
    tmp_root = json_object_new_object();
    tmp = json_object_new_int(world->max_players);
    json_object_object_add(tmp_root, "max", tmp);
    tmp = json_object_new_int(world->curr_players);
    json_object_object_add(tmp_root, "online", tmp);
    
    // TODO: get from world
    sample = json_object_new_array();
    sample_one = json_object_new_object();
    tmp = json_object_new_string("thinkofdeath");
    json_object_object_add(sample_one, "name", tmp);
    tmp = json_object_new_string("4566e69f-c907-48ee-8d71-d7ba5aa00d20");
    json_object_object_add(sample_one, "id", tmp);
    json_object_array_add(sample, sample_one);
    
    json_object_object_add(tmp_root, "sample", sample);
    json_object_object_add(root, "players", tmp_root);
    
    tmp_root = json_object_new_object();
    tmp = json_object_new_string("You C a C server"); // TODO: get from config
    json_object_object_add(tmp_root, "text", tmp);
    json_object_object_add(root, "description", tmp_root);
    
    // TODO: favicon data
    
    return root;
}

int mc_server_handshake(client_t *client, mc_package_header_t *header)
{
    int counter = 0;
    int proto_version, next_state, err;
    char *address = NULL;
    uint16_t port = 0;
    
    if (client->state != MC_STATE_HANDSHAKE) {
        printf("warn: got wrong state\n");
        return -EFAULT;
    }

    // FIXME: memleaks
    proto_version = mc_package_get_var_int(header, counter, &err);
    if (err < 0)
        return err;
    counter += err;
    address = mc_package_read_string(header, counter, &err);
    if (err < 0)
        return err;
    counter += err;
    port = mc_package_get_uint16(header, counter);
    counter += sizeof(uint16_t);
    next_state = mc_package_get_var_int(header, counter, &err);
    if (err < 0)
        return err;
    counter += err;
    
    printf("got client for %s:%d with version %d\n", address, port, proto_version);
    
    client->state = next_state;
    
    free(address);
    
    return counter;
}

int mc_server_handshake_status(client_t *client, mc_package_header_t *header)
{
    int counter = 0, err;
    json_object *json_status_o;
    const char *json_status;
    mc_package_header_t *package = mc_package_new();
    
    package = mc_package_new();

    json_status_o = create_status_json(client);
    json_status = json_object_to_json_string(json_status_o);

    mc_package_put_string(package, json_status);

    package->cmd = MC_PACKET_HANDSHAKE;

    if ((err = mc_send_package(client, package)) < 0)
        return err;

    json_object_put(json_status_o);
    mc_package_free(package);

    return counter;
}

int mc_server_status_ping(client_t *client, mc_package_header_t *header)
{
    int counter = 8, err;
    mc_package_header_t *package = mc_package_new();
    if (header == NULL)
        return -ENOMEM;
    
    package->data = calloc(header->length, sizeof(uint8_t));
    if (package->data == NULL)
        return -ENOMEM;
    
    memcpy(package->data, header->data, header->length);
    
    package->cmd = MC_PACKET_PING;
    package->length = 8;
    if ((err = mc_send_package(client, package)) < 0)
        return err;
    
    mc_package_free(package);
    
    return counter;
}

int mc_play_join_game(client_t *client)
{
    mc_package_header_t *header;
    int err = 0;
    header = mc_package_new();
    if (header == NULL)
        return -ENOMEM;
    header->cmd = MC_PACKET_JOIN_GAME;
    
    err = mc_package_put_i32(header, mc_play_user_get_eid(client->user));
    if (err < 0)
        goto out;
    
    // is hardcore
    err = mc_package_put_bool(header, false); // TODO: get from config
    if (err < 0)
        goto out;
    
    err = mc_package_put_u8(header, client->user->gamemode);
    if (err < 0)
        goto out;
    
    // prev gamemode
    err = mc_package_put_i8(header, -1);
    if (err < 0)
        goto out;
    
    // world count
    err = mc_package_put_var_int(header, 1);
    if (err <0)
        goto out;
    
    // world names
    err = mc_package_put_string(header, "minecraft:overworld");
    if (err < 0)
        goto out;
    
    // dimension codec
    
    err = mc_send_package(client, header);
    if (err < 0)
        goto out;
out:
    mc_package_free(header);
    return err;
}

/*int mc_play_send_player_info(client_t *client)
{
    mc_package_header_t *header;
    int err;
    mc_play_user_t *user;
    
    header = mc_package_new();
    if (header == NULL)
        return -ENOMEM;
    
    header->cmd = MC_PACKET_PLAYER_INFO;
    err = mc_package_put_var_int(header, MC_PLAYER_INFO_ACTION_ADD);
    if (err < 0)
        goto out;
    
    // FIXME: get from world
    err = mc_package_put_var_int(header, 1);
    if (err < 0)
        goto out;
    
    user = mc_play_user_new();
    if (user == NULL) {
        err = -ENOMEM;
        goto out;
    }
    
    user->uuid = calloc(16, sizeof(char));
    user->uuid[15] = 0x20;
    if (err < 0)
        goto out_user;
    
    char *name = "Test";
    user->name = malloc(strlen(name)+1);
    memcpy(user->name, name, strlen(name)+1);
    
    err = mc_package_put_user(header, user, 0);
    if (err < 0)
        goto out_user;
    
    err = mc_send_package(client, header);
    
out_user:
    mc_play_user_free(user);
out:
    mc_package_free(header);
    return err;
}*/

/*int mc_play_send_position(client_t *client)
{
    mc_package_header_t *package;
    int err;
    
    package = mc_package_new();
    if (package == NULL)
        return -ENOMEM;
    
    package->cmd = MC_PACKET_PLAYER_POSTITION;
    // x
    err = mc_package_put_double(package, 0.0);
    if (err < 0)
        goto out;
    
    // y
    err = mc_package_put_double(package, 0.0);
    if (err < 0)
        goto out;
    
    // z
    err = mc_package_put_double(package, 0.0);
    if (err < 0)
        goto out;
    
    // yaw
    err = mc_package_put_float(package, 0.0);
    if (err < 0)
        goto out;
    
    // pith
    err = mc_package_put_float(package, 0.0);
    if (err < 0)
        goto out;
    
    err = mc_package_put_u8(package, 0x0);
    if (err < 0)
        goto out;
    
    err = mc_package_put_var_int(package, 1234);
    if (err < 0)
        goto out;
    
    err = mc_package_put_bool(package, false);
    if (err < 0)
        goto out;
    
    err = mc_send_package(client, package);
    if (err < 0)
        goto out;
    
out:
    mc_package_free(package);
    return err;
}*/

int mc_server_login_start(client_t *client, mc_package_header_t *header)
{
    mc_package_header_t *package;
    mc_play_user_t *user;
    int counter = 0, err;
    char *name;
    unsigned int uuid;
    
    name = mc_package_read_string(header, counter, &counter);
    if (counter < 0)
        return counter;
    printf("user %s is logging in\n", name);
    
    
    package = mc_package_new();
    if (package == NULL)
        return -ENOMEM;
    package->cmd = MC_PACKET_LOGIN_SUCCESS;

    
    user = mc_play_user_new();
    client->user = user;
    user->name = name;
    user->uuid = calloc(16, sizeof(uint8_t));
    if (user->uuid == NULL) {
        err = -ENOMEM;
        goto out;
    }
    uuid = rand();
    memcpy(user->uuid, &uuid, sizeof(unsigned int));
    uuid = rand();
    memcpy(user->uuid + sizeof(unsigned int), &uuid, sizeof(unsigned int));
    
    
    // put uuid
    err = mc_package_put_uuid(package, user->uuid);
    if (err < 0)
        return err;
    
    err = mc_package_put_string(package, name);
    if (err < 0)
        return err;
    
    err = mc_send_package(client, package);
    if (err < 0)
        counter = err;
    
    client->state = MC_STATE_PLAY;

    
    /*err = mc_play_send_player_info(client);
    if (err < 0) {
        printf("could not send player info: %d\n", err);
        goto out;
    }
    
    err = mc_play_send_position(client);
    if (err < 0)
        goto out;*/
    
    err = mc_play_join_game(client);
    if (err < 0)
        goto out;
    
out:
    mc_package_free(package);
    return counter;
}

static const struct mc_package_op mc_packet_ops[] = {
    {
        .cmd = MC_PACKET_HANDSHAKE,
        .doit = mc_server_handshake,
        .direction = MC_DIRCETION_SERVER,
        .state = MC_STATE_HANDSHAKE,
    },
    {
        .cmd = MC_PACKET_HANDSHAKE,
        .doit = mc_server_handshake_status,
        .direction = MC_DIRCETION_SERVER,
        .state = MC_STATE_STATUS,
    },
    {
        .cmd = MC_PACKET_PING,
        .doit = mc_server_status_ping,
        .direction = MC_DIRCETION_SERVER,
        .state = MC_STATE_STATUS,
    },
    {
        .cmd = MC_PACKET_HANDSHAKE,
        .doit = mc_server_login_start,
        .direction = MC_DIRCETION_SERVER,
        .state = MC_STATE_LOGIN,
    }
};
static int mc_packet_op_n = sizeof(mc_packet_ops) / sizeof(struct mc_package_op);

int parse_packet(client_t *client, uint8_t *src)
{
    int err, i;
    mc_package_header_t *header;
    struct mc_package_op;
    
    // handle legacy pings
    // TODO: real legacy ping
    if (src[0] == 0xFE && src[1] == 0x01) {
        printf("got a 0xFE ping package\n");
        return -EFAULT;
    }
    
    header = mc_package_new();
    err = mc_package_parse(header, src);
    if (err < 0)
        return err;
    // TODO: something with err?
    
#ifdef DEBUG
    printf("got header:\n");
    mc_package_print(header);
#endif
    
    for (i = 0; i < mc_packet_op_n; i++) {
        if (mc_packet_ops[i].cmd == header->cmd
                && mc_packet_ops[i].direction == MC_DIRCETION_SERVER
                && mc_packet_ops[i].state == client->state) {
            err = mc_packet_ops[i].doit(client, header);
            if (err < 0)
                goto out;
            goto found;
        }
    }
    printf("did not find a doit for %02x state %d\n", header->cmd, client->state);
    
found:
    if (header->length != err)
        printf("length (%d) for cmd %02x (state: %d) not read (%d)\n", header->length, header->cmd, client->state, err);
        
out:
    mc_package_free(header);
    return err;
}