Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
emutools_socketapi.h
Go to the documentation of this file.
1 /* Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
2  Copyright (C)2016,2019-2021 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17 
18 #ifndef XEMU_COMMON_EMUTOOLS_SOCKETAPI_H_INCLUDED
19 #define XEMU_COMMON_EMUTOOLS_SOCKETAPI_H_INCLUDED
20 #ifdef XEMU_HAS_SOCKET_API
21 
22 #ifdef XEMU_ARCH_WIN
23 # include <winsock2.h>
24  typedef SOCKET xemusock_socket_t;
25  typedef int xemusock_socklen_t;
26  // it seems Windows has no EAGAIN thing ...
27 # define XSEAGAIN WSAEWOULDBLOCK
28 # define XSEWOULDBLOCK WSAEWOULDBLOCK
29 # define XSEINPROGRESS WSAEINPROGRESS
30 # define XSEALREADY WSAEALREADY
31 # define XSEINTR WSAEINTR
32 # define XS_INVALID_SOCKET INVALID_SOCKET
33 # define XS_SOCKET_ERROR SOCKET_ERROR
34 # define SHUT_RDWR SD_BOTH
35  extern const char *xemusock_strerror ( int err );
36 #else
37 # include <arpa/inet.h>
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <errno.h>
41  typedef int xemusock_socket_t;
42  typedef socklen_t xemusock_socklen_t;
43 # define XSEAGAIN EAGAIN
44 # define XSEWOULDBLOCK EWOULDBLOCK
45 # define XSEINPROGRESS EINPROGRESS
46 # define XSEALREADY EALREADY
47 # define XSEINTR EINTR
48 # define XS_INVALID_SOCKET -1
49 # define XS_SOCKET_ERROR -1
50 # define xemusock_strerror(_n) strerror(_n)
51 #endif
52 
53 #define XEMUSOCK_SELECT_R 1
54 #define XEMUSOCK_SELECT_W 2
55 #define XEMUSOCK_SELECT_E 4
56 
57 #define XEMUSOCK_UDP 0
58 #define XEMUSOCK_TCP 1
59 #define XEMUSOCK_BLOCKING 0
60 #define XEMUSOCK_NONBLOCKING 1
61 
62 extern int xemusock_close ( xemusock_socket_t sock, int *xerrno );
63 extern const char *xemusock_init( void );
64 extern void xemusock_uninit ( void );
65 extern int xemusock_select_1 ( xemusock_socket_t sock, int usec, int what, int *xerrno );
66 extern void xemusock_fill_servaddr_for_inet_ip_netlong ( struct sockaddr_in *servaddr, unsigned int ip_netlong, int port );
67 extern void xemusock_fill_servaddr_for_inet_ip_native ( struct sockaddr_in *servaddr, unsigned int ip_native, int port );
68 extern int xemusock_set_nonblocking ( xemusock_socket_t sock, int is_nonblock, int *xerrno );
69 extern int xemusock_connect ( xemusock_socket_t sock, struct sockaddr_in *servaddr, int *xerrno );
70 extern int xemusock_send ( xemusock_socket_t sock, const void *buffer, int length, int *xerrno );
71 extern int xemusock_sendto ( xemusock_socket_t sock, const void *buffer, int length, struct sockaddr_in *servaddr, int *xerrno );
72 extern int xemusock_recv ( xemusock_socket_t sock, void *buffer, int length, int *xerrno );
73 extern int xemusock_recvfrom ( xemusock_socket_t sock, void *buffer, int length, struct sockaddr_in *servaddr, int *xerrno );
74 extern int xemusock_shutdown ( xemusock_socket_t sock, int *xerrno );
75 extern int xemusock_bind ( xemusock_socket_t sock, struct sockaddr *addr, xemusock_socklen_t addrlen, int *xerrno );
76 extern int xemusock_listen ( xemusock_socket_t sock, int backlog, int *xerrno );
77 extern int xemusock_setsockopt ( xemusock_socket_t sock, int level, int option, const void *value, int len, int *xerrno );
78 extern int xemusock_setsockopt_reuseaddr ( xemusock_socket_t sock, int *xerrno );
79 extern xemusock_socket_t xemusock_accept ( xemusock_socket_t sock, struct sockaddr *addr, xemusock_socklen_t *addrlen, int *xerrno );
80 extern xemusock_socket_t xemusock_create_for_inet ( int is_tcp, int is_nonblock, int *xerrno );
81 
82 static inline int xemusock_should_repeat_from_error ( int xerr ) {
83  return (xerr == XSEAGAIN || xerr == XSEWOULDBLOCK || xerr == XSEINPROGRESS || xerr == XSEINTR || xerr == XSEALREADY);
84 }
85 static inline unsigned int xemusock_ipv4_octetarray_to_netlong ( const unsigned char ip[4] ) {
86  return htonl((unsigned int)((((unsigned int)ip[0]) << 24) + (((unsigned int)ip[1]) << 16) + (((unsigned int)ip[2]) << 8) + ((unsigned int)ip[3])));
87 }
88 static inline unsigned int xemusock_ipv4_netoctetarray_to_netlong ( const unsigned char ip[4] ) {
89  return ((unsigned int)((((unsigned int)ip[0]) << 24) + (((unsigned int)ip[1]) << 16) + (((unsigned int)ip[2]) << 8) + ((unsigned int)ip[3])));
90 }
91 static inline unsigned int xemusock_ipv4_octetstring_to_netlong ( const char *ipstr ) {
92  return inet_addr(ipstr);
93 }
94 
95 #endif
96 #endif
addr
int addr
Definition: dma65.c:81
value
int value
Definition: dma65.c:90