-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmap-connection.c
197 lines (158 loc) · 3.26 KB
/
dmap-connection.c
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
#include "dmap-connection.h"
#include "ksocket.h"
#include "dmap-trace-helpers.h"
#include "dmap-malloc.h"
#include <linux/gfp.h>
int dmap_con_init(struct dmap_connection *con)
{
memset(con, 0, sizeof(*con));
mutex_init(&con->mutex);
return 0;
}
void dmap_con_deinit(struct dmap_connection *con)
{
dmap_con_close(con);
}
int dmap_con_connect(struct dmap_connection *con, char *host, u16 port)
{
int r;
struct socket *sock;
mutex_lock(&con->mutex);
if (con->sock) {
r = -EEXIST;
goto unlock;
}
r = ksock_connect_host(&sock, host, port);
if (r) {
TRACE_ERR(r, "connect failed");
goto unlock;
}
r = ksock_set_nodelay(sock, true);
if (r) {
TRACE_ERR(r, "set no delay failed");
goto release_sock;
}
snprintf(con->host, ARRAY_SIZE(con->host), "%s", host);
con->port = port;
con->sock = sock;
r = 0;
goto unlock;
release_sock:
ksock_release(sock);
unlock:
mutex_unlock(&con->mutex);
return r;
}
int dmap_con_set_socket(struct dmap_connection *con, struct socket *sock)
{
int r;
mutex_lock(&con->mutex);
if (con->sock) {
r = -EEXIST;
goto unlock;
}
con->sock = sock;
r = 0;
unlock:
mutex_unlock(&con->mutex);
return r;
}
int dmap_con_close(struct dmap_connection *con)
{
mutex_lock(&con->mutex);
if (con->sock) {
ksock_release(con->sock);
con->sock = NULL;
con->host[0] = '\0';
con->port = 0;
}
mutex_unlock(&con->mutex);
return 0;
}
int dmap_con_send(struct dmap_connection *con, u32 type, u32 len,
u32 result, struct dmap_packet *packet)
{
int r;
mutex_lock(&con->mutex);
if (!con->sock) {
r = -EBADF;
goto unlock;
}
if (len > sizeof(packet->body)) {
r = -EINVAL;
goto unlock;
}
packet->header.magic = cpu_to_le32(DMAP_PACKET_MAGIC);
packet->header.len = cpu_to_le32(len);
packet->header.type = cpu_to_le32(type);
packet->header.result = cpu_to_le32(result);
r = ksock_send(con->sock, packet, sizeof(packet->header) + len);
if (r < 0)
goto unlock;
if (r != (sizeof(packet->header) + len)) {
r = -EIO;
goto unlock;
}
r = 0;
unlock:
mutex_unlock(&con->mutex);
return r;
}
int dmap_con_recv(struct dmap_connection *con, struct dmap_packet *packet,
u32 *type, u32 *len, u32 *result)
{
int r;
u32 magic, ltype, llen, lresult;
mutex_lock(&con->mutex);
if (!con->sock) {
r = -EBADF;
goto unlock;
}
r = ksock_recv(con->sock, &packet->header, sizeof(packet->header));
if (r < 0)
goto unlock;
if (r != sizeof(packet->header)) {
r = -EIO;
goto unlock;
}
magic = le32_to_cpu(packet->header.magic);
ltype = le32_to_cpu(packet->header.type);
llen = le32_to_cpu(packet->header.len);
lresult = le32_to_cpu(packet->header.result);
if (magic != DMAP_PACKET_MAGIC) {
r = -EBADF;
goto unlock;
}
if (llen > sizeof(packet->body)) {
r = -EBADF;
goto unlock;
}
r = ksock_recv(con->sock, packet->body, llen);
if (r < 0)
goto unlock;
if (r != llen) {
r = -EIO;
goto unlock;
}
*type = ltype;
*len = llen;
*result = lresult;
r = 0;
unlock:
mutex_unlock(&con->mutex);
return r;
}
int dmap_con_recv_check(struct dmap_connection *con, struct dmap_packet *packet,
u32 type, u32 len)
{
u32 ltype, llen, lresult;
int r;
r = dmap_con_recv(con, packet, <ype, &llen, &lresult);
if (r)
return r;
if (llen != len)
return -EBADF;
if (ltype != type)
return -EBADF;
return lresult;
}