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