istream-multiplex.c revision b78f4649c236bf4746c81c1d0e877675f0842ac8
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen/* all multiplex packets are [1 byte cid][4 byte length][data] */
8732bdd21579472feb40da8ffc99b8fd3b341417Timo Sirainen /* channel 0 is main channel */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen unsigned int remain;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstatic ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream);
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainenget_channel(struct multiplex_istream *mstream, uint8_t cid)
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp) {
1631885636d15abaf0375304a17928c8c23782cdTimo Sirainen if (*channelp != NULL && (*channelp)->cid == cid)
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstatic void propagate_error(struct multiplex_istream *mstream, int stream_errno)
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp)
ea1ae54fcfbd8e911978c149f9e7265a45b6380aTimo Sirainen (*channelp)->istream.istream.stream_errno = stream_errno;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstatic void propagate_eof(struct multiplex_istream *mstream)
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Sirainen array_foreach_modifiable(&mstream->channels, channelp) {
f77da594de6318312a7f31589c9e4c38e2b74c73Timo Siraineni_stream_multiplex_read(struct multiplex_istream *mstream, uint8_t cid)
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen struct multiplex_ichannel *req_channel = get_channel(mstream, cid);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen const unsigned char *data;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen (void)i_stream_get_data(mstream->parent, &len);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen (ret = i_stream_read(mstream->parent)) <= 0) {
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen propagate_error(mstream, mstream->parent->stream_errno);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen data = i_stream_get_data(mstream->parent, &len);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* can't return 0 with blocking istreams,
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen so try again from the beginning. */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* is it open? */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen struct istream_private *stream = &channel->istream;
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen bool alloc_ret = i_stream_try_alloc(stream, wanted, &avail);
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen i_stream_set_input_pending(&stream->istream, TRUE);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* dump into buffer */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen i_assert(stream->pos + channel->pending_pos + used <= stream->buffer_size);
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen memcpy(stream->w_buffer + stream->pos + channel->pending_pos,
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen i_stream_set_input_pending(&stream->istream, TRUE);
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen i_assert(stream->pos + used <= stream->buffer_size);
d38ca817bdcec666a3b91efb917064ab844c36a2Timo Sirainen memcpy(stream->w_buffer + stream->pos, data, used);
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen /* see if there is more to read */
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen /* need more data */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen ret = i_stream_multiplex_ichannel_read(&req_channel->istream);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* channel ID */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* data length */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen mstream->remain = be32_to_cpu_unaligned(data+1);
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen propagate_error(mstream, mstream->parent->stream_errno);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstatic ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream)
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* if previous multiplex read dumped data for us
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen actually serve it here. */
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen return i_stream_multiplex_read(channel->mstream, channel->cid);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Siraineni_stream_multiplex_ichannel_close(struct iostream_private *stream, bool close_parent)
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen array_foreach(&channel->mstream->channels, channelp)
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen if (*channelp != NULL && !(*channelp)->closed)
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainenstatic void i_stream_multiplex_try_destroy(struct multiplex_istream *mstream)
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen /* can't do anything until they are all closed */
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen array_foreach_modifiable(&mstream->channels, channelp)
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstatic void i_stream_multiplex_ichannel_destroy(struct iostream_private *stream)
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen i_stream_multiplex_ichannel_close(stream, TRUE);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen array_foreach_modifiable(&channel->mstream->channels, channelp) {
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen i_stream_multiplex_try_destroy(channel->mstream);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstatic struct istream *
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Siraineni_stream_add_channel_real(struct multiplex_istream *mstream, uint8_t cid)
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainen struct multiplex_ichannel *channel = i_new(struct multiplex_ichannel, 1);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen channel->istream.read = i_stream_multiplex_ichannel_read;
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen channel->istream.iostream.close = i_stream_multiplex_ichannel_close;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen channel->istream.iostream.destroy = i_stream_multiplex_ichannel_destroy;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen channel->istream.max_buffer_size = mstream->bufsize;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen channel->istream.istream.blocking = mstream->blocking;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen channel->istream.fd = i_stream_get_fd(mstream->parent);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen array_append(&channel->mstream->channels, &channel, 1);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen return i_stream_create(&channel->istream, NULL, channel->istream.fd);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainenstruct istream *i_stream_multiplex_add_channel(struct istream *stream, uint8_t cid)
cf63dc8723b971cc80638fccbf494d961cbafc7fTimo Sirainen (struct multiplex_ichannel *)stream->real_stream;
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen i_assert(get_channel(chan->mstream, cid) == NULL);
cdf294e5cceee81d58c8477c7c28d9ad6b55c36aTimo Sirainen return i_stream_add_channel_real(chan->mstream, cid);
c154a05c76d4d83de1cdf9746ce4e2da0869705dTimo Sirainenstruct istream *i_stream_create_multiplex(struct istream *parent, size_t bufsize)
c8ff6ff3a9b76f12b0e8269ca7bdd62d895f476bTimo Sirainenuint8_t i_stream_multiplex_get_channel_id(struct istream *stream)