106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include "unix_socket_client.h"
1706f6ba60Sopenharmony_ci#include <cstdint>
1806f6ba60Sopenharmony_ci#include <sys/socket.h>
1906f6ba60Sopenharmony_ci#include <unistd.h>
2006f6ba60Sopenharmony_ci#include <linux/un.h>
2106f6ba60Sopenharmony_ci#include "logging.h"
2206f6ba60Sopenharmony_ci#include "securec.h"
2306f6ba60Sopenharmony_ci#include "service_base.h"
2406f6ba60Sopenharmony_ci
2506f6ba60Sopenharmony_ciUnixSocketClient::UnixSocketClient()
2606f6ba60Sopenharmony_ci{
2706f6ba60Sopenharmony_ci    serviceBase_ = nullptr;
2806f6ba60Sopenharmony_ci    socketHandle_ = -1;
2906f6ba60Sopenharmony_ci}
3006f6ba60Sopenharmony_ci
3106f6ba60Sopenharmony_ciUnixSocketClient::~UnixSocketClient() {}
3206f6ba60Sopenharmony_ci
3306f6ba60Sopenharmony_cibool UnixSocketClient::Connect(const std::string addrname, ServiceBase& serviceBase, void (*callback)())
3406f6ba60Sopenharmony_ci{
3506f6ba60Sopenharmony_ci    PROFILER_LOG_ERROR(LOG_CORE, "UnixSocketClient connect");
3606f6ba60Sopenharmony_ci    CHECK_TRUE(socketHandle_ == -1, false, "socketHandle_ != -1 Already Connected");
3706f6ba60Sopenharmony_ci
3806f6ba60Sopenharmony_ci    int sock = socket(AF_UNIX, SOCK_STREAM, 0);
3906f6ba60Sopenharmony_ci    CHECK_TRUE(sock != -1, false, "Unix Socket Create FAIL");
4006f6ba60Sopenharmony_ci
4106f6ba60Sopenharmony_ci    struct sockaddr_un addr;
4206f6ba60Sopenharmony_ci    if (memset_s(&addr, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un)) != EOK) {
4306f6ba60Sopenharmony_ci        PROFILER_LOG_ERROR(LOG_CORE, "memset_s error!");
4406f6ba60Sopenharmony_ci    }
4506f6ba60Sopenharmony_ci    addr.sun_family = AF_UNIX;
4606f6ba60Sopenharmony_ci    if (strncpy_s(addr.sun_path, sizeof(addr.sun_path), addrname.c_str(), sizeof(addr.sun_path) - 1) != EOK) {
4706f6ba60Sopenharmony_ci        PROFILER_LOG_ERROR(LOG_CORE, "strncpy_s error!");
4806f6ba60Sopenharmony_ci    }
4906f6ba60Sopenharmony_ci
5006f6ba60Sopenharmony_ci    CHECK_TRUE(connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) != -1, close(sock) != 0,
5106f6ba60Sopenharmony_ci        "Unix Socket Connect FAIL");
5206f6ba60Sopenharmony_ci
5306f6ba60Sopenharmony_ci    serviceBase_ = &serviceBase;
5406f6ba60Sopenharmony_ci    struct RawPointToService rrs;
5506f6ba60Sopenharmony_ci    if (strncpy_s(rrs.serviceName_, sizeof(rrs.serviceName_),
5606f6ba60Sopenharmony_ci        serviceBase_->serviceName_.c_str(), serviceBase_->serviceName_.size()) != EOK) {
5706f6ba60Sopenharmony_ci        PROFILER_LOG_ERROR(LOG_CORE, "strncpy_s error!");
5806f6ba60Sopenharmony_ci    }
5906f6ba60Sopenharmony_ci    rrs.serviceName_[serviceBase_->serviceName_.size()] = 0;
6006f6ba60Sopenharmony_ci    CHECK_TRUE(
6106f6ba60Sopenharmony_ci        SendRaw(RAW_PROTOCOL_POINTTO_SERVICE, reinterpret_cast<int8_t*>(&rrs), sizeof(struct RawPointToService), sock),
6206f6ba60Sopenharmony_ci        close(sock) != 0, "Unix Socket SendRaw FAIL");
6306f6ba60Sopenharmony_ci    socketHandle_ = sock;
6406f6ba60Sopenharmony_ci    CHECK_TRUE(CreateRecvThread(callback), close(sock) != 0, "Unix Socket Create Recv Thread FAIL");
6506f6ba60Sopenharmony_ci
6606f6ba60Sopenharmony_ci    return true;
6706f6ba60Sopenharmony_ci}
68