Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "FwmarkClient.h" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"}; |
| 27 | |
| 28 | } // namespace |
| 29 | |
Sreeram Ramachandran | 5fc2757 | 2014-05-21 13:08:34 -0700 | [diff] [blame^] | 30 | bool FwmarkClient::shouldSetFwmark(int family) { |
| 31 | return (family == AF_INET || family == AF_INET6) && !getenv("ANDROID_NO_USE_FWMARK_CLIENT"); |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | FwmarkClient::FwmarkClient() : mChannel(-1) { |
| 35 | } |
| 36 | |
| 37 | FwmarkClient::~FwmarkClient() { |
| 38 | if (mChannel >= 0) { |
| 39 | // We don't care about errors while closing the channel, so restore any previous error. |
| 40 | int error = errno; |
| 41 | close(mChannel); |
| 42 | errno = error; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | bool FwmarkClient::send(void* data, size_t len, int fd) { |
| 47 | mChannel = socket(AF_UNIX, SOCK_STREAM, 0); |
| 48 | if (mChannel == -1) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH), |
| 53 | sizeof(FWMARK_SERVER_PATH))) == -1) { |
| 54 | // If we are unable to connect to the fwmark server, assume there's no error. This protects |
| 55 | // against future changes if the fwmark server goes away. |
| 56 | errno = 0; |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | iovec iov; |
| 61 | iov.iov_base = data; |
| 62 | iov.iov_len = len; |
| 63 | |
| 64 | msghdr message; |
| 65 | memset(&message, 0, sizeof(message)); |
| 66 | message.msg_iov = &iov; |
| 67 | message.msg_iovlen = 1; |
| 68 | |
| 69 | union { |
| 70 | cmsghdr cmh; |
| 71 | char cmsg[CMSG_SPACE(sizeof(fd))]; |
| 72 | } cmsgu; |
| 73 | |
| 74 | memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); |
| 75 | message.msg_control = cmsgu.cmsg; |
| 76 | message.msg_controllen = sizeof(cmsgu.cmsg); |
| 77 | |
| 78 | cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); |
| 79 | cmsgh->cmsg_len = CMSG_LEN(sizeof(fd)); |
| 80 | cmsgh->cmsg_level = SOL_SOCKET; |
| 81 | cmsgh->cmsg_type = SCM_RIGHTS; |
| 82 | memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd)); |
| 83 | |
| 84 | if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | int error = 0; |
| 89 | if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | errno = error; |
| 94 | return !error; |
| 95 | } |