~smoser/ubuntu/oneiric/openvpn/lp-794916

« back to all changes in this revision

Viewing changes to .pc/jjo-ipv6-support.patch/socket.h

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-05-05 03:06:19 UTC
  • mfrom: (10.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100505030619-cwre0snhgx1mql53
Tags: 2.1.0-2ubuntu1
* Merge from debian unstable.  Remaining changes:
  + debian/openvpn.init.d:
    - Do not use start-stop-daemon and use </dev/null to avoid blocking boot
    - Show per-VPN result messages
    - Add "--script-security 2" by default for backwards compatablitiy
   + debian/control: Add lsb-base >= 3.2-14 to allow status_of_proc() 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  OpenVPN -- An application to securely tunnel IP networks
 
3
 *             over a single TCP/UDP port, with support for SSL/TLS-based
 
4
 *             session authentication and key exchange,
 
5
 *             packet encryption, packet authentication, and
 
6
 *             packet compression.
 
7
 *
 
8
 *  Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
 
9
 *
 
10
 *  This program is free software; you can redistribute it and/or modify
 
11
 *  it under the terms of the GNU General Public License version 2
 
12
 *  as published by the Free Software Foundation.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program (see the file COPYING included with this
 
21
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 
22
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
 
 
25
#ifndef SOCKET_H
 
26
#define SOCKET_H
 
27
 
 
28
#include "buffer.h"
 
29
#include "common.h"
 
30
#include "error.h"
 
31
#include "proto.h"
 
32
#include "mtu.h"
 
33
#include "win32.h"
 
34
#include "event.h"
 
35
#include "proxy.h"
 
36
#include "socks.h"
 
37
#include "misc.h"
 
38
 
 
39
/*
 
40
 * OpenVPN's default port number as assigned by IANA.
 
41
 */
 
42
#define OPENVPN_PORT 1194
 
43
 
 
44
/*
 
45
 * Maximum size passed passed to setsockopt SNDBUF/RCVBUF
 
46
 */
 
47
#define SOCKET_SND_RCV_BUF_MAX 1000000
 
48
 
 
49
/*
 
50
 * Number of seconds that "resolv-retry infinite"
 
51
 * represents.
 
52
 */
 
53
#define RESOLV_RETRY_INFINITE 1000000000
 
54
 
 
55
/* 
 
56
 * packet_size_type is used to communicate packet size
 
57
 * over the wire when stream oriented protocols are
 
58
 * being used
 
59
 */
 
60
 
 
61
typedef uint16_t packet_size_type;
 
62
 
 
63
/* convert a packet_size_type from host to network order */
 
64
#define htonps(x) htons(x)
 
65
 
 
66
/* convert a packet_size_type from network to host order */
 
67
#define ntohps(x) ntohs(x)
 
68
 
 
69
/* OpenVPN sockaddr struct */
 
70
struct openvpn_sockaddr
 
71
{
 
72
  /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
 
73
  struct sockaddr_in sa;
 
74
};
 
75
 
 
76
/* actual address of remote, based on source address of received packets */
 
77
struct link_socket_actual
 
78
{
 
79
  /*int dummy;*/ /* add offset to force a bug if dest not explicitly dereferenced */
 
80
  struct openvpn_sockaddr dest;
 
81
#if ENABLE_IP_PKTINFO
 
82
  struct in_pktinfo pi;
 
83
#endif
 
84
};
 
85
 
 
86
/* IP addresses which are persistant across SIGUSR1s */
 
87
struct link_socket_addr
 
88
{
 
89
  struct openvpn_sockaddr local;
 
90
  struct openvpn_sockaddr remote;   /* initial remote */
 
91
  struct link_socket_actual actual; /* reply to this address */
 
92
};
 
93
 
 
94
struct link_socket_info
 
95
{
 
96
  struct link_socket_addr *lsa;
 
97
  bool connection_established;
 
98
  const char *ipchange_command;
 
99
  const struct plugin_list *plugins;
 
100
  bool remote_float;  
 
101
  int proto;                    /* Protocol (PROTO_x defined below) */
 
102
  int mtu_changed;              /* Set to true when mtu value is changed */
 
103
};
 
104
 
 
105
/*
 
106
 * Used to extract packets encapsulated in streams into a buffer,
 
107
 * in this case IP packets embedded in a TCP stream.
 
108
 */
 
109
struct stream_buf
 
110
{
 
111
  struct buffer buf_init;
 
112
  struct buffer residual;
 
113
  int maxlen;
 
114
  bool residual_fully_formed;
 
115
 
 
116
  struct buffer buf;
 
117
  struct buffer next;
 
118
  int len;     /* -1 if not yet known */
 
119
 
 
120
  bool error;  /* if true, fatal TCP error has occurred,
 
121
                  requiring that connection be restarted */
 
122
#if PORT_SHARE
 
123
# define PS_DISABLED 0
 
124
# define PS_ENABLED  1
 
125
# define PS_FOREIGN  2
 
126
  int port_share_state;
 
127
#endif
 
128
};
 
129
 
 
130
/*
 
131
 * Used to set socket buffer sizes
 
132
 */
 
133
struct socket_buffer_size
 
134
{
 
135
  int rcvbuf;
 
136
  int sndbuf;
 
137
};
 
138
 
 
139
/*
 
140
 * This is the main socket structure used by OpenVPN.  The SOCKET_
 
141
 * defines try to abstract away our implementation differences between
 
142
 * using sockets on Posix vs. Win32.
 
143
 */
 
144
struct link_socket
 
145
{
 
146
  struct link_socket_info info;
 
147
 
 
148
  socket_descriptor_t sd;
 
149
 
 
150
#ifdef ENABLE_SOCKS
 
151
  socket_descriptor_t ctrl_sd;  /* only used for UDP over Socks */
 
152
#endif
 
153
 
 
154
#ifdef WIN32
 
155
  struct overlapped_io reads;
 
156
  struct overlapped_io writes;
 
157
  struct rw_handle rw_handle;
 
158
  struct rw_handle listen_handle; /* For listening on TCP socket in server mode */
 
159
#endif
 
160
 
 
161
  /* used for printing status info only */
 
162
  unsigned int rwflags_debug;
 
163
 
 
164
  /* used for long-term queueing of pre-accepted socket listen */
 
165
  bool listen_persistent_queued;
 
166
 
 
167
  /* Does config file contain any <connection> ... </connection> blocks? */
 
168
  bool connection_profiles_defined;
 
169
 
 
170
  const char *remote_host;
 
171
  int remote_port;
 
172
  const char *local_host;
 
173
  int local_port;
 
174
  bool bind_local;
 
175
 
 
176
# define INETD_NONE   0
 
177
# define INETD_WAIT   1
 
178
# define INETD_NOWAIT 2
 
179
  int inetd;
 
180
 
 
181
# define LS_MODE_DEFAULT           0
 
182
# define LS_MODE_TCP_LISTEN        1
 
183
# define LS_MODE_TCP_ACCEPT_FROM   2
 
184
  int mode;
 
185
 
 
186
  int resolve_retry_seconds;
 
187
  int connect_retry_seconds;
 
188
  int connect_timeout;
 
189
  int connect_retry_max;
 
190
  int mtu_discover_type;
 
191
 
 
192
  struct socket_buffer_size socket_buffer_sizes;
 
193
 
 
194
  int mtu;                      /* OS discovered MTU, or 0 if unknown */
 
195
 
 
196
  bool did_resolve_remote;
 
197
 
 
198
# define SF_USE_IP_PKTINFO (1<<0)
 
199
# define SF_TCP_NODELAY (1<<1)
 
200
# define SF_PORT_SHARE (1<<2)
 
201
# define SF_HOST_RANDOMIZE (1<<3)
 
202
  unsigned int sockflags;
 
203
 
 
204
  /* for stream sockets */
 
205
  struct stream_buf stream_buf;
 
206
  struct buffer stream_buf_data;
 
207
  bool stream_reset;
 
208
 
 
209
#ifdef ENABLE_HTTP_PROXY
 
210
  /* HTTP proxy */
 
211
  struct http_proxy_info *http_proxy;
 
212
#endif
 
213
 
 
214
#ifdef ENABLE_SOCKS
 
215
  /* Socks proxy */
 
216
  struct socks_proxy_info *socks_proxy;
 
217
  struct link_socket_actual socks_relay; /* Socks UDP relay address */
 
218
#endif
 
219
 
 
220
#if defined(ENABLE_HTTP_PROXY) || defined(ENABLE_SOCKS)
 
221
  /* The OpenVPN server we will use the proxy to connect to */
 
222
  const char *proxy_dest_host;
 
223
  int proxy_dest_port;
 
224
#endif
 
225
 
 
226
#if PASSTOS_CAPABILITY
 
227
  /* used to get/set TOS. */
 
228
  uint8_t ptos;
 
229
  bool ptos_defined;
 
230
#endif
 
231
 
 
232
#ifdef ENABLE_DEBUG
 
233
  int gremlin; /* --gremlin bits */
 
234
#endif
 
235
};
 
236
 
 
237
/*
 
238
 * Some Posix/Win32 differences.
 
239
 */
 
240
 
 
241
#ifndef MSG_NOSIGNAL
 
242
#define MSG_NOSIGNAL 0
 
243
#endif
 
244
 
 
245
#ifdef WIN32
 
246
 
 
247
#define openvpn_close_socket(s) closesocket(s)
 
248
 
 
249
int socket_recv_queue (struct link_socket *sock, int maxsize);
 
250
 
 
251
int socket_send_queue (struct link_socket *sock,
 
252
                       struct buffer *buf,
 
253
                       const struct link_socket_actual *to);
 
254
 
 
255
int socket_finalize (
 
256
                     SOCKET s,
 
257
                     struct overlapped_io *io,
 
258
                     struct buffer *buf,
 
259
                     struct link_socket_actual *from);
 
260
 
 
261
#else
 
262
 
 
263
#define openvpn_close_socket(s) close(s)
 
264
 
 
265
#endif
 
266
 
 
267
struct link_socket *link_socket_new (void);
 
268
 
 
269
void socket_bind (socket_descriptor_t sd,
 
270
                  struct openvpn_sockaddr *local,
 
271
                  const char *prefix);
 
272
 
 
273
int openvpn_connect (socket_descriptor_t sd,
 
274
                     struct openvpn_sockaddr *remote,
 
275
                     int connect_timeout,
 
276
                     volatile int *signal_received);
 
277
 
 
278
/*
 
279
 * Initialize link_socket object.
 
280
 */
 
281
 
 
282
void
 
283
link_socket_init_phase1 (struct link_socket *sock,
 
284
                         const bool connection_profiles_defined,
 
285
                         const char *local_host,
 
286
                         int local_port,
 
287
                         const char *remote_host,
 
288
                         int remote_port,
 
289
                         int proto,
 
290
                         int mode,
 
291
                         const struct link_socket *accept_from,
 
292
#ifdef ENABLE_HTTP_PROXY
 
293
                         struct http_proxy_info *http_proxy,
 
294
#endif
 
295
#ifdef ENABLE_SOCKS
 
296
                         struct socks_proxy_info *socks_proxy,
 
297
#endif
 
298
#ifdef ENABLE_DEBUG
 
299
                         int gremlin,
 
300
#endif
 
301
                         bool bind_local,
 
302
                         bool remote_float,
 
303
                         int inetd,
 
304
                         struct link_socket_addr *lsa,
 
305
                         const char *ipchange_command,
 
306
                         const struct plugin_list *plugins,
 
307
                         int resolve_retry_seconds,
 
308
                         int connect_retry_seconds,
 
309
                         int connect_timeout,
 
310
                         int connect_retry_max,
 
311
                         int mtu_discover_type,
 
312
                         int rcvbuf,
 
313
                         int sndbuf,
 
314
                         unsigned int sockflags);
 
315
 
 
316
void link_socket_init_phase2 (struct link_socket *sock,
 
317
                              const struct frame *frame,
 
318
                              volatile int *signal_received);
 
319
 
 
320
void socket_adjust_frame_parameters (struct frame *frame, int proto);
 
321
 
 
322
void frame_adjust_path_mtu (struct frame *frame, int pmtu, int proto);
 
323
 
 
324
void link_socket_close (struct link_socket *sock);
 
325
 
 
326
void sd_close (socket_descriptor_t *sd);
 
327
 
 
328
#define PS_SHOW_PORT_IF_DEFINED (1<<0)
 
329
#define PS_SHOW_PORT            (1<<1)
 
330
#define PS_SHOW_PKTINFO         (1<<2)
 
331
#define PS_DONT_SHOW_ADDR       (1<<3)
 
332
 
 
333
const char *print_sockaddr_ex (const struct openvpn_sockaddr *addr,
 
334
                               const char* separator,
 
335
                               const unsigned int flags,
 
336
                               struct gc_arena *gc);
 
337
 
 
338
 
 
339
const char *print_sockaddr (const struct openvpn_sockaddr *addr,
 
340
                            struct gc_arena *gc);
 
341
 
 
342
const char *print_link_socket_actual_ex (const struct link_socket_actual *act,
 
343
                                         const char* separator,
 
344
                                         const unsigned int flags,
 
345
                                         struct gc_arena *gc);
 
346
 
 
347
const char *print_link_socket_actual (const struct link_socket_actual *act,
 
348
                                      struct gc_arena *gc);
 
349
 
 
350
 
 
351
#define IA_EMPTY_IF_UNDEF (1<<0)
 
352
#define IA_NET_ORDER      (1<<1)
 
353
const char *print_in_addr_t (in_addr_t addr, unsigned int flags, struct gc_arena *gc);
 
354
 
 
355
#define SA_IP_PORT        (1<<0)
 
356
#define SA_SET_IF_NONZERO (1<<1)
 
357
void setenv_sockaddr (struct env_set *es,
 
358
                      const char *name_prefix,
 
359
                      const struct openvpn_sockaddr *addr,
 
360
                      const bool flags);
 
361
 
 
362
void setenv_in_addr_t (struct env_set *es,
 
363
                       const char *name_prefix,
 
364
                       in_addr_t addr,
 
365
                       const bool flags);
 
366
 
 
367
void setenv_link_socket_actual (struct env_set *es,
 
368
                                const char *name_prefix,
 
369
                                const struct link_socket_actual *act,
 
370
                                const bool flags);
 
371
 
 
372
void bad_address_length (int actual, int expected);
 
373
 
 
374
in_addr_t link_socket_current_remote (const struct link_socket_info *info);
 
375
 
 
376
void link_socket_connection_initiated (const struct buffer *buf,
 
377
                                       struct link_socket_info *info,
 
378
                                       const struct link_socket_actual *addr,
 
379
                                       const char *common_name,
 
380
                                       struct env_set *es);
 
381
 
 
382
void link_socket_bad_incoming_addr (struct buffer *buf,
 
383
                                    const struct link_socket_info *info,
 
384
                                    const struct link_socket_actual *from_addr);
 
385
 
 
386
void link_socket_bad_outgoing_addr (void);
 
387
 
 
388
void setenv_trusted (struct env_set *es, const struct link_socket_info *info);
 
389
 
 
390
bool link_socket_update_flags (struct link_socket *ls, unsigned int sockflags);
 
391
void link_socket_update_buffer_sizes (struct link_socket *ls, int rcvbuf, int sndbuf);
 
392
 
 
393
/*
 
394
 * Low-level functions
 
395
 */
 
396
 
 
397
/* return values of openvpn_inet_aton */
 
398
#define OIA_HOSTNAME   0
 
399
#define OIA_IP         1
 
400
#define OIA_ERROR     -1
 
401
int openvpn_inet_aton (const char *dotted_quad, struct in_addr *addr);
 
402
 
 
403
/* integrity validation on pulled options */
 
404
bool ip_addr_dotted_quad_safe (const char *dotted_quad);
 
405
bool ip_or_dns_addr_safe (const char *addr, const bool allow_fqdn);
 
406
bool mac_addr_safe (const char *mac_addr);
 
407
 
 
408
socket_descriptor_t create_socket_tcp (void);
 
409
 
 
410
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
 
411
                                      struct link_socket_actual *act,
 
412
                                      const bool nowait);
 
413
 
 
414
#if UNIX_SOCK_SUPPORT
 
415
 
 
416
socket_descriptor_t create_socket_unix (void);
 
417
 
 
418
void socket_bind_unix (socket_descriptor_t sd,
 
419
                       struct sockaddr_un *local,
 
420
                       const char *prefix);
 
421
 
 
422
socket_descriptor_t socket_accept_unix (socket_descriptor_t sd,
 
423
                                        struct sockaddr_un *remote);
 
424
 
 
425
int socket_connect_unix (socket_descriptor_t sd,
 
426
                         struct sockaddr_un *remote);
 
427
 
 
428
void sockaddr_unix_init (struct sockaddr_un *local, const char *path);
 
429
 
 
430
const char *sockaddr_unix_name (const struct sockaddr_un *local, const char *null);
 
431
 
 
432
void socket_delete_unix (const struct sockaddr_un *local);
 
433
 
 
434
bool unix_socket_get_peer_uid_gid (const socket_descriptor_t sd, int *uid, int *gid);
 
435
 
 
436
#endif
 
437
 
 
438
/*
 
439
 * DNS resolution
 
440
 */
 
441
 
 
442
#define GETADDR_RESOLVE               (1<<0)
 
443
#define GETADDR_FATAL                 (1<<1)
 
444
#define GETADDR_HOST_ORDER            (1<<2)
 
445
#define GETADDR_MENTION_RESOLVE_RETRY (1<<3)
 
446
#define GETADDR_FATAL_ON_SIGNAL       (1<<4)
 
447
#define GETADDR_WARN_ON_SIGNAL        (1<<5)
 
448
#define GETADDR_MSG_VIRT_OUT          (1<<6)
 
449
#define GETADDR_TRY_ONCE              (1<<7)
 
450
#define GETADDR_UPDATE_MANAGEMENT_STATE (1<<8)
 
451
#define GETADDR_RANDOMIZE             (1<<9)
 
452
 
 
453
in_addr_t getaddr (unsigned int flags,
 
454
                   const char *hostname,
 
455
                   int resolve_retry_seconds,
 
456
                   bool *succeeded,
 
457
                   volatile int *signal_received);
 
458
 
 
459
/*
 
460
 * Transport protocol naming and other details.
 
461
 */
 
462
 
 
463
#define PROTO_UDPv4        0
 
464
#define PROTO_TCPv4_SERVER 1
 
465
#define PROTO_TCPv4_CLIENT 2
 
466
#define PROTO_TCPv4        3
 
467
#define PROTO_N            4
 
468
 
 
469
int ascii2proto (const char* proto_name);
 
470
const char *proto2ascii (int proto, bool display_form);
 
471
const char *proto2ascii_all (struct gc_arena *gc);
 
472
int proto_remote (int proto, bool remote);
 
473
 
 
474
/*
 
475
 * Overhead added to packets by various protocols.
 
476
 */
 
477
#define IPv4_UDP_HEADER_SIZE              28
 
478
#define IPv4_TCP_HEADER_SIZE              40
 
479
#define IPv6_UDP_HEADER_SIZE              40
 
480
 
 
481
extern const int proto_overhead[];
 
482
 
 
483
static inline int
 
484
datagram_overhead (int proto)
 
485
{
 
486
  ASSERT (proto >= 0 && proto < PROTO_N);
 
487
  return proto_overhead [proto];
 
488
}
 
489
 
 
490
/*
 
491
 * Misc inline functions
 
492
 */
 
493
 
 
494
static inline bool
 
495
legal_ipv4_port (int port)
 
496
{
 
497
  return port > 0 && port < 65536;
 
498
}
 
499
 
 
500
static inline bool
 
501
link_socket_proto_connection_oriented (int proto)
 
502
{
 
503
  return proto == PROTO_TCPv4_SERVER || proto == PROTO_TCPv4_CLIENT;
 
504
}
 
505
 
 
506
static inline bool
 
507
link_socket_connection_oriented (const struct link_socket *sock)
 
508
{
 
509
  if (sock)
 
510
    return link_socket_proto_connection_oriented (sock->info.proto);
 
511
  else
 
512
    return false;
 
513
}
 
514
 
 
515
static inline bool
 
516
addr_defined (const struct openvpn_sockaddr *addr)
 
517
{
 
518
  return addr->sa.sin_addr.s_addr != 0;
 
519
}
 
520
 
 
521
static inline bool
 
522
link_socket_actual_defined (const struct link_socket_actual *act)
 
523
{
 
524
  return act && addr_defined (&act->dest);
 
525
}
 
526
 
 
527
static inline bool
 
528
addr_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
 
529
{
 
530
  return a1->sa.sin_addr.s_addr == a2->sa.sin_addr.s_addr;
 
531
}
 
532
 
 
533
static inline in_addr_t
 
534
addr_host (const struct openvpn_sockaddr *s)
 
535
{
 
536
  return ntohl (s->sa.sin_addr.s_addr);
 
537
}
 
538
 
 
539
static inline bool
 
540
addr_port_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
 
541
{
 
542
  return a1->sa.sin_addr.s_addr == a2->sa.sin_addr.s_addr
 
543
    && a1->sa.sin_port == a2->sa.sin_port;
 
544
}
 
545
 
 
546
static inline bool
 
547
addr_match_proto (const struct openvpn_sockaddr *a1,
 
548
                  const struct openvpn_sockaddr *a2,
 
549
                  const int proto)
 
550
{
 
551
  return link_socket_proto_connection_oriented (proto)
 
552
    ? addr_match (a1, a2)
 
553
    : addr_port_match (a1, a2);
 
554
}
 
555
 
 
556
static inline bool
 
557
link_socket_actual_match (const struct link_socket_actual *a1, const struct link_socket_actual *a2)
 
558
{
 
559
  return addr_port_match (&a1->dest, &a2->dest);
 
560
}
 
561
 
 
562
#if PORT_SHARE
 
563
 
 
564
static inline bool
 
565
socket_foreign_protocol_detected (const struct link_socket *sock)
 
566
{
 
567
  return link_socket_connection_oriented (sock)
 
568
    && sock->stream_buf.port_share_state == PS_FOREIGN;
 
569
}
 
570
 
 
571
static inline const struct buffer *
 
572
socket_foreign_protocol_head (const struct link_socket *sock)
 
573
{
 
574
  return &sock->stream_buf.buf;
 
575
}
 
576
 
 
577
static inline int
 
578
socket_foreign_protocol_sd (const struct link_socket *sock)
 
579
{
 
580
  return sock->sd;
 
581
}
 
582
 
 
583
#endif
 
584
 
 
585
static inline bool
 
586
socket_connection_reset (const struct link_socket *sock, int status)
 
587
{
 
588
  if (link_socket_connection_oriented (sock))
 
589
    {
 
590
      if (sock->stream_reset || sock->stream_buf.error)
 
591
        return true;
 
592
      else if (status < 0)
 
593
        {
 
594
          const int err = openvpn_errno_socket ();
 
595
#ifdef WIN32
 
596
          return err == WSAECONNRESET || err == WSAECONNABORTED;
 
597
#else
 
598
          return err == ECONNRESET;
 
599
#endif
 
600
        }
 
601
    }
 
602
  return false;
 
603
}
 
604
 
 
605
static inline bool
 
606
link_socket_verify_incoming_addr (struct buffer *buf,
 
607
                                  const struct link_socket_info *info,
 
608
                                  const struct link_socket_actual *from_addr)
 
609
{
 
610
  if (buf->len > 0)
 
611
    {
 
612
      if (from_addr->dest.sa.sin_family != AF_INET)
 
613
        return false;
 
614
      if (!link_socket_actual_defined (from_addr))
 
615
        return false;
 
616
      if (info->remote_float || !addr_defined (&info->lsa->remote))
 
617
        return true;
 
618
      if (addr_match_proto (&from_addr->dest, &info->lsa->remote, info->proto))
 
619
        return true;
 
620
    }
 
621
  return false;
 
622
}
 
623
 
 
624
static inline void
 
625
link_socket_get_outgoing_addr (struct buffer *buf,
 
626
                              const struct link_socket_info *info,
 
627
                              struct link_socket_actual **act)
 
628
{
 
629
  if (buf->len > 0)
 
630
    {
 
631
      struct link_socket_addr *lsa = info->lsa;
 
632
      if (link_socket_actual_defined (&lsa->actual))
 
633
        *act = &lsa->actual;
 
634
      else
 
635
        {
 
636
          link_socket_bad_outgoing_addr ();
 
637
          buf->len = 0;
 
638
          *act = NULL;
 
639
        }
 
640
    }
 
641
}
 
642
 
 
643
static inline void
 
644
link_socket_set_outgoing_addr (const struct buffer *buf,
 
645
                               struct link_socket_info *info,
 
646
                               const struct link_socket_actual *act,
 
647
                               const char *common_name,
 
648
                               struct env_set *es)
 
649
{
 
650
  if (!buf || buf->len > 0)
 
651
    {
 
652
      struct link_socket_addr *lsa = info->lsa;
 
653
      if (
 
654
          /* new or changed address? */
 
655
          (!info->connection_established
 
656
           || !addr_match_proto (&act->dest, &lsa->actual.dest, info->proto))
 
657
          /* address undef or address == remote or --float */
 
658
          && (info->remote_float
 
659
              || !addr_defined (&lsa->remote)
 
660
              || addr_match_proto (&act->dest, &lsa->remote, info->proto))
 
661
          )
 
662
        {
 
663
          link_socket_connection_initiated (buf, info, act, common_name, es);
 
664
        }
 
665
    }
 
666
}
 
667
 
 
668
/*
 
669
 * Stream buffer handling -- stream_buf is a helper class
 
670
 * to assist in the packetization of stream transport protocols
 
671
 * such as TCP.
 
672
 */
 
673
 
 
674
void stream_buf_init (struct stream_buf *sb,
 
675
                      struct buffer *buf,
 
676
                      const unsigned int sockflags,
 
677
                      const int proto);
 
678
 
 
679
void stream_buf_close (struct stream_buf* sb);
 
680
bool stream_buf_added (struct stream_buf *sb, int length_added);
 
681
 
 
682
static inline bool
 
683
stream_buf_read_setup (struct link_socket* sock)
 
684
{
 
685
  bool stream_buf_read_setup_dowork (struct link_socket* sock);
 
686
  if (link_socket_connection_oriented (sock))
 
687
    return stream_buf_read_setup_dowork (sock);
 
688
  else
 
689
    return true;
 
690
}
 
691
 
 
692
/*
 
693
 * Socket Read Routines
 
694
 */
 
695
 
 
696
int link_socket_read_tcp (struct link_socket *sock,
 
697
                          struct buffer *buf);
 
698
 
 
699
#ifdef WIN32
 
700
 
 
701
static inline int
 
702
link_socket_read_udp_win32 (struct link_socket *sock,
 
703
                            struct buffer *buf,
 
704
                            struct link_socket_actual *from)
 
705
{
 
706
  return socket_finalize (sock->sd, &sock->reads, buf, from);
 
707
}
 
708
 
 
709
#else
 
710
 
 
711
int link_socket_read_udp_posix (struct link_socket *sock,
 
712
                                struct buffer *buf,
 
713
                                int maxsize,
 
714
                                struct link_socket_actual *from);
 
715
 
 
716
#endif
 
717
 
 
718
/* read a TCP or UDP packet from link */
 
719
static inline int
 
720
link_socket_read (struct link_socket *sock,
 
721
                  struct buffer *buf,
 
722
                  int maxsize,
 
723
                  struct link_socket_actual *from)
 
724
{
 
725
  if (sock->info.proto == PROTO_UDPv4)
 
726
    {
 
727
      int res;
 
728
 
 
729
#ifdef WIN32
 
730
      res = link_socket_read_udp_win32 (sock, buf, from);
 
731
#else
 
732
      res = link_socket_read_udp_posix (sock, buf, maxsize, from);
 
733
#endif
 
734
      return res;
 
735
    }
 
736
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
 
737
    {
 
738
      /* from address was returned by accept */
 
739
      from->dest.sa = sock->info.lsa->actual.dest.sa;
 
740
      return link_socket_read_tcp (sock, buf);
 
741
    }
 
742
  else
 
743
    {
 
744
      ASSERT (0);
 
745
      return -1; /* NOTREACHED */
 
746
    }
 
747
}
 
748
 
 
749
/*
 
750
 * Socket Write routines
 
751
 */
 
752
 
 
753
int link_socket_write_tcp (struct link_socket *sock,
 
754
                           struct buffer *buf,
 
755
                           struct link_socket_actual *to);
 
756
 
 
757
#ifdef WIN32
 
758
 
 
759
static inline int
 
760
link_socket_write_win32 (struct link_socket *sock,
 
761
                         struct buffer *buf,
 
762
                         struct link_socket_actual *to)
 
763
{
 
764
  int err = 0;
 
765
  int status = 0;
 
766
  if (overlapped_io_active (&sock->writes))
 
767
    {
 
768
      status = socket_finalize (sock->sd, &sock->writes, NULL, NULL);
 
769
      if (status < 0)
 
770
        err = WSAGetLastError ();
 
771
    }
 
772
  socket_send_queue (sock, buf, to);
 
773
  if (status < 0)
 
774
    {
 
775
      WSASetLastError (err);
 
776
      return status;
 
777
    }
 
778
  else
 
779
    return BLEN (buf);
 
780
}
 
781
 
 
782
#else
 
783
 
 
784
static inline int
 
785
link_socket_write_udp_posix (struct link_socket *sock,
 
786
                             struct buffer *buf,
 
787
                             struct link_socket_actual *to)
 
788
{
 
789
#if ENABLE_IP_PKTINFO
 
790
  int link_socket_write_udp_posix_sendmsg (struct link_socket *sock,
 
791
                                           struct buffer *buf,
 
792
                                           struct link_socket_actual *to);
 
793
 
 
794
  if (sock->sockflags & SF_USE_IP_PKTINFO)
 
795
    return link_socket_write_udp_posix_sendmsg (sock, buf, to);
 
796
  else
 
797
#endif
 
798
    return sendto (sock->sd, BPTR (buf), BLEN (buf), 0,
 
799
                   (struct sockaddr *) &to->dest.sa,
 
800
                   (socklen_t) sizeof (to->dest.sa));
 
801
}
 
802
 
 
803
static inline int
 
804
link_socket_write_tcp_posix (struct link_socket *sock,
 
805
                             struct buffer *buf,
 
806
                             struct link_socket_actual *to)
 
807
{
 
808
  return send (sock->sd, BPTR (buf), BLEN (buf), MSG_NOSIGNAL);
 
809
}
 
810
 
 
811
#endif
 
812
 
 
813
static inline int
 
814
link_socket_write_udp (struct link_socket *sock,
 
815
                       struct buffer *buf,
 
816
                       struct link_socket_actual *to)
 
817
{
 
818
#ifdef WIN32
 
819
  return link_socket_write_win32 (sock, buf, to);
 
820
#else
 
821
  return link_socket_write_udp_posix (sock, buf, to);
 
822
#endif
 
823
}
 
824
 
 
825
/* write a TCP or UDP packet to link */
 
826
static inline int
 
827
link_socket_write (struct link_socket *sock,
 
828
                   struct buffer *buf,
 
829
                   struct link_socket_actual *to)
 
830
{
 
831
  if (sock->info.proto == PROTO_UDPv4)
 
832
    {
 
833
      return link_socket_write_udp (sock, buf, to);
 
834
    }
 
835
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
 
836
    {
 
837
      return link_socket_write_tcp (sock, buf, to);
 
838
    }
 
839
  else
 
840
    {
 
841
      ASSERT (0);
 
842
      return -1; /* NOTREACHED */
 
843
    }
 
844
}
 
845
 
 
846
#if PASSTOS_CAPABILITY
 
847
 
 
848
/*
 
849
 * Extract TOS bits.  Assumes that ipbuf is a valid IPv4 packet.
 
850
 */
 
851
static inline void
 
852
link_socket_extract_tos (struct link_socket *ls, const struct buffer *ipbuf)
 
853
{
 
854
  if (ls && ipbuf)
 
855
    {
 
856
      struct openvpn_iphdr *iph = (struct openvpn_iphdr *) BPTR (ipbuf);
 
857
      ls->ptos = iph->tos;
 
858
      ls->ptos_defined = true;
 
859
    }
 
860
}
 
861
 
 
862
/*
 
863
 * Set socket properties to reflect TOS bits which were extracted
 
864
 * from tunnel packet.
 
865
 */
 
866
static inline void
 
867
link_socket_set_tos (struct link_socket *ls)
 
868
{
 
869
  if (ls && ls->ptos_defined)
 
870
    setsockopt (ls->sd, IPPROTO_IP, IP_TOS, &ls->ptos, sizeof (ls->ptos));
 
871
}
 
872
 
 
873
#endif
 
874
 
 
875
/*
 
876
 * Socket I/O wait functions
 
877
 */
 
878
 
 
879
static inline bool
 
880
socket_read_residual (const struct link_socket *s)
 
881
{
 
882
  return s && s->stream_buf.residual_fully_formed;
 
883
}
 
884
 
 
885
static inline event_t
 
886
socket_event_handle (const struct link_socket *s)
 
887
{
 
888
#ifdef WIN32
 
889
  return &s->rw_handle;
 
890
#else
 
891
  return s->sd;
 
892
#endif
 
893
}
 
894
 
 
895
event_t socket_listen_event_handle (struct link_socket *s);
 
896
 
 
897
unsigned int
 
898
socket_set (struct link_socket *s,
 
899
            struct event_set *es,
 
900
            unsigned int rwflags,
 
901
            void *arg,
 
902
            unsigned int *persistent);
 
903
 
 
904
static inline void
 
905
socket_set_listen_persistent (struct link_socket *s,
 
906
                              struct event_set *es,
 
907
                              void *arg)
 
908
{
 
909
  if (s && !s->listen_persistent_queued)
 
910
    {
 
911
      event_ctl (es, socket_listen_event_handle (s), EVENT_READ, arg);
 
912
      s->listen_persistent_queued = true;
 
913
    }
 
914
}
 
915
 
 
916
static inline void
 
917
socket_reset_listen_persistent (struct link_socket *s)
 
918
{
 
919
#ifdef WIN32
 
920
  reset_net_event_win32 (&s->listen_handle, s->sd);
 
921
#endif
 
922
}
 
923
 
 
924
const char *socket_stat (const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc);
 
925
 
 
926
#endif /* SOCKET_H */