static void pop3c_client_connect_ip(struct pop3c_client *client) { struct stat st; client->fd = net_connect_ip(&client->ip, client->set.port, NULL); if (client->fd == -1) { pop3c_client_disconnect(client); return; } client->input = client->raw_input = i_stream_create_fd(client->fd, POP3C_MAX_INBUF_SIZE, FALSE); client->output = client->raw_output = o_stream_create_fd(client->fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(client->output, TRUE); if (*client->set.rawlog_dir != '\0' && client->set.ssl_mode != POP3C_CLIENT_SSL_MODE_IMMEDIATE && stat(client->set.rawlog_dir, &st) == 0) { iostream_rawlog_create(client->set.rawlog_dir, &client->input, &client->output); } client->io = io_add(client->fd, IO_WRITE, pop3c_client_connected, client); client->to = timeout_add(POP3C_CONNECT_TIMEOUT_MSECS, pop3c_client_timeout, client); if (client->set.debug) { i_debug("pop3c(%s): Connecting to %s:%u", client->set.host, net_ip2addr(&client->ip), client->set.port); } }
int server_connection_create(struct doveadm_server *server, struct server_connection **conn_r) { #define DOVEADM_SERVER_HANDSHAKE "VERSION\tdoveadm-server\t1\t0\n" struct server_connection *conn; pool_t pool; pool = pool_alloconly_create("doveadm server connection", 1024*16); conn = p_new(pool, struct server_connection, 1); conn->pool = pool; conn->server = server; conn->fd = doveadm_connect_with_default_port(server->name, doveadm_settings->doveadm_port); net_set_nonblock(conn->fd, TRUE); conn->io = io_add(conn->fd, IO_READ, server_connection_input, conn); conn->input = i_stream_create_fd(conn->fd, MAX_INBUF_SIZE, FALSE); conn->output = o_stream_create_fd(conn->fd, (size_t)-1, FALSE); array_append(&conn->server->connections, &conn, 1); if (server_connection_read_settings(conn) < 0 || server_connection_init_ssl(conn) < 0) { server_connection_destroy(&conn); return -1; } o_stream_set_no_error_handling(conn->output, TRUE); conn->state = SERVER_REPLY_STATE_DONE; o_stream_nsend_str(conn->output, DOVEADM_SERVER_HANDSHAKE); *conn_r = conn; return 0; }
struct client_connection * client_connection_create(int fd, int listen_fd, bool ssl) { struct client_connection *conn; unsigned int port; pool_t pool; pool = pool_alloconly_create("doveadm client", 1024*16); conn = p_new(pool, struct client_connection, 1); conn->pool = pool; conn->fd = fd; conn->io = io_add(fd, IO_READ, client_connection_input, conn); conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); conn->output = o_stream_create_fd(fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(conn->output, TRUE); (void)net_getsockname(fd, &conn->local_ip, &port); (void)net_getpeername(fd, &conn->remote_ip, &port); if (client_connection_read_settings(conn) < 0) { client_connection_destroy(&conn); return NULL; } if (ssl) { if (client_connection_init_ssl(conn) < 0) { client_connection_destroy(&conn); return NULL; } } client_connection_send_auth_handshake(conn, listen_fd); return conn; }
static int lmtp_client_connect(struct lmtp_client *client) { i_assert(client->fd == -1); client->times.connect_started = ioloop_timeval; client->fd = net_connect_ip(&client->ip, client->port, NULL); if (client->fd == -1) { i_error("lmtp client: connect(%s, %u) failed: %m", client->host, client->port); return -1; } client->input = i_stream_create_fd(client->fd, LMTP_MAX_LINE_LEN); client->output = o_stream_create_fd(client->fd, (size_t)-1); o_stream_set_no_error_handling(client->output, TRUE); o_stream_set_flush_callback(client->output, lmtp_client_output, client); /* we're already sending data in ostream, so can't use IO_WRITE here */ client->io = io_add(client->fd, IO_READ, lmtp_client_wait_connect, client); if (client->set.timeout_secs > 0) { client->to = timeout_add(client->set.timeout_secs*1000, lmtp_client_connect_timeout, client); } return 0; }
static void connection_init_streams(struct connection *conn) { const struct connection_settings *set = &conn->list->set; i_assert(conn->io == NULL); i_assert(conn->input == NULL); i_assert(conn->output == NULL); i_assert(conn->to == NULL); conn->version_received = set->major_version == 0; if (set->input_max_size != 0) { conn->input = i_stream_create_fd(conn->fd_in, set->input_max_size, FALSE); i_stream_set_name(conn->input, conn->name); } if (set->output_max_size != 0) { conn->output = o_stream_create_fd(conn->fd_out, set->output_max_size, FALSE); o_stream_set_no_error_handling(conn->output, TRUE); o_stream_set_name(conn->output, conn->name); } conn->io = io_add(conn->fd_in, IO_READ, *conn->list->v.input, conn); if (set->input_idle_timeout_secs != 0) { conn->to = timeout_add(set->input_idle_timeout_secs*1000, connection_idle_timeout, conn); } if (set->major_version != 0 && !set->dont_send_version) { o_stream_nsend_str(conn->output, t_strdup_printf( "VERSION\t%s\t%u\t%u\n", set->service_name_out, set->major_version, set->minor_version)); } }
static void client_open_streams(struct client *client) { client->input = i_stream_create_fd(client->fd, LOGIN_MAX_INBUF_SIZE, FALSE); client->output = o_stream_create_fd(client->fd, LOGIN_MAX_OUTBUF_SIZE, FALSE); }
struct login_connection * login_connection_init(struct director *dir, int fd, struct auth_connection *auth, enum login_connection_type type) { struct login_connection *conn; conn = i_new(struct login_connection, 1); conn->refcount = 1; conn->fd = fd; conn->dir = dir; conn->output = o_stream_create_fd(conn->fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(conn->output, TRUE); if (type != LOGIN_CONNECTION_TYPE_AUTHREPLY) { i_assert(auth != NULL); conn->auth = auth; conn->io = io_add(conn->fd, IO_READ, login_connection_input, conn); auth_connection_set_callback(conn->auth, auth_input_line, conn); } else { i_assert(auth == NULL); conn->input = i_stream_create_fd(conn->fd, IO_BLOCK_SIZE, FALSE); conn->io = io_add(conn->fd, IO_READ, login_connection_authreply_input, conn); o_stream_nsend_str(conn->output, t_strdup_printf( "VERSION\tdirector-authreply-server\t%d\t%d\n", AUTHREPLY_PROTOCOL_MAJOR_VERSION, AUTHREPLY_PROTOCOL_MINOR_VERSION)); } conn->type = type; DLLIST_PREPEND(&login_connections, conn); return conn; }
struct sieve_storage_save_context * sieve_file_storage_save_init(struct sieve_storage *storage, const char *scriptname, struct istream *input) { struct sieve_file_storage *fstorage = (struct sieve_file_storage *)storage; struct sieve_file_save_context *fsctx; pool_t pool; const char *path; int fd; if ( sieve_file_storage_pre_modify(storage) < 0 ) return NULL; if ( scriptname != NULL ) { /* Prevent overwriting the active script link when it resides in the * sieve storage directory. */ i_assert( fstorage->link_path != NULL ); if ( *(fstorage->link_path) == '\0' ) { const char *svext; size_t namelen; svext = strrchr(fstorage->active_fname, '.'); namelen = svext - fstorage->active_fname; if ( svext != NULL && strncmp(svext+1, "sieve", 5) == 0 && strlen(scriptname) == namelen && strncmp(scriptname, fstorage->active_fname, namelen) == 0 ) { sieve_storage_set_error(storage, SIEVE_ERROR_BAD_PARAMS, "Script name `%s' is reserved for internal use.", scriptname); return NULL; } } } T_BEGIN { fd = sieve_file_storage_create_tmp(fstorage, scriptname, &path); if (fd == -1) { fsctx = NULL; } else { pool = pool_alloconly_create("sieve_file_save_context", 1024); fsctx = p_new(pool, struct sieve_file_save_context, 1); fsctx->context.scriptname = p_strdup(pool, scriptname); fsctx->context.input = input; fsctx->context.pool = pool; fsctx->fd = fd; fsctx->output = o_stream_create_fd(fsctx->fd, 0); fsctx->tmp_path = p_strdup(pool, path); } } T_END; return &fsctx->context; }
static void proxy_plain_connected(struct login_proxy *proxy) { proxy->server_input = i_stream_create_fd(proxy->server_fd, MAX_PROXY_INPUT_SIZE); proxy->server_output = o_stream_create_fd(proxy->server_fd, (size_t)-1); o_stream_set_no_error_handling(proxy->server_output, TRUE); proxy->server_io = io_add(proxy->server_fd, IO_READ, proxy_prelogin_input, proxy); }
struct config_connection *config_connection_create(int fd) { struct config_connection *conn; conn = i_new(struct config_connection, 1); conn->fd = fd; conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); conn->output = o_stream_create_fd(fd, (size_t)-1, FALSE); conn->io = io_add(fd, IO_READ, config_connection_input, conn); DLLIST_PREPEND(&config_connections, conn); return conn; }
static struct dns_client *dns_client_create(int fd) { struct dns_client *client; client = i_new(struct dns_client, 1); client->fd = fd; client->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); client->output = o_stream_create_fd(fd, MAX_OUTBUF_SIZE, FALSE); o_stream_set_no_error_handling(client->output, TRUE); client->io = io_add(fd, IO_READ, dns_client_input, client); return client; }
void script_client_init_streams(struct script_client *sclient) { if ( sclient->fd_out >= 0 ) { sclient->script_output = o_stream_create_fd(sclient->fd_out, MAX_OUTBUF_SIZE, FALSE); } if ( sclient->fd_in >= 0 ) { sclient->script_input = i_stream_create_fd(sclient->fd_in, (size_t)-1, FALSE); sclient->io = io_add (sclient->fd_in, IO_READ, script_client_script_input, sclient); } }
void testsuite_result_print (const struct sieve_runtime_env *renv) { struct ostream *out; out = o_stream_create_fd(1, 0, FALSE); o_stream_send_str(out, "\n--"); sieve_result_print(_testsuite_result, renv->scriptenv, out, NULL); o_stream_send_str(out, "--\n\n"); o_stream_destroy(&out); }
static void client_open_streams(struct client *client) { client->input = i_stream_create_fd(client->fd, LOGIN_MAX_INBUF_SIZE, FALSE); client->output = o_stream_create_fd(client->fd, LOGIN_MAX_OUTBUF_SIZE, FALSE); o_stream_set_no_error_handling(client->output, TRUE); if (login_rawlog_dir != NULL) { if (iostream_rawlog_create(login_rawlog_dir, &client->input, &client->output) < 0) login_rawlog_dir = NULL; } }
static void client_handle(int fd) { struct ostream *output; output = o_stream_create_fd(fd, (size_t)-1, TRUE); o_stream_send(output, ssl_params->data, ssl_params->used); if (o_stream_get_buffer_used_size(output) == 0) client_deinit(output); else { o_stream_set_flush_callback(output, client_output_flush, output); } }
static int astream_open_output(struct attachment_istream *astream) { int fd; i_assert(astream->part.temp_fd == -1); fd = astream->set.open_temp_fd(astream->context); if (fd == -1) return -1; astream->part.temp_fd = fd; astream->part.temp_output = o_stream_create_fd(fd, 0); o_stream_cork(astream->part.temp_output); return 0; }
struct istream * i_stream_create_rawlog(struct istream *input, const char *rawlog_path, int rawlog_fd, enum iostream_rawlog_flags flags) { struct ostream *rawlog_output; bool autoclose_fd = (flags & IOSTREAM_RAWLOG_FLAG_AUTOCLOSE) != 0; i_assert(rawlog_path != NULL); i_assert(rawlog_fd != -1); rawlog_output = o_stream_create_fd(rawlog_fd, 0, autoclose_fd); o_stream_set_name(rawlog_output, t_strdup_printf("rawlog(%s)", rawlog_path)); return i_stream_create_rawlog_from_stream(input, rawlog_output, flags); }
int worker_connection_connect(struct worker_connection *conn) { i_assert(conn->fd == -1); conn->fd = net_connect_unix(conn->socket_path); if (conn->fd == -1) { i_error("connect(%s) failed: %m", conn->socket_path); return -1; } conn->io = io_add(conn->fd, IO_READ, worker_connection_input, conn); conn->input = i_stream_create_fd(conn->fd, (size_t)-1); conn->output = o_stream_create_fd(conn->fd, (size_t)-1); o_stream_set_no_error_handling(conn->output, TRUE); o_stream_nsend_str(conn->output, INDEXER_MASTER_HANDSHAKE); return 0; }
struct client *client_create(int fd) { struct client *client; client = i_new(struct client, 1); client->fd = fd; client->io = io_add(fd, IO_READ, client_input, client); client->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); client->output = o_stream_create_fd(fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(client->output, TRUE); o_stream_set_flush_callback(client->output, client_output, client); client->cmd_pool = pool_alloconly_create("cmd pool", 1024); DLLIST_PREPEND(&clients, client); return client; }
struct auth_postfix_connection * auth_postfix_connection_create(struct auth *auth, int fd) { struct auth_postfix_connection *conn; conn = i_new(struct auth_postfix_connection, 1); conn->refcount = 1; conn->fd = fd; conn->auth = auth; conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); conn->output = o_stream_create_fd(fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(conn->output, TRUE); conn->io = io_add(fd, IO_READ, postfix_input, conn); DLLIST_PREPEND(&auth_postfix_connections, conn); return conn; }
void notify_connection_create(int fd, bool fifo) { struct notify_connection *conn; conn = i_new(struct notify_connection, 1); conn->refcount = 1; conn->fd = fd; conn->io = io_add(fd, IO_READ, notify_input, conn); conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); if (!fifo) { conn->output = o_stream_create_fd(fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(conn->output, TRUE); } DLLIST_PREPEND(&conns, conn); }
int auth_server_connection_connect(struct auth_server_connection *conn) { const char *handshake; int fd; i_assert(!conn->connected); i_assert(conn->fd == -1); conn->last_connect = ioloop_time; timeout_remove(&conn->to); /* max. 1 second wait here. */ fd = net_connect_unix_with_retries(conn->client->auth_socket_path, 1000); if (fd == -1) { if (errno == EACCES) { i_error("auth: %s", eacces_error_get("connect", conn->client->auth_socket_path)); } else { i_error("auth: connect(%s) failed: %m", conn->client->auth_socket_path); } return -1; } conn->fd = fd; conn->io = io_add(fd, IO_READ, auth_server_connection_input, conn); conn->input = i_stream_create_fd(fd, AUTH_SERVER_CONN_MAX_LINE_LENGTH); conn->output = o_stream_create_fd(fd, (size_t)-1); conn->connected = TRUE; handshake = t_strdup_printf("VERSION\t%u\t%u\nCPID\t%u\n", AUTH_CLIENT_PROTOCOL_MAJOR_VERSION, AUTH_CLIENT_PROTOCOL_MINOR_VERSION, conn->client->client_pid); if (o_stream_send_str(conn->output, handshake) < 0) { i_warning("Error sending handshake to auth server: %s", o_stream_get_error(conn->output)); auth_server_connection_disconnect(conn, o_stream_get_error(conn->output)); return -1; } conn->to = timeout_add(AUTH_HANDSHAKE_TIMEOUT, auth_client_handshake_timeout, conn); return 0; }
static struct client * client_create_standalone(const char *access_user, const char *const *access_applications, int fd_in, int fd_out, bool debug) { struct client *client; /* always use nonblocking I/O */ net_set_nonblock(fd_in, TRUE); net_set_nonblock(fd_out, TRUE); client = i_new(struct client, 1); i_array_init(&client->access_apps, 16); client->fd_in = fd_in; client->fd_out = fd_out; client->fd_ctrl = -1; if (access_user != NULL && *access_user != '\0') client->access_user = i_strdup(access_user); else { client->access_user = i_strdup("anonymous"); client->access_anonymous = TRUE; } if (access_applications != NULL) { const char *const *apps = access_applications; for (; *apps != NULL; apps++) { char *app = i_strdup(*apps); array_append(&client->access_apps, &app, 1); } } client->debug = debug; client->input = i_stream_create_fd(fd_in, MAX_INBUF_SIZE, FALSE); client->output = o_stream_create_fd(fd_out, (size_t)-1, FALSE); client->io = io_add(fd_in, IO_READ, client_input, client); client->to_idle = timeout_add(CLIENT_IDLE_TIMEOUT_MSECS, client_idle_timeout, client); o_stream_set_flush_callback(client->output, client_output, client); imap_urlauth_worker_client_count++; DLLIST_PREPEND(&imap_urlauth_worker_clients, client); i_set_failure_prefix("imap-urlauth[%s](%s): ", my_pid, client->access_user); return client; }
static int lmtp_client_connect(struct lmtp_client *client) { client->fd = net_connect_ip(&client->ip, client->port, NULL); if (client->fd == -1) { i_error("lmtp client: connect(%s, %u) failed: %m", client->host, client->port); return -1; } client->input = i_stream_create_fd(client->fd, LMTP_MAX_LINE_LEN, FALSE); client->output = o_stream_create_fd(client->fd, (size_t)-1, FALSE); o_stream_set_flush_callback(client->output, lmtp_client_output, client); /* we're already sending data in ostream, so can't use IO_WRITE here */ client->io = io_add(client->fd, IO_READ, lmtp_client_wait_connect, client); return 0; }
struct ostream *sieve_tool_open_output_stream(const char *filename) { struct ostream *outstream; int fd; if ( strcmp(filename, "-") == 0 ) outstream = o_stream_create_fd(1, 0, FALSE); else { if ( (fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0 ) { i_fatal("failed to open file for writing: %m"); } outstream = o_stream_create_fd_autoclose(&fd, 0); } return outstream; }
static void test_ostream_file_random(void) { struct ostream *output; string_t *path = t_str_new(128); char buf[MAX_BUFSIZE*4], buf2[MAX_BUFSIZE*4], randbuf[MAX_BUFSIZE]; unsigned int i, offset, size; ssize_t ret; int fd; memset(buf, 0, sizeof(buf)); fd = safe_mkstemp(path, 0600, (uid_t)-1, (gid_t)-1); if (fd == -1) i_fatal("safe_mkstemp(%s) failed: %m", str_c(path)); if (unlink(str_c(path)) < 0) i_fatal("unlink(%s) failed: %m", str_c(path)); output = o_stream_create_fd(fd, MAX_BUFSIZE, FALSE); o_stream_cork(output); size = (rand() % MAX_BUFSIZE) + 1; random_fill_weak(randbuf, size); memcpy(buf, randbuf, size); test_assert(o_stream_send(output, buf, size) > 0); for (i = 0; i < 10; i++) { offset = rand() % (MAX_BUFSIZE*3); size = (rand() % MAX_BUFSIZE) + 1; random_fill_weak(randbuf, size); memcpy(buf + offset, randbuf, size); test_assert(o_stream_pwrite(output, randbuf, size, offset) == 0); if (rand() % 10 == 0) test_assert(o_stream_flush(output) > 0); } test_assert(o_stream_flush(output) > 0); o_stream_uncork(output); ret = pread(fd, buf2, sizeof(buf2), 0); if (ret < 0) i_fatal("pread() failed: %m"); else { i_assert(ret > 0); test_assert(memcmp(buf, buf2, ret) == 0); } o_stream_unref(&output); i_close_fd(&fd); }
static void replicator_connection_connect(struct replicator_connection *conn) { unsigned int n; int fd = -1; if (conn->fd != -1) return; if (conn->port == 0) { fd = net_connect_unix(conn->path); if (fd == -1) i_error("net_connect_unix(%s) failed: %m", conn->path); } else { for (n = 0; n < conn->ips_count; n++) { unsigned int idx = conn->ip_idx; conn->ip_idx = (conn->ip_idx + 1) % conn->ips_count; fd = net_connect_ip(&conn->ips[idx], conn->port, NULL); if (fd != -1) break; i_error("connect(%s, %u) failed: %m", net_ip2addr(&conn->ips[idx]), conn->port); } } if (fd == -1) { if (conn->to == NULL) { conn->to = timeout_add(REPLICATOR_RECONNECT_MSECS, replicator_connection_connect, conn); } return; } if (conn->to != NULL) timeout_remove(&conn->to); conn->fd = fd; conn->io = io_add(fd, IO_READ, replicator_input, conn); conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); conn->output = o_stream_create_fd(fd, (size_t)-1, FALSE); o_stream_set_no_error_handling(conn->output, TRUE); o_stream_nsend_str(conn->output, REPLICATOR_HANDSHAKE); o_stream_set_flush_callback(conn->output, replicator_output, conn); }
int squat_uidlist_build_init(struct squat_uidlist *uidlist, struct squat_uidlist_build_context **ctx_r) { struct squat_uidlist_build_context *ctx; int ret; i_assert(!uidlist->building); ret = squat_uidlist_open_or_create(uidlist); if (ret == 0 && lseek(uidlist->fd, uidlist->hdr.used_file_size, SEEK_SET) < 0) { i_error("lseek(%s) failed: %m", uidlist->path); ret = -1; } if (ret < 0) { if (uidlist->file_lock != NULL) file_unlock(&uidlist->file_lock); if (uidlist->dotlock != NULL) file_dotlock_delete(&uidlist->dotlock); return -1; } ctx = i_new(struct squat_uidlist_build_context, 1); ctx->uidlist = uidlist; ctx->output = o_stream_create_fd(uidlist->fd, 0); if (ctx->output->offset == 0) { struct squat_uidlist_file_header hdr; memset(&hdr, 0, sizeof(hdr)); o_stream_nsend(ctx->output, &hdr, sizeof(hdr)); } o_stream_cork(ctx->output); i_array_init(&ctx->lists, 10240); i_array_init(&ctx->block_offsets, 128); i_array_init(&ctx->block_end_indexes, 128); ctx->list_start_idx = uidlist->hdr.count; ctx->build_hdr = uidlist->hdr; uidlist->building = TRUE; *ctx_r = ctx; return 0; }
struct auth_worker_client * auth_worker_client_create(struct auth *auth, int fd) { struct auth_worker_client *client; client = i_new(struct auth_worker_client, 1); client->refcount = 1; client->auth = auth; client->fd = fd; client->input = i_stream_create_fd(fd, AUTH_WORKER_MAX_LINE_LENGTH, FALSE); client->output = o_stream_create_fd(fd, (size_t)-1, FALSE); o_stream_set_flush_callback(client->output, auth_worker_output, client); client->io = io_add(fd, IO_READ, auth_worker_input, client); auth_worker_client = client; return client; }
static struct dsync_ibc * cmd_dsync_icb_stream_init(struct dsync_cmd_context *ctx, const char *name, const char *temp_prefix) { if (ctx->input == NULL) { fd_set_nonblock(ctx->fd_in, TRUE); fd_set_nonblock(ctx->fd_out, TRUE); ctx->input = i_stream_create_fd(ctx->fd_in, (size_t)-1, FALSE); ctx->output = o_stream_create_fd(ctx->fd_out, (size_t)-1, FALSE); } i_stream_ref(ctx->input); o_stream_ref(ctx->output); if (ctx->rawlog_path != NULL) { iostream_rawlog_create_path(ctx->rawlog_path, &ctx->input, &ctx->output); } return dsync_ibc_init_stream(ctx->input, ctx->output, name, temp_prefix); }