2000-03-26 00:33:07 +00:00
|
|
|
/*
|
|
|
|
utils.h -- header file for utils.c
|
2009-09-24 21:42:30 +00:00
|
|
|
Copyright (C) 1999-2005 Ivo Timmermans
|
2013-08-13 18:38:57 +00:00
|
|
|
2000-2013 Guus Sliepen <guus@tinc-vpn.org>
|
2000-03-26 00:33:07 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2009-09-24 22:01:00 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-03-26 00:33:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __TINC_UTILS_H__
|
|
|
|
#define __TINC_UTILS_H__
|
|
|
|
|
2014-05-12 12:35:12 +00:00
|
|
|
extern int hex2bin(const char *src, void *dst, int length);
|
|
|
|
extern int bin2hex(const void *src, char *dst, int length);
|
2000-03-26 00:33:07 +00:00
|
|
|
|
2014-05-12 12:35:12 +00:00
|
|
|
extern int b64encode(const void *src, char *dst, int length);
|
|
|
|
extern int b64encode_urlsafe(const void *src, char *dst, int length);
|
|
|
|
extern int b64decode(const char *src, void *dst, int length);
|
2011-07-03 20:13:58 +00:00
|
|
|
|
2003-08-24 20:38:31 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2009-05-28 21:18:22 +00:00
|
|
|
extern const char *winerror(int);
|
2003-08-24 20:38:31 +00:00
|
|
|
#define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
|
2009-10-24 23:40:07 +00:00
|
|
|
#define sockerrno WSAGetLastError()
|
|
|
|
#define sockstrerror(x) winerror(x)
|
|
|
|
#define sockwouldblock(x) ((x) == WSAEWOULDBLOCK || (x) == WSAEINTR)
|
|
|
|
#define sockmsgsize(x) ((x) == WSAEMSGSIZE)
|
|
|
|
#define sockinprogress(x) ((x) == WSAEINPROGRESS || (x) == WSAEWOULDBLOCK)
|
2009-11-05 22:29:28 +00:00
|
|
|
#define sockinuse(x) ((x) == WSAEADDRINUSE)
|
Fix connection event error handling.
Commit 86a99c6b999671ed444711139db1937617e802a0 changed the way we
handle connection events to protect against spurious event loop
callbacks. Unfortunately, it turns out that calling connect() twice on
the same socket results in different behaviors depending on the platform
(even though it seems well defined in POSIX). On Windows this resulted
in the connection handling code being unable to react to connection
errors (such as connection refused), always hitting the timeout; on
Linux this resulted in spurious error messages about connect() returning
success.
In POSIX and on Linux, using connect() on a socket where the previous
attempt failed will attempt to connect again, resulting in unnecessary
network activity. Using getsockopt(SO_ERROR) before connect() solves
that, but introduces a race condition if a connection failure happens
between the two calls.
For this reason, this commit switches from connect() to a zero-sized
send() call, which is more consistent (though not completely, see the
truth table in the comments) and simpler to use for that purpose. Note
that Windows explictly support empty send() calls; POSIX says nothing
on the subject, but testing shows it works at least on Linux.
(Surprisingly enough, Windows seems more POSIX-compliant than Linux on
this one!)
2014-06-28 10:13:29 +00:00
|
|
|
#define socknotconn(x) ((x) == WSAENOTCONN)
|
2009-10-24 23:40:07 +00:00
|
|
|
#else
|
|
|
|
#define sockerrno errno
|
|
|
|
#define sockstrerror(x) strerror(x)
|
|
|
|
#define sockwouldblock(x) ((x) == EWOULDBLOCK || (x) == EINTR)
|
|
|
|
#define sockmsgsize(x) ((x) == EMSGSIZE)
|
|
|
|
#define sockinprogress(x) ((x) == EINPROGRESS)
|
2009-11-05 22:29:28 +00:00
|
|
|
#define sockinuse(x) ((x) == EADDRINUSE)
|
Fix connection event error handling.
Commit 86a99c6b999671ed444711139db1937617e802a0 changed the way we
handle connection events to protect against spurious event loop
callbacks. Unfortunately, it turns out that calling connect() twice on
the same socket results in different behaviors depending on the platform
(even though it seems well defined in POSIX). On Windows this resulted
in the connection handling code being unable to react to connection
errors (such as connection refused), always hitting the timeout; on
Linux this resulted in spurious error messages about connect() returning
success.
In POSIX and on Linux, using connect() on a socket where the previous
attempt failed will attempt to connect again, resulting in unnecessary
network activity. Using getsockopt(SO_ERROR) before connect() solves
that, but introduces a race condition if a connection failure happens
between the two calls.
For this reason, this commit switches from connect() to a zero-sized
send() call, which is more consistent (though not completely, see the
truth table in the comments) and simpler to use for that purpose. Note
that Windows explictly support empty send() calls; POSIX says nothing
on the subject, but testing shows it works at least on Linux.
(Surprisingly enough, Windows seems more POSIX-compliant than Linux on
this one!)
2014-06-28 10:13:29 +00:00
|
|
|
#define socknotconn(x) ((x) == ENOTCONN)
|
2003-08-24 20:38:31 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-28 21:36:52 +00:00
|
|
|
extern unsigned int bitfield_to_int(const void *bitfield, size_t size);
|
2009-09-09 10:04:08 +00:00
|
|
|
|
2014-08-25 04:55:42 +00:00
|
|
|
extern bool check_id(const char *);
|
2016-04-17 12:38:37 +00:00
|
|
|
extern bool check_netname(const char *, bool strict);
|
2014-08-25 02:49:27 +00:00
|
|
|
char *replace_name(const char *name);
|
|
|
|
|
2012-10-10 15:17:49 +00:00
|
|
|
#endif /* __TINC_UTILS_H__ */
|