connection.c revision d951320d498ae0800b677b754dde71574102123b
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2013-2016 Dovecot authors, see the included COPYING file */
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenstatic void connection_idle_timeout(struct connection *conn)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen conn->disconnect_reason = CONNECTION_DISCONNECT_IDLE_TIMEOUT;
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenstatic void connection_connect_timeout(struct connection *conn)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen conn->disconnect_reason = CONNECTION_DISCONNECT_CONNECT_TIMEOUT;
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainenvoid connection_input_default(struct connection *conn)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen case 0: /* allow calling this function for buffered input */
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch while (!input->closed && (line = i_stream_next_line(input)) != NULL) {
c649139f889c02154fc9a153728b81619edb5663Timo Sirainen conn->disconnect_reason = CONNECTION_DISCONNECT_DEINIT;
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Boschint connection_verify_version(struct connection *conn,
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen const char *const *args)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen /* VERSION <tab> service_name <tab> major version <tab> minor version */
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen str_to_uint(args[2], &recv_major_version) < 0 ||
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen str_to_uint(args[3], &conn->minor_version) < 0) {
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen i_error("%s didn't reply with a valid VERSION line",
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen if (strcmp(args[1], conn->list->set.service_name_in) != 0) {
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen i_error("%s: Connected to wrong socket type. "
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen "We want '%s', but received '%s'", conn->name,
df1713bd29d29a3e3f3ebfdf05f929525825a7d3Timo Sirainen if (recv_major_version != conn->list->set.major_version) {
df1713bd29d29a3e3f3ebfdf05f929525825a7d3Timo Sirainen i_error("%s: Socket supports major version %u, "
df1713bd29d29a3e3f3ebfdf05f929525825a7d3Timo Sirainen "but we support only %u (mixed old and new binaries?)",
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Boschint connection_input_line_default(struct connection *conn, const char *line)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen const char *const *args;
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch if (connection_verify_version(conn, args) < 0)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenstatic void connection_init_streams(struct connection *conn)
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch const struct connection_settings *set = &conn->list->set;
c215ca02d468b0e542523df1ed18e5f2d7e63968Timo Sirainen conn->version_received = set->major_version == 0;
88b90ce9dfe1056d1ec5497b95592d30a849e5f1Timo Sirainen conn->io = io_add_istream(conn->input, *conn->list->v.input, conn);
c215ca02d468b0e542523df1ed18e5f2d7e63968Timo Sirainen conn->io = io_add(conn->fd_in, IO_READ, *conn->list->v.input, conn);
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen conn->output = o_stream_create_fd(conn->fd_out,
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen o_stream_set_no_error_handling(conn->output, TRUE);
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch conn->to = timeout_add(set->input_idle_timeout_secs*1000,
f7fa93fb42a5b9d4a7e2f0367f03f920ef7ec443Timo Sirainen if (set->major_version != 0 && !set->dont_send_version) {
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen o_stream_nsend_str(conn->output, t_strdup_printf(
f7fa93fb42a5b9d4a7e2f0367f03f920ef7ec443Timo Sirainen "VERSION\t%s\t%u\t%u\n", set->service_name_out,
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Boschstatic void connection_client_connected(struct connection *conn, bool success)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen conn->list->v.client_connected(conn, success);
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenvoid connection_init_server(struct connection_list *list,
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenvoid connection_init_client_ip(struct connection_list *list,
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen conn->name = i_strdup_printf("%s:%u", net_ip2addr(ip), port);
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenvoid connection_init_client_unix(struct connection_list *list,
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenvoid connection_init_from_streams(struct connection_list *list,
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen struct istream *input, struct ostream *output)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen o_stream_set_no_error_handling(conn->output, TRUE);
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen conn->io = io_add_istream(conn->input, *list->v.input, conn);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstatic void connection_socket_connected(struct connection *conn)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen connection_client_connected(conn, errno == 0);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenint connection_client_connect(struct connection *conn)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const struct connection_settings *set = &conn->list->set;
91c58af8e992d028eb325707904debb58ae80438Timo Sirainen fd = net_connect_ip(&conn->ip, conn->port, NULL);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen else if (conn->list->set.unix_client_connect_msecs == 0)
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen fd = net_connect_unix_with_retries(conn->name, conn->list->set.unix_client_connect_msecs);
34a41fd572d2516549b3a3b340c36730f284612aTimo Sirainen conn->list->set.delayed_unix_client_connected_callback) {
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen conn->to = timeout_add(set->client_connect_timeout_msecs,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenvoid connection_disconnect(struct connection *conn)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen memset(&conn->last_input_tv, 0, sizeof(conn->last_input_tv));
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen if (conn->fd_in != conn->fd_out && close(conn->fd_out) < 0)
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen i_error("close(%s/out) failed: %m", conn->name);
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainenvoid connection_deinit(struct connection *conn)
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen DLLIST_REMOVE(&conn->list->connections, conn);
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainenint connection_input_read(struct connection *conn)
34a41fd572d2516549b3a3b340c36730f284612aTimo Sirainen /* buffer full */
34a41fd572d2516549b3a3b340c36730f284612aTimo Sirainen switch (conn->list->set.input_full_behavior) {
acc4e0a41f1c8ef0559a19c280afc1b97b9e0818Timo Sirainen /* disconnected */
b8835b8a21c617ceb82ddc5a176243faf36aa8f7Timo Sirainen /* nothing new read */
b8835b8a21c617ceb82ddc5a176243faf36aa8f7Timo Sirainen /* something was read */
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainenconst char *connection_disconnect_reason(struct connection *conn)
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen return "Deinitializing";
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch unsigned int msecs =
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch return t_strdup_printf("connect() timed out in %u.%03u secs",
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch return "Idle timeout";
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch return t_strdup_printf("connect() failed: %m");
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch /* fall through */
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch return io_stream_get_disconnect_reason(conn->input, conn->output);
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Boschconst char *connection_input_timeout_reason(struct connection *conn)
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch int diff = timeval_diff_msecs(&ioloop_timeval, &conn->last_input_tv);
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch return t_strdup_printf("No input for %u.%03u secs",
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch } else if (conn->connect_finished.tv_sec != 0) {
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch int diff = timeval_diff_msecs(&ioloop_timeval, &conn->connect_finished);
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch "No input since connected %u.%03u secs ago",
1a115c1eb46e9103f81228dde272852cb78ed4b5Timo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &conn->connect_started);
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Bosch return t_strdup_printf("connect() timed out after %u.%03u secs",
2d7df7973f80011033e8e9fa676d3ff4c14468d8Stephan Boschvoid connection_switch_ioloop(struct connection *conn)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenconnection_list_init(const struct connection_settings *set,
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen set->input_full_behavior != CONNECTION_BEHAVIOR_ALLOW);
48566ca412a7cf3b42512fd0ec112744778e5da0Timo Sirainen list->v.input_line = connection_input_line_default;
c215ca02d468b0e542523df1ed18e5f2d7e63968Timo Sirainenvoid connection_list_deinit(struct connection_list **_list)