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 <array> 172498b56bSopenharmony_ci 182498b56bSopenharmony_ci#include "dgram_socket_server.h" 192498b56bSopenharmony_ci 202498b56bSopenharmony_cinamespace OHOS { 212498b56bSopenharmony_cinamespace HiviewDFX { 222498b56bSopenharmony_ciint DgramSocketServer::RecvPacket(std::vector<char>& buffer, struct ucred *cred) 232498b56bSopenharmony_ci{ 242498b56bSopenharmony_ci uint16_t packetLen = 0; 252498b56bSopenharmony_ci if (auto status = Recv(&packetLen, sizeof(packetLen)); status < 0) { 262498b56bSopenharmony_ci return status; 272498b56bSopenharmony_ci } 282498b56bSopenharmony_ci if (packetLen > maxPacketLength) { 292498b56bSopenharmony_ci Recv(&packetLen, sizeof(packetLen), 0); // skip too long packet 302498b56bSopenharmony_ci return 0; 312498b56bSopenharmony_ci } 322498b56bSopenharmony_ci 332498b56bSopenharmony_ci std::array<char, CMSG_SPACE(sizeof(struct ucred))> control = {0}; 342498b56bSopenharmony_ci 352498b56bSopenharmony_ci struct msghdr msgh = {0}; 362498b56bSopenharmony_ci int ret = 0; 372498b56bSopenharmony_ci if (cred != nullptr) { 382498b56bSopenharmony_ci struct iovec iov; 392498b56bSopenharmony_ci iov.iov_base = buffer.data(); 402498b56bSopenharmony_ci iov.iov_len = packetLen; 412498b56bSopenharmony_ci msgh.msg_iov = &iov; 422498b56bSopenharmony_ci msgh.msg_iovlen = 1; 432498b56bSopenharmony_ci 442498b56bSopenharmony_ci msgh.msg_control = control.data(); 452498b56bSopenharmony_ci msgh.msg_controllen = control.size(); 462498b56bSopenharmony_ci 472498b56bSopenharmony_ci msgh.msg_name = nullptr; 482498b56bSopenharmony_ci msgh.msg_namelen = 0; 492498b56bSopenharmony_ci msgh.msg_flags = 0; 502498b56bSopenharmony_ci 512498b56bSopenharmony_ci ret = RecvMsg(&msgh); 522498b56bSopenharmony_ci } else { 532498b56bSopenharmony_ci ret = Recv(buffer.data(), packetLen, 0); 542498b56bSopenharmony_ci } 552498b56bSopenharmony_ci 562498b56bSopenharmony_ci if (ret <= 0) { 572498b56bSopenharmony_ci return ret; 582498b56bSopenharmony_ci } else if (cred != nullptr) { 592498b56bSopenharmony_ci struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); 602498b56bSopenharmony_ci struct ucred *receivedUcred = (struct ucred*)CMSG_DATA(cmsg); 612498b56bSopenharmony_ci if (receivedUcred == nullptr) { 622498b56bSopenharmony_ci return 0; 632498b56bSopenharmony_ci } 642498b56bSopenharmony_ci *cred = *receivedUcred; 652498b56bSopenharmony_ci } 662498b56bSopenharmony_ci buffer[ret - 1] = 0; 672498b56bSopenharmony_ci 682498b56bSopenharmony_ci return ret; 692498b56bSopenharmony_ci} 702498b56bSopenharmony_ci} // namespace HiviewDFX 712498b56bSopenharmony_ci} // namespace OHOS 72