~ubuntu-branches/ubuntu/lucid/curl/lucid-security

« back to all changes in this revision

Viewing changes to lib/select.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-03-23 18:41:29 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050323184129-9hgq7luenq51umpu
Tags: upstream-7.12.3
ImportĀ upstreamĀ versionĀ 7.12.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
 
9
 *
 
10
 * This software is licensed as described in the file COPYING, which
 
11
 * you should have received as part of this distribution. The terms
 
12
 * are also available at http://curl.haxx.se/docs/copyright.html.
 
13
 *
 
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
15
 * copies of the Software, and permit persons to whom the Software is
 
16
 * furnished to do so, under the terms of the COPYING file.
 
17
 *
 
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 
19
 * KIND, either express or implied.
 
20
 *
 
21
 * $Id: select.c,v 1.5 2004/11/20 08:57:56 bagder Exp $
 
22
 ***************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#ifdef HAVE_SYS_SELECT_H
 
27
#include <sys/select.h>
 
28
#endif
 
29
#ifdef HAVE_SYS_TYPES_H
 
30
#include <sys/types.h>
 
31
#endif
 
32
 
 
33
#ifdef HAVE_SYS_TIME_H
 
34
#include <sys/time.h>
 
35
#endif
 
36
 
 
37
#ifndef HAVE_SELECT
 
38
#error "We can't compile without select() support!"
 
39
#endif
 
40
 
 
41
#include "select.h"
 
42
 
 
43
#ifdef WIN32
 
44
#define VALID_SOCK(s) (1)  /* Win-sockets are not in range [0..FD_SETSIZE> */
 
45
#else
 
46
#define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
 
47
#endif
 
48
 
 
49
/*
 
50
 * This is an internal function used for waiting for read or write
 
51
 * events on single file descriptors.  It attempts to replace select()
 
52
 * in order to avoid limits with FD_SETSIZE.
 
53
 *
 
54
 * Return values:
 
55
 *   -1 = system call error
 
56
 *    0 = timeout
 
57
 *    CSELECT_IN | CSELECT_OUT | CSELECT_ERR
 
58
 */
 
59
int Curl_select(curl_socket_t readfd, curl_socket_t writefd, int timeout_ms)
 
60
{
 
61
#ifdef HAVE_POLL_FINE
 
62
  struct pollfd pfd[2];
 
63
  int num;
 
64
  int r;
 
65
  int ret;
 
66
 
 
67
  num = 0;
 
68
  if (readfd != CURL_SOCKET_BAD) {
 
69
    pfd[num].fd = readfd;
 
70
    pfd[num].events = POLLIN;
 
71
    num++;
 
72
  }
 
73
  if (writefd != CURL_SOCKET_BAD) {
 
74
    pfd[num].fd = writefd;
 
75
    pfd[num].events = POLLOUT;
 
76
    num++;
 
77
  }
 
78
 
 
79
  r = poll(pfd, num, timeout_ms);
 
80
 
 
81
  if (r < 0)
 
82
    return -1;
 
83
  if (r == 0)
 
84
    return 0;
 
85
 
 
86
  ret = 0;
 
87
  num = 0;
 
88
  if (readfd != CURL_SOCKET_BAD) {
 
89
    if (pfd[num].revents & POLLIN)
 
90
      ret |= CSELECT_IN;
 
91
    if (pfd[num].revents & POLLERR)
 
92
      ret |= CSELECT_ERR;
 
93
    num++;
 
94
  }
 
95
  if (writefd != CURL_SOCKET_BAD) {
 
96
    if (pfd[num].revents & POLLOUT)
 
97
      ret |= CSELECT_OUT;
 
98
    if (pfd[num].revents & POLLERR)
 
99
      ret |= CSELECT_ERR;
 
100
  }
 
101
 
 
102
  return ret;
 
103
#else
 
104
  struct timeval timeout;
 
105
  fd_set fds_read;
 
106
  fd_set fds_write;
 
107
  fd_set fds_err;
 
108
  curl_socket_t maxfd;
 
109
  int r;
 
110
  int ret;
 
111
 
 
112
  timeout.tv_sec = timeout_ms / 1000;
 
113
  timeout.tv_usec = (timeout_ms % 1000) * 1000;
 
114
 
 
115
  FD_ZERO(&fds_err);
 
116
  maxfd = -1;
 
117
 
 
118
  FD_ZERO(&fds_read);
 
119
  if (readfd != CURL_SOCKET_BAD) {
 
120
    if (!VALID_SOCK(readfd)) {
 
121
      errno = EINVAL;
 
122
      return -1;
 
123
    }
 
124
    FD_SET(readfd, &fds_read);
 
125
    FD_SET(readfd, &fds_err);
 
126
    maxfd = readfd;
 
127
  }
 
128
 
 
129
  FD_ZERO(&fds_write);
 
130
  if (writefd != CURL_SOCKET_BAD) {
 
131
    if (!VALID_SOCK(writefd)) {
 
132
      errno = EINVAL;
 
133
      return -1;
 
134
    }
 
135
    FD_SET(writefd, &fds_write);
 
136
    FD_SET(writefd, &fds_err);
 
137
    if (writefd > maxfd)
 
138
      maxfd = writefd;
 
139
  }
 
140
 
 
141
  r = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
 
142
 
 
143
  if (r < 0)
 
144
    return -1;
 
145
  if (r == 0)
 
146
    return 0;
 
147
 
 
148
  ret = 0;
 
149
  if (readfd != CURL_SOCKET_BAD) {
 
150
    if (FD_ISSET(readfd, &fds_read))
 
151
      ret |= CSELECT_IN;
 
152
    if (FD_ISSET(readfd, &fds_err))
 
153
      ret |= CSELECT_ERR;
 
154
  }
 
155
  if (writefd != CURL_SOCKET_BAD) {
 
156
    if (FD_ISSET(writefd, &fds_write))
 
157
      ret |= CSELECT_OUT;
 
158
    if (FD_ISSET(writefd, &fds_err))
 
159
      ret |= CSELECT_ERR;
 
160
  }
 
161
 
 
162
  return ret;
 
163
#endif
 
164
}
 
165
 
 
166
/*
 
167
 * This is a wrapper around poll().  If poll() does not exist, then
 
168
 * select() is used instead.  An error is returned if select() is
 
169
 * being used and a file descriptor too large for FD_SETSIZE.
 
170
 *
 
171
 * Return values:
 
172
 *   -1 = system call error or fd >= FD_SETSIZE
 
173
 *    0 = timeout
 
174
 *    1 = number of structures with non zero revent fields
 
175
 */
 
176
int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
 
177
{
 
178
#ifdef HAVE_POLL_FINE
 
179
    return poll(ufds, nfds, timeout_ms);
 
180
#else
 
181
  struct timeval timeout;
 
182
  struct timeval *ptimeout;
 
183
  fd_set fds_read;
 
184
  fd_set fds_write;
 
185
  fd_set fds_err;
 
186
  curl_socket_t maxfd;
 
187
  int r;
 
188
  unsigned int i;
 
189
 
 
190
  FD_ZERO(&fds_read);
 
191
  FD_ZERO(&fds_write);
 
192
  FD_ZERO(&fds_err);
 
193
  maxfd = -1;
 
194
 
 
195
  for (i = 0; i < nfds; i++) {
 
196
    if (ufds[i].fd == CURL_SOCKET_BAD)
 
197
      continue;
 
198
#ifndef WIN32  /* This is harmless and wrong on Win32 */
 
199
    if (ufds[i].fd >= FD_SETSIZE) {
 
200
      errno = EINVAL;
 
201
      return -1;
 
202
    }
 
203
#endif
 
204
    if (ufds[i].fd > maxfd)
 
205
      maxfd = ufds[i].fd;
 
206
    if (ufds[i].events & POLLIN)
 
207
      FD_SET(ufds[i].fd, &fds_read);
 
208
    if (ufds[i].events & POLLOUT)
 
209
      FD_SET(ufds[i].fd, &fds_write);
 
210
    if (ufds[i].events & POLLERR)
 
211
      FD_SET(ufds[i].fd, &fds_err);
 
212
  }
 
213
 
 
214
  if (timeout_ms < 0) {
 
215
    ptimeout = NULL;      /* wait forever */
 
216
  } else {
 
217
    timeout.tv_sec = timeout_ms / 1000;
 
218
    timeout.tv_usec = (timeout_ms % 1000) * 1000;
 
219
    ptimeout = &timeout;
 
220
  }
 
221
 
 
222
  r = select(maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
 
223
 
 
224
  if (r < 0)
 
225
    return -1;
 
226
  if (r == 0)
 
227
    return 0;
 
228
 
 
229
  r = 0;
 
230
  for (i = 0; i < nfds; i++) {
 
231
    ufds[i].revents = 0;
 
232
    if (ufds[i].fd == CURL_SOCKET_BAD)
 
233
      continue;
 
234
    if (FD_ISSET(ufds[i].fd, &fds_read))
 
235
      ufds[i].revents |= POLLIN;
 
236
    if (FD_ISSET(ufds[i].fd, &fds_write))
 
237
      ufds[i].revents |= POLLOUT;
 
238
    if (FD_ISSET(ufds[i].fd, &fds_err))
 
239
      ufds[i].revents |= POLLERR;
 
240
    if (ufds[i].revents != 0)
 
241
      r++;
 
242
  }
 
243
 
 
244
  return r;
 
245
#endif
 
246
}