425
evutil_socket_geterror(evutil_socket_t sock)
427
int optval, optvallen=sizeof(optval);
428
int err = WSAGetLastError();
429
if (err == WSAEWOULDBLOCK && sock >= 0) {
430
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval,
440
/* XXX we should use an enum here. */
441
/* 2 for connection refused, 1 for connected, 0 for not yet, -1 for error. */
443
evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen)
448
if ((*fd_ptr = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
451
if (evutil_make_socket_nonblocking(*fd_ptr) < 0) {
456
if (connect(*fd_ptr, sa, socklen) < 0) {
457
int e = evutil_socket_geterror(*fd_ptr);
458
if (EVUTIL_ERR_CONNECT_RETRIABLE(e))
460
if (EVUTIL_ERR_CONNECT_REFUSED(e))
469
evutil_closesocket(*fd_ptr);
475
/* Check whether a socket on which we called connect() is done
476
connecting. Return 1 for connected, 0 for not yet, -1 for error. In the
477
error case, set the current socket errno to the error that happened during
478
the connect operation. */
480
evutil_socket_finished_connecting(evutil_socket_t fd)
483
ev_socklen_t elen = sizeof(e);
485
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&e, &elen) < 0)
489
if (EVUTIL_ERR_CONNECT_RETRIABLE(e))
491
EVUTIL_SET_SOCKET_ERROR(e);
498
#if (EVUTIL_AI_PASSIVE|EVUTIL_AI_CANONNAME|EVUTIL_AI_NUMERICHOST| \
499
EVUTIL_AI_NUMERICSERV|EVUTIL_AI_V4MAPPED|EVUTIL_AI_ALL| \
500
EVUTIL_AI_ADDRCONFIG) != \
501
(EVUTIL_AI_PASSIVE^EVUTIL_AI_CANONNAME^EVUTIL_AI_NUMERICHOST^ \
502
EVUTIL_AI_NUMERICSERV^EVUTIL_AI_V4MAPPED^EVUTIL_AI_ALL^ \
503
EVUTIL_AI_ADDRCONFIG)
504
#error "Some of our EVUTIL_AI_* flags seem to overlap with system AI_* flags"
507
/* We sometimes need to know whether we have an ipv4 address and whether we
508
have an ipv6 address. If 'have_checked_interfaces', then we've already done
509
the test. If 'had_ipv4_address', then it turns out we had an ipv4 address.
510
If 'had_ipv6_address', then it turns out we had an ipv6 address. These are
511
set by evutil_check_interfaces. */
512
static int have_checked_interfaces, had_ipv4_address, had_ipv6_address;
514
/* Macro: True iff the IPv4 address 'addr', in host order, is in 127.0.0.0/8
516
#define EVUTIL_V4ADDR_IS_LOCALHOST(addr) (((addr)>>24) == 127)
518
/* Macro: True iff the IPv4 address 'addr', in host order, is a class D
519
* (multiclass) address.
521
#define EVUTIL_V4ADDR_IS_CLASSD(addr) ((((addr)>>24) & 0xf0) == 0xe0)
523
/* Test whether we have an ipv4 interface and an ipv6 interface. Return 0 if
524
* the test seemed successful. */
526
evutil_check_interfaces(int force_recheck)
528
const char ZEROES[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
529
"\x00\x00\x00\x00\x00\x00\x00\x00";
530
evutil_socket_t fd = -1;
531
struct sockaddr_in sin, sin_out;
532
struct sockaddr_in6 sin6, sin6_out;
533
ev_socklen_t sin_out_len = sizeof(sin_out);
534
ev_socklen_t sin6_out_len = sizeof(sin6_out);
537
if (have_checked_interfaces && !force_recheck)
540
/* To check whether we have an interface open for a given protocol, we
541
* try to make a UDP 'connection' to a remote host on the internet.
542
* We don't actually use it, so the address doesn't matter, but we
543
* want to pick one that keep us from using a host- or link-local
545
memset(&sin, 0, sizeof(sin));
546
sin.sin_family = AF_INET;
547
sin.sin_port = htons(53);
548
r = evutil_inet_pton(AF_INET, "18.244.0.188", &sin.sin_addr);
551
memset(&sin6, 0, sizeof(sin6));
552
sin6.sin6_family = AF_INET6;
553
sin6.sin6_port = htons(53);
554
r = evutil_inet_pton(AF_INET6, "2001:4860:b002::68", &sin6.sin6_addr);
557
memset(&sin_out, 0, sizeof(sin_out));
558
memset(&sin6_out, 0, sizeof(sin6_out));
560
/* XXX some errnos mean 'no address'; some mean 'not enough sockets'. */
561
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0 &&
562
connect(fd, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
563
getsockname(fd, (struct sockaddr*)&sin_out, &sin_out_len) == 0) {
564
/* We might have an IPv4 interface. */
565
ev_uint32_t addr = ntohl(sin_out.sin_addr.s_addr);
567
EVUTIL_V4ADDR_IS_LOCALHOST(addr) ||
568
EVUTIL_V4ADDR_IS_CLASSD(addr)) {
569
evutil_inet_ntop(AF_INET, &sin_out.sin_addr,
571
/* This is a reserved, ipv4compat, ipv4map, loopback,
572
* link-local or unspecified address. The host should
573
* never have given it to us; it could never connect
575
event_warnx("Got a strange local ipv4 address %s",buf);
577
event_debug(("Detected an IPv4 interface"));
578
had_ipv4_address = 1;
582
evutil_closesocket(fd);
584
if ((fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) >= 0 &&
585
connect(fd, (struct sockaddr*)&sin6, sizeof(sin6)) == 0 &&
586
getsockname(fd, (struct sockaddr*)&sin6_out, &sin6_out_len) == 0) {
587
/* We might have an IPv6 interface. */
588
const unsigned char *addr =
589
(unsigned char*)sin6_out.sin6_addr.s6_addr;
590
if (!memcmp(addr, ZEROES, 8) ||
591
(addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80)) {
592
/* This is a reserved, ipv4compat, ipv4map, loopback,
593
* link-local or unspecified address. The host should
594
* never have given it to us; it could never connect
596
evutil_inet_ntop(AF_INET6, &sin6_out.sin6_addr,
598
event_warnx("Got a strange local ipv6 address %s",buf);
600
event_debug(("Detected an IPv4 interface"));
601
had_ipv6_address = 1;
606
evutil_closesocket(fd);
611
/* Internal addrinfo flag. This one is set when we allocate the addrinfo from
612
* inside libevent. Otherwise, the built-in getaddrinfo() function allocated
613
* it, and we should trust what they said.
615
#define EVUTIL_AI_LIBEVENT_ALLOCATED 0x80000000
617
/* Helper: construct a new addrinfo containing the socket address in
618
* 'sa', which must be a sockaddr_in or a sockaddr_in6. Take the
619
* socktype and protocol info from hints. If they weren't set, then
620
* allocate both a TCP and a UDP addrinfo.
622
struct evutil_addrinfo *
623
evutil_new_addrinfo(struct sockaddr *sa, ev_socklen_t socklen,
624
const struct evutil_addrinfo *hints)
626
struct evutil_addrinfo *res;
627
EVUTIL_ASSERT(hints);
629
if (hints->ai_socktype == 0 && hints->ai_protocol == 0) {
630
/* Indecisive user! Give them a UDP and a TCP. */
631
struct evutil_addrinfo *r1, *r2;
632
struct evutil_addrinfo tmp;
633
memcpy(&tmp, hints, sizeof(tmp));
634
tmp.ai_socktype = SOCK_STREAM; tmp.ai_protocol = IPPROTO_TCP;
635
r1 = evutil_new_addrinfo(sa, socklen, &tmp);
638
tmp.ai_socktype = SOCK_DGRAM; tmp.ai_protocol = IPPROTO_UDP;
639
r2 = evutil_new_addrinfo(sa, socklen, &tmp);
641
evutil_freeaddrinfo(r1);
648
/* We're going to allocate extra space to hold the sockaddr. */
649
res = mm_calloc(1,sizeof(struct evutil_addrinfo)+socklen);
652
res->ai_addr = (struct sockaddr*)
653
(((char*)res) + sizeof(struct evutil_addrinfo));
654
memcpy(res->ai_addr, sa, socklen);
655
res->ai_addrlen = socklen;
656
res->ai_family = sa->sa_family; /* Same or not? XXX */
657
res->ai_flags = EVUTIL_AI_LIBEVENT_ALLOCATED;
658
res->ai_socktype = hints->ai_socktype;
659
res->ai_protocol = hints->ai_protocol;
664
/* Append the addrinfo 'append' to the end of 'first', and return the start of
665
* the list. Either element can be NULL, in which case we return the element
666
* that is not NULL. */
667
struct evutil_addrinfo *
668
evutil_addrinfo_append(struct evutil_addrinfo *first,
669
struct evutil_addrinfo *append)
671
struct evutil_addrinfo *ai = first;
676
ai->ai_next = append;
682
parse_numeric_servname(const char *servname)
686
n = (int) strtol(servname, &endptr, 10);
687
if (n>=0 && n <= 65535 && servname[0] && endptr && !endptr[0])
693
/** Parse a service name in 'servname', which can be a decimal port.
694
* Return the port number, or -1 on error.
697
evutil_parse_servname(const char *servname, const char *protocol,
698
const struct evutil_addrinfo *hints)
700
int n = parse_numeric_servname(servname);
703
#if defined(_EVENT_HAVE_GETSERVBYNAME) || defined(WIN32)
704
if (!(hints->ai_flags & EVUTIL_AI_NUMERICSERV)) {
705
struct servent *ent = getservbyname(servname, protocol);
707
return ntohs(ent->s_port);
714
/* Return a string corresponding to a protocol number that we can pass to
717
evutil_unparse_protoname(int proto)
731
#ifdef _EVENT_HAVE_GETPROTOBYNUMBER
733
struct protoent *ent = getprotobynumber(proto);
743
evutil_getaddrinfo_infer_protocols(struct evutil_addrinfo *hints)
745
/* If we can guess the protocol from the socktype, do so. */
746
if (!hints->ai_protocol && hints->ai_socktype) {
747
if (hints->ai_socktype == SOCK_DGRAM)
748
hints->ai_protocol = IPPROTO_UDP;
749
else if (hints->ai_socktype == SOCK_STREAM)
750
hints->ai_protocol = IPPROTO_TCP;
753
/* Set the socktype if it isn't set. */
754
if (!hints->ai_socktype && hints->ai_protocol) {
755
if (hints->ai_protocol == IPPROTO_UDP)
756
hints->ai_socktype = SOCK_DGRAM;
757
else if (hints->ai_protocol == IPPROTO_TCP)
758
hints->ai_socktype = SOCK_STREAM;
760
else if (hints->ai_protocol == IPPROTO_SCTP)
761
hints->ai_socktype = SOCK_STREAM;
766
#if AF_UNSPEC != PF_UNSPEC
767
#error "I cannot build on a system where AF_UNSPEC != PF_UNSPEC"
770
/** Implements the part of looking up hosts by name that's common to both
771
* the blocking and nonblocking resolver:
772
* - Adjust 'hints' to have a reasonable socktype and protocol.
773
* - Look up the port based on 'servname', and store it in *portnum,
774
* - Handle the nodename==NULL case
775
* - Handle some invalid arguments cases.
776
* - Handle the cases where nodename is an IPv4 or IPv6 address.
778
* If we need the resolver to look up the hostname, we return
779
* EVUTIL_EAI_NEED_RESOLVE. Otherwise, we can completely implement
780
* getaddrinfo: we return 0 or an appropriate EVUTIL_EAI_* error, and
781
* set *res as getaddrinfo would.
784
evutil_getaddrinfo_common(const char *nodename, const char *servname,
785
struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum)
790
if (nodename == NULL && servname == NULL)
791
return EVUTIL_EAI_NONAME;
793
/* We only understand 3 families */
794
if (hints->ai_family != PF_UNSPEC && hints->ai_family != PF_INET &&
795
hints->ai_family != PF_INET6)
796
return EVUTIL_EAI_FAMILY;
798
evutil_getaddrinfo_infer_protocols(hints);
800
/* Look up the port number and protocol, if possible. */
801
pname = evutil_unparse_protoname(hints->ai_protocol);
803
/* XXXX We could look at the protocol we got back from
804
* getservbyname, but it doesn't seem too useful. */
805
port = evutil_parse_servname(servname, pname, hints);
807
return EVUTIL_EAI_NONAME;
811
/* If we have no node name, then we're supposed to bind to 'any' and
812
* connect to localhost. */
813
if (nodename == NULL) {
814
struct evutil_addrinfo *res4=NULL, *res6=NULL;
815
if (hints->ai_family != PF_INET) { /* INET6 or UNSPEC. */
816
struct sockaddr_in6 sin6;
817
memset(&sin6, 0, sizeof(sin6));
818
sin6.sin6_family = AF_INET6;
819
sin6.sin6_port = htons(port);
820
if (hints->ai_flags & EVUTIL_AI_PASSIVE) {
824
sin6.sin6_addr.s6_addr[15] = 1;
826
res6 = evutil_new_addrinfo((struct sockaddr*)&sin6,
827
sizeof(sin6), hints);
829
return EVUTIL_EAI_MEMORY;
832
if (hints->ai_family != PF_INET6) { /* INET or UNSPEC */
833
struct sockaddr_in sin;
834
memset(&sin, 0, sizeof(sin));
835
sin.sin_family = AF_INET;
836
sin.sin_port = htons(port);
837
if (hints->ai_flags & EVUTIL_AI_PASSIVE) {
838
/* Bind to 0.0.0.0 */
840
/* connect to 127.0.0.1 */
841
sin.sin_addr.s_addr = htonl(0x7f000001);
843
res4 = evutil_new_addrinfo((struct sockaddr*)&sin,
847
evutil_freeaddrinfo(res6);
848
return EVUTIL_EAI_MEMORY;
851
*res = evutil_addrinfo_append(res4, res6);
855
/* If we can, we should try to parse the hostname without resolving
858
if (hints->ai_family == PF_INET6 || hints->ai_family == PF_UNSPEC) {
859
struct sockaddr_in6 sin6;
860
memset(&sin6, 0, sizeof(sin6));
861
if (1==evutil_inet_pton(AF_INET6, nodename, &sin6.sin6_addr)) {
862
/* Got an ipv6 address. */
863
sin6.sin6_family = AF_INET6;
864
sin6.sin6_port = htons(port);
865
*res = evutil_new_addrinfo((struct sockaddr*)&sin6,
866
sizeof(sin6), hints);
868
return EVUTIL_EAI_MEMORY;
874
if (hints->ai_family == PF_INET || hints->ai_family == PF_UNSPEC) {
875
struct sockaddr_in sin;
876
memset(&sin, 0, sizeof(sin));
877
if (1==evutil_inet_pton(AF_INET, nodename, &sin.sin_addr)) {
878
/* Got an ipv6 address. */
879
sin.sin_family = AF_INET;
880
sin.sin_port = htons(port);
881
*res = evutil_new_addrinfo((struct sockaddr*)&sin,
884
return EVUTIL_EAI_MEMORY;
890
/* If we have reached this point, we definitely need to do a DNS
892
if ((hints->ai_flags & EVUTIL_AI_NUMERICHOST)) {
893
/* If we're not allowed to do one, then say so. */
894
return EVUTIL_EAI_NONAME;
897
return EVUTIL_EAI_NEED_RESOLVE;
900
#ifdef _EVENT_HAVE_GETADDRINFO
901
#define USE_NATIVE_GETADDRINFO
904
#ifdef USE_NATIVE_GETADDRINFO
905
/* A mask of all the flags that we declare, so we can clear them before calling
906
* the native getaddrinfo */
907
static const unsigned int ALL_NONNATIVE_AI_FLAGS =
912
EVUTIL_AI_CANONNAME |
914
#ifndef AI_NUMERICHOST
915
EVUTIL_AI_NUMERICHOST |
917
#ifndef AI_NUMERICSERV
918
EVUTIL_AI_NUMERICSERV |
920
#ifndef AI_ADDRCONFIG
921
EVUTIL_AI_ADDRCONFIG |
929
EVUTIL_AI_LIBEVENT_ALLOCATED;
931
static const unsigned int ALL_NATIVE_AI_FLAGS =
938
#ifdef AI_NUMERICHOST
941
#ifdef AI_NUMERICSERV
956
#ifndef USE_NATIVE_GETADDRINFO
957
/* Helper for systems with no getaddrinfo(): make one or more addrinfos out of
960
static struct evutil_addrinfo *
961
addrinfo_from_hostent(const struct hostent *ent,
962
int port, const struct evutil_addrinfo *hints)
965
struct sockaddr_in sin;
966
struct sockaddr_in6 sin6;
969
struct evutil_addrinfo *res=NULL, *ai;
972
if (ent->h_addrtype == PF_INET) {
973
memset(&sin, 0, sizeof(sin));
974
sin.sin_family = AF_INET;
975
sin.sin_port = htons(port);
976
sa = (struct sockaddr *)&sin;
977
socklen = sizeof(struct sockaddr_in);
978
addrp = &sin.sin_addr;
979
if (ent->h_length != sizeof(sin.sin_addr)) {
980
event_warnx("Weird h_length from gethostbyname");
983
} else if (ent->h_addrtype == PF_INET6) {
984
memset(&sin6, 0, sizeof(sin6));
985
sin6.sin6_family = AF_INET6;
986
sin6.sin6_port = htons(port);
987
sa = (struct sockaddr *)&sin6;
988
socklen = sizeof(struct sockaddr_in);
989
addrp = &sin6.sin6_addr;
990
if (ent->h_length != sizeof(sin6.sin6_addr)) {
991
event_warnx("Weird h_length from gethostbyname");
997
for (i = 0; ent->h_addr_list[i]; ++i) {
998
memcpy(addrp, ent->h_addr_list[i], ent->h_length);
999
ai = evutil_new_addrinfo(sa, socklen, hints);
1001
evutil_freeaddrinfo(res);
1004
res = evutil_addrinfo_append(res, ai);
1007
if (res && ((hints->ai_flags & EVUTIL_AI_CANONNAME) && ent->h_name)) {
1008
res->ai_canonname = mm_strdup(ent->h_name);
1009
if (res->ai_canonname == NULL) {
1010
evutil_freeaddrinfo(res);
1019
/* If the EVUTIL_AI_ADDRCONFIG flag is set on hints->ai_flags, and
1020
* hints->ai_family is PF_UNSPEC, then revise the value of hints->ai_family so
1021
* that we'll only get addresses we could maybe connect to.
1024
evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints)
1026
if (!(hints->ai_flags & EVUTIL_AI_ADDRCONFIG))
1028
if (hints->ai_family != PF_UNSPEC)
1030
if (!have_checked_interfaces)
1031
evutil_check_interfaces(0);
1032
if (had_ipv4_address && !had_ipv6_address) {
1033
hints->ai_family = PF_INET;
1034
} else if (!had_ipv4_address && had_ipv6_address) {
1035
hints->ai_family = PF_INET6;
1039
#ifdef USE_NATIVE_GETADDRINFO
1040
static int need_numeric_port_hack_=0;
1041
static int need_socktype_protocol_hack_=0;
1042
static int tested_for_getaddrinfo_hacks=0;
1044
/* Some older BSDs (like OpenBSD up to 4.6) used to believe that
1045
giving a numeric port without giving an ai_socktype was verboten.
1046
We test for this so we can apply an appropriate workaround. If it
1047
turns out that the bug is present, then:
1049
- If nodename==NULL and servname is numeric, we build an answer
1050
ourselves using evutil_getaddrinfo_common().
1052
- If nodename!=NULL and servname is numeric, then we set
1053
servname=NULL when calling getaddrinfo, and post-process the
1054
result to set the ports on it.
1056
We test for this bug at runtime, since otherwise we can't have the
1057
same binary run on multiple BSD versions.
1059
- Some versions of Solaris believe that it's nice to leave to protocol
1060
field set to 0. We test for this so we can apply an appropriate
1064
test_for_getaddrinfo_hacks(void)
1067
struct evutil_addrinfo *ai=NULL, *ai2=NULL;
1068
struct evutil_addrinfo hints;
1070
memset(&hints,0,sizeof(hints));
1071
hints.ai_family = PF_UNSPEC;
1073
#ifdef AI_NUMERICHOST
1076
#ifdef AI_NUMERICSERV
1080
r = getaddrinfo("1.2.3.4", "80", &hints, &ai);
1081
hints.ai_socktype = SOCK_STREAM;
1082
r2 = getaddrinfo("1.2.3.4", "80", &hints, &ai2);
1083
if (r2 == 0 && r != 0) {
1084
need_numeric_port_hack_=1;
1086
if (ai2 && ai2->ai_protocol == 0) {
1087
need_socktype_protocol_hack_=1;
1094
tested_for_getaddrinfo_hacks=1;
1098
need_numeric_port_hack(void)
1100
if (!tested_for_getaddrinfo_hacks)
1101
test_for_getaddrinfo_hacks();
1102
return need_numeric_port_hack_;
1106
need_socktype_protocol_hack(void)
1108
if (!tested_for_getaddrinfo_hacks)
1109
test_for_getaddrinfo_hacks();
1110
return need_socktype_protocol_hack_;
1114
apply_numeric_port_hack(int port, struct evutil_addrinfo **ai)
1116
/* Now we run through the list and set the ports on all of the
1117
* results where ports would make sense. */
1118
for ( ; *ai; ai = &(*ai)->ai_next) {
1119
struct sockaddr *sa = (*ai)->ai_addr;
1120
if (sa && sa->sa_family == AF_INET) {
1121
struct sockaddr_in *sin = (struct sockaddr_in*)sa;
1122
sin->sin_port = htons(port);
1123
} else if (sa && sa->sa_family == AF_INET6) {
1124
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
1125
sin6->sin6_port = htons(port);
1127
/* A numeric port makes no sense here; remove this one
1129
struct evutil_addrinfo *victim = *ai;
1130
*ai = victim->ai_next;
1131
victim->ai_next = NULL;
1132
freeaddrinfo(victim);
1138
apply_socktype_protocol_hack(struct evutil_addrinfo *ai)
1140
struct evutil_addrinfo *ai_new;
1141
for (; ai; ai = ai->ai_next) {
1142
evutil_getaddrinfo_infer_protocols(ai);
1143
if (ai->ai_socktype || ai->ai_protocol)
1145
ai_new = mm_malloc(sizeof(*ai_new));
1148
memcpy(ai_new, ai, sizeof(*ai_new));
1149
ai->ai_socktype = SOCK_STREAM;
1150
ai->ai_protocol = IPPROTO_TCP;
1151
ai_new->ai_socktype = SOCK_DGRAM;
1152
ai_new->ai_protocol = IPPROTO_UDP;
1154
ai_new->ai_next = ai->ai_next;
1155
ai->ai_next = ai_new;
1162
evutil_getaddrinfo(const char *nodename, const char *servname,
1163
const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res)
1165
#ifdef USE_NATIVE_GETADDRINFO
1166
struct evutil_addrinfo hints;
1167
int portnum=-1, need_np_hack, err;
1170
memcpy(&hints, hints_in, sizeof(hints));
1172
memset(&hints, 0, sizeof(hints));
1173
hints.ai_family = PF_UNSPEC;
1176
#ifndef AI_ADDRCONFIG
1177
/* Not every system has AI_ADDRCONFIG, so fake it. */
1178
if (hints.ai_family == PF_UNSPEC &&
1179
(hints.ai_flags & EVUTIL_AI_ADDRCONFIG)) {
1180
evutil_adjust_hints_for_addrconfig(&hints);
1184
#ifndef AI_NUMERICSERV
1185
/* Not every system has AI_NUMERICSERV, so fake it. */
1186
if (hints.ai_flags & EVUTIL_AI_NUMERICSERV) {
1187
if (servname && parse_numeric_servname(servname)<0)
1188
return EVUTIL_EAI_NONAME;
1192
/* Enough operating systems handle enough common non-resolve
1193
* cases here weirdly enough that we are better off just
1194
* overriding them. For example:
1196
* - Windows doesn't like to infer the protocol from the
1197
* socket type, or fill in socket or protocol types much at
1198
* all. It also seems to do its own broken implicit
1199
* always-on version of AI_ADDRCONFIG that keeps it from
1200
* ever resolving even a literal IPv6 address when
1201
* ai_addrtype is PF_UNSPEC.
1206
err = evutil_getaddrinfo_common(nodename,servname,&hints,
1209
err == EVUTIL_EAI_MEMORY ||
1210
err == EVUTIL_EAI_NONAME)
1212
/* If we make it here, the system getaddrinfo can
1213
* have a crack at it. */
1217
/* See documentation for need_numeric_port_hack above.*/
1218
need_np_hack = need_numeric_port_hack() && servname && !hints.ai_socktype
1219
&& ((portnum=parse_numeric_servname(servname)) >= 0);
1222
return evutil_getaddrinfo_common(
1223
NULL,servname,&hints, res, &portnum);
1227
if (need_socktype_protocol_hack()) {
1228
evutil_getaddrinfo_infer_protocols(&hints);
1231
/* Make sure that we didn't actually steal any AI_FLAGS values that
1232
* the system is using. (This is a constant expression, and should ge
1235
* XXXX Turn this into a compile-time failure rather than a run-time
1238
EVUTIL_ASSERT((ALL_NONNATIVE_AI_FLAGS & ALL_NATIVE_AI_FLAGS) == 0);
1240
/* Clear any flags that only libevent understands. */
1241
hints.ai_flags &= ~ALL_NONNATIVE_AI_FLAGS;
1243
err = getaddrinfo(nodename, servname, &hints, res);
1245
apply_numeric_port_hack(portnum, res);
1247
if (need_socktype_protocol_hack()) {
1248
if (apply_socktype_protocol_hack(*res) < 0) {
1249
evutil_freeaddrinfo(*res);
1251
return EVUTIL_EAI_MEMORY;
1257
struct hostent *ent = NULL;
1258
struct evutil_addrinfo hints;
1261
memcpy(&hints, hints_in, sizeof(hints));
1263
memset(&hints, 0, sizeof(hints));
1264
hints.ai_family = PF_UNSPEC;
1267
evutil_adjust_hints_for_addrconfig(&hints);
1269
err = evutil_getaddrinfo_common(nodename, servname, &hints, res, &port);
1270
if (err != EVUTIL_EAI_NEED_RESOLVE) {
1271
/* We either succeeded or failed. No need to continue */
1276
/* Use any of the various gethostbyname_r variants as available. */
1278
#ifdef _EVENT_HAVE_GETHOSTBYNAME_R_6_ARG
1279
/* This one is what glibc provides. */
1281
struct hostent hostent;
1283
r = gethostbyname_r(nodename, &hostent, buf, sizeof(buf), &ent,
1285
#elif defined(_EVENT_HAVE_GETHOSTBYNAME_R_5_ARG)
1287
struct hostent hostent;
1288
ent = gethostbyname_r(nodename, &hostent, buf, sizeof(buf),
1290
#elif defined(_EVENT_HAVE_GETHOSTBYNAME_R_3_ARG)
1291
struct hostent_data data;
1292
struct hostent hostent;
1293
memset(&data, 0, sizeof(data));
1294
err = gethostbyname_r(nodename, &hostent, &data);
1295
ent = err ? NULL : &hostent;
1297
/* fall back to gethostbyname. */
1298
/* XXXX This needs a lock everywhere but Windows. */
1299
ent = gethostbyname(nodename);
1301
err = WSAGetLastError();
1307
/* Now we have either ent or err set. */
1309
/* XXX is this right for windows ? */
1312
return EVUTIL_EAI_AGAIN;
1315
return EVUTIL_EAI_FAIL;
1316
case HOST_NOT_FOUND:
1317
return EVUTIL_EAI_NONAME;
1319
#if NO_DATA != NO_ADDRESS
1322
return EVUTIL_EAI_NODATA;
1326
if (ent->h_addrtype != hints.ai_family &&
1327
hints.ai_family != PF_UNSPEC) {
1328
/* This wasn't the type we were hoping for. Too bad
1329
* we never had a chance to ask gethostbyname for what
1331
return EVUTIL_EAI_NONAME;
1334
/* Make sure we got _some_ answers. */
1335
if (ent->h_length == 0)
1336
return EVUTIL_EAI_NODATA;
1338
/* If we got an address type we don't know how to make a
1339
sockaddr for, give up. */
1340
if (ent->h_addrtype != PF_INET && ent->h_addrtype != PF_INET6)
1341
return EVUTIL_EAI_FAMILY;
1343
*res = addrinfo_from_hostent(ent, port, &hints);
1345
return EVUTIL_EAI_MEMORY;
1353
evutil_freeaddrinfo(struct evutil_addrinfo *ai)
1355
#ifdef _EVENT_HAVE_GETADDRINFO
1356
if (!(ai->ai_flags & EVUTIL_AI_LIBEVENT_ALLOCATED)) {
1362
struct evutil_addrinfo *next = ai->ai_next;
1363
if (ai->ai_canonname)
1364
mm_free(ai->ai_canonname);
1370
static evdns_getaddrinfo_fn evdns_getaddrinfo_impl = NULL;
1373
evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn)
1375
if (!evdns_getaddrinfo_impl)
1376
evdns_getaddrinfo_impl = fn;
1379
/* Internal helper function: act like evdns_getaddrinfo if dns_base is set;
1380
* otherwise do a blocking resolve and pass the result to the callback in the
1381
* way that evdns_getaddrinfo would.
1384
evutil_getaddrinfo_async(struct evdns_base *dns_base,
1385
const char *nodename, const char *servname,
1386
const struct evutil_addrinfo *hints_in,
1387
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg)
1389
if (dns_base && evdns_getaddrinfo_impl) {
1390
evdns_getaddrinfo_impl(
1391
dns_base, nodename, servname, hints_in, cb, arg);
1393
struct evutil_addrinfo *ai=NULL;
1395
err = evutil_getaddrinfo(nodename, servname, hints_in, &ai);
1402
evutil_gai_strerror(int err)
1404
/* As a sneaky side-benefit, this case statement will get most
1405
* compilers to tell us if any of the error codes we defined
1406
* conflict with the platform's native error codes. */
1408
case EVUTIL_EAI_CANCEL:
1409
return "Request canceled";
1413
case EVUTIL_EAI_ADDRFAMILY:
1414
return "address family for nodename not supported";
1415
case EVUTIL_EAI_AGAIN:
1416
return "temporary failure in name resolution";
1417
case EVUTIL_EAI_BADFLAGS:
1418
return "invalid value for ai_flags";
1419
case EVUTIL_EAI_FAIL:
1420
return "non-recoverable failure in name resolution";
1421
case EVUTIL_EAI_FAMILY:
1422
return "ai_family not supported";
1423
case EVUTIL_EAI_MEMORY:
1424
return "memory allocation failure";
1425
case EVUTIL_EAI_NODATA:
1426
return "no address associated with nodename";
1427
case EVUTIL_EAI_NONAME:
1428
return "nodename nor servname provided, or not known";
1429
case EVUTIL_EAI_SERVICE:
1430
return "servname not supported for ai_socktype";
1431
case EVUTIL_EAI_SOCKTYPE:
1432
return "ai_socktype not supported";
1433
case EVUTIL_EAI_SYSTEM:
1434
return "system error";
1436
#if defined(USE_NATIVE_GETADDRINFO) && defined(WIN32)
1437
return gai_strerrorA(err);
1438
#elif defined(USE_NATIVE_GETADDRINFO)
1439
return gai_strerror(err);
1441
return "Unknown error code";
1447
#define E(code, s) { code, (s " [" #code " ]") }
1448
static struct { int code; const char *msg; } windows_socket_errors[] = {
1449
E(WSAEINTR, "Interrupted function call"),
1450
E(WSAEACCES, "Permission denied"),
1451
E(WSAEFAULT, "Bad address"),
1452
E(WSAEINVAL, "Invalid argument"),
1453
E(WSAEMFILE, "Too many open files"),
1454
E(WSAEWOULDBLOCK, "Resource temporarily unavailable"),
1455
E(WSAEINPROGRESS, "Operation now in progress"),
1456
E(WSAEALREADY, "Operation already in progress"),
1457
E(WSAENOTSOCK, "Socket operation on nonsocket"),
1458
E(WSAEDESTADDRREQ, "Destination address required"),
1459
E(WSAEMSGSIZE, "Message too long"),
1460
E(WSAEPROTOTYPE, "Protocol wrong for socket"),
1461
E(WSAENOPROTOOPT, "Bad protocol option"),
1462
E(WSAEPROTONOSUPPORT, "Protocol not supported"),
1463
E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
1464
/* What's the difference between NOTSUPP and NOSUPPORT? :) */
1465
E(WSAEOPNOTSUPP, "Operation not supported"),
1466
E(WSAEPFNOSUPPORT, "Protocol family not supported"),
1467
E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
1468
E(WSAEADDRINUSE, "Address already in use"),
1469
E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
1470
E(WSAENETDOWN, "Network is down"),
1471
E(WSAENETUNREACH, "Network is unreachable"),
1472
E(WSAENETRESET, "Network dropped connection on reset"),
1473
E(WSAECONNABORTED, "Software caused connection abort"),
1474
E(WSAECONNRESET, "Connection reset by peer"),
1475
E(WSAENOBUFS, "No buffer space available"),
1476
E(WSAEISCONN, "Socket is already connected"),
1477
E(WSAENOTCONN, "Socket is not connected"),
1478
E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
1479
E(WSAETIMEDOUT, "Connection timed out"),
1480
E(WSAECONNREFUSED, "Connection refused"),
1481
E(WSAEHOSTDOWN, "Host is down"),
1482
E(WSAEHOSTUNREACH, "No route to host"),
1483
E(WSAEPROCLIM, "Too many processes"),
1485
/* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
1486
E(WSASYSNOTREADY, "Network subsystem is unavailable"),
1487
E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
1488
E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
1489
E(WSAEDISCON, "Graceful shutdown now in progress"),
1490
#ifdef WSATYPE_NOT_FOUND
1491
E(WSATYPE_NOT_FOUND, "Class type not found"),
1493
E(WSAHOST_NOT_FOUND, "Host not found"),
1494
E(WSATRY_AGAIN, "Nonauthoritative host not found"),
1495
E(WSANO_RECOVERY, "This is a nonrecoverable error"),
1496
E(WSANO_DATA, "Valid name, no data record of requested type)"),
1498
/* There are some more error codes whose numeric values are marked
1499
* <b>OS dependent</b>. They start with WSA_, apparently for the same
1500
* reason that practitioners of some craft traditions deliberately
1501
* introduce imperfections into their baskets and rugs "to allow the
1502
* evil spirits to escape." If we catch them, then our binaries
1503
* might not report consistent results across versions of Windows.
1504
* Thus, I'm going to let them all fall through.
1509
/** Equivalent to strerror, but for windows socket errors. */
1511
evutil_socket_error_to_string(int errcode)
1513
/* XXXX Is there really no built-in function to do this? */
1515
for (i=0; windows_socket_errors[i].code >= 0; ++i) {
1516
if (errcode == windows_socket_errors[i].code)
1517
return windows_socket_errors[i].msg;
1519
return strerror(errcode);
231
1524
evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
242
1535
evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
245
int r = _vsnprintf(buf, buflen, format, ap);
1541
r = _vsnprintf(buf, buflen, format, ap);
1543
r = _vscprintf(format, ap);
1545
/* Make sure we always use the correct vsnprintf on IRIX */
1546
extern int _xpg5_vsnprintf(char * __restrict,
1547
__SGI_LIBC_NAMESPACE_QUALIFIER size_t,
1548
const char * __restrict, /* va_list */ char *);
1550
r = _xpg5_vsnprintf(buf, buflen, format, ap);
1552
r = vsnprintf(buf, buflen, format, ap);
246
1554
buf[buflen-1] = '\0';
1558
#define USE_INTERNAL_NTOP
1559
#define USE_INTERNAL_PTON
1562
evutil_inet_ntop(int af, const void *src, char *dst, size_t len)
1564
#if defined(_EVENT_HAVE_INET_NTOP) && !defined(USE_INTERNAL_NTOP)
1565
return inet_ntop(af, src, dst, len);
1567
if (af == AF_INET) {
1568
const struct in_addr *in = src;
1569
const ev_uint32_t a = ntohl(in->s_addr);
1571
r = evutil_snprintf(dst, len, "%d.%d.%d.%d",
1572
(int)(ev_uint8_t)((a>>24)&0xff),
1573
(int)(ev_uint8_t)((a>>16)&0xff),
1574
(int)(ev_uint8_t)((a>>8 )&0xff),
1575
(int)(ev_uint8_t)((a )&0xff));
1576
if (r<0||(size_t)r>=len)
1581
} else if (af == AF_INET6) {
1582
const struct in6_addr *addr = src;
1584
int longestGapLen = 0, longestGapPos = -1, i,
1585
curGapPos = -1, curGapLen = 0;
1586
ev_uint16_t words[8];
1587
for (i = 0; i < 8; ++i) {
1589
(((ev_uint16_t)addr->s6_addr[2*i])<<8) + addr->s6_addr[2*i+1];
1591
if (words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 &&
1592
words[4] == 0 && ((words[5] == 0 && words[6] && words[7]) ||
1593
(words[5] == 0xffff))) {
1594
/* This is an IPv4 address. */
1595
if (words[5] == 0) {
1596
evutil_snprintf(buf, sizeof(buf), "::%d.%d.%d.%d",
1597
addr->s6_addr[12], addr->s6_addr[13],
1598
addr->s6_addr[14], addr->s6_addr[15]);
1600
evutil_snprintf(buf, sizeof(buf), "::%x:%d.%d.%d.%d", words[5],
1601
addr->s6_addr[12], addr->s6_addr[13],
1602
addr->s6_addr[14], addr->s6_addr[15]);
1604
if (strlen(buf) > len)
1606
strlcpy(dst, buf, len);
1611
if (words[i] == 0) {
1614
while (i<8 && words[i] == 0) {
1617
if (curGapLen > longestGapLen) {
1618
longestGapPos = curGapPos;
1619
longestGapLen = curGapLen;
1625
if (longestGapLen<=1)
1629
for (i = 0; i < 8; ++i) {
1630
if (words[i] == 0 && longestGapPos == i) {
1634
while (i < 8 && words[i] == 0)
1636
--i; /* to compensate for loop increment. */
1639
sizeof(buf)-(cp-buf), "%x", (unsigned)words[i]);
1646
if (strlen(buf) > len)
1648
strlcpy(dst, buf, len);
1658
evutil_inet_pton(int af, const char *src, void *dst)
1660
#if defined(_EVENT_HAVE_INET_PTON) && !defined(USE_INTERNAL_PTON)
1661
return inet_pton(af, src, dst);
1663
if (af == AF_INET) {
1666
struct in_addr *addr = dst;
1667
if (sscanf(src, "%d.%d.%d.%d%c", &a,&b,&c,&d,&more) != 4)
1669
if (a < 0 || a > 255) return 0;
1670
if (b < 0 || b > 255) return 0;
1671
if (c < 0 || c > 255) return 0;
1672
if (d < 0 || d > 255) return 0;
1673
addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d);
1676
} else if (af == AF_INET6) {
1677
struct in6_addr *out = dst;
1678
ev_uint16_t words[8];
1679
int gapPos = -1, i, setWords=0;
1680
const char *dot = strchr(src, '.');
1681
const char *eow; /* end of words. */
1685
eow = src+strlen(src);
1687
int byte1,byte2,byte3,byte4;
1689
for (eow = dot-1; eow >= src && EVUTIL_ISDIGIT(*eow); --eow)
1693
/* We use "scanf" because some platform inet_aton()s are too lax
1694
* about IPv4 addresses of the form "1.2.3" */
1695
if (sscanf(eow, "%d.%d.%d.%d%c",
1696
&byte1,&byte2,&byte3,&byte4,&more) != 4)
1699
if (byte1 > 255 || byte1 < 0 ||
1700
byte2 > 255 || byte2 < 0 ||
1701
byte3 > 255 || byte3 < 0 ||
1702
byte4 > 255 || byte4 < 0)
1705
words[6] = (byte1<<8) | byte2;
1706
words[7] = (byte3<<8) | byte4;
1714
if (EVUTIL_ISXDIGIT(*src)) {
1716
long r = strtol(src, &next, 16);
1724
words[i++] = (ev_uint16_t)r;
1727
if (*src != ':' && src != eow)
1730
} else if (*src == ':' && i > 0 && gapPos==-1) {
1733
} else if (*src == ':' && i == 0 && src[1] == ':' && gapPos==-1) {
1742
(setWords == 8 && gapPos != -1) ||
1743
(setWords < 8 && gapPos == -1))
1747
int nToMove = setWords - (dot ? 2 : 0) - gapPos;
1748
int gapLen = 8 - setWords;
1749
/* assert(nToMove >= 0); */
1751
return -1; /* should be impossible */
1752
memmove(&words[gapPos+gapLen], &words[gapPos],
1753
sizeof(ev_uint16_t)*nToMove);
1754
memset(&words[gapPos], 0, sizeof(ev_uint16_t)*gapLen);
1756
for (i = 0; i < 8; ++i) {
1757
out->s6_addr[2*i ] = words[i] >> 8;
1758
out->s6_addr[2*i+1] = words[i] & 0xff;
1770
evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *outlen)
1774
const char *cp, *addr_part, *port_part;
1776
/* recognized formats are:
1784
cp = strchr(ip_as_string, ':');
1785
if (*ip_as_string == '[') {
1787
if (!(cp = strchr(ip_as_string, ']'))) {
1790
len = (int) ( cp-(ip_as_string + 1) );
1791
if (len > (int)sizeof(buf)-1) {
1794
memcpy(buf, ip_as_string+1, len);
1802
} else if (cp && strchr(cp+1, ':')) {
1804
addr_part = ip_as_string;
1808
if (cp - ip_as_string > (int)sizeof(buf)-1) {
1811
memcpy(buf, ip_as_string, cp-ip_as_string);
1812
buf[cp-ip_as_string] = '\0';
1816
addr_part = ip_as_string;
1821
if (port_part == NULL) {
1824
port = atoi(port_part);
1825
if (port <= 0 || port > 65535) {
1831
return -1; /* Should be impossible. */
1835
struct sockaddr_in6 sin6;
1836
memset(&sin6, 0, sizeof(sin6));
1837
#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
1838
sin6.sin6_len = sizeof(sin6);
1840
sin6.sin6_family = AF_INET6;
1841
sin6.sin6_port = htons(port);
1842
if (1 != evutil_inet_pton(AF_INET6, addr_part, &sin6.sin6_addr))
1844
if ((int)sizeof(sin6) > *outlen)
1846
memset(out, 0, *outlen);
1847
memcpy(out, &sin6, sizeof(sin6));
1848
*outlen = sizeof(sin6);
1854
struct sockaddr_in sin;
1855
memset(&sin, 0, sizeof(sin));
1856
#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1857
sin.sin_len = sizeof(sin);
1859
sin.sin_family = AF_INET;
1860
sin.sin_port = htons(port);
1861
if (1 != evutil_inet_pton(AF_INET, addr_part, &sin.sin_addr))
1863
if ((int)sizeof(sin) > *outlen)
1865
memset(out, 0, *outlen);
1866
memcpy(out, &sin, sizeof(sin));
1867
*outlen = sizeof(sin);
1873
evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen)
1876
const char *res=NULL;
1878
if (sa->sa_family == AF_INET) {
1879
const struct sockaddr_in *sin = (const struct sockaddr_in*)sa;
1880
res = evutil_inet_ntop(AF_INET, &sin->sin_addr,b,sizeof(b));
1881
port = ntohs(sin->sin_port);
1883
evutil_snprintf(out, outlen, "%s:%d", b, port);
1886
} else if (sa->sa_family == AF_INET6) {
1887
const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6*)sa;
1888
res = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr,b,sizeof(b));
1889
port = ntohs(sin6->sin6_port);
1891
evutil_snprintf(out, outlen, "[%s]:%d", b, port);
1896
evutil_snprintf(out, outlen, "<addr with socktype %d>",
1897
(int)sa->sa_family);
1902
evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
1906
if (0 != (r = (sa1->sa_family - sa2->sa_family)))
250
return _vscprintf(format, ap);
252
int r = vsnprintf(buf, buflen, format, ap);
253
buf[buflen-1] = '\0';
1909
if (sa1->sa_family == AF_INET) {
1910
const struct sockaddr_in *sin1, *sin2;
1911
sin1 = (const struct sockaddr_in *)sa1;
1912
sin2 = (const struct sockaddr_in *)sa2;
1913
if (sin1->sin_addr.s_addr < sin2->sin_addr.s_addr)
1915
else if (sin1->sin_addr.s_addr > sin2->sin_addr.s_addr)
1917
else if (include_port &&
1918
(r = ((int)sin1->sin_port - (int)sin2->sin_port)))
1924
else if (sa1->sa_family == AF_INET6) {
1925
const struct sockaddr_in6 *sin1, *sin2;
1926
sin1 = (const struct sockaddr_in6 *)sa1;
1927
sin2 = (const struct sockaddr_in6 *)sa2;
1928
if ((r = memcmp(sin1->sin6_addr.s6_addr, sin2->sin6_addr.s6_addr, 16)))
1930
else if (include_port &&
1931
(r = ((int)sin1->sin6_port - (int)sin2->sin6_port)))
1940
/* Tables to implement ctypes-replacement EVUTIL_IS*() functions. Each table
1941
* has 256 bits to look up whether a character is in some set or not. This
1942
* fails on non-ASCII platforms, but so does every other place where we
1943
* take a char and write it onto the network.
1945
static const ev_uint32_t EVUTIL_ISALPHA_TABLE[8] =
1946
{ 0, 0, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 };
1947
static const ev_uint32_t EVUTIL_ISALNUM_TABLE[8] =
1948
{ 0, 0x3ff0000, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 };
1949
static const ev_uint32_t EVUTIL_ISSPACE_TABLE[8] = { 0x3e00, 0x1, 0, 0, 0, 0, 0, 0 };
1950
static const ev_uint32_t EVUTIL_ISXDIGIT_TABLE[8] =
1951
{ 0, 0x3ff0000, 0x7e, 0x7e, 0, 0, 0, 0 };
1952
static const ev_uint32_t EVUTIL_ISDIGIT_TABLE[8] = { 0, 0x3ff0000, 0, 0, 0, 0, 0, 0 };
1953
static const ev_uint32_t EVUTIL_ISPRINT_TABLE[8] =
1954
{ 0, 0xffffffff, 0xffffffff, 0x7fffffff, 0, 0, 0, 0x0 };
1955
static const ev_uint32_t EVUTIL_ISUPPER_TABLE[8] = { 0, 0, 0x7fffffe, 0, 0, 0, 0, 0 };
1956
static const ev_uint32_t EVUTIL_ISLOWER_TABLE[8] = { 0, 0, 0, 0x7fffffe, 0, 0, 0, 0 };
1957
/* Upper-casing and lowercasing tables to map characters to upper/lowercase
1959
static const unsigned char EVUTIL_TOUPPER_TABLE[256] = {
1960
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
1961
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
1962
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
1963
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
1964
64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
1965
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
1966
96,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
1967
80,81,82,83,84,85,86,87,88,89,90,123,124,125,126,127,
1968
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
1969
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
1970
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
1971
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
1972
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
1973
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
1974
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
1975
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
1977
static const unsigned char EVUTIL_TOLOWER_TABLE[256] = {
1978
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
1979
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
1980
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
1981
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
1982
64,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
1983
112,113,114,115,116,117,118,119,120,121,122,91,92,93,94,95,
1984
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
1985
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
1986
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
1987
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
1988
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
1989
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
1990
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
1991
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
1992
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
1993
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
1996
#define IMPL_CTYPE_FN(name) \
1997
int EVUTIL_##name(char c) { \
1999
return !!(EVUTIL_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
2001
IMPL_CTYPE_FN(ISALPHA)
2002
IMPL_CTYPE_FN(ISALNUM)
2003
IMPL_CTYPE_FN(ISSPACE)
2004
IMPL_CTYPE_FN(ISDIGIT)
2005
IMPL_CTYPE_FN(ISXDIGIT)
2006
IMPL_CTYPE_FN(ISPRINT)
2007
IMPL_CTYPE_FN(ISLOWER)
2008
IMPL_CTYPE_FN(ISUPPER)
2010
char EVUTIL_TOLOWER(char c)
2012
return ((char)EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c]);
2014
char EVUTIL_TOUPPER(char c)
2016
return ((char)EVUTIL_TOUPPER_TABLE[(ev_uint8_t)c]);
2019
evutil_ascii_strcasecmp(const char *s1, const char *s2)
2023
c1 = EVUTIL_TOLOWER(*s1++);
2024
c2 = EVUTIL_TOLOWER(*s2++);
2033
int evutil_ascii_strncasecmp(const char *s1, const char *s2, size_t n)
2037
c1 = EVUTIL_TOLOWER(*s1++);
2038
c2 = EVUTIL_TOLOWER(*s2++);