Files
sun_fan 017f716d4b init : fix param .
Signed-off-by: sun_fan <sun_fan1@hoperun.com>
2021-09-13 19:58:03 +08:00

87 lines
3.6 KiB
C
Executable File

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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 "param_message.h"
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#define LABEL "PARAM_MSG"
int ConntectServer(int fd, const char *servername)
{
PARAM_CHECK(fd >= 0, return -1, "Invalid fd %d", fd);
PARAM_CHECK(servername != NULL, return -1, "Invalid servername");
struct sockaddr_un addr;
/* fill socket address structure with server's address */
int ret = memset_s(&addr, sizeof(addr), 0, sizeof(addr));
PARAM_CHECK(ret == 0, return -1, "Failed to memset server address");
addr.sun_family = AF_UNIX;
ret = sprintf_s(addr.sun_path, sizeof(addr.sun_path) - 1, "%s", servername);
PARAM_CHECK(ret > EOK, return -1, "Failed to sprintf_s server address");
int len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path);
ret = connect(fd, (struct sockaddr *)&addr, len);
PARAM_CHECK(ret != -1, return -1, "Failed to connect server %s %s", servername, strerror(errno));
return 0;
}
int FillParamMsgContent(ParamMessage *request, uint32_t *start, int type, const char *value, uint32_t length)
{
PARAM_CHECK(request != NULL && start != NULL, return -1, "Invalid param");
PARAM_CHECK(value != NULL && length > 0, return -1, "Invalid value");
uint32_t bufferSize = request->msgSize - sizeof(ParamMessage);
uint32_t offset = *start;
PARAM_CHECK((offset + sizeof(ParamMsgContent) + length) <= bufferSize,
return -1, "Invalid msgSize %u offset %u %d", request->msgSize, offset, type);
ParamMsgContent *content = (ParamMsgContent *)(request->data + offset);
content->type = type;
content->contentSize = length + 1;
int ret = memcpy_s(content->content, content->contentSize - 1, value, length);
PARAM_CHECK(ret == EOK, return -1, "Failed to copy value for %d", type);
content->content[length] = '\0';
offset += sizeof(ParamMsgContent) + PARAM_ALIGN(content->contentSize);
*start = offset;
return 0;
}
ParamMessage *CreateParamMessage(int type, const char *name, uint32_t msgSize)
{
if (msgSize < sizeof(ParamMessage)) {
msgSize = sizeof(ParamMessage);
}
ParamMessage *msg = (ParamMessage *)malloc(msgSize);
PARAM_CHECK(msg != NULL, return NULL, "Failed to malloc message");
msg->type = type;
msg->id.msgId = 0;
msg->msgSize = msgSize;
int ret = strcpy_s(msg->key, sizeof(msg->key) - 1, name);
PARAM_CHECK(ret == EOK, free(msg);
return NULL, "Failed to fill name");
return msg;
}
ParamMsgContent *GetNextContent(const ParamMessage *reqest, uint32_t *offset)
{
ParamMessage *msg = (ParamMessage *)reqest;
if ((msg == NULL) || ((*offset + sizeof(ParamMessage) + sizeof(ParamMsgContent)) >= msg->msgSize)) {
return NULL;
}
ParamMsgContent *content = (ParamMsgContent *)(msg->data + *offset);
*offset += sizeof(ParamMsgContent) + PARAM_ALIGN(content->contentSize);
return content;
}