12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#include "socket.h"
172498b56bSopenharmony_ci
182498b56bSopenharmony_ci#include <cerrno>
192498b56bSopenharmony_ci#include <cstdint>
202498b56bSopenharmony_ci#include <sys/socket.h>
212498b56bSopenharmony_ci#include <sys/uio.h>
222498b56bSopenharmony_ci#include <unistd.h>
232498b56bSopenharmony_ci
242498b56bSopenharmony_cinamespace OHOS {
252498b56bSopenharmony_cinamespace HiviewDFX {
262498b56bSopenharmony_ciSocket::Socket(int socketType) : socketType(socketType) {}
272498b56bSopenharmony_ci
282498b56bSopenharmony_civoid Socket::SetType(uint32_t socketOption)
292498b56bSopenharmony_ci{
302498b56bSopenharmony_ci    socketType = socketOption;
312498b56bSopenharmony_ci}
322498b56bSopenharmony_ci
332498b56bSopenharmony_civoid Socket::SetCredential(struct ucred& cred)
342498b56bSopenharmony_ci{
352498b56bSopenharmony_ci    socketCred = cred;
362498b56bSopenharmony_ci}
372498b56bSopenharmony_ci
382498b56bSopenharmony_ciuid_t Socket::GetUid()
392498b56bSopenharmony_ci{
402498b56bSopenharmony_ci    return socketCred.uid;
412498b56bSopenharmony_ci}
422498b56bSopenharmony_ci
432498b56bSopenharmony_cipid_t Socket::GetPid()
442498b56bSopenharmony_ci{
452498b56bSopenharmony_ci    return socketCred.pid;
462498b56bSopenharmony_ci}
472498b56bSopenharmony_ci
482498b56bSopenharmony_ciint Socket::GenerateFD()
492498b56bSopenharmony_ci{
502498b56bSopenharmony_ci    int tmpFd = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
512498b56bSopenharmony_ci    int res = tmpFd;
522498b56bSopenharmony_ci    if (tmpFd == 0) {
532498b56bSopenharmony_ci        res = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
542498b56bSopenharmony_ci        close(tmpFd);
552498b56bSopenharmony_ci    }
562498b56bSopenharmony_ci    return res;
572498b56bSopenharmony_ci}
582498b56bSopenharmony_ci
592498b56bSopenharmony_ciint Socket::Create()
602498b56bSopenharmony_ci{
612498b56bSopenharmony_ci    if (socketHandler != 0) {
622498b56bSopenharmony_ci        return socketHandler;
632498b56bSopenharmony_ci    }
642498b56bSopenharmony_ci
652498b56bSopenharmony_ci    int fd = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
662498b56bSopenharmony_ci    if (fd < 0) {
672498b56bSopenharmony_ci#ifdef DEBUG
682498b56bSopenharmony_ci        std::cout << "Create socket failed: " <<  fd << std::endl;
692498b56bSopenharmony_ci#endif
702498b56bSopenharmony_ci        return fd;
712498b56bSopenharmony_ci    }
722498b56bSopenharmony_ci
732498b56bSopenharmony_ci    socketHandler = fd;
742498b56bSopenharmony_ci    return socketHandler;
752498b56bSopenharmony_ci}
762498b56bSopenharmony_ci
772498b56bSopenharmony_ciint Socket::Poll()
782498b56bSopenharmony_ci{
792498b56bSopenharmony_ci    return -1;
802498b56bSopenharmony_ci}
812498b56bSopenharmony_ci
822498b56bSopenharmony_ciint Socket::Write(const char *data, unsigned int len)
832498b56bSopenharmony_ci{
842498b56bSopenharmony_ci    if (data == nullptr) {
852498b56bSopenharmony_ci        return -1;
862498b56bSopenharmony_ci    }
872498b56bSopenharmony_ci
882498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(write(socketHandler, data, len));
892498b56bSopenharmony_ci}
902498b56bSopenharmony_ci
912498b56bSopenharmony_ciint Socket::WriteAll(const char *data, unsigned int len)
922498b56bSopenharmony_ci{
932498b56bSopenharmony_ci    const char *ptr = data;
942498b56bSopenharmony_ci    int sizeLeft = static_cast<int>(len);
952498b56bSopenharmony_ci    int midRes = 0;
962498b56bSopenharmony_ci
972498b56bSopenharmony_ci    if (data == nullptr) {
982498b56bSopenharmony_ci        return -1;
992498b56bSopenharmony_ci    }
1002498b56bSopenharmony_ci
1012498b56bSopenharmony_ci    while (sizeLeft > 0) {
1022498b56bSopenharmony_ci        midRes = Write(ptr, sizeLeft);
1032498b56bSopenharmony_ci        if (midRes < 0) {
1042498b56bSopenharmony_ci            break;
1052498b56bSopenharmony_ci        }
1062498b56bSopenharmony_ci        sizeLeft -= midRes;
1072498b56bSopenharmony_ci        ptr += midRes;
1082498b56bSopenharmony_ci    }
1092498b56bSopenharmony_ci
1102498b56bSopenharmony_ci    return (midRes < 0) ? midRes : static_cast<int>(len);
1112498b56bSopenharmony_ci}
1122498b56bSopenharmony_ci
1132498b56bSopenharmony_ci
1142498b56bSopenharmony_ciint Socket::WriteV(const iovec *vec, unsigned int len)
1152498b56bSopenharmony_ci{
1162498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(writev(socketHandler, vec, len));
1172498b56bSopenharmony_ci}
1182498b56bSopenharmony_ci
1192498b56bSopenharmony_ciint Socket::Read(char *buffer, unsigned int len)
1202498b56bSopenharmony_ci{
1212498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(read(socketHandler, buffer, len));
1222498b56bSopenharmony_ci}
1232498b56bSopenharmony_ci
1242498b56bSopenharmony_ciint Socket::Recv(void *buffer, unsigned int bufferLen, int flags)
1252498b56bSopenharmony_ci{
1262498b56bSopenharmony_ci    return TEMP_FAILURE_RETRY(recv(socketHandler, buffer, bufferLen, flags));
1272498b56bSopenharmony_ci}
1282498b56bSopenharmony_ci
1292498b56bSopenharmony_cibool Socket::SetHandler(int handler)
1302498b56bSopenharmony_ci{
1312498b56bSopenharmony_ci    if (socketHandler > 0) {
1322498b56bSopenharmony_ci        return false;
1332498b56bSopenharmony_ci    }
1342498b56bSopenharmony_ci    socketHandler = handler;
1352498b56bSopenharmony_ci    return true;
1362498b56bSopenharmony_ci}
1372498b56bSopenharmony_ci
1382498b56bSopenharmony_cibool Socket::CloseHandler()
1392498b56bSopenharmony_ci{
1402498b56bSopenharmony_ci    if (socketHandler > 0) {
1412498b56bSopenharmony_ci        close(socketHandler);
1422498b56bSopenharmony_ci        socketHandler = -1;
1432498b56bSopenharmony_ci        return true;
1442498b56bSopenharmony_ci    }
1452498b56bSopenharmony_ci    return false;
1462498b56bSopenharmony_ci}
1472498b56bSopenharmony_ci
1482498b56bSopenharmony_ciSocket::~Socket()
1492498b56bSopenharmony_ci{
1502498b56bSopenharmony_ci    close(socketHandler);
1512498b56bSopenharmony_ci    socketHandler = -1;
1522498b56bSopenharmony_ci}
1532498b56bSopenharmony_ci} // namespace HiviewDFX
1542498b56bSopenharmony_ci} // namespace OHOS
155