| | varnish-cache/bin/varnishtest/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 <sys/socket.h> |
33 |
|
|
34 |
|
#include <netinet/in.h> |
35 |
|
|
36 |
|
#include <unistd.h> |
37 |
|
|
38 |
|
#include "vtc.h" |
39 |
|
|
40 |
|
#include "vend.h" |
41 |
|
#include "vsa.h" |
42 |
|
#include "vtcp.h" |
43 |
|
|
44 |
|
static const char vpx1_sig[] = {'P', 'R', 'O', 'X', 'Y'}; |
45 |
|
static const char vpx2_sig[] = { |
46 |
|
'\r', '\n', '\r', '\n', '\0', '\r', '\n', |
47 |
|
'Q', 'U', 'I', 'T', '\n', |
48 |
|
}; |
49 |
|
|
50 |
|
static void |
51 |
108 |
vpx_enc_addr(struct vsb *vsb, int proto, const struct suckaddr *s) |
52 |
|
{ |
53 |
|
const struct sockaddr_in *sin4; |
54 |
|
const struct sockaddr_in6 *sin6; |
55 |
|
socklen_t sl; |
56 |
|
|
57 |
108 |
if (proto == PF_INET6) { |
58 |
54 |
sin6 = VSA_Get_Sockaddr(s, &sl); //lint !e826 |
59 |
54 |
AN(sin6); |
60 |
54 |
assert(sl >= sizeof(*sin6)); |
61 |
54 |
VSB_bcat(vsb, &sin6->sin6_addr, sizeof(sin6->sin6_addr)); |
62 |
54 |
} else { |
63 |
54 |
sin4 = VSA_Get_Sockaddr(s, &sl); //lint !e826 |
64 |
54 |
AN(sin4); |
65 |
54 |
assert(sl >= sizeof(*sin4)); |
66 |
54 |
VSB_bcat(vsb, &sin4->sin_addr, sizeof(sin4->sin_addr)); |
67 |
|
} |
68 |
108 |
} |
69 |
|
|
70 |
|
static void |
71 |
108 |
vpx_enc_port(struct vsb *vsb, const struct suckaddr *s) |
72 |
|
{ |
73 |
|
uint8_t b[2]; |
74 |
|
|
75 |
108 |
vbe16enc(b, (uint16_t)VSA_Port(s)); |
76 |
108 |
VSB_bcat(vsb, b, sizeof(b)); |
77 |
108 |
} |
78 |
|
|
79 |
|
int |
80 |
270 |
vtc_send_proxy(int fd, int version, const struct suckaddr *sac, |
81 |
|
const struct suckaddr *sas) |
82 |
|
{ |
83 |
|
struct vsb *vsb; |
84 |
|
char hc[VTCP_ADDRBUFSIZE]; |
85 |
|
char pc[VTCP_PORTBUFSIZE]; |
86 |
|
char hs[VTCP_ADDRBUFSIZE]; |
87 |
|
char ps[VTCP_PORTBUFSIZE]; |
88 |
|
int i; |
89 |
|
int proto; |
90 |
|
|
91 |
270 |
AN(sac); |
92 |
270 |
AN(sas); |
93 |
|
|
94 |
270 |
assert(version == 1 || version == 2); |
95 |
270 |
vsb = VSB_new_auto(); |
96 |
270 |
AN(vsb); |
97 |
|
|
98 |
270 |
proto = VSA_Get_Proto(sas); |
99 |
270 |
assert(proto == PF_INET6 || proto == PF_INET); |
100 |
|
|
101 |
270 |
if (version == 1) { |
102 |
216 |
VSB_bcat(vsb, vpx1_sig, sizeof(vpx1_sig)); |
103 |
216 |
if (proto == PF_INET6) |
104 |
108 |
VSB_cat(vsb, " TCP6 "); |
105 |
108 |
else if (proto == PF_INET) |
106 |
108 |
VSB_cat(vsb, " TCP4 "); |
107 |
216 |
VTCP_name(sac, hc, sizeof(hc), pc, sizeof(pc)); |
108 |
216 |
VTCP_name(sas, hs, sizeof(hs), ps, sizeof(ps)); |
109 |
216 |
VSB_printf(vsb, "%s %s %s %s\r\n", hc, hs, pc, ps); |
110 |
270 |
} else if (version == 2) { |
111 |
54 |
VSB_bcat(vsb, vpx2_sig, sizeof(vpx2_sig)); |
112 |
54 |
VSB_putc(vsb, 0x21); |
113 |
54 |
if (proto == PF_INET6) { |
114 |
27 |
VSB_putc(vsb, 0x21); |
115 |
27 |
VSB_putc(vsb, 0x00); |
116 |
27 |
VSB_putc(vsb, 0x24); |
117 |
54 |
} else if (proto == PF_INET) { |
118 |
27 |
VSB_putc(vsb, 0x11); |
119 |
27 |
VSB_putc(vsb, 0x00); |
120 |
27 |
VSB_putc(vsb, 0x0c); |
121 |
27 |
} |
122 |
54 |
vpx_enc_addr(vsb, proto, sac); |
123 |
54 |
vpx_enc_addr(vsb, proto, sas); |
124 |
54 |
vpx_enc_port(vsb, sac); |
125 |
54 |
vpx_enc_port(vsb, sas); |
126 |
54 |
} else |
127 |
0 |
WRONG("Wrong proxy version"); |
128 |
|
|
129 |
270 |
AZ(VSB_finish(vsb)); |
130 |
270 |
i = VSB_tofile(vsb, fd); |
131 |
270 |
VSB_destroy(&vsb); |
132 |
270 |
return (i); |
133 |
|
} |