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