40struct port_share *port_share = NULL;
43#define PROXY_CONNECTION_BUFFER_SIZE 1500
46#define COMMAND_REDIRECT 10
47#define COMMAND_EXIT 11
50#define RESPONSE_INIT_SUCCEEDED 20
51#define RESPONSE_INIT_FAILED 21
57#define IOSTAT_EAGAIN_ON_READ 0
58#define IOSTAT_EAGAIN_ON_WRITE 1
59#define IOSTAT_READ_ERROR 2
60#define IOSTAT_WRITE_ERROR 3
67struct proxy_connection
70 struct proxy_connection *next;
71 struct proxy_connection *counterpart;
103 for (
i = 3;
i <= 100; ++
i)
137 if (size ==
sizeof(
c))
152 if (size ==
sizeof(
c))
168#if defined(__GNUC__) || defined(__clang__)
169#pragma GCC diagnostic push
170#pragma GCC diagnostic ignored "-Wsign-conversion"
194 head ?
BLEN(head) : -1);
200 iov[0].iov_base = &cmd;
201 iov[0].iov_len =
sizeof(cmd);
208 iov[1].iov_base = (
char *)
CBPTR(head);
209 iov[1].iov_len =
BLENZ(head);
215 mesg.msg_controllen = cmsg_size();
216 mesg.msg_control = (
char *)malloc(mesg.msg_controllen);
220 h = CMSG_FIRSTHDR(&mesg);
221 h->cmsg_level = SOL_SOCKET;
222 h->cmsg_type = SCM_RIGHTS;
227 memcpy(CMSG_DATA(h), &sd_send,
sizeof(sd_send));
231 socketpair(PF_UNIX, SOCK_DGRAM, 0, sd_null);
232 memcpy(CMSG_DATA(h), &sd_null[0],
sizeof(sd_null[0]));
239 "PORT SHARE: sendmsg failed -- unable to communicate with background process (%d,%d,%d,%d)",
240 sd, sd_send, sd_null[0], sd_null[1]);
243 close_socket_if_defined(sd_null[0]);
244 close_socket_if_defined(sd_null[1]);
245 free(mesg.msg_control);
250proxy_entry_close_sd(
struct proxy_connection *pc,
struct event_set *es)
268proxy_entry_mark_for_close(
struct proxy_connection *pc,
struct event_set *es)
272 struct proxy_connection *cp = pc->counterpart;
273 proxy_entry_close_sd(pc, es);
275 pc->buffer_initial =
false;
284 if (cp && cp->defined && cp->counterpart == pc)
286 proxy_entry_mark_for_close(cp, es);
296proxy_list_housekeeping(
struct proxy_connection **list)
300 struct proxy_connection *prev = NULL;
301 struct proxy_connection *pc = *list;
305 struct proxy_connection *next = pc->next;
332journal_add(
const char *journal_dir,
const struct proxy_connection *pc,
struct proxy_connection *cp)
336 socklen_t slen =
sizeof(from.addr);
337 socklen_t dlen =
sizeof(to.addr);
338 if (!getpeername(pc->sd, (
struct sockaddr *)&from.addr.sa, &slen)
339 && !getsockname(cp->sd, (
struct sockaddr *)&to.addr.sa, &dlen))
344 size_t fnlen = strlen(journal_dir) + strlen(t) + 2;
345 char *jfn = (
char *)malloc(fnlen);
347 snprintf(jfn, fnlen,
"%s/%s", journal_dir, t);
350 const int flags = O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW;
351 int fd =
platform_open(jfn, flags, S_IRUSR | S_IWUSR | S_IRGRP);
352 if (fd == -1 && errno == EEXIST)
360 const size_t write_len = strlen(f);
361 if (
write(fd, f, write_len) != (ssize_t)write_len)
363 msg(
M_WARN,
"PORT SHARE: writing to journal file (%s) failed", jfn);
370 msg(
M_WARN |
M_ERRNO,
"PORT SHARE: unable to write journal file in %s", jfn);
381proxy_list_close(
struct proxy_connection **
list)
385 struct proxy_connection *pc = *list;
388 proxy_entry_mark_for_close(pc, NULL);
391 proxy_list_housekeeping(list);
396proxy_connection_io_requeue(
struct proxy_connection *pc,
const unsigned int rwflags_new,
403 event_ctl(es, pc->sd, rwflags_new, (
void *)pc);
404 pc->rwflags = rwflags_new;
416proxy_entry_new(
struct proxy_connection **list,
struct event_set *es,
418 const struct buffer *initial_data,
const char *journal_dir)
422 struct proxy_connection *pc;
423 struct proxy_connection *cp;
426 if ((sd_server = socket(server_addr.
addr.
sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
434 msg(
M_WARN,
"PORT SHARE PROXY: connect to port-share server failed");
450 pc->counterpart = cp;
451 pc->buf = *initial_data;
452 pc->buffer_initial =
true;
459 cp->counterpart = pc;
460 cp->buf =
alloc_buf(PROXY_CONNECTION_BUFFER_SIZE);
461 cp->buffer_initial =
false;
471 journal_add(journal_dir, pc, cp);
478 proxy_connection_io_requeue(pc,
EVENT_READ, es);
491control_message_from_parent(
const socket_descriptor_t sd_control,
struct proxy_connection **list,
493 const int max_initial_buf,
const char *journal_dir)
508 iov[0].iov_base = &command;
509 iov[0].iov_len =
sizeof(command);
510 iov[1].iov_base =
BPTR(&buf);
511 iov[1].iov_len =
BCAP(&buf);
515 mesg.msg_controllen = cmsg_size();
516 mesg.msg_control = (
char *)malloc(mesg.msg_controllen);
520 h = CMSG_FIRSTHDR(&mesg);
522 h->cmsg_level = SOL_SOCKET;
523 h->cmsg_type = SCM_RIGHTS;
525 memcpy(CMSG_DATA(h), &socket_undefined,
sizeof(socket_undefined));
531 || h->cmsg_level != SOL_SOCKET || h->cmsg_type != SCM_RIGHTS)
533 msg(
M_WARN,
"PORT SHARE PROXY: received unknown message");
538 memcpy(&received_fd, CMSG_DATA(h),
sizeof(received_fd));
541 if (
status >= 2 && command == COMMAND_REDIRECT)
544 if (proxy_entry_new(list, es, server_addr, received_fd, &buf, journal_dir))
561 free(mesg.msg_control);
567proxy_connection_io_recv(
struct proxy_connection *pc)
573 return (errno == EAGAIN) ? IOSTAT_EAGAIN_ON_READ : IOSTAT_READ_ERROR;
579 return IOSTAT_READ_ERROR;
582 pc->buf.len = (int)
status;
588proxy_connection_io_send(
struct proxy_connection *pc,
int *bytes_sent)
596 return (e == EAGAIN) ? IOSTAT_EAGAIN_ON_WRITE : IOSTAT_WRITE_ERROR;
600 *bytes_sent += (int)
status;
601 if (
status != pc->buf.len)
606 return IOSTAT_EAGAIN_ON_WRITE;
617 if (pc->buffer_initial)
620 pc->buf =
alloc_buf(PROXY_CONNECTION_BUFFER_SIZE);
621 pc->buffer_initial =
false;
626#if defined(__GNUC__) || defined(__clang__)
627#pragma GCC diagnostic pop
635proxy_connection_io_xfer(
struct proxy_connection *pc,
const int max_transfer)
638 while (transferred < max_transfer)
642 const int status = proxy_connection_io_recv(pc);
643 if (
status != IOSTAT_GOOD)
651 const int status = proxy_connection_io_send(pc, &transferred);
652 if (
status != IOSTAT_GOOD)
658 return IOSTAT_EAGAIN_ON_READ;
665proxy_connection_io_status(
const int status,
unsigned int *rwflags_pc,
unsigned int *rwflags_cp)
669 case IOSTAT_EAGAIN_ON_READ:
671 *rwflags_cp &= ~EVENT_WRITE;
674 case IOSTAT_EAGAIN_ON_WRITE:
675 *rwflags_pc &= ~EVENT_READ;
679 case IOSTAT_READ_ERROR:
682 case IOSTAT_WRITE_ERROR:
696proxy_connection_io_dispatch(
struct proxy_connection *pc,
const unsigned int rwflags,
699 const int max_transfer_per_iteration = 10000;
700 struct proxy_connection *cp = pc->counterpart;
701 unsigned int rwflags_pc = pc->rwflags;
702 unsigned int rwflags_cp = cp->rwflags;
704 ASSERT(pc->defined && cp->defined && cp->counterpart == pc);
708 const int status = proxy_connection_io_xfer(pc, max_transfer_per_iteration);
709 if (!proxy_connection_io_status(
status, &rwflags_pc, &rwflags_cp))
716 const int status = proxy_connection_io_xfer(cp, max_transfer_per_iteration);
717 if (!proxy_connection_io_status(
status, &rwflags_cp, &rwflags_pc))
722 proxy_connection_io_requeue(pc, rwflags_pc, es);
723 proxy_connection_io_requeue(cp, rwflags_cp, es);
728 proxy_entry_mark_for_close(pc, es);
737 const int max_initial_buf,
const char *journal_dir)
741 void *sd_control_marker = (
void *)1;
745 struct proxy_connection *list = NULL;
746 time_t last_housekeeping = 0;
762 current = time(NULL);
766 for (i = 0; i < n_events; ++i)
769 if (e->
arg == sd_control_marker)
771 if (!control_message_from_parent(sd_control, &list, es, hostaddr,
772 max_initial_buf, journal_dir))
779 struct proxy_connection *pc = (
struct proxy_connection *)e->
arg;
782 proxy_connection_io_dispatch(pc, e->
rwflags, es);
787 else if (n_events < 0)
791 if (current > last_housekeeping)
793 proxy_list_housekeeping(&list);
794 last_housekeeping = current;
799 proxy_list_close(&list);
802 msg(
M_INFO,
"PORT SHARE PROXY: proxy exiting");
810port_share_open(
const char *host,
const char *port,
const int max_initial_buf,
811 const char *journal_dir)
815 struct port_share *ps;
818 ps->foreground_fd = -1;
819 ps->background_pid = -1;
826 0, NULL, AF_UNSPEC, &ai);
828 ASSERT(
sizeof(hostaddr.
addr) >= ai->ai_addrlen);
829 memcpy(&hostaddr.
addr.
sa, ai->ai_addr, ai->ai_addrlen);
844 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) == -1)
866 ps->background_pid = pid;
882 ps->foreground_fd = fd[0];
887 msg(
M_ERR,
"PORT SHARE: unexpected init recv_control status=%d", recv_status);
902#ifdef ENABLE_MANAGEMENT
914 port_share_proxy(hostaddr, fd[1], max_initial_buf, journal_dir);
923 port_share_close(ps);
928port_share_close(
struct port_share *ps)
932 if (ps->foreground_fd >= 0)
939 if (ps->background_pid > 0)
941 waitpid(ps->background_pid, NULL, 0);
946 ps->foreground_fd = -1;
954port_share_abort(
struct port_share *ps)
959 if (ps->foreground_fd >= 0)
963 ps->foreground_fd = -1;
974is_openvpn_protocol(
const struct buffer *buf)
976 const unsigned char *p = (
const unsigned char *)
CBSTR(buf);
977 const int len =
BLEN(buf);
980 int plen = (p[0] << 8) | p[1];
998 return (plen >= 336 && plen < (1024 + 255));
1004 return plen >= 14 && plen <= 255
1010 int plen = (p[0] << 8) | p[1];
1011 return plen >= 14 && plen <= 255;
1029 port_share_sendmsg(ps->foreground_fd, COMMAND_REDIRECT, head, sd);
static void set_signals(void)
#define RESPONSE_INIT_SUCCEEDED
static int recv_control(int fd)
static int send_control(int fd, int code)
static void close_fds_except(int keep)
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
#define BLEN(buf)
Return the length of the buffer content in bytes.
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
#define CBSTR(buf)
Return the buffer content pointer cast to const char *.
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Data Channel Cryptography Module.
struct event_set * event_set_init(int *maxevents, unsigned int flags)
static void event_free(struct event_set *es)
static void event_del(struct event_set *es, event_t event)
static int event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
void set_nonblock(socket_descriptor_t fd)
void set_cloexec(socket_descriptor_t fd)
static SERVICE_STATUS status
static bool msg_test(msglvl_t flags)
Return true if flags represent an enabled, not muted log level.
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
#define openvpn_close_socket(s)
static const char * print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_arena *gc)
#define P_CONTROL_HARD_RESET_CLIENT_V2
#define P_CONTROL_HARD_RESET_CLIENT_V3
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
Garbage collection arena used to keep track of dynamically allocated memory.
struct gc_entry * list
First element of the linked list of gc_entry structures.
union openvpn_sockaddr::@27 addr
SOCKET socket_descriptor_t
static int socket_defined(const socket_descriptor_t sd)