Skip to content

Commit

Permalink
lib: connection - Record and update connection properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanbosch committed Mar 1, 2019
1 parent b730d27 commit 1c4dc94
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
45 changes: 41 additions & 4 deletions src/lib/connection.c
Expand Up @@ -221,6 +221,38 @@ void connection_input_resume(struct connection *conn)
}
}

static void
connection_update_properties(struct connection *conn)
{
int fd = (conn->fd_in < 0 ? conn->fd_out : conn->fd_in);
struct net_unix_cred cred;

if (conn->remote_ip.family != 0)
i_assert(conn->remote_port != 0);
else if (conn->fd_in != conn->fd_out || fd < 0 ||
net_getpeername(fd, &conn->remote_ip,
&conn->remote_port) < 0 ||
conn->remote_ip.family == 0) {
conn->remote_ip.family = 0;
conn->remote_port = 0;

if (conn->unix_peer_known) {
/* already known */
} else if (fd < 0 || errno == ENOTSOCK ||
net_getunixcred(fd, &cred) < 0) {
conn->remote_uid = (uid_t)-1;
conn->remote_pid = (pid_t)-1;
} else {
conn->remote_pid = cred.pid;
conn->remote_uid = cred.uid;
}
conn->unix_peer_known = TRUE;
} else {
conn->remote_uid = (uid_t)-1;
conn->remote_pid = (pid_t)-1;
}
}

static void connection_init_streams(struct connection *conn)
{
const struct connection_settings *set = &conn->list->set;
Expand Down Expand Up @@ -284,6 +316,8 @@ static void connection_client_connected(struct connection *conn, bool success)

i_assert(conn->list->set.client);

connection_update_properties(conn);

conn->connect_finished = ioloop_timeval;
event_add_timeval(conn->event, "connect_finished_time",
&ioloop_timeval);
Expand Down Expand Up @@ -335,6 +369,8 @@ connection_init_full(struct connection_list *list, struct connection *conn,
DLLIST_PREPEND(&list->connections, conn);
list->connections_count++;
}

connection_update_properties(conn);
connection_set_default_handlers(conn);
}

Expand Down Expand Up @@ -398,8 +434,6 @@ void connection_init_client_ip_from(struct connection_list *list,
if (name == NULL)
name = t_strdup_printf("%s:%u", net_ip2addr(ip), port);

connection_init(list, conn, name);

conn->remote_ip = *ip;
conn->remote_port = port;

Expand All @@ -408,6 +442,8 @@ void connection_init_client_ip_from(struct connection_list *list,
else
i_zero(&conn->local_ip);

connection_init(list, conn, name);

if (my_ip != NULL)
event_add_str(conn->event, "client_ip", net_ip2addr(my_ip));
event_add_str(conn->event, "ip", net_ip2addr(ip));
Expand All @@ -428,10 +464,10 @@ void connection_init_client_unix(struct connection_list *list,
{
i_assert(list->set.client);

connection_init(list, conn, path);

conn->unix_socket = TRUE;

connection_init(list, conn, path);

event_field_clear(conn->event, "ip");
event_field_clear(conn->event, "port");
event_field_clear(conn->event, "client_ip");
Expand Down Expand Up @@ -513,6 +549,7 @@ int connection_client_connect(struct connection *conn)

if (conn->remote_port != 0 ||
conn->list->set.delayed_unix_client_connected_callback) {
connection_update_properties(conn);
conn->io = io_add_to(conn->ioloop, conn->fd_out, IO_WRITE,
connection_socket_connected, conn);
e_debug(conn->event,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/connection.h
Expand Up @@ -126,9 +126,11 @@ struct connection {
struct event *event_parent;
struct event *event;

/* for IP client: */
/* connection properties */
struct ip_addr local_ip, remote_ip;
in_port_t remote_port;
pid_t remote_pid;
uid_t remote_uid;

/* received minor version */
unsigned int minor_version;
Expand All @@ -141,6 +143,7 @@ struct connection {
bool version_received:1;
bool handshake_received:1;
bool unix_socket:1;
bool unix_peer_known:1;
bool disconnected:1;
};

Expand Down

0 comments on commit 1c4dc94

Please sign in to comment.