~ubuntu-branches/ubuntu/saucy/openvpn/saucy

« back to all changes in this revision

Viewing changes to socket.h

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Gonzalez Iniesta
  • Date: 2005-01-05 19:03:11 UTC
  • mto: (1.4.1) (10.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050105190311-d0uioiqtor5xbzre
Tags: upstream-1.99+2.rc6
ImportĀ upstreamĀ versionĀ 1.99+2.rc6

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "buffer.h"
30
30
#include "common.h"
31
31
#include "error.h"
 
32
#include "proto.h"
32
33
#include "mtu.h"
33
 
#include "io.h"
 
34
#include "win32.h"
 
35
#include "event.h"
34
36
#include "proxy.h"
35
37
#include "socks.h"
 
38
#include "misc.h"
 
39
 
 
40
/*
 
41
 * OpenVPN's default port number as assigned by IANA.
 
42
 */
 
43
#define OPENVPN_PORT 1194
 
44
 
 
45
/*
 
46
 * Number of seconds that "resolv-retry infinite"
 
47
 * represents.
 
48
 */
 
49
#define RESOLV_RETRY_INFINITE 1000000000
 
50
 
 
51
#define REMOTE_LIST_SIZE 64
 
52
 
 
53
struct remote_entry
 
54
{
 
55
  const char *hostname;
 
56
  int port;
 
57
};
 
58
 
 
59
struct remote_list
 
60
{
 
61
  int len;
 
62
  int current;
 
63
  struct remote_entry array[REMOTE_LIST_SIZE];
 
64
};
36
65
 
37
66
/* 
38
 
 * packet_size_type is used communicate packet size
 
67
 * packet_size_type is used to communicate packet size
39
68
 * over the wire when stream oriented protocols are
40
69
 * being used
41
70
 */
56
85
  struct sockaddr_in actual; /* remote may change due to --float */
57
86
};
58
87
 
 
88
struct link_socket_info
 
89
{
 
90
  struct link_socket_addr *lsa;
 
91
  bool connection_established;
 
92
  const char *ipchange_command;
 
93
  const struct plugin_list *plugins;
 
94
  bool remote_float;  
 
95
  int proto;                    /* Protocol (PROTO_x defined below) */
 
96
  int mtu_changed;              /* Set to true when mtu value is changed */
 
97
};
 
98
 
59
99
/*
60
100
 * Used to extract packets encapsulated in streams into a buffer,
61
101
 * in this case IP packets embedded in a TCP stream.
76
116
};
77
117
 
78
118
/*
 
119
 * Used to set socket buffer sizes
 
120
 */
 
121
struct socket_buffer_size
 
122
{
 
123
  int rcvbuf;
 
124
  int sndbuf;
 
125
};
 
126
 
 
127
/*
79
128
 * This is the main socket structure used by OpenVPN.  The SOCKET_
80
129
 * defines try to abstract away our implementation differences between
81
130
 * using sockets on Posix vs. Win32.
82
131
 */
83
132
struct link_socket
84
133
{
85
 
  /* if true, indicates a stream protocol returned more than one encapsulated packet */
86
 
# define SOCKET_READ_RESIDUAL(sock) (sock.stream_buf.residual_fully_formed)
 
134
  struct link_socket_info info;
 
135
 
 
136
  socket_descriptor_t sd;
 
137
 
 
138
#ifdef ENABLE_SOCKS
 
139
  socket_descriptor_t ctrl_sd;  /* only used for UDP over Socks */
 
140
#endif
87
141
 
88
142
#ifdef WIN32
89
 
  /* these macros are called in the context of the openvpn() function */
90
 
# define SOCKET_SET_READ(sock) { if (stream_buf_read_setup (&sock)) { \
91
 
                                   wait_add (&event_wait, sock.reads.overlapped.hEvent); \
92
 
                                   socket_recv_queue (&sock, 0); }}
93
 
# define SOCKET_SET_WRITE(sock) { wait_add (&event_wait, sock.writes.overlapped.hEvent); }
94
 
# define SOCKET_ISSET(sock, set) ( wait_trigger (&event_wait, sock.set.overlapped.hEvent))
95
 
# define SOCKET_SETMAXFD(sock)
96
 
# define SOCKET_READ_STAT(sock)  (overlapped_io_state_ascii (&sock.reads,  "sr"))
97
 
# define SOCKET_WRITE_STAT(sock) (overlapped_io_state_ascii (&sock.writes, "sw"))
98
143
  struct overlapped_io reads;
99
144
  struct overlapped_io writes;
100
 
#else
101
 
  /* these macros are called in the context of the openvpn() function */
102
 
# define SOCKET_SET_READ(sock) {  if (stream_buf_read_setup (&sock)) \
103
 
                                    FD_SET (sock.sd, &event_wait.reads); }
104
 
# define SOCKET_SET_WRITE(sock) { FD_SET (sock.sd, &event_wait.writes); }
105
 
# define SOCKET_ISSET(sock, set) (FD_ISSET (sock.sd, &event_wait.set))
106
 
# define SOCKET_SETMAXFD(sock) { wait_update_maxfd (&event_wait, sock.sd); }
107
 
# define SOCKET_READ_STAT(sock)  (SOCKET_ISSET (sock, reads) ?  "SR" : "sr")
108
 
# define SOCKET_WRITE_STAT(sock) (SOCKET_ISSET (sock, writes) ? "SW" : "sw")
 
145
  struct rw_handle rw_handle;
 
146
  struct rw_handle listen_handle; /* For listening on TCP socket in server mode */
109
147
#endif
110
148
 
111
 
  socket_descriptor_t sd;
112
 
  socket_descriptor_t ctrl_sd;  /* only used for UDP over Socks */
 
149
  /* used for printing status info only */
 
150
  unsigned int rwflags_debug;
 
151
 
 
152
  /* used for long-term queueing of pre-accepted socket listen */
 
153
  bool listen_persistent_queued;
113
154
 
114
155
  /* set on initial call to init phase 1 */
 
156
  struct remote_list *remote_list;
 
157
  const char *remote_host;
 
158
  int remote_port;
115
159
  const char *local_host;
116
 
  const char *remote_host;
117
160
  int local_port;
118
 
  int remote_port;
119
 
  int proto;                    /* Protocol (PROTO_x defined below) */
120
161
  bool bind_local;
121
 
  bool remote_float;
122
162
 
123
163
# define INETD_NONE   0
124
164
# define INETD_WAIT   1
125
165
# define INETD_NOWAIT 2
126
166
  int inetd;
127
167
 
128
 
  struct link_socket_addr *lsa;
129
 
  const char *ipchange_command;
 
168
# define LS_MODE_DEFAULT           0
 
169
# define LS_MODE_TCP_LISTEN        1
 
170
# define LS_MODE_TCP_ACCEPT_FROM   2
 
171
  int mode;
 
172
 
130
173
  int resolve_retry_seconds;
131
174
  int connect_retry_seconds;
132
175
  int mtu_discover_type;
133
176
 
 
177
  struct socket_buffer_size socket_buffer_sizes;
 
178
 
134
179
  int mtu;                      /* OS discovered MTU, or 0 if unknown */
135
 
  int mtu_changed;              /* Set to true when mtu value is changed */
136
180
 
137
181
  bool did_resolve_remote;
138
182
 
139
 
# define CONNECTION_ESTABLISHED(ls) ((ls)->set_outgoing_initial)
140
 
  bool set_outgoing_initial;
141
 
 
142
183
  /* for stream sockets */
143
184
  struct stream_buf stream_buf;
144
185
  struct buffer stream_buf_data;
145
186
  bool stream_reset;
146
187
 
 
188
#ifdef ENABLE_HTTP_PROXY
147
189
  /* HTTP proxy */
148
190
  struct http_proxy_info *http_proxy;
 
191
#endif
149
192
 
 
193
#ifdef ENABLE_SOCKS
150
194
  /* Socks proxy */
151
195
  struct socks_proxy_info *socks_proxy;
152
196
  struct sockaddr_in socks_relay; /* Socks UDP relay address */
 
197
#endif
153
198
 
 
199
#if defined(ENABLE_HTTP_PROXY) || defined(ENABLE_SOCKS)
154
200
  /* The OpenVPN server we will use the proxy to connect to */
155
201
  const char *proxy_dest_host;
156
202
  int proxy_dest_port;
 
203
#endif
 
204
 
 
205
#if PASSTOS_CAPABILITY
 
206
  /* used to get/set TOS. */
 
207
  uint8_t ptos;
 
208
  bool ptos_defined;
 
209
#endif
 
210
 
 
211
#ifdef ENABLE_DEBUG
 
212
  int gremlin; /* --gremlin bits */
 
213
#endif
157
214
};
158
215
 
159
216
/*
166
223
 
167
224
#ifdef WIN32
168
225
 
169
 
#define ECONNRESET WSAECONNRESET
170
226
#define openvpn_close_socket(s) closesocket(s)
171
227
 
172
 
int inet_aton (const char *name, struct in_addr *addr);
173
 
 
174
228
int socket_recv_queue (struct link_socket *sock, int maxsize);
175
229
 
176
230
int socket_send_queue (struct link_socket *sock,
189
243
 
190
244
#endif
191
245
 
192
 
int link_socket_read_socks_udp (struct link_socket *sock,
193
 
                                struct buffer *buf,
194
 
                                struct sockaddr_in *from);
195
 
 
196
 
int link_socket_write_socks_udp (struct link_socket *sock,
197
 
                                 struct buffer *buf,
198
 
                                 struct sockaddr_in *to);
199
 
 
200
 
void link_socket_reset (struct link_socket *sock);
201
 
 
202
 
void link_socket_init_phase1 (struct link_socket *sock,
203
 
                              const char *local_host,
204
 
                              const char *remote_host,
205
 
                              int local_port,
206
 
                              int remote_port,
207
 
                              int proto,
208
 
                              struct http_proxy_info *http_proxy,
209
 
                              struct socks_proxy_info *socks_proxy,
210
 
                              bool bind_local,
211
 
                              bool remote_float,
212
 
                              int inetd,
213
 
                              struct link_socket_addr *lsa,
214
 
                              const char *ipchange_command,
215
 
                              int resolve_retry_seconds,
216
 
                              int connect_retry_seconds,
217
 
                              int mtu_discover_type);
218
 
 
 
246
struct link_socket *link_socket_new (void);
 
247
 
 
248
/*
 
249
 * Initialize link_socket object.
 
250
 */
 
251
 
 
252
void
 
253
link_socket_init_phase1 (struct link_socket *sock,
 
254
                         const char *local_host,
 
255
                         struct remote_list *remote_list,
 
256
                         int local_port,
 
257
                         int proto,
 
258
                         int mode,
 
259
                         const struct link_socket *accept_from,
 
260
#ifdef ENABLE_HTTP_PROXY
 
261
                         struct http_proxy_info *http_proxy,
 
262
#endif
 
263
#ifdef ENABLE_SOCKS
 
264
                         struct socks_proxy_info *socks_proxy,
 
265
#endif
 
266
#ifdef ENABLE_DEBUG
 
267
                         int gremlin,
 
268
#endif
 
269
                         bool bind_local,
 
270
                         bool remote_float,
 
271
                         int inetd,
 
272
                         struct link_socket_addr *lsa,
 
273
                         const char *ipchange_command,
 
274
                         const struct plugin_list *plugins,
 
275
                         int resolve_retry_seconds,
 
276
                         int connect_retry_seconds,
 
277
                         int mtu_discover_type,
 
278
                         int rcvbuf,
 
279
                         int sndbuf);
219
280
 
220
281
void link_socket_init_phase2 (struct link_socket *sock,
221
282
                              const struct frame *frame,
222
283
                              volatile int *signal_received);
223
284
 
 
285
void link_socket_post_fork (const struct link_socket *sock,
 
286
                            const struct sockaddr_in *remote);
 
287
 
224
288
void socket_adjust_frame_parameters (struct frame *frame, int proto);
225
289
 
226
290
void frame_adjust_path_mtu (struct frame *frame, int pmtu, int proto);
227
291
 
228
 
void link_socket_set_outgoing_addr (const struct buffer *buf,
229
 
                                    struct link_socket *sock,
230
 
                                    const struct sockaddr_in *addr);
231
 
 
232
 
void link_socket_incoming_addr (struct buffer *buf,
233
 
                                const struct link_socket *sock,
234
 
                                const struct sockaddr_in *from_addr);
235
 
 
236
 
void link_socket_get_outgoing_addr (struct buffer *buf,
237
 
                                    const struct link_socket *sock,
238
 
                                    struct sockaddr_in *addr);
239
 
 
240
292
void link_socket_close (struct link_socket *sock);
241
293
 
242
294
const char *print_sockaddr_ex (const struct sockaddr_in *addr,
243
295
                               bool do_port,
244
 
                               const char* separator);
245
 
 
246
 
const char *print_sockaddr (const struct sockaddr_in *addr);
247
 
 
248
 
const char *print_in_addr_t (in_addr_t addr, bool empty_if_undef);
249
 
 
250
 
void setenv_sockaddr (const char *name_prefix,
251
 
                      const struct sockaddr_in *addr);
 
296
                               const char* separator,
 
297
                               struct gc_arena *gc);
 
298
 
 
299
const char *print_sockaddr (const struct sockaddr_in *addr,
 
300
                            struct gc_arena *gc);
 
301
 
 
302
#define IA_EMPTY_IF_UNDEF (1<<0)
 
303
#define IA_NET_ORDER      (1<<1)
 
304
const char *print_in_addr_t (in_addr_t addr, unsigned int flags, struct gc_arena *gc);
 
305
 
 
306
#define SA_IP_PORT        (1<<0)
 
307
#define SA_SET_IF_NONZERO (1<<1)
 
308
void setenv_sockaddr (struct env_set *es,
 
309
                      const char *name_prefix,
 
310
                      const struct sockaddr_in *addr,
 
311
                      const bool flags);
 
312
 
 
313
void setenv_in_addr_t (struct env_set *es,
 
314
                       const char *name_prefix,
 
315
                       in_addr_t addr,
 
316
                       const bool flags);
252
317
 
253
318
void bad_address_length (int actual, int expected);
254
319
 
255
 
in_addr_t link_socket_current_remote (const struct link_socket *sock);
 
320
in_addr_t link_socket_current_remote (const struct link_socket_info *info);
 
321
 
 
322
void link_socket_connection_initiated (const struct buffer *buf,
 
323
                                       struct link_socket_info *info,
 
324
                                       const struct sockaddr_in *addr,
 
325
                                       const char *common_name,
 
326
                                       struct env_set *es);
 
327
 
 
328
void link_socket_bad_incoming_addr (struct buffer *buf,
 
329
                                    const struct link_socket_info *info,
 
330
                                    const struct sockaddr_in *from_addr);
 
331
 
 
332
void link_socket_bad_outgoing_addr (void);
 
333
 
 
334
void setenv_trusted (struct env_set *es, const struct link_socket_info *info);
 
335
 
 
336
void remote_list_randomize (struct remote_list *l);
 
337
 
 
338
/*
 
339
 * Low-level functions
 
340
 */
 
341
 
 
342
/* return values of openvpn_inet_aton */
 
343
#define OIA_HOSTNAME   0
 
344
#define OIA_IP         1
 
345
#define OIA_ERROR     -1
 
346
int openvpn_inet_aton (const char *dotted_quad, struct in_addr *addr);
 
347
 
 
348
socket_descriptor_t create_socket_tcp (void);
 
349
 
 
350
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
 
351
                                      struct sockaddr_in *remote,
 
352
                                      const bool nowait);
256
353
 
257
354
/*
258
355
 * DNS resolution
264
361
#define GETADDR_MENTION_RESOLVE_RETRY (1<<3)
265
362
#define GETADDR_FATAL_ON_SIGNAL       (1<<4)
266
363
#define GETADDR_WARN_ON_SIGNAL        (1<<5)
 
364
#define GETADDR_MSG_VIRT_OUT          (1<<6)
267
365
 
268
366
in_addr_t getaddr (unsigned int flags,
269
367
                   const char *hostname,
278
376
#define PROTO_UDPv4        0
279
377
#define PROTO_TCPv4_SERVER 1
280
378
#define PROTO_TCPv4_CLIENT 2
281
 
#define PROTO_N            3
 
379
#define PROTO_TCPv4        3
 
380
#define PROTO_N            4
282
381
 
283
382
int ascii2proto (const char* proto_name);
284
383
const char *proto2ascii (int proto, bool display_form);
285
 
const char *proto2ascii_all ();
 
384
const char *proto2ascii_all (struct gc_arena *gc);
286
385
int proto_remote (int proto, bool remote);
287
386
 
288
387
/*
309
408
 * Misc inline functions
310
409
 */
311
410
 
 
411
static inline int
 
412
remote_list_len (const struct remote_list *rl)
 
413
{
 
414
  if (rl)
 
415
    return rl->len;
 
416
  else
 
417
    return 0;
 
418
}
 
419
 
312
420
static inline bool
313
421
legal_ipv4_port (int port)
314
422
{
324
432
static inline bool
325
433
link_socket_connection_oriented (const struct link_socket *sock)
326
434
{
327
 
  return link_socket_proto_connection_oriented (sock->proto);
 
435
  if (sock)
 
436
    return link_socket_proto_connection_oriented (sock->info.proto);
 
437
  else
 
438
    return false;
328
439
}
329
440
 
330
441
static inline bool
355
466
static inline bool
356
467
addr_match_proto (const struct sockaddr_in *a1,
357
468
                  const struct sockaddr_in *a2,
358
 
                  int proto)
 
469
                  const int proto)
359
470
{
360
471
  return link_socket_proto_connection_oriented (proto)
361
472
    ? addr_match (a1, a2)
372
483
      else if (status < 0)
373
484
        {
374
485
          const int err = openvpn_errno_socket ();
 
486
#ifdef WIN32
 
487
          return err == WSAECONNRESET || err == WSAECONNABORTED;
 
488
#else
375
489
          return err == ECONNRESET;
376
 
        }
377
 
    }
378
 
  return false;
 
490
#endif
 
491
        }
 
492
    }
 
493
  return false;
 
494
}
 
495
 
 
496
static inline bool
 
497
link_socket_verify_incoming_addr (struct buffer *buf,
 
498
                                  const struct link_socket_info *info,
 
499
                                  const struct sockaddr_in *from_addr)
 
500
{
 
501
  if (buf->len > 0)
 
502
    {
 
503
      if (from_addr->sin_family != AF_INET)
 
504
        return false;
 
505
      if (!addr_defined (from_addr))
 
506
        return false;
 
507
      if (info->remote_float || !addr_defined (&info->lsa->remote))
 
508
        return true;
 
509
      if (addr_match_proto (from_addr, &info->lsa->remote, info->proto))
 
510
        return true;
 
511
    }
 
512
  return false;
 
513
}
 
514
 
 
515
static inline void
 
516
link_socket_get_outgoing_addr (struct buffer *buf,
 
517
                              const struct link_socket_info *info,
 
518
                              struct sockaddr_in *addr)
 
519
{
 
520
  if (buf->len > 0)
 
521
    {
 
522
      struct link_socket_addr *lsa = info->lsa;
 
523
      if (addr_defined (&lsa->actual))
 
524
        {
 
525
          addr->sin_family = lsa->actual.sin_family;
 
526
          addr->sin_addr.s_addr = lsa->actual.sin_addr.s_addr;
 
527
          addr->sin_port = lsa->actual.sin_port;
 
528
        }
 
529
      else
 
530
        {
 
531
          link_socket_bad_outgoing_addr ();
 
532
          buf->len = 0;
 
533
        }
 
534
    }
 
535
}
 
536
 
 
537
static inline void
 
538
link_socket_set_outgoing_addr (const struct buffer *buf,
 
539
                               struct link_socket_info *info,
 
540
                               const struct sockaddr_in *addr,
 
541
                               const char *common_name,
 
542
                               struct env_set *es)
 
543
{
 
544
  if (!buf || buf->len > 0)
 
545
    {
 
546
      struct link_socket_addr *lsa = info->lsa;
 
547
      if (
 
548
          /* new or changed address? */
 
549
          (!info->connection_established
 
550
           || !addr_match_proto (addr, &lsa->actual, info->proto))
 
551
          /* address undef or address == remote or --float */
 
552
          && (info->remote_float
 
553
              || !addr_defined (&lsa->remote)
 
554
              || addr_match_proto (addr, &lsa->remote, info->proto))
 
555
          )
 
556
        {
 
557
          link_socket_connection_initiated (buf, info, addr, common_name, es);
 
558
        }
 
559
    }
379
560
}
380
561
 
381
562
/*
388
569
void stream_buf_close (struct stream_buf* sb);
389
570
bool stream_buf_added (struct stream_buf *sb, int length_added);
390
571
 
391
 
bool stream_buf_read_setup (struct link_socket* sock);
 
572
static inline bool
 
573
stream_buf_read_setup (struct link_socket* sock)
 
574
{
 
575
  bool stream_buf_read_setup_dowork (struct link_socket* sock);
 
576
  if (link_socket_connection_oriented (sock))
 
577
    return stream_buf_read_setup_dowork (sock);
 
578
  else
 
579
    return true;
 
580
}
392
581
 
393
582
/*
394
583
 * Socket Read Routines
395
584
 */
396
585
 
397
 
int
398
 
link_socket_read_tcp (struct link_socket *sock,
399
 
                      struct buffer *buf);
 
586
int link_socket_read_tcp (struct link_socket *sock,
 
587
                          struct buffer *buf);
400
588
 
401
589
#ifdef WIN32
402
590
 
410
598
 
411
599
#else
412
600
 
413
 
static inline int
414
 
link_socket_read_udp_posix (struct link_socket *sock,
415
 
                            struct buffer *buf,
416
 
                            int maxsize,
417
 
                            struct sockaddr_in *from)
418
 
{
419
 
  socklen_t fromlen = sizeof (*from);
420
 
  CLEAR (*from);
421
 
  ASSERT (buf_safe (buf, maxsize));
422
 
  buf->len = recvfrom (sock->sd, BPTR (buf), maxsize, 0,
423
 
                       (struct sockaddr *) from, &fromlen);
424
 
  if (fromlen != sizeof (*from))
425
 
    bad_address_length (fromlen, sizeof (*from));
426
 
  return buf->len;
427
 
}
 
601
int link_socket_read_udp_posix (struct link_socket *sock,
 
602
                                struct buffer *buf,
 
603
                                int maxsize,
 
604
                                struct sockaddr_in *from);
428
605
 
429
606
#endif
430
607
 
435
612
                  int maxsize,
436
613
                  struct sockaddr_in *from)
437
614
{
438
 
  if (sock->proto == PROTO_UDPv4)
 
615
  if (sock->info.proto == PROTO_UDPv4)
439
616
    {
440
617
      int res;
441
618
 
444
621
#else
445
622
      res = link_socket_read_udp_posix (sock, buf, maxsize, from);
446
623
#endif
447
 
 
448
 
      if (sock->socks_proxy && res > 0)
449
 
        res = link_socket_read_socks_udp (sock, buf, from);
450
 
 
451
624
      return res;
452
625
    }
453
 
  else if (sock->proto == PROTO_TCPv4_SERVER || sock->proto == PROTO_TCPv4_CLIENT)
 
626
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
454
627
    {
455
628
      /* from address was returned by accept */
456
 
      *from = sock->lsa->actual;
 
629
      *from = sock->info.lsa->actual;
457
630
      return link_socket_read_tcp (sock, buf);
458
631
    }
459
632
  else
467
640
 * Socket Write routines
468
641
 */
469
642
 
470
 
int link_socket_read_tcp (struct link_socket *sock,
471
 
                          struct buffer *buf);
 
643
int link_socket_write_tcp (struct link_socket *sock,
 
644
                           struct buffer *buf,
 
645
                           struct sockaddr_in *to);
472
646
 
473
647
#ifdef WIN32
474
648
 
522
696
                       struct buffer *buf,
523
697
                       struct sockaddr_in *to)
524
698
{
525
 
  if (sock->socks_proxy)
526
 
    {
527
 
      return link_socket_write_socks_udp (sock, buf, to);
528
 
    }
529
 
  else
530
 
    {
531
 
#ifdef WIN32
532
 
      return link_socket_write_win32 (sock, buf, to);
533
 
#else
534
 
      return link_socket_write_udp_posix (sock, buf, to);
535
 
#endif
536
 
    }
537
 
}
538
 
 
539
 
static inline int
540
 
link_socket_write_tcp (struct link_socket *sock,
541
 
                       struct buffer *buf,
542
 
                       struct sockaddr_in *to)
543
 
{
544
 
  packet_size_type len = BLEN (buf);
545
 
  msg (D_STREAM_DEBUG, "STREAM: WRITE %d offset=%d", (int)len, buf->offset);
546
 
  ASSERT (len <= sock->stream_buf.maxlen);
547
 
  len = htonps (len);
548
 
  ASSERT (buf_write_prepend (buf, &len, sizeof (len)));
549
699
#ifdef WIN32
550
700
  return link_socket_write_win32 (sock, buf, to);
551
701
#else
552
 
  return link_socket_write_tcp_posix (sock, buf, to);  
 
702
  return link_socket_write_udp_posix (sock, buf, to);
553
703
#endif
554
704
}
555
705
 
559
709
                   struct buffer *buf,
560
710
                   struct sockaddr_in *to)
561
711
{
562
 
  if (sock->proto == PROTO_UDPv4)
 
712
  if (sock->info.proto == PROTO_UDPv4)
563
713
    {
564
714
      return link_socket_write_udp (sock, buf, to);
565
715
    }
566
 
  else if (sock->proto == PROTO_TCPv4_SERVER || sock->proto == PROTO_TCPv4_CLIENT)
 
716
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
567
717
    {
568
718
      return link_socket_write_tcp (sock, buf, to);
569
719
    }
574
724
    }
575
725
}
576
726
 
 
727
#if PASSTOS_CAPABILITY
 
728
 
 
729
/*
 
730
 * Extract TOS bits.  Assumes that ipbuf is a valid IPv4 packet.
 
731
 */
 
732
static inline void
 
733
link_socket_extract_tos (struct link_socket *ls, const struct buffer *ipbuf)
 
734
{
 
735
  if (ls && ipbuf)
 
736
    {
 
737
      struct openvpn_iphdr *iph = (struct openvpn_iphdr *) BPTR (ipbuf);
 
738
      ls->ptos = iph->tos;
 
739
      ls->ptos_defined = true;
 
740
    }
 
741
}
 
742
 
 
743
/*
 
744
 * Set socket properties to reflect TOS bits which were extracted
 
745
 * from tunnel packet.
 
746
 */
 
747
static inline void
 
748
link_socket_set_tos (struct link_socket *ls)
 
749
{
 
750
  if (ls && ls->ptos_defined)
 
751
    setsockopt (ls->sd, IPPROTO_IP, IP_TOS, &ls->ptos, sizeof (ls->ptos));
 
752
}
 
753
 
 
754
#endif
 
755
 
 
756
/*
 
757
 * Socket I/O wait functions
 
758
 */
 
759
 
 
760
static inline bool
 
761
socket_read_residual (const struct link_socket *s)
 
762
{
 
763
  return s && s->stream_buf.residual_fully_formed;
 
764
}
 
765
 
 
766
static inline event_t
 
767
socket_event_handle (const struct link_socket *s)
 
768
{
 
769
#ifdef WIN32
 
770
  return &s->rw_handle;
 
771
#else
 
772
  return s->sd;
 
773
#endif
 
774
}
 
775
 
 
776
event_t socket_listen_event_handle (struct link_socket *s);
 
777
 
 
778
unsigned int
 
779
socket_set (struct link_socket *s,
 
780
            struct event_set *es,
 
781
            unsigned int rwflags,
 
782
            void *arg,
 
783
            unsigned int *persistent);
 
784
 
 
785
static inline void
 
786
socket_set_listen_persistent (struct link_socket *s,
 
787
                              struct event_set *es,
 
788
                              void *arg)
 
789
{
 
790
  if (s && !s->listen_persistent_queued)
 
791
    {
 
792
      event_ctl (es, socket_listen_event_handle (s), EVENT_READ, arg);
 
793
      s->listen_persistent_queued = true;
 
794
    }
 
795
}
 
796
 
 
797
static inline void
 
798
socket_reset_listen_persistent (struct link_socket *s)
 
799
{
 
800
#ifdef WIN32
 
801
  reset_net_event_win32 (&s->listen_handle, s->sd);
 
802
#endif
 
803
}
 
804
 
 
805
const char *socket_stat (const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc);
 
806
 
577
807
#endif /* SOCKET_H */