Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 1 | // 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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_SOCKET_H_ |
| 6 | #define PATCHPANEL_SOCKET_H_ |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 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> |
Andreea Costinas | 74f45d2 | 2020-03-13 10:29:31 +0100 | [diff] [blame] | 17 | #include <brillo/brillo_export.h> |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 18 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 19 | namespace patchpanel { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 20 | |
| 21 | // Wrapper around various syscalls used for socket communications. |
Andreea Costinas | 74f45d2 | 2020-03-13 10:29:31 +0100 | [diff] [blame] | 22 | class BRILLO_EXPORT Socket { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 23 | public: |
| 24 | Socket(int family, int type); |
| 25 | explicit Socket(base::ScopedFD fd); |
| 26 | virtual ~Socket() = default; |
| 27 | |
Garrick Evans | fa872f6 | 2019-05-28 16:49:25 +0900 | [diff] [blame] | 28 | bool Bind(const struct sockaddr* addr, socklen_t addrlen); |
| 29 | bool Connect(const struct sockaddr* addr, socklen_t addrlen); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 30 | bool Listen(int backlog) const; |
| 31 | std::unique_ptr<Socket> Accept(struct sockaddr* addr = nullptr, |
| 32 | socklen_t* addrlen = nullptr) const; |
| 33 | |
Garrick Evans | 1cce71a | 2019-06-21 10:43:14 +0900 | [diff] [blame] | 34 | ssize_t SendTo(const void* data, |
| 35 | size_t len, |
| 36 | const struct sockaddr* addr = nullptr, |
| 37 | socklen_t addrlen = 0); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 38 | ssize_t RecvFrom(void* data, |
| 39 | size_t len, |
| 40 | struct sockaddr* addr = nullptr, |
| 41 | socklen_t addrlen = 0); |
| 42 | |
| 43 | int fd() const { return fd_.get(); } |
| 44 | |
Andreea Costinas | fd7f66b | 2020-04-12 21:38:37 +0200 | [diff] [blame] | 45 | // Releases the underlying fd rendering the Socket instance invalid. |
| 46 | int release() { return fd_.release(); } |
| 47 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 48 | private: |
| 49 | base::ScopedFD fd_; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(Socket); |
| 52 | }; |
| 53 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 54 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 55 | const Socket& socket); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 56 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 57 | } // namespace patchpanel |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 58 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 59 | #endif // PATCHPANEL_SOCKET_H_ |