-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
48 lines (39 loc) · 1.13 KB
/
test.c
File metadata and controls
48 lines (39 loc) · 1.13 KB
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
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
const int version = htonl(3);
const char key[] = "dbname";
const char value[] = "postgres";
const int packetsize = htonl(4 + 4 + 1 + sizeof(key) + sizeof(value));
// char buf[1024] = "";
// bzero(buf, 1024);
// memcpy(buf, &packetsize, sizeof(int));
// memcpy(buf + 4, &version, sizeof(int));
// buf[8] = '\0';
// memcpy(buf + 9, &key, sizeof(key));
// memcpy(buf + 9 + sizeof(key), &value, sizeof(value));
// buf[packetsize] = '\0';
char buf[1024] = "";
bzero(buf, 1024);
memcpy(buf, &(packetsize), sizeof(packetsize));
int offset = sizeof(packetsize);
memcpy(buf + offset, &(version), sizeof(version));
offset += sizeof(version);
buf[8] = '\0';
++ offset;
memcpy(buf + offset, &key, sizeof(key));
offset += sizeof(key);
memcpy(buf + offset, &value, sizeof(value));
offset += sizeof(value);
// printf("len: %d\n", packetsize);
return 0;
}