-
Notifications
You must be signed in to change notification settings - Fork 28
/
agent_comm_socket.c
296 lines (239 loc) · 7.34 KB
/
agent_comm_socket.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Copyright (c) 2015 Open Networking Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
/*
#define _GNU_SOURCE
#include <stdio.h>
*/
#include <libnetconf.h>
#include "comm.h"
char *recv_msg(int socket, size_t len, struct nc_err **err);
int sock = -1;
comm_t *
comm_init(int __attribute__ ((unused)) crashed)
{
struct sockaddr_un server;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
nc_verb_error("Unable to create communication socket (%s).",
strerror(errno));
return NULL;
}
server.sun_family = AF_UNIX;
strncpy(server.sun_path, OFC_SOCK_PATH, (sizeof server.sun_path) - 1);
if (connect(sock, (struct sockaddr *) &server, sizeof server) == -1) {
nc_verb_error("Unable to connect to the ofc-server (%s).",
strerror(errno));
/* cleanup */
close(sock);
sock = -1;
return NULL;
}
nc_verb_verbose("Agent connected with server via UNIX socket");
return &sock;
}
char **
comm_get_srv_cpblts(comm_t *c)
{
msgtype_t op = COMM_SOCK_GET_CPBLTS;
msgtype_t result = 0;
int count, i;
unsigned int len;
char **cpblts = NULL;
if (*c == -1) {
nc_verb_error("Invalid communication channel (%s)", __func__);
return NULL;
}
/* operation ID */
send(*c, &op, sizeof op, OFC_SOCK_SENDFLAGS);
/* done, now get the result */
recv(*c, &result, sizeof (result), OFC_SOCK_SENDFLAGS);
if (op != result) {
nc_verb_error("Communication failed, sending %d, but received %d.", op,
result);
return NULL;
}
/* get the data */
recv(*c, &count, sizeof count, OFC_SOCK_SENDFLAGS);
cpblts = calloc(count + 1, sizeof *cpblts);
cpblts[count] = NULL;
for (i = 0; i < count; i++) {
recv(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
if ((cpblts[i] = recv_msg(*c, len, NULL)) == NULL) {
/* something went wrong */
for (i--; i >= 0; i--) {
free(cpblts[i]);
}
free(cpblts);
return NULL;
}
}
return cpblts;
}
int
comm_session_info_send(comm_t *c, const char *username, const char *sid,
struct nc_cpblts *cpblts)
{
msgtype_t op = COMM_SOCK_SET_SESSION;
msgtype_t result = 0;
const char *cpblt;
unsigned int len;
uint16_t pid;
int ccount;
if (*c == -1) {
nc_verb_error("Invalid communication channel (%s)", __func__);
return EXIT_FAILURE;
}
/* operation ID */
send(*c, &op, sizeof op, OFC_SOCK_SENDFLAGS);
/* send session attributes */
len = strlen(sid) + 1;
send(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
send(*c, sid, len, OFC_SOCK_SENDFLAGS);
pid = (uint16_t) getpid();
send(*c, &pid, sizeof pid, OFC_SOCK_SENDFLAGS);
len = strlen(username) + 1;
send(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
send(*c, username, len, OFC_SOCK_SENDFLAGS);
ccount = nc_cpblts_count(cpblts);
send(*c, &ccount, sizeof ccount, OFC_SOCK_SENDFLAGS);
while ((cpblt = nc_cpblts_iter_next(cpblts)) != NULL) {
len = strlen(cpblt) + 1;
send(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
send(*c, cpblt, len, OFC_SOCK_SENDFLAGS);
}
/* done, now get the result */
recv(*c, &result, sizeof result, OFC_SOCK_SENDFLAGS);
if (op != result) {
nc_verb_error("Communication failed, sending %d, but received %d.", op,
result);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
nc_reply *
comm_operation(comm_t *c, const nc_rpc *rpc)
{
msgtype_t result = 0, op = COMM_SOCK_GENERICOP;
struct nc_err *err = NULL;
nc_reply *reply;
char *msg_dump;
unsigned int len;
if (*c == -1) {
nc_verb_error("Invalid communication channel (%s)", __func__);
return NULL;
}
/* operation ID */
send(*c, &op, sizeof op, OFC_SOCK_SENDFLAGS);
/* rpc */
msg_dump = nc_rpc_dump(rpc);
len = strlen(msg_dump) + 1;
send(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
send(*c, msg_dump, len, OFC_SOCK_SENDFLAGS);
free(msg_dump);
/* done, now get the result */
recv(*c, &result, sizeof result, OFC_SOCK_SENDFLAGS);
if (op != result) {
nc_verb_error("Communication failed, sending %d, but received %d.", op,
result);
err = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(err, NC_ERR_PARAM_MSG,
"agent-server communication failed.");
return nc_reply_error(err);
}
/* get the reply message */
recv(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
msg_dump = recv_msg(*c, len, &err);
if (err != NULL) {
return nc_reply_error(err);
}
reply = nc_reply_build(msg_dump);
/* cleanup */
free(msg_dump);
return reply;
}
static int
comm_close(comm_t *c)
{
msgtype_t result = 0, op = COMM_SOCK_CLOSE_SESSION;
if (*c == -1) {
nc_verb_error("Invalid communication channel (%s)", __func__);
return EXIT_FAILURE;
}
/* operation ID */
send(*c, &op, sizeof op, OFC_SOCK_SENDFLAGS);
/* done, now get the result */
recv(*c, &result, sizeof result, OFC_SOCK_SENDFLAGS);
if (op != result) {
nc_verb_error("Communication failed, sending %d, but received %d.", op,
result);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
nc_reply *
comm_kill_session(comm_t *c, const char *sid)
{
struct nc_err *err = NULL;
msgtype_t result = 0, op = COMM_SOCK_KILL_SESSION;
unsigned int len;
char *errmsg = NULL;
if (*c == -1) {
nc_verb_error("Invalid communication channel (%s)", __func__);
return NULL;
}
/* operation ID */
send(*c, &op, sizeof op, OFC_SOCK_SENDFLAGS);
/* session to kill */
len = strlen(sid) + 1;
send(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
send(*c, sid, len, OFC_SOCK_SENDFLAGS);
/* done, now get the result */
recv(*c, &result, sizeof result, OFC_SOCK_SENDFLAGS);
if (result == COMM_SOCK_RESULT_ERROR) {
recv(*c, &len, sizeof len, OFC_SOCK_SENDFLAGS);
errmsg = recv_msg(*c, len, NULL);
goto fillerr;
} else if (op != result) {
nc_verb_error("Communication failed, sending %d, but received %d.", op,
result);
goto fillerr;
}
return nc_reply_ok();
fillerr:
err = nc_err_new(NC_ERR_OP_FAILED);
if (errmsg) {
nc_err_set(err, NC_ERR_PARAM_MSG, errmsg);
}
return nc_reply_error(err);
}
void
comm_destroy(comm_t *c)
{
if (c == NULL || *c == -1) {
return;
}
comm_close(c);
/* close listen socket */
close(*c);
*c = -1;
}