-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon-cli.h
90 lines (77 loc) · 1.65 KB
/
common-cli.h
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
#pragma once
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define DBG_PREFIX "- "
#define ERR_PREFIX "-! "
#include "common.h"
static int writeU32(const int sock, const uint32_t v) {
return writeAll(sock, &v, sizeof(v));
}
static int writeString(const int sock, const char *const v) {
const uint16_t len = strlen(v);
if (writeAll(sock, &len, sizeof(len)) != 0)
return -1;
if (writeAll(sock, v, len) != 0)
return -1;
return 0;
}
static int writeCommand(const int sock, const uint8_t cmd) {
return writeAll(sock, &cmd, sizeof(cmd));
}
static inline X_ToAbstract(connect)
static int connectToServer() {
const char *const sockName = getServSockName();
if (sockName == NULL) {
errno = EACCES;
return -1;
}
const int sock = connectToAbstract(sockName);
if (sock == -1) {
errno = EACCES;
return -1;
}
if (checkClientSocket(sock) != 0) {
close(sock);
errno = EACCES;
return -1;
}
return sock;
}
static int processError(const int sock) {
int err;
if (readAll(sock, &err, sizeof(err)) != 0) {
close(sock);
errno = EACCES;
return -1;
}
close(sock);
errno = err;
return 0;
}
#define Write(P, V) if (write##P(sock, (V)) != 0) {\
close(sock);\
errno = EACCES;\
return -1;\
}
#define WriteR(P, V, R) if (write##P(sock, (V)) != 0) {\
close(sock);\
errno = EACCES;\
return R;\
}
#define ReadBuf(V, L) if (readAll(sock, (V), (L)) != 0) {\
close(sock);\
errno = EACCES;\
return -1;\
}
#define WriteBuf(V, L) if (writeAll(sock, (V), (L)) != 0) {\
close(sock);\
errno = EACCES;\
return -1;\
}