2 MiddleMan filtering proxy server
3 Copyright (C) 2002 Jason McLaughlin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <netinet/in.h>
28 #include <sys/types.h>
32 extern char **environ;
34 #ifdef HAVE_GETHOSTBYNAME_R
37 wrapper for reentrant gethostbyname function
39 HOSTENT *p_gethostbyname(char *host)
44 struct hostent h, *hp;
46 ret = gethostbyname_r(host, &h, buf, sizeof(buf), &hp, &err);
50 hostent = xmalloc(sizeof(HOSTENT));
51 hostent->addr = xmalloc(sizeof(struct in_addr));
52 memcpy(hostent->addr, h.h_addr_list[0], sizeof(struct in_addr));
53 hostent->len = sizeof(struct in_addr);
60 pthread_mutex_t gethostbyname_mutex = PTHREAD_MUTEX_INITIALIZER;
63 stupid work-around for systems without gethostbyname_r
65 HOSTENT *p_gethostbyname(char *host)
70 pthread_mutex_lock(&gethostbyname_mutex);
72 h = gethostbyname(host);
74 pthread_mutex_unlock(&gethostbyname_mutex);
79 hostent = xmalloc(sizeof(HOSTENT));
80 hostent->addr = xmalloc(h->h_length);
81 memcpy(hostent->addr, h->h_addr_list[0], h->h_length);
82 hostent->len = h->h_length;
84 pthread_mutex_unlock(&gethostbyname_mutex);
89 #endif /* HAVE_GETHOSTBYNAME_R */
94 wrapper for poll() for systems that actually implement this
96 int p_poll(struct pollfd *pollfd, int fds, int timeout)
98 return poll(pollfd, fds, timeout);
104 basic poll() wrapper using select for system's lacking that feature
106 int p_poll(struct pollfd *pfd, int fds, int timeout)
109 fd_set readlist, writelist;
116 tv.tv_usec = timeout * 1000;
117 tv.tv_sec = tv.tv_usec / 1000000;
118 tv.tv_usec %= 1000000;
121 for (i = 0; i < fds; i++) {
122 if (pfd[i].fd >= hfd)
127 if (pfd[i].events & POLLIN)
128 FD_SET(pfd[i].fd, &readlist);
130 if (pfd[i].events & POLLOUT)
131 FD_SET(pfd[i].fd, &writelist);
134 ret = select(hfd, &readlist, &writelist, NULL, (timeout >= 0) ? &tv : NULL);
137 for (i = 0; i < fds; i++) {
138 if (FD_ISSET(pfd[i].fd, &readlist))
139 pfd[i].revents |= POLLIN;
141 if (FD_ISSET(pfd[i].fd, &writelist))
142 pfd[i].revents |= POLLOUT;
144 if (fcntl(pfd[i].fd, F_GETFL, O_NDELAY) == -1)
145 pfd[i].revents |= POLLHUP;
152 #endif /* HAVE_POLL */
156 int p_setenv(char *var, char *value, int overwrite)
158 return setenv(var, value, overwrite);
164 solaris, et al lack setenv(), this will emulate it's behavior
166 int p_setenv(char *var, char *value, int overwrite)
170 if (!overwrite && getenv(var))
173 buf = xmalloc(strlen(var) + strlen(value) + 2);
175 sprintf(buf, "%s=%s", var, value);
180 #endif /* HAVE_SETENV */
184 clearenv() is glibc-specific, setting environ to NULL will do for other platforms