• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, in_addr_t destinationAddress,
47                            uint16_t destinationPort, const Message& message);
48     // Send |message| as a UDP datagram on a raw socket. The source address of
49     // the message will be |source|:|sourcePort| and the destination will be
50     // |destination|:|destinationPort|. The message will be sent on the
51     // interface indicated by |interfaceIndex|.
52     Result sendRawUdp(in_addr_t source, uint16_t sourcePort, in_addr_t destination,
53                       uint16_t destinationPort, unsigned int interfaceIndex,
54                       const Message& message);
55     // Receive data on the socket and indicate which interface the data was
56     // received on in |interfaceIndex|. The received data is placed in |message|
57     Result receiveFromInterface(Message* message, unsigned int* interfaceIndex);
58     // Receive UDP data on a raw socket. Expect that the protocol in the IP
59     // header is UDP and that the port in the UDP header is |expectedPort|. If
60     // the received data is valid then |isValid| will be set to true, otherwise
61     // false. The validity check includes the expected values as well as basic
62     // size requirements to fit the expected protocol headers.  The method will
63     // only return an error result if the actual receiving fails.
64     Result receiveRawUdp(uint16_t expectedPort, Message* message, bool* isValid);
65     // Enable |optionName| on option |level|. These values are the same as used
66     // in setsockopt calls.
67     Result enableOption(int level, int optionName);
68 
69   private:
70     int mSocketFd;
71 };
72