network.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <utility>
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. namespace Network {
  10. class Socket;
  11. /// Error code for network functions
  12. enum class Errno {
  13. SUCCESS,
  14. BADF,
  15. INVAL,
  16. MFILE,
  17. NOTCONN,
  18. AGAIN,
  19. CONNREFUSED,
  20. HOSTUNREACH,
  21. NETDOWN,
  22. NETUNREACH,
  23. OTHER,
  24. };
  25. /// Address families
  26. enum class Domain {
  27. INET, ///< Address family for IPv4
  28. };
  29. /// Socket types
  30. enum class Type {
  31. STREAM,
  32. DGRAM,
  33. RAW,
  34. SEQPACKET,
  35. };
  36. /// Protocol values for sockets
  37. enum class Protocol {
  38. ICMP,
  39. TCP,
  40. UDP,
  41. };
  42. /// Shutdown mode
  43. enum class ShutdownHow {
  44. RD,
  45. WR,
  46. RDWR,
  47. };
  48. /// Array of IPv4 address
  49. using IPv4Address = std::array<u8, 4>;
  50. /// Cross-platform sockaddr structure
  51. struct SockAddrIn {
  52. Domain family;
  53. IPv4Address ip;
  54. u16 portno;
  55. };
  56. /// Cross-platform poll fd structure
  57. enum class PollEvents : u16 {
  58. // Using Pascal case because IN is a macro on Windows.
  59. In = 1 << 0,
  60. Pri = 1 << 1,
  61. Out = 1 << 2,
  62. Err = 1 << 3,
  63. Hup = 1 << 4,
  64. Nval = 1 << 5,
  65. };
  66. DECLARE_ENUM_FLAG_OPERATORS(PollEvents);
  67. struct PollFD {
  68. Socket* socket;
  69. PollEvents events;
  70. PollEvents revents;
  71. };
  72. class NetworkInstance {
  73. public:
  74. explicit NetworkInstance();
  75. ~NetworkInstance();
  76. };
  77. /// @brief Returns host's IPv4 address
  78. /// @return Pair of an array of human ordered IPv4 address (e.g. 192.168.0.1) and an error code
  79. std::pair<IPv4Address, Errno> GetHostIPv4Address();
  80. } // namespace Network