bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi/* all multiplex packets are [1 byte cid][4 byte length][data] */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* channel 0 is main channel */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomiget_channel(struct multiplex_istream *mstream, uint8_t cid)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_foreach_modifiable(&mstream->channels, channelp) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void propagate_error(struct multiplex_istream *mstream, int stream_errno)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_foreach_modifiable(&mstream->channels, channelp)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi (*channelp)->istream.istream.stream_errno = stream_errno;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void propagate_eof(struct multiplex_istream *mstream)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_foreach_modifiable(&mstream->channels, channelp) {
45ba3f8b55734aa2c96b6fc510919565ba70038cTimo Sirainen (*channelp)->istream.istream.stream_errno = EPIPE;
45ba3f8b55734aa2c96b6fc510919565ba70038cTimo Sirainen io_stream_set_error(&(*channelp)->istream.iostream,
45ba3f8b55734aa2c96b6fc510919565ba70038cTimo Sirainen "Unexpected EOF - %u bytes remaining in packet",
f01fe9df4240c5b15d0b1c4b485a38f7c5736781Timo Siraineni_stream_multiplex_read(struct multiplex_istream *mstream,
b78f4649c236bf4746c81c1d0e877675f0842ac8Timo Sirainen (void)i_stream_get_data(mstream->parent, &len);
bcd286622779a93f809b11993db0550f8c7cc9b5Timo Sirainen (ret = i_stream_read_memarea(mstream->parent)) <= 0) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi propagate_error(mstream, mstream->parent->stream_errno);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi if (len == 0) {
c50acc5547b9d92784825a09e3de97db0a806f4eTimo Sirainen /* can't return 0 with blocking istreams,
c50acc5547b9d92784825a09e3de97db0a806f4eTimo Sirainen so try again from the beginning. */
f01fe9df4240c5b15d0b1c4b485a38f7c5736781Timo Sirainen return i_stream_multiplex_read(mstream, req_channel);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* is it open? */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream_private *stream = &channel->istream;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi bool alloc_ret = i_stream_try_alloc(stream, wanted, &avail);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi i_stream_set_input_pending(&stream->istream, TRUE);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* dump into buffer */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi i_assert(stream->pos + channel->pending_pos + used <= stream->buffer_size);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi memcpy(stream->w_buffer + stream->pos + channel->pending_pos,
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi i_stream_set_input_pending(&stream->istream, TRUE);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi i_assert(stream->pos + used <= stream->buffer_size);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi memcpy(stream->w_buffer + stream->pos, data, used);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* see if there is more to read */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* need more data */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi ret = i_stream_multiplex_ichannel_read(&req_channel->istream);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* channel ID */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* data length */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi propagate_error(mstream, mstream->parent->stream_errno);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* if previous multiplex read dumped data for us
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi actually serve it here. */
f01fe9df4240c5b15d0b1c4b485a38f7c5736781Timo Sirainen return i_stream_multiplex_read(channel->mstream, channel);
038c2831447440bf0bef89b43dd0968afc298abcStephan Boschi_stream_multiplex_ichannel_switch_ioloop_to(struct istream_private *stream,
36aa1b351b8e9d02a93aa17e5de8a06ac5440084Timo Sirainen struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
038c2831447440bf0bef89b43dd0968afc298abcStephan Bosch i_stream_switch_ioloop_to(channel->mstream->parent, ioloop);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomii_stream_multiplex_ichannel_close(struct iostream_private *stream, bool close_parent)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_foreach(&channel->mstream->channels, channelp)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void i_stream_multiplex_try_destroy(struct multiplex_istream *mstream)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* can't do anything until they are all closed */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_foreach_modifiable(&mstream->channels, channelp)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void i_stream_multiplex_ichannel_destroy(struct iostream_private *stream)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_foreach_modifiable(&channel->mstream->channels, channelp) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic struct istream *
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomii_stream_add_channel_real(struct multiplex_istream *mstream, uint8_t cid)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct multiplex_ichannel *channel = i_new(struct multiplex_ichannel, 1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi channel->istream.read = i_stream_multiplex_ichannel_read;
038c2831447440bf0bef89b43dd0968afc298abcStephan Bosch channel->istream.switch_ioloop_to = i_stream_multiplex_ichannel_switch_ioloop_to;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi channel->istream.iostream.close = i_stream_multiplex_ichannel_close;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi channel->istream.iostream.destroy = i_stream_multiplex_ichannel_destroy;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi channel->istream.max_buffer_size = mstream->bufsize;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi channel->istream.istream.blocking = mstream->blocking;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi channel->istream.fd = i_stream_get_fd(mstream->parent);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi array_append(&channel->mstream->channels, &channel, 1);
2974dca6be5120e49279f06c8aa952e5fac56048Timo Sirainen return i_stream_create(&channel->istream, NULL, channel->istream.fd, 0);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistruct istream *i_stream_multiplex_add_channel(struct istream *stream, uint8_t cid)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi i_assert(get_channel(chan->mstream, cid) == NULL);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi return i_stream_add_channel_real(chan->mstream, cid);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistruct istream *i_stream_create_multiplex(struct istream *parent, size_t bufsize)