Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing stream_interface structures |
| 3 | * |
| 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <common/compat.h> |
| 23 | #include <common/config.h> |
| 24 | #include <common/debug.h> |
| 25 | #include <common/standard.h> |
| 26 | #include <common/ticks.h> |
| 27 | #include <common/time.h> |
| 28 | |
| 29 | #include <proto/buffers.h> |
| 30 | #include <proto/client.h> |
| 31 | #include <proto/fd.h> |
Willy Tarreau | 269358d | 2009-09-20 20:14:49 +0200 | [diff] [blame] | 32 | #include <proto/stream_interface.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 33 | #include <proto/stream_sock.h> |
| 34 | #include <proto/task.h> |
| 35 | |
| 36 | /* |
| 37 | * This function only has to be called once after a wakeup event in case of |
| 38 | * suspected timeout. It controls the stream interface timeouts and sets |
| 39 | * si->flags accordingly. It does NOT close anything, as this timeout may |
| 40 | * be used for any purpose. It returns 1 if the timeout fired, otherwise |
| 41 | * zero. |
| 42 | */ |
| 43 | int stream_int_check_timeouts(struct stream_interface *si) |
| 44 | { |
| 45 | if (tick_is_expired(si->exp, now_ms)) { |
| 46 | si->flags |= SI_FL_EXP; |
| 47 | return 1; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 52 | /* to be called only when in SI_ST_DIS with SI_FL_ERR */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 53 | void stream_int_report_error(struct stream_interface *si) |
| 54 | { |
| 55 | if (!si->err_type) |
| 56 | si->err_type = SI_ET_DATA_ERR; |
| 57 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 58 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | si->ib->flags |= BF_READ_ERROR; |
| 60 | } |
| 61 | |
| 62 | /* |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 63 | * Erase any content from input and output buffers, and return a message into |
| 64 | * the output buffer. The message is provided as a "chunk". If it is null, |
| 65 | * then an empty message is used. |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 66 | */ |
| 67 | void stream_int_return(struct stream_interface *si, const struct chunk *msg) |
| 68 | { |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 69 | buffer_erase(si->ib); |
Willy Tarreau | cb359e3 | 2009-09-15 21:23:54 +0200 | [diff] [blame] | 70 | buffer_cut_tail(si->ob); |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 71 | if (msg && msg->len) |
| 72 | buffer_write(si->ob, msg->str, msg->len); |
| 73 | } |
| 74 | |
| 75 | /* |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 76 | * Returns a message to the client ; the connection is shut down for read, |
| 77 | * and the request is cleared so that no server connection can be initiated. |
| 78 | * The buffer is marked for read shutdown on the other side to protect the |
| 79 | * message, and the buffer write is enabled. The message is contained in a |
| 80 | * "chunk". If it is null, then an empty message is used. The reply buffer |
| 81 | * doesn't need to be empty before this. The goal of this function is to |
| 82 | * return error messages to a client. |
| 83 | */ |
| 84 | void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg) |
| 85 | { |
| 86 | buffer_abort(si->ib); |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 87 | buffer_erase(si->ob); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 88 | buffer_shutr_now(si->ob); |
| 89 | if (msg && msg->len) |
| 90 | buffer_write(si->ob, msg->str, msg->len); |
| 91 | |
| 92 | si->ob->wex = tick_add_ifset(now_ms, si->ob->wto); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 93 | buffer_auto_close(si->ob); |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 97 | * Local variables: |
| 98 | * c-indent-level: 8 |
| 99 | * c-basic-offset: 8 |
| 100 | * End: |
| 101 | */ |