• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "param_message.h"
16 
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 
20 #include "param_utils.h"
21 #include "securec.h"
22 
ConnectServer(int fd,const char * servername)23 int ConnectServer(int fd, const char *servername)
24 {
25     PARAM_CHECK(fd >= 0, return -1, "Invalid fd %d", fd);
26     PARAM_CHECK(servername != NULL, return -1, "Invalid servername");
27     int opt = 1;
28     int ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
29     PARAM_CHECK(ret == 0, return -1, "Failed to set socket option");
30     struct sockaddr_un addr;
31     /* fill socket address structure with server's address */
32     ret = memset_s(&addr, sizeof(addr), 0, sizeof(addr));
33     PARAM_CHECK(ret == 0, return -1, "Failed to memset server address");
34     addr.sun_family = AF_UNIX;
35     ret = sprintf_s(addr.sun_path, sizeof(addr.sun_path) - 1, "%s", servername);
36     PARAM_CHECK(ret > EOK, return -1, "Failed to sprintf_s server address");
37     socklen_t len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path);
38     ret = connect(fd, (struct sockaddr *)&addr, len);
39     PARAM_CHECK(ret != -1, return -1, "Failed to connect server %s %d", servername, errno);
40     PARAM_LOGV("ConnectServer %s success", servername);
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, 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     if (length > 0) {
56         int ret = memcpy_s(content->content, content->contentSize - 1, value, length);
57         PARAM_CHECK(ret == EOK, return -1, "Failed to copy value for %d", type);
58     }
59     content->content[length] = '\0';
60     offset += sizeof(ParamMsgContent) + PARAM_ALIGN(content->contentSize);
61     *start = offset;
62     return 0;
63 }
64 
CreateParamMessage(int type,const char * name,uint32_t msgSize)65 ParamMessage *CreateParamMessage(int type, const char *name, uint32_t msgSize)
66 {
67     PARAM_CHECK(name != NULL, return NULL, "Invalid name");
68     PARAM_CHECK(msgSize < PARAM_BUFFER_MAX, return NULL, "Invalid msg size %u", msgSize);
69     uint32_t size = msgSize;
70     if (msgSize < sizeof(ParamMessage)) {
71         size = sizeof(ParamMessage);
72     }
73     ParamMessage *msg = (ParamMessage *)calloc(1, size);
74     PARAM_CHECK(msg != NULL, return NULL, "Failed to malloc message");
75     msg->type = type;
76     msg->id.msgId = 0;
77     msg->msgSize = size;
78     int ret = strcpy_s(msg->key, sizeof(msg->key) - 1, name);
79     PARAM_CHECK(ret == EOK, free(msg);
80         return NULL, "Failed to fill name");
81     return msg;
82 }
83 
GetNextContent(const ParamMessage * request,uint32_t * offset)84 ParamMsgContent *GetNextContent(const ParamMessage *request, uint32_t *offset)
85 {
86     PARAM_CHECK(request != NULL, return NULL, "Invalid request");
87     PARAM_CHECK(offset != NULL, return NULL, "Invalid offset");
88     ParamMessage *msg = (ParamMessage *)request;
89     if ((*offset + sizeof(ParamMessage) + sizeof(ParamMsgContent)) >= msg->msgSize) {
90         return NULL;
91     }
92     ParamMsgContent *content = (ParamMsgContent *)(msg->data + *offset);
93     *offset += sizeof(ParamMsgContent) + PARAM_ALIGN(content->contentSize);
94     return content;
95 }
96