/* * Do an accept(2) on the sock and return the new lttcomm socket. The socket * MUST be bind(2) before. */ LTTNG_HIDDEN struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock) { int new_fd; socklen_t len; struct lttcomm_sock *new_sock; unsigned long timeout; if (sock->proto == LTTCOMM_SOCK_UDP) { /* * accept(2) does not exist for UDP so simply return the passed socket. */ new_sock = sock; goto end; } new_sock = lttcomm_alloc_sock(sock->proto); if (new_sock == NULL) { goto error; } len = sizeof(new_sock->sockaddr.addr.sin); /* Blocking call */ new_fd = accept(sock->fd, (struct sockaddr *) &new_sock->sockaddr.addr.sin, &len); if (new_fd < 0) { PERROR("accept inet"); goto error; } timeout = lttcomm_get_network_timeout(); if (timeout) { int ret; ret = lttcomm_setsockopt_rcv_timeout(new_fd, timeout); if (ret) { goto error_close; } ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout); if (ret) { goto error_close; } } new_sock->fd = new_fd; new_sock->ops = &inet_ops; end: return new_sock; error_close: if (close(new_fd) < 0) { PERROR("accept inet close fd"); } error: free(new_sock); return NULL; }
/* * Return allocated lttcomm socket structure from lttng URI. */ LTTNG_HIDDEN struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri) { int ret; int _sock_proto; struct lttcomm_sock *sock = NULL; /* Safety net */ assert(uri); /* Check URI protocol */ if (uri->proto == LTTNG_TCP) { _sock_proto = LTTCOMM_SOCK_TCP; } else { ERR("Relayd invalid URI proto: %d", uri->proto); goto alloc_error; } sock = lttcomm_alloc_sock(_sock_proto); if (sock == NULL) { goto alloc_error; } /* Check destination type */ if (uri->dtype == LTTNG_DST_IPV4) { ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4, uri->port); if (ret < 0) { goto error; } } else if (uri->dtype == LTTNG_DST_IPV6) { ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6, uri->port); if (ret < 0) { goto error; } } else { /* Command URI is invalid */ ERR("Relayd invalid URI dst type: %d", uri->dtype); goto error; } return sock; error: lttcomm_destroy_sock(sock); alloc_error: return NULL; }
/* * Return an allocated lttcomm socket structure and copy src content into * the newly created socket. * * This is mostly useful when lttcomm_sock are passed between process where the * fd and ops have to be changed within the correct address space. */ LTTNG_HIDDEN struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src) { struct lttcomm_sock *sock; /* Safety net */ assert(src); sock = lttcomm_alloc_sock(src->proto); if (sock == NULL) { goto alloc_error; } lttcomm_copy_sock(sock, src); alloc_error: return sock; }
/* * Do an accept(2) on the sock and return the new lttcomm socket. The socket * MUST be bind(2) before. */ LTTNG_HIDDEN struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock) { int new_fd; socklen_t len; struct lttcomm_sock *new_sock; if (sock->proto == LTTCOMM_SOCK_UDP) { /* * accept(2) does not exist for UDP so simply return the passed socket. */ new_sock = sock; goto end; } new_sock = lttcomm_alloc_sock(sock->proto); if (new_sock == NULL) { goto error; } len = sizeof(new_sock->sockaddr.addr.sin); /* Blocking call */ new_fd = accept(sock->fd, (struct sockaddr *) &new_sock->sockaddr.addr.sin, &len); if (new_fd < 0) { PERROR("accept inet"); goto error; } new_sock->fd = new_fd; new_sock->ops = &inet_ops; end: return new_sock; error: free(new_sock); return NULL; }