static int
fake_getaddrinfo(const char *hostname, struct addrinfo *ai)
{
- struct hostent *he;
- he = gethostbyname(hostname);
- if (!he)
- return (-1);
- ai->ai_family = he->h_addrtype;
+ struct hostent *he = NULL;
+ struct sockaddr_in *sa;
+ if (hostname) {
+ he = gethostbyname(hostname);
+ if (!he)
+ return (-1);
+ }
+ ai->ai_family = he ? he->h_addrtype : AF_INET;
ai->ai_socktype = SOCK_STREAM;
ai->ai_protocol = 0;
- ai->ai_addrlen = he->h_length;
+ ai->ai_addrlen = sizeof(struct sockaddr_in);
if (NULL == (ai->ai_addr = malloc(ai->ai_addrlen)))
return (-1);
- memcpy(ai->ai_addr, &he->h_addr_list[0], ai->ai_addrlen);
+ sa = (struct sockaddr_in*)ai->ai_addr;
+ memset(sa, 0, ai->ai_addrlen);
+ if (he) {
+ sa->sin_family = he->h_addrtype;
+ memcpy(&sa->sin_addr, he->h_addr_list[0], he->h_length);
+ } else {
+ sa->sin_family = AF_INET;
+ sa->sin_addr.s_addr = INADDR_ANY;
+ }
ai->ai_next = NULL;
return (0);
}
strsep(char **s, const char *del)
{
char *d, *tok;
- assert(strlen(del) == 1);
+ assert(strlen(del) == 1);
if (!s || !*s)
return NULL;
tok = *s;
cur_p = gmtime(&t);
#else
gmtime_r(&t, &cur);
- cur_p = &cur;
+ cur_p = &cur;
#endif
if (strftime(date, sizeof(date),
"%a, %d %b %Y %H:%M:%S GMT", cur_p) != 0) {
event_warn("%s: bad accept", __func__);
return;
}
- if (evutil_make_socket_nonblocking(nfd) < 0)
- return;
+ if (evutil_make_socket_nonblocking(nfd) < 0)
+ return;
evhttp_get_request(http, nfd, (struct sockaddr *)&ss, addrlen);
}
struct addrinfo ai, *aitop;
int ai_result;
- memset(&ai, 0, sizeof (ai));
+ memset(&ai, 0, sizeof(ai));
ai.ai_family = AF_INET;
ai.ai_socktype = SOCK_RAW;
ai.ai_flags = 0;
static struct addrinfo *
make_addrinfo(const char *address, u_short port)
{
- struct addrinfo ai[2], *aitop = NULL;
+ struct addrinfo *aitop = NULL;
#ifdef HAVE_GETADDRINFO
+ struct addrinfo ai;
char strport[NI_MAXSERV];
int ai_result;
- memset(&ai[0], 0, sizeof (ai[0]));
- ai[0].ai_family = AF_INET;
- ai[0].ai_socktype = SOCK_STREAM;
- ai[0].ai_flags = AI_PASSIVE; /* turn NULL host name into INADDR_ANY */
- snprintf(strport, sizeof (strport), "%d", port);
- if ((ai_result = getaddrinfo(address, strport, &ai[0], &aitop)) != 0) {
+ memset(&ai, 0, sizeof(ai));
+ ai.ai_family = AF_INET;
+ ai.ai_socktype = SOCK_STREAM;
+ ai.ai_flags = AI_PASSIVE; /* turn NULL host name into INADDR_ANY */
+ snprintf(strport, sizeof(strport), "%d", port);
+ if ((ai_result = getaddrinfo(address, strport, &ai, &aitop)) != 0) {
if ( ai_result == EAI_SYSTEM )
event_warn("getaddrinfo");
else
}
#else
static int cur;
+ static struct addrinfo ai[2]; /* We will be returning the address of some of this memory so it has to last even after this call. */
if (++cur == 2) cur = 0; /* allow calling this function twice */
if (fake_getaddrinfo(address, &ai[cur]) < 0) {
return (NULL);
}
aitop = &ai[cur];
+ ((struct sockaddr_in *) aitop->ai_addr)->sin_port = htons(port);
#endif
return (aitop);
bind_socket(const char *address, u_short port)
{
int fd;
- struct addrinfo *aitop = make_addrinfo(address, port);
+ struct addrinfo *aitop = make_addrinfo(address, port);
if (aitop == NULL)
return (-1);