istream-multiplex.c revision 36aa1b351b8e9d02a93aa17e5de8a06ac5440084
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen/* all multiplex packets are [1 byte cid][4 byte length][data] */
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen /* channel 0 is main channel */
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen unsigned int remain;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenstatic ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenget_channel(struct multiplex_istream *mstream, uint8_t cid)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (*channelp != NULL && (*channelp)->cid == cid)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void propagate_error(struct multiplex_istream *mstream, int stream_errno)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen (*channelp)->istream.istream.stream_errno = stream_errno;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void propagate_eof(struct multiplex_istream *mstream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Siraineni_stream_multiplex_read(struct multiplex_istream *mstream, uint8_t cid)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen struct multiplex_ichannel *req_channel = get_channel(mstream, cid);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen const unsigned char *data;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen (void)i_stream_get_data(mstream->parent, &len);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen (ret = i_stream_read(mstream->parent)) <= 0) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen propagate_error(mstream, mstream->parent->stream_errno);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen data = i_stream_get_data(mstream->parent, &len);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen /* can't return 0 with blocking istreams,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen so try again from the beginning. */
380dbb60ae291cbe39d1f710284562ca9167150bTimo Sirainen /* is it open? */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct istream_private *stream = &channel->istream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen bool alloc_ret = i_stream_try_alloc(stream, wanted, &avail);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen i_stream_set_input_pending(&stream->istream, TRUE);
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen /* dump into buffer */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_assert(stream->pos + channel->pending_pos + used <= stream->buffer_size);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen memcpy(stream->w_buffer + stream->pos + channel->pending_pos,
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen i_stream_set_input_pending(&stream->istream, TRUE);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_assert(stream->pos + used <= stream->buffer_size);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen memcpy(stream->w_buffer + stream->pos, data, used);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* see if there is more to read */
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen /* need more data */
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen ret = i_stream_multiplex_ichannel_read(&req_channel->istream);
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen /* channel ID */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* data length */
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen mstream->remain = be32_to_cpu_unaligned(data+1);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen propagate_error(mstream, mstream->parent->stream_errno);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream)
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* if previous multiplex read dumped data for us
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen actually serve it here. */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return i_stream_multiplex_read(channel->mstream, channel->cid);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Siraineni_stream_multiplex_ichannel_switch_ioloop(struct istream_private *stream)
50e20db49f29917fe9adcf1b56b11badf28bd0e4Timo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen i_stream_switch_ioloop(channel->mstream->parent);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Siraineni_stream_multiplex_ichannel_close(struct iostream_private *stream, bool close_parent)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen array_foreach(&channel->mstream->channels, channelp)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (*channelp != NULL && !(*channelp)->closed)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void i_stream_multiplex_try_destroy(struct multiplex_istream *mstream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen /* can't do anything until they are all closed */
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void i_stream_multiplex_ichannel_destroy(struct iostream_private *stream)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen i_stream_multiplex_ichannel_close(stream, TRUE);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen array_foreach_modifiable(&channel->mstream->channels, channelp) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen i_stream_multiplex_try_destroy(channel->mstream);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic struct istream *
a64adf62fa33f2463a86f990217b0c9078531a40Timo Siraineni_stream_add_channel_real(struct multiplex_istream *mstream, uint8_t cid)
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen struct multiplex_ichannel *channel = i_new(struct multiplex_ichannel, 1);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen channel->istream.read = i_stream_multiplex_ichannel_read;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen channel->istream.switch_ioloop = i_stream_multiplex_ichannel_switch_ioloop;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen channel->istream.iostream.close = i_stream_multiplex_ichannel_close;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen channel->istream.iostream.destroy = i_stream_multiplex_ichannel_destroy;
383d0e8c24451468d6bea17e4b55d74de744abe6Timo Sirainen channel->istream.max_buffer_size = mstream->bufsize;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen channel->istream.istream.blocking = mstream->blocking;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen channel->istream.fd = i_stream_get_fd(mstream->parent);
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen array_append(&channel->mstream->channels, &channel, 1);
2024157e8de36edd31f5fd72f5ea7364a0955fa7Timo Sirainen return i_stream_create(&channel->istream, NULL, channel->istream.fd);
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainenstruct istream *i_stream_multiplex_add_channel(struct istream *stream, uint8_t cid)
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen (struct multiplex_ichannel *)stream->real_stream;
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen i_assert(get_channel(chan->mstream, cid) == NULL);
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen return i_stream_add_channel_real(chan->mstream, cid);
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainenstruct istream *i_stream_create_multiplex(struct istream *parent, size_t bufsize)
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainenuint8_t i_stream_multiplex_get_channel_id(struct istream *stream)