1 /*
2  * Copyright 2017, 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 #pragma once
18 
19 #include "result.h"
20 
21 #include <arpa/inet.h>
22 
23 class Message;
24 
25 class Socket {
26 public:
27     Socket();
28     Socket(const Socket&) = delete;
29     ~Socket();
30 
31     Socket& operator=(const Socket&) = delete;
32 
get()33     int get() const { return mSocketFd; }
34     // Open a socket, |domain|, |type| and |protocol| are as described in the
35     // man pages for socket.
36     Result open(int domain, int type, int protocol);
37     // Bind to a generic |sockaddr| of size |sockaddrLength|
38     Result bind(const void* sockaddr, size_t sockaddrLength);
39     // Bind to an IP |address| and |port|
40     Result bindIp(in_addr_t address, uint16_t port);
41     // Bind a raw socket to the interface with index |interfaceIndex|.
42     Result bindRaw(unsigned int interfaceIndex);
43     // Send data in |message| on an IP socket to
44     // |destinationAddress|:|destinationPort|, the message will egress on the
45     // interface specified by |interfaceIndex|
46     Result sendOnInterface(unsigned int interfaceIndex,
47                            in_addr_t destinationAddress,
48                            uint16_t destinationPort,
49                            const Message& message);
50     // Send |message| as a UDP datagram on a raw socket. The source address of
51     // the message will be |source|:|sourcePort| and the destination will be
52     // |destination|:|destinationPort|. The message will be sent on the
53     // interface indicated by |interfaceIndex|.
54     Result sendRawUdp(in_addr_t source,
55                       uint16_t sourcePort,
56                       in_addr_t destination,
57                       uint16_t destinationPort,
58                       unsigned int interfaceIndex,
59                       const Message& message);
60     // Receive data on the socket and indicate which interface the data was
61     // received on in |interfaceIndex|. The received data is placed in |message|
62     Result receiveFromInterface(Message* message, unsigned int* interfaceIndex);
63     // Receive UDP data on a raw socket. Expect that the protocol in the IP
64     // header is UDP and that the port in the UDP header is |expectedPort|. If
65     // the received data is valid then |isValid| will be set to true, otherwise
66     // false. The validity check includes the expected values as well as basic
67     // size requirements to fit the expected protocol headers.  The method will
68     // only return an error result if the actual receiving fails.
69     Result receiveRawUdp(uint16_t expectedPort,
70                          Message* message,
71                          bool* isValid);
72     // Enable |optionName| on option |level|. These values are the same as used
73     // in setsockopt calls.
74     Result enableOption(int level, int optionName);
75 private:
76     int mSocketFd;
77 };
78 
79