| | varnish-cache/bin/varnishtest/vtest2/src/vtc_proxy.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2015 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
*/ |
| 29 |
|
|
| 30 |
|
#include "config.h" |
| 31 |
|
|
| 32 |
|
#include <stdlib.h> |
| 33 |
|
#include <string.h> |
| 34 |
|
#include <sys/socket.h> |
| 35 |
|
|
| 36 |
|
#include <netinet/in.h> |
| 37 |
|
|
| 38 |
|
#include <unistd.h> |
| 39 |
|
|
| 40 |
|
#include "vtc.h" |
| 41 |
|
|
| 42 |
|
#include "vend.h" |
| 43 |
|
#include "vsa.h" |
| 44 |
|
#include "vtcp.h" |
| 45 |
|
|
| 46 |
|
static const char vpx1_sig[] = {'P', 'R', 'O', 'X', 'Y'}; |
| 47 |
|
static const char vpx2_sig[] = { |
| 48 |
|
'\r', '\n', '\r', '\n', '\0', '\r', '\n', |
| 49 |
|
'Q', 'U', 'I', 'T', '\n', |
| 50 |
|
}; |
| 51 |
|
|
| 52 |
|
//lint -esym(750, PP2_*) |
| 53 |
|
#define PP2_TYPE_ALPN 0x01 |
| 54 |
|
#define PP2_TYPE_AUTHORITY 0x02 |
| 55 |
|
#define PP2_TYPE_CRC32C 0x03 |
| 56 |
|
#define PP2_TYPE_NOOP 0x04 |
| 57 |
|
#define PP2_TYPE_UNIQUE_ID 0x05 |
| 58 |
|
#define PP2_TYPE_SSL 0x20 |
| 59 |
|
#define PP2_SUBTYPE_SSL_VERSION 0x21 |
| 60 |
|
#define PP2_SUBTYPE_SSL_CN 0x22 |
| 61 |
|
#define PP2_SUBTYPE_SSL_CIPHER 0x23 |
| 62 |
|
#define PP2_SUBTYPE_SSL_SIG_ALG 0x24 |
| 63 |
|
#define PP2_SUBTYPE_SSL_KEY_ALG 0x25 |
| 64 |
|
#define PP2_SUBTYPE_SSL_MAX 0x25 |
| 65 |
|
#define PP2_TYPE_NETNS 0x30 |
| 66 |
|
|
| 67 |
|
struct pp2_type { |
| 68 |
|
const char * name; |
| 69 |
|
uint8_t type; |
| 70 |
|
}; |
| 71 |
|
|
| 72 |
|
/* sorted ! */ |
| 73 |
|
static const struct pp2_type pp2_types[] = { |
| 74 |
|
{"alpn", PP2_TYPE_ALPN}, |
| 75 |
|
{"authority", PP2_TYPE_AUTHORITY}, |
| 76 |
|
{"crc32c", PP2_TYPE_CRC32C}, |
| 77 |
|
{"netns", PP2_TYPE_NETNS}, |
| 78 |
|
{"noop", PP2_TYPE_NOOP}, |
| 79 |
|
{"unique_id", PP2_TYPE_UNIQUE_ID} |
| 80 |
|
}; |
| 81 |
|
|
| 82 |
|
static int |
| 83 |
240 |
pp2cmp(const void *va, const void *vb) |
| 84 |
|
{ |
| 85 |
240 |
const struct pp2_type *a = va; |
| 86 |
240 |
const struct pp2_type *b = vb; |
| 87 |
240 |
return (strcmp(a->name, b->name)); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
void |
| 91 |
80 |
vtc_proxy_tlv(struct vtclog *vl, struct vsb *vsb, const char *kva) |
| 92 |
|
{ |
| 93 |
|
struct pp2_type *pp2, needle; |
| 94 |
80 |
char *save = NULL, *kv; |
| 95 |
|
struct vsb *vsb2; |
| 96 |
|
const char *p; |
| 97 |
|
uint16_t le; |
| 98 |
|
ssize_t sz; |
| 99 |
|
|
| 100 |
80 |
kv = strdup(kva); |
| 101 |
80 |
AN(kv); |
| 102 |
|
|
| 103 |
80 |
p = strtok_r(kv, "=", &save); |
| 104 |
80 |
AN(p); |
| 105 |
80 |
if (p[0] == '0' && p[1] == 'x') { |
| 106 |
0 |
p += 2; |
| 107 |
0 |
vsb2 = vtc_hex_to_bin(vl, p); |
| 108 |
0 |
AN(vsb2); |
| 109 |
0 |
if (VSB_len(vsb2) != 1) |
| 110 |
0 |
vtc_fatal(vl, "tlv hex type has wrong length"); |
| 111 |
0 |
VSB_bcat(vsb, VSB_data(vsb2), 1); |
| 112 |
0 |
VSB_destroy(&vsb2); |
| 113 |
0 |
} |
| 114 |
|
else { |
| 115 |
80 |
needle = (typeof(needle)){p, 0}; |
| 116 |
80 |
pp2 = bsearch(&needle, pp2_types, sizeof pp2_types / sizeof pp2_types[0], |
| 117 |
|
sizeof pp2_types[0], pp2cmp); |
| 118 |
80 |
if (pp2 == NULL) |
| 119 |
0 |
vtc_fatal(vl, "tlv type %s not found", p); |
| 120 |
80 |
VSB_putc(vsb, pp2->type); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
80 |
p = strtok_r(NULL, "", &save); |
| 124 |
80 |
if (p == NULL) |
| 125 |
0 |
vtc_fatal(vl, "tlv value missing"); |
| 126 |
80 |
if (p[0] == '0' && p[1] == 'x') |
| 127 |
0 |
vsb2 = vtc_hex_to_bin(vl, p + 2); |
| 128 |
|
else { |
| 129 |
80 |
vsb2 = VSB_new_auto(); |
| 130 |
80 |
AN(vsb2); |
| 131 |
80 |
VSB_cat(vsb2, p); |
| 132 |
80 |
AZ(VSB_finish(vsb2)); |
| 133 |
|
} |
| 134 |
80 |
AN(vsb2); |
| 135 |
80 |
free(kv); |
| 136 |
|
|
| 137 |
80 |
sz = VSB_len(vsb2); |
| 138 |
80 |
assert(sz >= 0); |
| 139 |
80 |
assert(sz <= UINT16_MAX); |
| 140 |
|
|
| 141 |
80 |
vbe16enc(&le, (uint16_t)sz); |
| 142 |
80 |
assert(sizeof(le) == 2); |
| 143 |
80 |
VSB_bcat(vsb, &le, 2); |
| 144 |
80 |
VSB_bcat(vsb, VSB_data(vsb2), sz); |
| 145 |
80 |
VSB_destroy(&vsb2); |
| 146 |
80 |
} |
| 147 |
|
|
| 148 |
|
static void |
| 149 |
320 |
vpx_enc_addr(struct vsb *vsb, int proto, const struct suckaddr *s) |
| 150 |
|
{ |
| 151 |
|
const struct sockaddr_in *sin4; |
| 152 |
|
const struct sockaddr_in6 *sin6; |
| 153 |
|
socklen_t sl; |
| 154 |
|
|
| 155 |
320 |
if (proto == PF_INET6) { |
| 156 |
160 |
sin6 = VSA_Get_Sockaddr(s, &sl); //lint !e826 |
| 157 |
160 |
AN(sin6); |
| 158 |
160 |
assert(sl >= sizeof(*sin6)); |
| 159 |
160 |
VSB_bcat(vsb, &sin6->sin6_addr, sizeof(sin6->sin6_addr)); |
| 160 |
160 |
} else { |
| 161 |
160 |
sin4 = VSA_Get_Sockaddr(s, &sl); //lint !e826 |
| 162 |
160 |
AN(sin4); |
| 163 |
160 |
assert(sl >= sizeof(*sin4)); |
| 164 |
160 |
VSB_bcat(vsb, &sin4->sin_addr, sizeof(sin4->sin_addr)); |
| 165 |
|
} |
| 166 |
320 |
} |
| 167 |
|
|
| 168 |
|
static void |
| 169 |
320 |
vpx_enc_port(struct vsb *vsb, const struct suckaddr *s) |
| 170 |
|
{ |
| 171 |
|
uint8_t b[2]; |
| 172 |
|
|
| 173 |
320 |
vbe16enc(b, (uint16_t)VSA_Port(s)); |
| 174 |
320 |
VSB_bcat(vsb, b, sizeof(b)); |
| 175 |
320 |
} |
| 176 |
|
|
| 177 |
|
int |
| 178 |
480 |
vtc_send_proxy(int fd, int version, const struct suckaddr *sac, |
| 179 |
|
const struct suckaddr *sas, struct vsb *tlv) |
| 180 |
|
{ |
| 181 |
|
struct vsb *vsb; |
| 182 |
|
char hc[VTCP_ADDRBUFSIZE]; |
| 183 |
|
char pc[VTCP_PORTBUFSIZE]; |
| 184 |
|
char hs[VTCP_ADDRBUFSIZE]; |
| 185 |
|
char ps[VTCP_PORTBUFSIZE]; |
| 186 |
|
uint16_t le, l; |
| 187 |
|
int i; |
| 188 |
|
int proto; |
| 189 |
|
|
| 190 |
480 |
AN(sac); |
| 191 |
480 |
AN(sas); |
| 192 |
|
|
| 193 |
480 |
assert(version == 1 || version == 2); |
| 194 |
480 |
vsb = VSB_new_auto(); |
| 195 |
480 |
AN(vsb); |
| 196 |
|
|
| 197 |
480 |
proto = VSA_Get_Proto(sas); |
| 198 |
480 |
assert(proto == PF_INET6 || proto == PF_INET); |
| 199 |
|
|
| 200 |
480 |
if (tlv == NULL) |
| 201 |
0 |
l = 0; |
| 202 |
|
else |
| 203 |
480 |
l = VSB_len(tlv); |
| 204 |
|
|
| 205 |
480 |
assert(l <= UINT16_MAX - 0x24); |
| 206 |
|
|
| 207 |
480 |
if (version == 1) { |
| 208 |
320 |
VSB_bcat(vsb, vpx1_sig, sizeof(vpx1_sig)); |
| 209 |
320 |
if (proto == PF_INET6) |
| 210 |
160 |
VSB_cat(vsb, " TCP6 "); |
| 211 |
160 |
else if (proto == PF_INET) |
| 212 |
160 |
VSB_cat(vsb, " TCP4 "); |
| 213 |
320 |
VTCP_name(sac, hc, sizeof(hc), pc, sizeof(pc)); |
| 214 |
320 |
VTCP_name(sas, hs, sizeof(hs), ps, sizeof(ps)); |
| 215 |
320 |
VSB_printf(vsb, "%s %s %s %s\r\n", hc, hs, pc, ps); |
| 216 |
480 |
} else if (version == 2) { |
| 217 |
160 |
VSB_bcat(vsb, vpx2_sig, sizeof(vpx2_sig)); |
| 218 |
160 |
VSB_putc(vsb, 0x21); |
| 219 |
160 |
if (proto == PF_INET6) { |
| 220 |
80 |
VSB_putc(vsb, 0x21); |
| 221 |
80 |
l += 0x24; |
| 222 |
160 |
} else if (proto == PF_INET) { |
| 223 |
80 |
VSB_putc(vsb, 0x11); |
| 224 |
80 |
l += 0x0c; |
| 225 |
80 |
} else |
| 226 |
0 |
WRONG("proto"); |
| 227 |
|
|
| 228 |
160 |
vbe16enc(&le, l); |
| 229 |
160 |
assert(sizeof(le) == 2); |
| 230 |
160 |
VSB_bcat(vsb, &le, 2); |
| 231 |
160 |
vpx_enc_addr(vsb, proto, sac); |
| 232 |
160 |
vpx_enc_addr(vsb, proto, sas); |
| 233 |
160 |
vpx_enc_port(vsb, sac); |
| 234 |
160 |
vpx_enc_port(vsb, sas); |
| 235 |
160 |
} else |
| 236 |
0 |
WRONG("Wrong proxy version"); |
| 237 |
|
|
| 238 |
480 |
AZ(VSB_finish(vsb)); |
| 239 |
480 |
i = VSB_tofile(vsb, fd); |
| 240 |
480 |
VSB_destroy(&vsb); |
| 241 |
480 |
if (i != 0 && tlv != NULL) |
| 242 |
0 |
VSB_destroy(&tlv); |
| 243 |
480 |
if (i != 0 || tlv == NULL) |
| 244 |
0 |
return (i); |
| 245 |
|
|
| 246 |
480 |
i = VSB_tofile(tlv, fd); |
| 247 |
480 |
VSB_destroy(&tlv); |
| 248 |
480 |
return (i); |
| 249 |
480 |
} |