blob: db2009ffd52866f1c1ab14c0b2e9d8c2b4d08eab [file] [log] [blame]
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -07001/*
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
24namespace {
25
26const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"};
27
28} // namespace
29
Sreeram Ramachandran5fc27572014-05-21 13:08:34 -070030bool FwmarkClient::shouldSetFwmark(int family) {
31 return (family == AF_INET || family == AF_INET6) && !getenv("ANDROID_NO_USE_FWMARK_CLIENT");
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070032}
33
34FwmarkClient::FwmarkClient() : mChannel(-1) {
35}
36
37FwmarkClient::~FwmarkClient() {
38 if (mChannel >= 0) {
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070039 close(mChannel);
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070040 }
41}
42
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070043int FwmarkClient::send(void* data, size_t len, int fd) {
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070044 mChannel = socket(AF_UNIX, SOCK_STREAM, 0);
45 if (mChannel == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070046 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070047 }
48
49 if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast<const sockaddr*>(&FWMARK_SERVER_PATH),
50 sizeof(FWMARK_SERVER_PATH))) == -1) {
51 // If we are unable to connect to the fwmark server, assume there's no error. This protects
52 // against future changes if the fwmark server goes away.
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070053 return 0;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070054 }
55
56 iovec iov;
57 iov.iov_base = data;
58 iov.iov_len = len;
59
60 msghdr message;
61 memset(&message, 0, sizeof(message));
62 message.msg_iov = &iov;
63 message.msg_iovlen = 1;
64
65 union {
66 cmsghdr cmh;
67 char cmsg[CMSG_SPACE(sizeof(fd))];
68 } cmsgu;
69
70 memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg));
71 message.msg_control = cmsgu.cmsg;
72 message.msg_controllen = sizeof(cmsgu.cmsg);
73
74 cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message);
75 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
76 cmsgh->cmsg_level = SOL_SOCKET;
77 cmsgh->cmsg_type = SCM_RIGHTS;
78 memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd));
79
80 if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070081 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070082 }
83
84 int error = 0;
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070085
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070086 if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) {
Sreeram Ramachandran3a069e62014-06-22 11:02:57 -070087 return -errno;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070088 }
89
Sreeram Ramachandran31f42102014-06-20 11:51:48 -070090 return error;
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070091}