1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "param_message.h"
17
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/un.h>
23 #include <unistd.h>
24
ConntectServer(int fd,const char * servername)25 int ConntectServer(int fd, const char *servername)
26 {
27 PARAM_CHECK(fd >= 0, return -1, "Invalid fd %d", fd);
28 int opt = 1;
29 int ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
30 PARAM_CHECK(servername != NULL, return -1, "Invalid servername");
31 struct sockaddr_un addr;
32 /* fill socket address structure with server's address */
33 ret = memset_s(&addr, sizeof(addr), 0, sizeof(addr));
34 PARAM_CHECK(ret == 0, return -1, "Failed to memset server address");
35 addr.sun_family = AF_UNIX;
36 ret = sprintf_s(addr.sun_path, sizeof(addr.sun_path) - 1, "%s", servername);
37 PARAM_CHECK(ret > EOK, return -1, "Failed to sprintf_s server address");
38 int len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path);
39 ret = connect(fd, (struct sockaddr *)&addr, len);
40 PARAM_CHECK(ret != -1, return -1, "Failed to connect server %s %d", servername, errno);
41 return 0;
42 }
43
FillParamMsgContent(const ParamMessage * request,uint32_t * start,int type,const char * value,uint32_t length)44 int FillParamMsgContent(const ParamMessage *request, uint32_t *start, int type, const char *value, uint32_t length)
45 {
46 PARAM_CHECK(request != NULL && start != NULL, return -1, "Invalid param");
47 PARAM_CHECK(value != NULL && length > 0, return -1, "Invalid value");
48 uint32_t bufferSize = request->msgSize - sizeof(ParamMessage);
49 uint32_t offset = *start;
50 PARAM_CHECK((offset + sizeof(ParamMsgContent) + length) <= bufferSize,
51 return -1, "Invalid msgSize %u offset %u %d", request->msgSize, offset, type);
52 ParamMsgContent *content = (ParamMsgContent *)(request->data + offset);
53 content->type = type;
54 content->contentSize = length + 1;
55 int ret = memcpy_s(content->content, content->contentSize - 1, value, length);
56 PARAM_CHECK(ret == EOK, return -1, "Failed to copy value for %d", type);
57 content->content[length] = '\0';
58 offset += sizeof(ParamMsgContent) + PARAM_ALIGN(content->contentSize);
59 *start = offset;
60 return 0;
61 }
62
CreateParamMessage(int type,const char * name,uint32_t msgSize)63 ParamMessage *CreateParamMessage(int type, const char *name, uint32_t msgSize)
64 {
65 PARAM_CHECK(name != NULL, return NULL, "Invalid name");
66 if (msgSize < sizeof(ParamMessage)) {
67 msgSize = sizeof(ParamMessage);
68 }
69 ParamMessage *msg = (ParamMessage *)calloc(1, msgSize);
70 PARAM_CHECK(msg != NULL, return NULL, "Failed to malloc message");
71 msg->type = type;
72 msg->id.msgId = 0;
73 msg->msgSize = msgSize;
74 int ret = strcpy_s(msg->key, sizeof(msg->key) - 1, name);
75 PARAM_CHECK(ret == EOK, free(msg);
76 return NULL, "Failed to fill name");
77 return msg;
78 }
79
GetNextContent(const ParamMessage * reqest,uint32_t * offset)80 ParamMsgContent *GetNextContent(const ParamMessage *reqest, uint32_t *offset)
81 {
82 PARAM_CHECK(reqest != NULL, return NULL, "Invalid reqest");
83 PARAM_CHECK(offset != NULL, return NULL, "Invalid offset");
84 ParamMessage *msg = (ParamMessage *)reqest;
85 if ((msg == NULL) || ((*offset + sizeof(ParamMessage) + sizeof(ParamMsgContent)) >= msg->msgSize)) {
86 return NULL;
87 }
88 ParamMsgContent *content = (ParamMsgContent *)(msg->data + *offset);
89 *offset += sizeof(ParamMsgContent) + PARAM_ALIGN(content->contentSize);
90 return content;
91 }
92