Skip to content

Commit ed7f90d

Browse files
committed
Copy packets rather than string
1 parent 5d813be commit ed7f90d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/buffer.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ int buffer_write_packet(buffer_t *buffer, packet_t *packet)
7070

7171
new_block = (packet_block_t *) malloc(sizeof(packet_block_t));
7272
if (new_block == NULL) return -1;
73-
74-
new_block -> packet = *packet;
73+
memcpy(&(new_block -> packet), packet, sizeof(packet_t));
7574

7675
linked_list_add((*buffer), new_block);
7776
return 1;

src/packet.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int packet_new(packet_t *packet, char *buffer, int len)
3333
if (len > sizeof(packet -> data)) return -1;
3434

3535
memset(packet -> data, 0, sizeof(PACKET_DATA_SIZE));
36-
strncpy(packet -> data, buffer, len);
36+
memcpy(packet -> data, buffer, len);
3737
packet_serialize(packet);
3838

3939
return len;
@@ -43,10 +43,10 @@ int packet_new_handshake(packet_t *packet)
4343
{
4444
char buffer[8 * sizeof(uint32_t)];
4545
uint32_t *p = NULL;
46-
uint32_t flight_flag_size = 0,
47-
id = 0,
46+
uint32_t flight_flag_size = 10,
47+
id = 10,
4848
req_type = 0,
49-
cookie = 0;
49+
cookie = 10;
5050

5151
packet_clear_header (*packet);
5252
packet_set_ctrl (*packet);
@@ -57,13 +57,19 @@ int packet_new_handshake(packet_t *packet)
5757
p = (uint32_t *) buffer;
5858
*p++ = UDT_VERSION;
5959
*p++ = connection.type;
60-
*p++ = 123123; /* TODO: Generate random number */
60+
*p++ = 0x123123; /* TODO: Generate random number */
6161
*p++ = MAX_PACKET_SIZE;
6262
*p++ = flight_flag_size;
6363
*p++ = req_type;
6464
*p++ = id;
6565
*p++ = cookie;
6666

67+
p = (uint32_t *) (packet -> data);
68+
for (int i = 0; i < 8; ++i) {
69+
*p = htonl(*p);
70+
p++;
71+
}
72+
6773
return packet_new(packet, buffer, sizeof(buffer));
6874
}
6975

0 commit comments

Comments
 (0)