Skip to content

Commit 403a325

Browse files
committed
Add handshake info and logging functions
1 parent b1736d5 commit 403a325

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

include/config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@
66

77
#define UDT_VERSION 4
88

9+
#define MAX_DATA_SIZE 1024
10+
#define MAX_PACKET_SIZE (4 * sizeof(uint32_t) + MAX_DATA_SIZE)
11+
12+
/* comment the following definition to turn off debuf */
13+
#define DEBUG 1
14+
915
#endif /* end of include guard: CONFIG_H_HW87WHOK */

include/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct {
1212
int is_open;
1313
int is_connected;
1414
int is_client;
15+
int type;
1516
} conn_t;
1617

1718
conn_t connection;

include/packet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#define PACKET_H_R7ONXCYA
33

44
#include <inttypes.h>
5-
5+
#include "config.h"
66

77
#define PACKET_HEADER_SIZE 4
8-
#define PACKET_DATA_SIZE 200
8+
#define PACKET_DATA_SIZE MAX_DATA_SIZE
99

1010

1111
#define PACKET_MASK_CTRL 0x80000000

include/util.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define UTIL_H_RSWIA2KL
33

44
#include <pthread.h>
5+
#include "config.h"
56

67
#define linked_list_add(BUFFER, BLOCK) \
78
{ \
@@ -37,4 +38,17 @@ typedef void * (*thread_worker_t) (void *);
3738
tid_t thread_start (thread_worker_t, void *);
3839
void thread_stop (tid_t);
3940

41+
#ifdef DEBUG
42+
43+
#include <stdio.h>
44+
#define console_log_mod(MODIFIER, LOGDATA) printf(MODIFIER, LOGDATA)
45+
#define console_log(LOGDATA) printf("%s\n", LOGDATA)
46+
47+
#else
48+
49+
#define console_log_mod(MODIFIER, LOGDATA)
50+
#define console_log(LOGDATA)
51+
52+
#endif /* end of DEBUG */
53+
4054
#endif /* end of include guard: UTIL_H_RSWIA2KL */

src/api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ socket_t udt_socket(af_type af, sock_type type, int protocol)
2222
errno = 5003; /* bad params */
2323
return -1;
2424
}
25+
connection.type = type;
2526

2627
return socket(af, type, 0);
2728
}

src/packet.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "packet.h"
77
#include "buffer.h"
88
#include "core.h"
9+
#include "util.h"
910

1011
extern conn_t connection;
1112

@@ -42,10 +43,7 @@ int packet_new_handshake(packet_t *packet)
4243
{
4344
char buffer[8 * sizeof(uint32_t)];
4445
uint32_t *p = NULL;
45-
uint32_t type = 0,
46-
isn = 0,
47-
mss = 0,
48-
flight_flag_size = 0,
46+
uint32_t flight_flag_size = 0,
4947
id = 0,
5048
req_type = 0,
5149
cookie = 0;
@@ -58,9 +56,9 @@ int packet_new_handshake(packet_t *packet)
5856

5957
p = (uint32_t *) buffer;
6058
*p++ = UDT_VERSION;
61-
*p++ = type;
62-
*p++ = isn;
63-
*p++ = mss;
59+
*p++ = connection.type;
60+
*p++ = 123123; /* TODO: Generate random number */
61+
*p++ = MAX_PACKET_SIZE;
6462
*p++ = flight_flag_size;
6563
*p++ = req_type;
6664
*p++ = id;
@@ -76,6 +74,7 @@ void packet_parse(packet_t packet)
7674
switch (packet_get_type(packet)) {
7775

7876
case PACKET_TYPE_HANDSHAKE: /* handshake */
77+
console_log("packet: handshake");
7978
if (connection.is_client) {
8079
handshake_terminate();
8180
break;
@@ -86,37 +85,47 @@ void packet_parse(packet_t packet)
8685
break;
8786

8887
case PACKET_TYPE_KEEPALIVE: /* keep-alive */
88+
console_log("packet: keep alive");
8989
break;
9090

9191
case PACKET_TYPE_ACK: /* ack */
92+
console_log("packet: ack");
9293
break;
9394

9495
case PACKET_TYPE_NAK: /* nak */
96+
console_log("packet: nak");
9597
break;
9698

9799
case PACKET_TYPE_CONGDELAY: /* congestion-delay warn */
100+
console_log("packet: congestion delay");
98101
break;
99102

100103
case PACKET_TYPE_SHUTDOWN: /* shutdown */
104+
console_log("packet: shutdown");
101105
connection.is_open = 0;
102106
connection.is_connected = 0;
103107
break;
104108

105109
case PACKET_TYPE_ACK2: /* ack of ack */
110+
console_log("packet: ack of ack");
106111
break;
107112

108113
case PACKET_TYPE_DROPREQ: /* message drop request */
114+
console_log("packet: drop request");
109115
break;
110116

111117
case PACKET_TYPE_ERRSIG: /* error signal */
118+
console_log("packet: error signal");
112119
break;
113120

114121
default: /* unsupported packet type */
115-
recv_buffer_write("Unknown message", 16);
122+
console_log("packet: unknown");
116123

117124
}
118125
} else { /* data packet */
119126

127+
console_log("packet: data");
128+
120129
if (packet.header._head1 & 0x80000000 &&
121130
packet.header._head1 & 0x40000000) /* solo packet */
122131
recv_buffer_write(packet.data, PACKET_DATA_SIZE);

0 commit comments

Comments
 (0)