blob: ef34a2312e1d48b423a2ee841f74bcafa879466e [file] [log] [blame]
Garrick Evans3cbac7c2019-04-18 15:31:31 +09001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef ARC_NETWORK_SOCKET_H_
6#define ARC_NETWORK_SOCKET_H_
7
8#include <netinet/ip.h>
9#include <sys/socket.h>
10#include <sys/types.h>
11
12#include <memory>
13#include <string>
14
15#include <base/files/scoped_file.h>
16#include <base/macros.h>
17
18namespace arc_networkd {
19
20// Wrapper around various syscalls used for socket communications.
21class Socket {
22 public:
23 Socket(int family, int type);
24 explicit Socket(base::ScopedFD fd);
25 virtual ~Socket() = default;
26
Garrick Evansfa872f62019-05-28 16:49:25 +090027 bool Bind(const struct sockaddr* addr, socklen_t addrlen);
28 bool Connect(const struct sockaddr* addr, socklen_t addrlen);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090029 bool Listen(int backlog) const;
30 std::unique_ptr<Socket> Accept(struct sockaddr* addr = nullptr,
31 socklen_t* addrlen = nullptr) const;
32
33 ssize_t SendTo(const void* data, size_t len, struct sockaddr* addr = nullptr);
34 ssize_t RecvFrom(void* data,
35 size_t len,
36 struct sockaddr* addr = nullptr,
37 socklen_t addrlen = 0);
38
39 int fd() const { return fd_.get(); }
40
41 private:
42 base::ScopedFD fd_;
43
44 DISALLOW_COPY_AND_ASSIGN(Socket);
45};
46
47std::ostream& operator<<(std::ostream& stream, const Socket& socket);
48
49} // namespace arc_networkd
50
51#endif // ARC_NETWORK_SOCKET_H_