blob: f7e8cc24829b12c720b7c269f610337138253aa3 [file] [log] [blame]
Sreeram Ramachandran8f95def2014-05-12 11:20:12 -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#include "netd_client/FwmarkCommands.h"
19
20#include <sys/socket.h>
Sreeram Ramachandran0c2dfc32014-05-13 15:42:23 -070021#include <unistd.h>
22
23#define CLOSE_FD_AND_RESTORE_ERRNO(fd) \
24 do { \
25 int error = errno; \
26 close(fd); \
27 errno = error; \
28 } while (0)
Sreeram Ramachandran8f95def2014-05-12 11:20:12 -070029
30namespace {
31
32typedef int (*ConnectFunctionType)(int, const sockaddr*, socklen_t);
Sreeram Ramachandran0c2dfc32014-05-13 15:42:23 -070033typedef int (*AcceptFunctionType)(int, sockaddr*, socklen_t*);
Sreeram Ramachandran8f95def2014-05-12 11:20:12 -070034
35ConnectFunctionType libcConnect = 0;
Sreeram Ramachandran0c2dfc32014-05-13 15:42:23 -070036AcceptFunctionType libcAccept = 0;
Sreeram Ramachandran8f95def2014-05-12 11:20:12 -070037
38int netdClientConnect(int sockfd, const sockaddr* addr, socklen_t addrlen) {
39 if (FwmarkClient::shouldSetFwmark(sockfd, addr)) {
40 char data[] = {FWMARK_COMMAND_ON_CONNECT};
41 if (!FwmarkClient().send(data, sizeof(data), sockfd)) {
42 return -1;
43 }
44 }
45 return libcConnect(sockfd, addr, addrlen);
46}
47
Sreeram Ramachandran0c2dfc32014-05-13 15:42:23 -070048int netdClientAccept(int sockfd, sockaddr* addr, socklen_t* addrlen) {
49 int acceptedSocket = libcAccept(sockfd, addr, addrlen);
50 if (acceptedSocket == -1) {
51 return -1;
52 }
53 sockaddr socketAddress;
54 if (!addr) {
55 socklen_t socketAddressLen = sizeof(socketAddress);
56 if (getsockname(acceptedSocket, &socketAddress, &socketAddressLen) == -1) {
57 CLOSE_FD_AND_RESTORE_ERRNO(acceptedSocket);
58 return -1;
59 }
60 addr = &socketAddress;
61 }
62 if (FwmarkClient::shouldSetFwmark(acceptedSocket, addr)) {
63 char data[] = {FWMARK_COMMAND_ON_ACCEPT};
64 if (!FwmarkClient().send(data, sizeof(data), acceptedSocket)) {
65 CLOSE_FD_AND_RESTORE_ERRNO(acceptedSocket);
66 return -1;
67 }
68 }
69 return acceptedSocket;
70}
71
Sreeram Ramachandran8f95def2014-05-12 11:20:12 -070072} // namespace
73
74extern "C" void netdClientInitConnect(ConnectFunctionType* function) {
75 if (function && *function) {
76 libcConnect = *function;
77 *function = netdClientConnect;
78 }
79}
Sreeram Ramachandran0c2dfc32014-05-13 15:42:23 -070080
81extern "C" void netdClientInitAccept(AcceptFunctionType* function) {
82 if (function && *function) {
83 libcAccept = *function;
84 *function = netdClientAccept;
85 }
86}