~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

MergeĀ fromĀ trunk

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, bool set_blocking_mode, bool *old_mode)
97
96
{
98
97
  int r=0;
99
98
 
100
99
  *old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
101
100
 
102
 
#if !defined(NO_FCNTL_NONBLOCK)
103
101
  if (vio->sd >= 0)
104
102
  {
105
103
    int old_fcntl=vio->fcntl_mode;
116
114
      }
117
115
    }
118
116
  }
119
 
#else
120
 
  r= set_blocking_mode ? 0 : 1;
121
 
#endif /* !defined(NO_FCNTL_NONBLOCK) */
 
117
 
122
118
  return r;
123
119
}
124
120
 
125
 
my_bool
 
121
bool
126
122
vio_is_blocking(Vio * vio)
127
123
{
128
 
  my_bool r;
 
124
  bool r;
129
125
  r = !(vio->fcntl_mode & O_NONBLOCK);
130
126
 
131
127
  return r;
134
130
 
135
131
int vio_fastsend(Vio * vio __attribute__((unused)))
136
132
{
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
 
  }
 
133
  int nodelay = 1;
 
134
  int r;
 
135
 
 
136
  r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
 
137
                &nodelay, sizeof(nodelay));
 
138
  assert(r == 0);
158
139
 
159
140
  return r;
160
141
}
161
142
 
162
 
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
 
143
int32_t vio_keepalive(Vio* vio, bool set_keep_alive)
163
144
{
164
145
  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
 
  }
 
146
  uint32_t opt= 0;
 
147
 
 
148
  if (set_keep_alive)
 
149
    opt= 1;
 
150
 
 
151
  r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
174
152
 
175
153
  return r;
176
154
}
177
155
 
178
156
 
179
 
my_bool
 
157
bool
180
158
vio_should_retry(Vio * vio __attribute__((unused)))
181
159
{
182
160
  int en = socket_errno;
185
163
}
186
164
 
187
165
 
188
 
my_bool
 
166
bool
189
167
vio_was_interrupted(Vio *vio __attribute__((unused)))
190
168
{
191
169
  int en= socket_errno;
227
205
  return vio->sd;
228
206
}
229
207
 
230
 
my_bool vio_peer_addr(Vio *vio, char *buf, uint16 *port, size_t buflen)
 
208
bool vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
231
209
{
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
 
  }
 
210
  int error;
 
211
  char port_buf[NI_MAXSERV];
 
212
  size_socket addrLen = sizeof(vio->remote);
 
213
 
 
214
  if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
 
215
                  &addrLen) != 0)
 
216
  {
 
217
    return true;
 
218
  }
 
219
  vio->addrLen= (int)addrLen;
 
220
 
 
221
  if ((error= getnameinfo((struct sockaddr *)(&vio->remote), 
 
222
                          addrLen,
 
223
                          buf, buflen,
 
224
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
 
225
  {
 
226
    return true;
 
227
  }
 
228
 
 
229
  *port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
269
230
 
270
231
  return false;
271
232
}
273
234
 
274
235
/* Return 0 if there is data to be read */
275
236
 
276
 
my_bool vio_poll_read(Vio *vio,uint timeout)
 
237
bool vio_poll_read(Vio *vio,uint timeout)
277
238
{
278
 
#if defined(HAVE_POLL)
279
239
  struct pollfd fds;
280
240
  int res;
281
241
 
287
247
    return res < 0 ? false : true;              /* Don't return 1 on errors */
288
248
  }
289
249
  return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
290
 
#else
291
 
  return 0;
292
 
#endif
293
250
}
294
251
 
295
252
 
296
 
my_bool vio_peek_read(Vio *vio, uint *bytes)
 
253
bool vio_peek_read(Vio *vio, uint32_t *bytes)
297
254
{
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
255
  char buf[1024];
306
256
  ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
 
257
 
307
258
  if (res < 0)
308
 
    return TRUE;
309
 
  *bytes= res;
310
 
  return FALSE;
311
 
#endif
 
259
    return true;
 
260
  *bytes= (uint32_t)res;
 
261
  return false;
312
262
}
313
263
 
314
264
void vio_timeout(Vio *vio, uint which, uint timeout)
315
265
{
316
 
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
317
266
  int r;
318
267
 
319
 
  {
320
268
  /* POSIX specifies time as struct timeval. */
321
269
  struct timeval wait_timeout;
322
270
  wait_timeout.tv_sec= timeout;
325
273
  r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
326
274
                IF_WIN(const char*, const void*)&wait_timeout,
327
275
                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
 
276
  assert(r == 0);
336
277
}