bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstatic void connection_idle_timeout(struct connection *conn)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen conn->disconnect_reason = CONNECTION_DISCONNECT_IDLE_TIMEOUT;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstatic void connection_connect_timeout(struct connection *conn)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen conn->disconnect_reason = CONNECTION_DISCONNECT_CONNECT_TIMEOUT;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid connection_input_default(struct connection *conn)
a21f618de284dc22a480af1371d5f5cea50a39dfTimo Sirainen case 0: /* allow calling this function for buffered input */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen while (!input->closed && (line = i_stream_next_line(input)) != NULL) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen conn->disconnect_reason = CONNECTION_DISCONNECT_DEINIT;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenint connection_verify_version(struct connection *conn,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char *const *args)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* VERSION <tab> service_name <tab> major version <tab> minor version */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen str_to_uint(args[2], &recv_major_version) < 0 ||
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen str_to_uint(args[3], &conn->minor_version) < 0) {
15dd18994a8a4933e39e2f87233255d0ca82ba3eTimo Sirainen i_error("%s didn't reply with a valid VERSION line: %s",
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (strcmp(args[1], conn->list->set.service_name_in) != 0) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen i_error("%s: Connected to wrong socket type. "
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen "We want '%s', but received '%s'", conn->name,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (recv_major_version != conn->list->set.major_version) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen i_error("%s: Socket supports major version %u, "
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen "but we support only %u (mixed old and new binaries?)",
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenint connection_input_line_default(struct connection *conn, const char *line)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (connection_verify_version(conn, args) < 0)
a5b64f1abb1cb0a9718d5bf7f0ae808072237259Timo Sirainen if (args[0] == NULL && !conn->list->set.allow_empty_args_input) {
a5b64f1abb1cb0a9718d5bf7f0ae808072237259Timo Sirainen i_error("%s: Unexpectedly received empty line", conn->name);
c85f67cfb8e1cef2de2b681debf4703d5818dc01Stephan Boschvoid connection_input_halt(struct connection *conn)
c85f67cfb8e1cef2de2b681debf4703d5818dc01Stephan Boschvoid connection_input_resume(struct connection *conn)
c85f67cfb8e1cef2de2b681debf4703d5818dc01Stephan Bosch const struct connection_settings *set = &conn->list->set;
c85f67cfb8e1cef2de2b681debf4703d5818dc01Stephan Bosch if (conn->from_streams || set->input_max_size != 0) {
c85f67cfb8e1cef2de2b681debf4703d5818dc01Stephan Bosch conn->io = io_add_istream_to(conn->ioloop, conn->input,
c85f67cfb8e1cef2de2b681debf4703d5818dc01Stephan Bosch conn->io = io_add_to(conn->ioloop, conn->fd_in, IO_READ,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstatic void connection_init_streams(struct connection *conn)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const struct connection_settings *set = &conn->list->set;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen conn->version_received = set->major_version == 0;
36e5cd9ab4c86cf9da0762fa327cd5be04397676Aki Tuomi i_stream_switch_ioloop_to(conn->input, conn->ioloop);
a327d9301f593433c228c4cc8cca05c95b37f6fbTimo Sirainen o_stream_set_no_error_handling(conn->output, TRUE);
8a4a0ef8264d95ffb2ba8f6f109f94ea7f3454e8Timo Sirainen o_stream_set_finish_via_child(conn->output, FALSE);
36e5cd9ab4c86cf9da0762fa327cd5be04397676Aki Tuomi o_stream_switch_ioloop_to(conn->output, conn->ioloop);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (set->major_version != 0 && !set->dont_send_version) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen o_stream_nsend_str(conn->output, t_strdup_printf(
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen "VERSION\t%s\t%u\t%u\n", set->service_name_out,
ed567dac7e55ab3e8dd53d9c86c31ebb2032d4dfTimo Sirainenvoid connection_streams_changed(struct connection *conn)
ed567dac7e55ab3e8dd53d9c86c31ebb2032d4dfTimo Sirainen const struct connection_settings *set = &conn->list->set;
ed567dac7e55ab3e8dd53d9c86c31ebb2032d4dfTimo Sirainen if (set->input_max_size != 0 && conn->io != NULL) {
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainenstatic void connection_client_connected(struct connection *conn, bool success)
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen conn->list->v.client_connected(conn, success);
4b9e7a8752803928aa0897f8cc1fc34592452f07Stephan Boschvoid connection_init(struct connection_list *list,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid connection_init_server(struct connection_list *list,
54c6a8135c0ded324f7ae7d0cdf1ef177004ee2dStephan Boschvoid connection_init_client_ip_from(struct connection_list *list,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen conn->name = i_strdup_printf("%s:%u", net_ip2addr(ip), port);
54c6a8135c0ded324f7ae7d0cdf1ef177004ee2dStephan Boschvoid connection_init_client_ip(struct connection_list *list,
54c6a8135c0ded324f7ae7d0cdf1ef177004ee2dStephan Bosch connection_init_client_ip_from(list, conn, ip, port, NULL);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid connection_init_client_unix(struct connection_list *list,
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Boschvoid connection_init_from_streams(struct connection_list *list,
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch struct istream *input, struct ostream *output)
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch o_stream_set_no_error_handling(conn->output, TRUE);
6793538c389d3e725456e3eabb697e2743233646Stephan Boschstatic void connection_socket_connected(struct connection *conn)
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen connection_client_connected(conn, errno == 0);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenint connection_client_connect(struct connection *conn)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const struct connection_settings *set = &conn->list->set;
54c6a8135c0ded324f7ae7d0cdf1ef177004ee2dStephan Bosch (conn->my_ip.family != 0 ? &conn->my_ip : NULL));
54c6a8135c0ded324f7ae7d0cdf1ef177004ee2dStephan Bosch } else if (conn->list->set.unix_client_connect_msecs == 0)
4487c66123ca4830f8afbf4efcd7a260848d0e05Timo Sirainen fd = net_connect_unix_with_retries(conn->name, conn->list->set.unix_client_connect_msecs);
cb3ab2fd5668700a89b274a43595cfbfa1616e4bTimo Sirainen conn->list->set.delayed_unix_client_connected_callback) {
086b73efd1a5812a64acc951366a499d325509a6Stephan Bosch conn->io = io_add_to(conn->ioloop, conn->fd_out, IO_WRITE,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid connection_disconnect(struct connection *conn)
33a80622828063f5be6f743855d5273fabe8ae58Timo Sirainen fd_close_maybe_stdio(&conn->fd_in, &conn->fd_out);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid connection_deinit(struct connection *conn)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen DLLIST_REMOVE(&conn->list->connections, conn);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenint connection_input_read(struct connection *conn)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* buffer full */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen switch (conn->list->set.input_full_behavior) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* disconnected */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* nothing new read */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* something was read */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenconst char *connection_disconnect_reason(struct connection *conn)
c61779cfa45c1684b1f7c462011088bad0b8318cTimo Sirainen return "Deinitializing";
c61779cfa45c1684b1f7c462011088bad0b8318cTimo Sirainen return t_strdup_printf("connect() timed out in %u.%03u secs",
c61779cfa45c1684b1f7c462011088bad0b8318cTimo Sirainen return "Idle timeout";
c61779cfa45c1684b1f7c462011088bad0b8318cTimo Sirainen return t_strdup_printf("connect() failed: %m");
c61779cfa45c1684b1f7c462011088bad0b8318cTimo Sirainen /* fall through */
c61779cfa45c1684b1f7c462011088bad0b8318cTimo Sirainen return io_stream_get_disconnect_reason(conn->input, conn->output);
d951320d498ae0800b677b754dde71574102123bTimo Sirainenconst char *connection_input_timeout_reason(struct connection *conn)
d951320d498ae0800b677b754dde71574102123bTimo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &conn->last_input_tv);
d951320d498ae0800b677b754dde71574102123bTimo Sirainen return t_strdup_printf("No input for %u.%03u secs",
d951320d498ae0800b677b754dde71574102123bTimo Sirainen } else if (conn->connect_finished.tv_sec != 0) {
d951320d498ae0800b677b754dde71574102123bTimo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &conn->connect_finished);
d951320d498ae0800b677b754dde71574102123bTimo Sirainen "No input since connected %u.%03u secs ago",
d951320d498ae0800b677b754dde71574102123bTimo Sirainen int diff = timeval_diff_msecs(&ioloop_timeval, &conn->connect_started);
d951320d498ae0800b677b754dde71574102123bTimo Sirainen return t_strdup_printf("connect() timed out after %u.%03u secs",
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Boschvoid connection_switch_ioloop_to(struct connection *conn,
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Bosch conn->io = io_loop_move_io_to(ioloop, &conn->io);
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Bosch conn->to = io_loop_move_timeout_to(ioloop, &conn->to);
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Bosch i_stream_switch_ioloop_to(conn->input, ioloop);
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Bosch o_stream_switch_ioloop_to(conn->output, ioloop);
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Boschvoid connection_switch_ioloop(struct connection *conn)
ddaf416216a83e71bc1bfc1b6faf2ead9d774613Stephan Bosch connection_switch_ioloop_to(conn, current_ioloop);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenconnection_list_init(const struct connection_settings *set,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen set->input_full_behavior != CONNECTION_BEHAVIOR_ALLOW);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen list->v.input_line = connection_input_line_default;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid connection_list_deinit(struct connection_list **_list)