sockets.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* headers to use the BSD sockets */
  2. #ifndef QEMU_SOCKETS_H
  3. #define QEMU_SOCKETS_H
  4. #ifdef _WIN32
  5. int inet_aton(const char *cp, struct in_addr *ia);
  6. #endif /* !_WIN32 */
  7. #include "qapi-types.h"
  8. /* misc helpers */
  9. int qemu_socket(int domain, int type, int protocol);
  10. int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  11. int socket_set_cork(int fd, int v);
  12. int socket_set_nodelay(int fd);
  13. void qemu_set_block(int fd);
  14. void qemu_set_nonblock(int fd);
  15. int socket_set_fast_reuse(int fd);
  16. #ifdef WIN32
  17. /* Windows has different names for the same constants with the same values */
  18. #define SHUT_RD 0
  19. #define SHUT_WR 1
  20. #define SHUT_RDWR 2
  21. #endif
  22. /* callback function for nonblocking connect
  23. * valid fd on success, negative error code on failure
  24. */
  25. typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque);
  26. InetSocketAddress *inet_parse(const char *str, Error **errp);
  27. int inet_listen(const char *str, char *ostr, int olen,
  28. int socktype, int port_offset, Error **errp);
  29. int inet_connect(const char *str, Error **errp);
  30. int inet_nonblocking_connect(const char *str,
  31. NonBlockingConnectHandler *callback,
  32. void *opaque, Error **errp);
  33. NetworkAddressFamily inet_netfamily(int family);
  34. int unix_listen(const char *path, char *ostr, int olen, Error **errp);
  35. int unix_connect(const char *path, Error **errp);
  36. int unix_nonblocking_connect(const char *str,
  37. NonBlockingConnectHandler *callback,
  38. void *opaque, Error **errp);
  39. SocketAddress *socket_parse(const char *str, Error **errp);
  40. int socket_connect(SocketAddress *addr, Error **errp,
  41. NonBlockingConnectHandler *callback, void *opaque);
  42. int socket_listen(SocketAddress *addr, Error **errp);
  43. void socket_listen_cleanup(int fd, Error **errp);
  44. int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
  45. /* Old, ipv4 only bits. Don't use for new code. */
  46. int parse_host_port(struct sockaddr_in *saddr, const char *str);
  47. int socket_init(void);
  48. /**
  49. * socket_sockaddr_to_address:
  50. * @sa: socket address struct
  51. * @salen: size of @sa struct
  52. * @errp: pointer to uninitialized error object
  53. *
  54. * Get the string representation of the socket
  55. * address. A pointer to the allocated address information
  56. * struct will be returned, which the caller is required to
  57. * release with a call qapi_free_SocketAddress when no
  58. * longer required.
  59. *
  60. * Returns: the socket address struct, or NULL on error
  61. */
  62. SocketAddress *
  63. socket_sockaddr_to_address(struct sockaddr_storage *sa,
  64. socklen_t salen,
  65. Error **errp);
  66. /**
  67. * socket_local_address:
  68. * @fd: the socket file handle
  69. * @errp: pointer to uninitialized error object
  70. *
  71. * Get the string representation of the local socket
  72. * address. A pointer to the allocated address information
  73. * struct will be returned, which the caller is required to
  74. * release with a call qapi_free_SocketAddress when no
  75. * longer required.
  76. *
  77. * Returns: the socket address struct, or NULL on error
  78. */
  79. SocketAddress *socket_local_address(int fd, Error **errp);
  80. /**
  81. * socket_remote_address:
  82. * @fd: the socket file handle
  83. * @errp: pointer to uninitialized error object
  84. *
  85. * Get the string representation of the remote socket
  86. * address. A pointer to the allocated address information
  87. * struct will be returned, which the caller is required to
  88. * release with a call qapi_free_SocketAddress when no
  89. * longer required.
  90. *
  91. * Returns: the socket address struct, or NULL on error
  92. */
  93. SocketAddress *socket_remote_address(int fd, Error **errp);
  94. /**
  95. * socket_address_to_string:
  96. * @addr: the socket address struct
  97. * @errp: pointer to uninitialized error object
  98. *
  99. * Get the string representation of the socket
  100. * address. A pointer to the char array containing
  101. * string format will be returned, the caller is
  102. * required to release the returned value when no
  103. * longer required with g_free.
  104. *
  105. * Returns: the socket address in string format, or NULL on error
  106. */
  107. char *socket_address_to_string(struct SocketAddress *addr, Error **errp);
  108. #endif /* QEMU_SOCKETS_H */