IDA C++ SDK 9.2
Loading...
Searching...
No Matches
pronet.h
Go to the documentation of this file.
1// socket API wrappers
2
3#ifndef __PRONET_H__
4#define __PRONET_H__
5
6#ifdef __NT__
7# define WIN32_LEAN_AND_MEAN
8# pragma pack(push)
9# include <winsock2.h> // may change structure packing?!
10# pragma pack(pop)
11#else // __NT__
12# include <errno.h>
13# include <netdb.h>
14# include <poll.h>
15# include <sys/socket.h>
16# include <sys/select.h>
17#endif
18
19#ifdef __NT__
20#pragma comment(lib, "WS2_32.lib")
21#endif
22
30
31//---------------------------------------------------------------------------
32#ifdef __NT__
33# define SIG_SAFE_CALL(expr) return expr
34# define SOCKLEN_T int
35# define SOCKBUF_T char *
36#else
37# define SIG_SAFE_CALL(expr) \
38 do \
39 { \
40 long rc = expr; \
41 if ( rc != -1 || errno != EINTR ) \
42 return rc; \
43 } \
44 while ( true )
45# define SOCKLEN_T socklen_t
46# define SOCKBUF_T void *
47#endif
48
49//---------------------------------------------------------------------------
50inline ssize_t qsendto(int socket, const SOCKBUF_T buf, size_t size, int flags, const struct sockaddr *dest_addr, SOCKLEN_T addrlen)
51{
52 SIG_SAFE_CALL(::sendto(socket, buf, size, flags, dest_addr, addrlen));
53}
54
55//---------------------------------------------------------------------------
56inline ssize_t qrecvfrom(int socket, SOCKBUF_T buf, size_t size, int flags, struct sockaddr *src_addr, SOCKLEN_T *addrlen)
57{
58 SIG_SAFE_CALL(::recvfrom(socket, buf, size, flags, src_addr, addrlen));
59}
60
61//---------------------------------------------------------------------------
62inline ssize_t qsend(int socket, const void *buf, size_t size)
63{
64#ifdef __NT__
65 return qsendto(socket, (SOCKBUF_T)buf, size, 0, nullptr, 0);
66#else
67 SIG_SAFE_CALL(::send(socket, buf, size, 0));
68#endif
69}
70
71//---------------------------------------------------------------------------
72inline ssize_t qrecv(int socket, void *buf, size_t size)
73{
74#ifdef __NT__
75 return qrecvfrom(socket, (SOCKBUF_T)buf, size, 0, nullptr, nullptr);
76#else
77 SIG_SAFE_CALL(::recv(socket, buf, size, 0));
78#endif
79}
80
81//---------------------------------------------------------------------------
82inline int qselect(int nflds, fd_set *rds, fd_set *wds, fd_set *eds, struct timeval *timeout)
83{
84 SIG_SAFE_CALL(::select(nflds, rds, wds, eds, timeout));
85}
86
87//-------------------------------------------------------------------------
88inline int qpoll(pollfd *fds, uint32 nfds, int timeout_ms)
89{
90#ifdef __NT__
91 return WSAPoll(fds, nfds, timeout_ms);
92#else
93 SIG_SAFE_CALL(::poll(fds, nfds, timeout_ms));
94#endif
95}
96
97//---------------------------------------------------------------------------
98// Prevent using of the socket functions directly
99// (compiler diagnostics: call of overloaded ... is ambiguous)
101{
102 inline ssize_t sendto(int, const SOCKBUF_T, size_t, int, const struct sockaddr *, SOCKLEN_T) { return 0; }
103 inline ssize_t recvfrom(int, SOCKBUF_T, size_t, int, struct sockaddr *, SOCKLEN_T *) { return 0; }
104 inline ssize_t send(int, const SOCKBUF_T, size_t, int) { return 0; }
105 inline ssize_t recv(int, SOCKBUF_T, size_t, int) { return 0; }
106 inline int select(int, fd_set *, fd_set *, fd_set *, struct timeval *) { return 0; }
107}
108using namespace DONT_USE_FUNCS;
109
110//-------------------------------------------------------------------------
119idaman bool ida_export qhost2addr_(
120 void *out,
121 const char *name,
122 ushort family,
123 ushort port = 0);
124
125//-------------------------------------------------------------------------
126inline bool qhost2addr(struct sockaddr_in *out, const char *name, ushort port = 0)
127{
128 return qhost2addr_(out, name, AF_INET, port);
129}
130
131//-------------------------------------------------------------------------
132inline bool qhost2addr(struct sockaddr_in6 *out, const char *name, ushort port = 0)
133{
134 return qhost2addr_(out, name, AF_INET6, port);
135}
136
137//-------------------------------------------------------------------------
138// Get the local host IP
139bool get_my_ip(char out[NI_MAXHOST], const ushort family = AF_INET);
140
141//-------------------------------------------------------------------------
142// Get the local host name (utf-8)
143idaman bool ida_export qgethostname(qstring *out);
144
145
146#undef SIG_SAFE_CALL
147#undef SOCKLEN_T
148#undef SOCKBUF_T
149
150#endif // __PRONET_H__
asize_t size
Definition kernwin.hpp:6339
Definition pronet.h:101
ssize_t recv(int, SOCKBUF_T, size_t, int)
Definition pronet.h:105
ssize_t send(int, const SOCKBUF_T, size_t, int)
Definition pronet.h:104
ssize_t sendto(int, const SOCKBUF_T, size_t, int, const struct sockaddr *, SOCKLEN_T)
Definition pronet.h:102
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
Definition pronet.h:106
ssize_t recvfrom(int, SOCKBUF_T, size_t, int, struct sockaddr *, SOCKLEN_T *)
Definition pronet.h:103
unsigned int uint32
unsigned 32 bit value
Definition pro.h:348
ptrdiff_t ssize_t
Signed size_t - used to check for size overflows when the counter becomes negative.
Definition pro.h:381
unsigned short ushort
unsigned 16 bit value
Definition pro.h:338
_qstring< char > qstring
regular string
Definition pro.h:3694
ssize_t qsendto(int socket, const SOCKBUF_T buf, size_t size, int flags, const struct sockaddr *dest_addr, SOCKLEN_T addrlen)
Definition pronet.h:50
ssize_t qsend(int socket, const void *buf, size_t size)
Definition pronet.h:62
idaman bool ida_export qgethostname(qstring *out)
idaman bool ida_export qhost2addr_(void *out, const char *name, ushort family, ushort port=0)
Get the IPv4 or IPv6 address corresponding to the given host.
ssize_t qrecv(int socket, void *buf, size_t size)
Definition pronet.h:72
ssize_t qrecvfrom(int socket, SOCKBUF_T buf, size_t size, int flags, struct sockaddr *src_addr, SOCKLEN_T *addrlen)
Definition pronet.h:56
int qpoll(pollfd *fds, uint32 nfds, int timeout_ms)
Definition pronet.h:88
int qselect(int nflds, fd_set *rds, fd_set *wds, fd_set *eds, struct timeval *timeout)
Definition pronet.h:82
bool get_my_ip(char out[NI_MAXHOST], const ushort family=AF_INET)
bool qhost2addr(struct sockaddr_in *out, const char *name, ushort port=0)
Definition pronet.h:126