~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

  • Committer: Brian Aker
  • Date: 2008-07-16 01:30:24 UTC
  • Revision ID: brian@tangent.org-20080716013024-nmnogwdpa459jrch
First pass of cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
  /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
36
36
  assert(vio->read_end == vio->read_pos);
37
 
  errno=0;                                      /* For linux */
38
 
  r = read(vio->sd, buf, size);
 
37
  r= read(vio->sd, buf, size);
 
38
 
39
39
  return r;
40
40
}
41
41
 
92
92
  return r;
93
93
}
94
94
 
95
 
int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode,
96
 
                 my_bool *old_mode)
 
95
int vio_blocking(Vio * vio __attribute__((unused)), bool set_blocking_mode,
 
96
                 bool *old_mode)
97
97
{
98
98
  int r=0;
99
99
 
100
100
  *old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
101
101
 
102
 
#if !defined(NO_FCNTL_NONBLOCK)
103
102
  if (vio->sd >= 0)
104
103
  {
105
104
    int old_fcntl=vio->fcntl_mode;
116
115
      }
117
116
    }
118
117
  }
119
 
#else
120
 
  r= set_blocking_mode ? 0 : 1;
121
 
#endif /* !defined(NO_FCNTL_NONBLOCK) */
 
118
 
122
119
  return r;
123
120
}
124
121
 
125
 
my_bool
 
122
bool
126
123
vio_is_blocking(Vio * vio)
127
124
{
128
 
  my_bool r;
 
125
  bool r;
129
126
  r = !(vio->fcntl_mode & O_NONBLOCK);
130
127
 
131
128
  return r;
134
131
 
135
132
int vio_fastsend(Vio * vio __attribute__((unused)))
136
133
{
137
 
  int r=0;
138
 
 
139
 
#if defined(IPTOS_THROUGHPUT)
140
 
  {
141
 
    int tos = IPTOS_THROUGHPUT;
142
 
    r= setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos));
143
 
  }
144
 
#endif                                    /* IPTOS_THROUGHPUT */
145
 
  if (!r)
146
 
  {
147
 
    int nodelay = 1;
148
 
 
149
 
    r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
150
 
                  IF_WIN(const char*, void*) &nodelay,
151
 
                  sizeof(nodelay));
152
 
 
153
 
  }
154
 
  if (r)
155
 
  {
156
 
    r= -1;
157
 
  }
 
134
  int nodelay = 1;
 
135
  int r;
 
136
 
 
137
  r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
 
138
                &nodelay, sizeof(nodelay));
 
139
  assert(r == 0);
158
140
 
159
141
  return r;
160
142
}
161
143
 
162
 
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
 
144
int32_t vio_keepalive(Vio* vio, bool set_keep_alive)
163
145
{
164
146
  int r= 0;
165
 
  uint opt= 0;
166
 
 
167
 
  if (vio->type != VIO_TYPE_NAMEDPIPE)
168
 
  {
169
 
    if (set_keep_alive)
170
 
      opt= 1;
171
 
    r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
172
 
                  sizeof(opt));
173
 
  }
 
147
  uint32_t opt= 0;
 
148
 
 
149
  if (set_keep_alive)
 
150
    opt= 1;
 
151
 
 
152
  r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
174
153
 
175
154
  return r;
176
155
}
177
156
 
178
157
 
179
 
my_bool
 
158
bool
180
159
vio_should_retry(Vio * vio __attribute__((unused)))
181
160
{
182
161
  int en = socket_errno;
185
164
}
186
165
 
187
166
 
188
 
my_bool
 
167
bool
189
168
vio_was_interrupted(Vio *vio __attribute__((unused)))
190
169
{
191
170
  int en= socket_errno;
227
206
  return vio->sd;
228
207
}
229
208
 
230
 
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen)
 
209
bool vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
231
210
{
232
 
  if (vio->localhost)
233
 
  {
234
 
    strmov(buf, "127.0.0.1");
235
 
    *port= 0;
236
 
  }
237
 
  else
238
 
  {
239
 
    int error;
240
 
    char port_buf[NI_MAXSERV];
241
 
    size_socket addrLen = sizeof(vio->remote);
242
 
    if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
243
 
                    &addrLen) != 0)
244
 
    {
245
 
      return true;
246
 
    }
247
 
    vio->addrLen= (int)addrLen;
248
 
 
249
 
    if ((error= getnameinfo((struct sockaddr *)(&vio->remote), 
250
 
                            addrLen,
251
 
                            buf, buflen,
252
 
                            port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
253
 
    {
254
 
      return true;
255
 
    }
256
 
 
257
 
    *port= (uint16)strtol(port_buf, (char **)NULL, 10);
258
 
 
259
 
    /*
260
 
      A lot of users do not have IPv6 loopback resolving to localhost
261
 
      correctly setup. Should this exist? No. If we do not do it though
262
 
      we will be getting a lot of support questions from users who
263
 
      have bad setups. This code should be removed by say... 2012.
264
 
        -Brian
265
 
    */
266
 
    if (!memcmp(buf, "::ffff:127.0.0.1", sizeof("::ffff:127.0.0.1")))
267
 
      strmov(buf, "127.0.0.1");
268
 
  }
 
211
  int error;
 
212
  char port_buf[NI_MAXSERV];
 
213
  size_socket addrLen = sizeof(vio->remote);
 
214
 
 
215
  if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
 
216
                  &addrLen) != 0)
 
217
  {
 
218
    return true;
 
219
  }
 
220
  vio->addrLen= (int)addrLen;
 
221
 
 
222
  if ((error= getnameinfo((struct sockaddr *)(&vio->remote), 
 
223
                          addrLen,
 
224
                          buf, buflen,
 
225
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
 
226
  {
 
227
    return true;
 
228
  }
 
229
 
 
230
  *port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
269
231
 
270
232
  return false;
271
233
}
273
235
 
274
236
/* Return 0 if there is data to be read */
275
237
 
276
 
my_bool vio_poll_read(Vio *vio,uint timeout)
 
238
bool vio_poll_read(Vio *vio,uint timeout)
277
239
{
278
 
#if defined(HAVE_POLL)
279
240
  struct pollfd fds;
280
241
  int res;
281
242
 
287
248
    return res < 0 ? false : true;              /* Don't return 1 on errors */
288
249
  }
289
250
  return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
290
 
#else
291
 
  return 0;
292
 
#endif
293
251
}
294
252
 
295
253
 
296
 
my_bool vio_peek_read(Vio *vio, uint *bytes)
 
254
bool vio_peek_read(Vio *vio, uint32_t *bytes)
297
255
{
298
 
#if FIONREAD_IN_SYS_IOCTL
299
 
  int len;
300
 
  if (ioctl(vio->sd, FIONREAD, &len) < 0)
301
 
    return true;
302
 
  *bytes= len;
303
 
  return false;
304
 
#else
305
256
  char buf[1024];
306
257
  ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
 
258
 
307
259
  if (res < 0)
308
260
    return true;
309
 
  *bytes= res;
 
261
  *bytes= (uint32_t)res;
310
262
  return false;
311
 
#endif
312
263
}
313
264
 
314
265
void vio_timeout(Vio *vio, uint which, uint timeout)
315
266
{
316
 
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
317
267
  int r;
318
268
 
319
 
  {
320
269
  /* POSIX specifies time as struct timeval. */
321
270
  struct timeval wait_timeout;
322
271
  wait_timeout.tv_sec= timeout;
325
274
  r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
326
275
                IF_WIN(const char*, const void*)&wait_timeout,
327
276
                sizeof(wait_timeout));
328
 
 
329
 
  }
330
 
#else
331
 
/*
332
 
  Platforms not suporting setting of socket timeout should either use
333
 
  thr_alarm or just run without read/write timeout(s)
334
 
*/
335
 
#endif
 
277
  assert(r == 0);
336
278
}