File: conn.c

package info (click to toggle)
proftpd-mod-proxy 0.9.2-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,972 kB
  • sloc: perl: 43,469; ansic: 43,171; sh: 3,479; makefile: 247
file content (1601 lines) | stat: -rw-r--r-- 44,838 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
/*
 * ProFTPD - mod_proxy conn implementation
 * Copyright (c) 2012-2022 TJ Saunders
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 */

#include "mod_proxy.h"

#ifdef HAVE_SYS_UIO_H
# include <sys/uio.h>
#endif /* HAVE_SYS_UIO_H */

#include "proxy/conn.h"
#include "proxy/dns.h"
#include "proxy/netio.h"
#include "proxy/inet.h"
#include "proxy/session.h"
#include "proxy/tls.h"
#include "proxy/uri.h"

struct proxy_conn {
  pool *pconn_pool;

  const char *pconn_uri;
  const char *pconn_proto;
  const char *pconn_host;
  const char *pconn_hostport;
  int pconn_port;
  int pconn_tls;

  int pconn_use_dns_srv;
  int pconn_use_dns_txt;

  /* These are only used for DNS SRV, DNS TXT URLs. */
  int pconn_dns_ttl;
  int pconn_dns_timer_id;

  /* Note that these are deliberately NOT 'const', so that they can be
   * scrubbed in the per-session memory space, once backend authentication
   * has occurred.
   */
  char *pconn_username;
  char *pconn_password;

  const pr_netaddr_t *pconn_addr;
  array_header *pconn_addrs;
};

static const char *supported_protocols[] = {
  "ftp",
  "ftp+srv",
  "ftp+txt",
  "ftps",
  "ftps+srv",
  "ftps+txt",
  "sftp",
  "sftp+srv",
  "sftp+txt",

  NULL
};

/* PROXY protocol V2 */
#define PROXY_PROTOCOL_V2_SIGLEN		12
#define PROXY_PROTOCOL_V2_HDRLEN		16
#define PROXY_PROTOCOL_V2_TRANSPORT_STREAM	0x01
#define PROXY_PROTOCOL_V2_FAMILY_INET		0x10
#define PROXY_PROTOCOL_V2_FAMILY_INET6		0x20
#define PROXY_PROTOCOL_V2_ADDRLEN_INET		(4 + 4 + 2 + 2)
#define PROXY_PROTOCOL_V2_ADDRLEN_INET6		(16 + 16 + 2 + 2)
static uint8_t proxy_protocol_v2_sig[PROXY_PROTOCOL_V2_SIGLEN] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";

#define PROXY_PROTOCOL_V2_TLV_ALPN		0x01
#define PROXY_PROTOCOL_V2_TLV_AUTHORITY		0x02
#define PROXY_PROTOCOL_V2_TLV_UNIQUE_ID		0x05
#define PROXY_PROTOCOL_V2_TLV_TLS		0x20
#define PROXY_PROTOCOL_V2_TLV_TLS_VERSION	0x21
#define PROXY_PROTOCOL_V2_TLV_TLS_CN		0x22
#define PROXY_PROTOCOL_V2_TLV_TLS_CIPHER	0x23
#define PROXY_PROTOCOL_V2_TLV_TLS_SIG_ALGO	0x24
#define PROXY_PROTOCOL_V2_TLV_TLS_KEY_ALGO	0x25

static const char *trace_channel = "proxy.conn";

static int supported_protocol(const char *proto) {
  register unsigned int i;

  for (i = 0; supported_protocols[i] != NULL; i++) {
    if (strcmp(proto, supported_protocols[i]) == 0) {
      return 0;
    }
  }

  errno = ENOENT;
  return -1;
}

int proxy_conn_connect_timeout_cb(CALLBACK_FRAME) {
  const struct proxy_session *proxy_sess;
  const pr_netaddr_t *server_addr;

  proxy_sess = pr_table_get(session.notes, "mod_proxy.proxy-session", NULL);
  server_addr = pr_table_get(session.notes, "mod_proxy.proxy-connect-address",
    NULL);

  if (proxy_sess == NULL ||
      server_addr == NULL) {
    /* Do not restart the timer. */
    return 0;
  }

  (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
    "timed out connecting to %s:%d after %d %s",
    pr_netaddr_get_ipstr(server_addr), ntohs(pr_netaddr_get_port(server_addr)),
    proxy_sess->connect_timeout,
    proxy_sess->connect_timeout != 1 ? "seconds" : "second");

  pr_event_generate("mod_proxy.timeout-connect", NULL);

#if 0
  /* XXX We might not want to disconnect the frontend client here, right? */
  pr_log_pri(PR_LOG_NOTICE, "%s", "Connect timed out, disconnected");
  pr_session_disconnect(&proxy_module, PR_SESS_DISCONNECT_TIMEOUT,
    "ProxyTimeoutConnect");
#endif

  /* Do not restart the timer. */
  return 0;
}

static struct proxy_conn *proxy_conn_get_addrs(pool *p, const char *uri,
    struct proxy_conn *pconn) {
  pr_netaddr_t *pconn_addr;

  pconn_addr = (pr_netaddr_t *) pr_netaddr_get_addr(pconn->pconn_pool,
    pconn->pconn_host, &(pconn->pconn_addrs));
  if (pconn_addr == NULL) {
    pr_trace_msg(trace_channel, 2, "unable to resolve '%s' from URI '%s': %s",
      pconn->pconn_host, uri, strerror(errno));
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "unable to resolve '%s' from URI '%s'", pconn->pconn_host, uri);
    errno = EINVAL;
    return NULL;
  }

  if (pr_netaddr_set_port2(pconn_addr, pconn->pconn_port) < 0) {
    int xerrno = errno;

    pr_trace_msg(trace_channel, 3,
      "unable to set port %d from URI '%s': %s", pconn->pconn_port, uri,
      strerror(xerrno));
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "unable to set port %d from URI '%s': %s", pconn->pconn_port, uri,
      strerror(xerrno));
    errno = EINVAL;
    return NULL;
  }

  pconn->pconn_addr = pconn_addr;

  if (pconn->pconn_addrs != NULL) {
    register unsigned int i;
    pr_netaddr_t **elts;

    elts = pconn->pconn_addrs->elts;
    for (i = 0; i < pconn->pconn_addrs->nelts; i++) {
      pr_netaddr_t *elt;

      elt = elts[i];

      if (pr_netaddr_set_port2(elt, pconn->pconn_port) < 0) {
        pr_trace_msg(trace_channel, 3,
          "unable to set port %d from URI '%s': %s", pconn->pconn_port, uri,
          strerror(errno));
      }
    }
  }

  return pconn;
}

static struct proxy_conn *proxy_conn_use_dns_srv_addrs(pool *p, const char *uri,
    struct proxy_conn *pconn, unsigned int flags) {
  int res;
  const char *name;
  proxy_dns_type_e dns_type = PROXY_DNS_SRV;
  array_header *resp = NULL;
  uint32_t srv_ttl = 0;

  name = pconn->pconn_host;

  res = proxy_dns_resolve(pconn->pconn_pool, name, dns_type, &resp, &srv_ttl);
  if (res > 0) {
    pr_netaddr_t **elts, *first_addr;

    elts = resp->elts;

    /* Slightly naughty way to pop the first address of the array. */
    first_addr = elts[0];
    resp->elts = &(elts[1]);
    resp->nelts--;

    pconn->pconn_addr = first_addr;
    pconn->pconn_port = ntohs(pr_netaddr_get_port(first_addr));
    pconn->pconn_addrs = resp;

    pconn->pconn_dns_ttl = (int) srv_ttl;

    if (flags & PROXY_CONN_CREATE_FL_USE_DNS_TTL) {
      /* XXX TODO: Schedule timer for re-resolving URL on TTL.
       *
       * The existing Timer API does not provide room for custom "user data"
       * pointers; need to fix that.  In the mean time, we'll just need to track
       * things ourselves with a lookup table: timer ID -> pconn.
       *
       * This has the advantage of providing a way to iterate through the table,
       * removing all timer IDs (then destroying the table) in a session
       * process.
       *
       * What memory pool should be used for this table, that would be available
       * at startup time?  proxy_pool?
       *
       * pconn->pconn_dns_timer_id = pr_timer_add(pconn->pconn_dns_ttl, -1,
       *   &proxy_module, proxy_conn_resolve_cb, ...);
       */
    }

    return pconn;
  }

  /* Always fall back to normal name resolution. */
  return proxy_conn_get_addrs(p, uri, pconn);
}

static struct proxy_conn *proxy_conn_use_dns_txt_addrs(pool *p, const char *uri,
    struct proxy_conn *pconn, unsigned int flags) {
  int res;
  const char *name;
  proxy_dns_type_e dns_type = PROXY_DNS_TXT;
  array_header *resp = NULL;

  name = pconn->pconn_host;

  res = proxy_dns_resolve(p, name, dns_type, &resp, NULL);
  if (res > 0) {
    register unsigned int i;
    const char **elts;

    elts = resp->elts;
    for (i = 0; i < resp->nelts; i++) {
      const char *elt;
      char *scheme, *host;
      unsigned int port;
      int str_flags = PR_STR_FL_IGNORE_CASE;
      struct proxy_conn *elt_pconn;

      elt = elts[i];

      /* Many domains have multiple TXT records, for SPF, domain validation,
       * etc.  So we are only interested in any TXT records are that valid
       * (to us) URLs.
       */

      res = proxy_uri_parse(p, elt, &scheme, &host, &port, NULL, NULL);
      if (res < 0) {
        pr_trace_msg(trace_channel, 19,
          "skipping non-URL TXT record '%s' discovered for '%s'", elt, uri);
        continue;
      }

      /* If the URL found in a TXT record itself uses a DNS SRV or TXT
       * variant, skip it.  That way lies circular madness.
       */
      if (pr_strnrstr(scheme, 0, "+srv", 0, str_flags) == TRUE ||
          pr_strnrstr(scheme, 0, "+txt", 0, str_flags) == TRUE) {
        pr_trace_msg(trace_channel, 19,
          "skipping URL TXT record '%s' discovered for '%s'", elt, uri);
        continue;
      }

      elt_pconn = (struct proxy_conn *) proxy_conn_create(p, elt, 0);
      if (elt_pconn != NULL) {
        destroy_pool(pconn->pconn_pool);
        return elt_pconn;
      }
    }
  }

  /* Always fall back to normal name resolution. */
  return proxy_conn_get_addrs(p, uri, pconn);
}

const struct proxy_conn *proxy_conn_create(pool *p, const char *uri,
    unsigned int flags) {
  int res, xerrno;
  int use_dns_srv = FALSE, use_dns_txt = FALSE, use_tls = PROXY_TLS_ENGINE_AUTO;
  char *ptr = NULL;
  char hostport[512], *proto, *remote_host, *username = NULL, *password = NULL;
  unsigned int remote_port;
  struct proxy_conn *pconn, *pconn2;
  pool *pconn_pool;

  if (p == NULL ||
      uri == NULL) {
    errno = EINVAL;
    return NULL;
  }

  res = proxy_uri_parse(p, uri, &proto, &remote_host, &remote_port, &username,
    &password);
  if (res < 0) {
    return NULL;
  }

  if (supported_protocol(proto) < 0) {
    pr_trace_msg(trace_channel, 4, "unsupported protocol '%s' in URI '%.100s'",
      proto, uri);
    errno = EPERM;
    return NULL;
  }

  if (strcmp(proto, "ftps") == 0 ||
      strncmp(proto, "ftps+", 5) == 0) {
    /* If the 'ftps' scheme is used, then FTPS is REQUIRED for connections
     * to this server.
     */
    use_tls = PROXY_TLS_ENGINE_ON;

    /* We automatically (and only) use implicit FTPS for port 990.  Note that
     * we do NOT support implicit FTPS for URLs using DNS SRV, TXT.
     */
    if (strcmp(proto, "ftps") == 0 &&
        remote_port == PROXY_TLS_IMPLICIT_FTPS_PORT) {
      use_tls = PROXY_TLS_ENGINE_IMPLICIT;
    }

  } else if (strcmp(proto, "sftp") == 0 ||
             strncmp(proto, "sftp+", 5) == 0) {
    /* As might be obvious, do not try to use TLS against an SSH2/SFTP
     * server.
     */
    use_tls = PROXY_TLS_ENGINE_OFF;
  }

  if (pr_strnrstr(proto, 0, "+srv", 0, PR_STR_FL_IGNORE_CASE) == TRUE) {
    use_dns_srv = TRUE;
  }

  if (pr_strnrstr(proto, 0, "+txt", 0, PR_STR_FL_IGNORE_CASE) == TRUE) {
    use_dns_txt = TRUE;
  }

  memset(hostport, '\0', sizeof(hostport));
  snprintf(hostport, sizeof(hostport)-1, "%s:%u", remote_host, remote_port);

  pconn_pool = pr_pool_create_sz(p, 128); 
  pr_pool_tag(pconn_pool, "proxy connection pool");

  pconn = pcalloc(pconn_pool, sizeof(struct proxy_conn));
  pconn->pconn_pool = pconn_pool;
  pconn->pconn_host = pstrdup(pconn_pool, remote_host);
  pconn->pconn_port = remote_port;
  pconn->pconn_hostport = pstrdup(pconn_pool, hostport);
  pconn->pconn_uri = pstrdup(pconn_pool, uri);
  pconn->pconn_tls = use_tls;
  pconn->pconn_use_dns_srv = use_dns_srv;
  pconn->pconn_use_dns_txt = use_dns_txt;

  /* Adjust the proto (scheme, actually) to account for possible DNS SRV,
   * TXT usage.
   */
  ptr = strchr(proto, '+');
  if (ptr != NULL) {
    pconn->pconn_proto = pstrndup(pconn_pool, proto, ptr - proto);

  } else {
    pconn->pconn_proto = pstrdup(pconn_pool, proto);
  }

  if (username != NULL) {
    pconn->pconn_username = pstrdup(pconn_pool, username);
  }
  if (password != NULL) {
    pconn->pconn_password = pstrdup(pconn_pool, password);
  }

  /* Here is where we discover the addresses for this URI.  We might use
   * DNS SRV, DNS TXT, or normal DNS A/AAAA records.
   */

  if (use_dns_srv == TRUE ||
      use_dns_txt == TRUE) {
    pr_trace_msg(trace_channel, 5,
      "ignoring port %u from URI '%.100s' since port will be discovered "
      "from %s DNS records", remote_port, uri, use_dns_srv ? "SRV" : "TXT");
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "ignoring port %u from URI '%.100s' since port will be discovered "
      "from %s DNS records", remote_port, uri, use_dns_srv ? "SRV" : "TXT");
  }

  if (use_dns_srv == TRUE) {
    pconn2 = proxy_conn_use_dns_srv_addrs(p, uri, pconn, flags);
    xerrno = errno;

  } else if (use_dns_txt == TRUE) {
    pconn2 = proxy_conn_use_dns_txt_addrs(p, uri, pconn, flags);
    xerrno = errno;

  } else {
    pconn2 = proxy_conn_get_addrs(p, uri, pconn);
    xerrno = errno;
  }

  if (pconn2 == NULL) {
    destroy_pool(pconn->pconn_pool);
    errno = xerrno;
    return NULL;
  }

  return pconn2;
}

void proxy_conn_free(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    return;
  }

  destroy_pool(pconn->pconn_pool);
}

const pr_netaddr_t *proxy_conn_get_addr(const struct proxy_conn *pconn,
    array_header **addrs) {
  if (pconn == NULL) {
    errno = EINVAL;
    return NULL;
  }

  if (addrs != NULL) {
    *addrs = pconn->pconn_addrs;
  }

  return pconn->pconn_addr;
}

int proxy_conn_get_dns_ttl(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return -1;
  }

  /* We really only care about/honor DNS TTLs for the DNS SRV. */
  if (pconn->pconn_use_dns_srv == FALSE) {
    errno = EPERM;
    return -1;
  }

  if (pconn->pconn_dns_ttl <= 0) {
    errno = ENOENT;
    return -1;
  }

  return pconn->pconn_dns_ttl;
}

const char *proxy_conn_get_host(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return NULL;
  }

  return pconn->pconn_host;
}

const char *proxy_conn_get_hostport(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return NULL;
  }

  return pconn->pconn_hostport;
}

int proxy_conn_get_port(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return -1;
  }

  return pconn->pconn_port;
}

void proxy_conn_clear_username(const struct proxy_conn *pconn) {
  size_t len;
  struct proxy_conn *conn;

  if (pconn == NULL) {
    return;
  }

  if (pconn->pconn_username == NULL) {
    return;
  }

  len = strlen(pconn->pconn_username);

  conn = (struct proxy_conn *) pconn;
  pr_memscrub(conn->pconn_username, len);
  conn->pconn_username = NULL;
}

const char *proxy_conn_get_username(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return NULL;
  }

  return pconn->pconn_username;
}

void proxy_conn_clear_password(const struct proxy_conn *pconn) {
  size_t len;
  struct proxy_conn *conn;

  if (pconn == NULL) {
    return;
  }

  if (pconn->pconn_password == NULL) {
    return;
  }

  len = strlen(pconn->pconn_password);

  conn = (struct proxy_conn *) pconn;
  pr_memscrub(conn->pconn_password, len);
  conn->pconn_password = NULL;
}

const char *proxy_conn_get_password(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return NULL;
  }
  
  return pconn->pconn_password;
}

int proxy_conn_get_tls(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return -1;
  }

  return pconn->pconn_tls;
}

int proxy_conn_use_dns_srv(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return -1;
  }

  return pconn->pconn_use_dns_srv;
}

int proxy_conn_use_dns_txt(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return -1;
  }

  return pconn->pconn_use_dns_txt;
}

/* Borrowed from proftpd/src/netaddr.c. */
static int addr_ncmp(const unsigned char *aptr, const unsigned char *bptr,
    unsigned int masklen) {
  unsigned char nbits, nbytes;
  int res;

  nbytes = masklen / 8;
  nbits = masklen % 8;

  res = memcmp(aptr, bptr, nbytes);
  if (res != 0) {
    return -1;
  }

  if (nbits > 0) {
    unsigned char abyte, bbyte, mask;

    abyte = aptr[nbytes];
    bbyte = bptr[nbytes];

    mask = (0xff << (8 - nbits)) & 0xff;

    if ((abyte & mask) > (bbyte & mask)) {
      return 1;
    }

    if ((abyte & mask) < (bbyte & mask)) {
      return -1;
    }
  }

  return 0;
}

static int is_127_xxx_addr(uint32_t addrno) {
  uint32_t rfc1918_addrno;

  rfc1918_addrno = htonl(0x7f000000);
  return addr_ncmp((const unsigned char *) &addrno,
    (const unsigned char *) &rfc1918_addrno, 8);
}

static int netaddr_is_private(const pr_netaddr_t *addr) {
  if (pr_netaddr_is_rfc1918(addr) == TRUE) {
    return TRUE;
  }

  switch (pr_netaddr_get_family(addr)) {
    case AF_INET: {
      uint32_t addrno;

      addrno = pr_netaddr_get_addrno(addr);
      if (is_127_xxx_addr(addrno) == 0) {
        return TRUE;
      }

      break;
    }

#if defined(PR_USE_IPV6)
    case AF_INET6:
      if (pr_netaddr_is_v4mappedv6(addr) == TRUE) {
        pool *tmp_pool;
        pr_netaddr_t *v4addr;
        int res;

        tmp_pool = make_sub_pool(proxy_pool);
        v4addr = pr_netaddr_v6tov4(tmp_pool, addr);

        res = netaddr_is_private(v4addr);
        destroy_pool(tmp_pool);

        return res;
      }
      break;
#endif /* PR_USE_IPV6 */
  }

  return FALSE;
}

conn_t *proxy_conn_get_server_conn(pool *p, struct proxy_session *proxy_sess,
    const pr_netaddr_t *remote_addr) {
  const pr_netaddr_t *bind_addr = NULL, *local_addr = NULL;
  const char *remote_ipstr = NULL;
  unsigned int remote_port;
  conn_t *server_conn, *ctrl_conn;
  int res;

  if (proxy_sess->connect_timeout > 0) {
    const char *notes_key = "mod_proxy.proxy-connect-address";

    proxy_sess->connect_timerno = pr_timer_add(proxy_sess->connect_timeout,
      -1, &proxy_module, proxy_conn_connect_timeout_cb, "ProxyTimeoutConnect");

    (void) pr_table_remove(session.notes, notes_key, NULL);

    if (pr_table_add(session.notes, notes_key, remote_addr,
        sizeof(pr_netaddr_t)) < 0) {
      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error stashing proxy connect address note: %s", strerror(errno));
    }
  }

  remote_ipstr = pr_netaddr_get_ipstr(remote_addr);
  remote_port = ntohs(pr_netaddr_get_port(remote_addr));

  /* Check the family of the retrieved address vs what we'll be using
   * to connect.  If there's a mismatch, we need to get an addr with the
   * matching family.
   */

  if (pr_netaddr_get_family(session.c->local_addr) == pr_netaddr_get_family(remote_addr)) {
    local_addr = session.c->local_addr;

  } else {
    /* In this scenario, the proxy has an IPv6 socket, but the remote/backend
     * server has an IPv4 (or IPv4-mapped IPv6) address.  OR it's the proxy
     * which has an IPv4 socket, and the remote/backend server has an IPv6
     * address.
     */
    if (pr_netaddr_get_family(session.c->local_addr) == AF_INET) {
      char *ip_str;

      /* Convert the local address from an IPv4 to an IPv6 addr. */
      ip_str = pcalloc(p, INET6_ADDRSTRLEN + 1);
      snprintf(ip_str, INET6_ADDRSTRLEN, "::ffff:%s",
        pr_netaddr_get_ipstr(session.c->local_addr));
      local_addr = pr_netaddr_get_addr(p, ip_str, NULL);

    } else {
      local_addr = pr_netaddr_v6tov4(p, session.c->local_addr);
      if (local_addr == NULL) {
        pr_trace_msg(trace_channel, 4,
          "error converting IPv6 local address %s to IPv4 address: %s",
          pr_netaddr_get_ipstr(session.c->local_addr), strerror(errno));

        if (proxy_sess->src_addr == NULL) {
          (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
            "local address '%s' is an IPv6 address, and remote address "
            "'%s' is an IPv4 address; consider using ProxySourceAddress "
            "directive to configure an IPv4 address",
            pr_netaddr_get_ipstr(session.c->local_addr),
            pr_netaddr_get_ipstr(remote_addr));
        }

      } else {
        pr_trace_msg(trace_channel, 9,
          "converted IPv6 local address %s to IPv4 address %s",
          pr_netaddr_get_ipstr(session.c->local_addr),
          pr_netaddr_get_ipstr(local_addr));
      }
    }

    if (local_addr == NULL) {
      local_addr = session.c->local_addr;
    }
  }

  bind_addr = proxy_sess->src_addr;
  if (bind_addr == NULL) {
    bind_addr = local_addr;
  }

  /* Note: IF mod_proxy is running on localhost, and the connection to be
   * made is to a public IP address, then this connect(2) attempt would most
   * likely fail with ENETUNREACH, since localhost is a loopback network,
   * and of course not reachable from a public IP.  Thus we check for this
   * edge case (which happens often for development).
   */
  if (pr_netaddr_is_loopback(bind_addr) == TRUE &&
      pr_netaddr_is_loopback(remote_addr) != TRUE) {
    const char *local_name;
    const pr_netaddr_t *new_local_addr;

    local_name = pr_netaddr_get_localaddr_str(p);
    new_local_addr = pr_netaddr_get_addr(p, local_name, NULL);

    if (new_local_addr != NULL) {
      int local_family, remote_family;

      /* We need to make sure our local address family matches that
       * of the remote address.
       */
      local_family = pr_netaddr_get_family(new_local_addr);
      remote_family = pr_netaddr_get_family(remote_addr);
      if (local_family != remote_family) {
        pr_netaddr_t *new_addr = NULL;

#if defined(PR_USE_IPV6)
        if (local_family == AF_INET) {
          new_addr = pr_netaddr_v4tov6(p, new_local_addr);

        } else {
          new_addr = pr_netaddr_v6tov4(p, new_local_addr);
        }
#endif /* PR_USE_IPV6 */

        if (new_addr != NULL) {
          new_local_addr = new_addr;
        }
      }

      pr_trace_msg(trace_channel, 14,
        "%s is a loopback address, and unable to reach %s; using %s instead",
        pr_netaddr_get_ipstr(bind_addr), remote_ipstr,
        pr_netaddr_get_ipstr(new_local_addr));
      bind_addr = new_local_addr;
    }
  }

  server_conn = pr_inet_create_conn(p, -1, bind_addr, INPORT_ANY, FALSE);
  if (server_conn == NULL) {
    int xerrno = errno;

    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error creating connection to %s: %s", pr_netaddr_get_ipstr(bind_addr),
      strerror(xerrno));

    pr_timer_remove(proxy_sess->connect_timerno, &proxy_module);
    errno = xerrno;
    return NULL;
  }

  pr_trace_msg(trace_channel, 12,
    "connecting to backend address %s#%u from %s#%u", remote_ipstr, remote_port,
    pr_netaddr_get_ipstr(server_conn->local_addr), server_conn->local_port);

  res = pr_inet_connect_nowait(p, server_conn, remote_addr,
    ntohs(pr_netaddr_get_port(remote_addr)));
  if (res < 0) {
    int xerrno = errno;

    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error starting connect to %s#%u: %s", remote_ipstr, remote_port,
      strerror(xerrno));

    /* If there is a mismatch in public/private addresses for our
     * source/destination addresses, it might cause a connection error. Check
     * for those particular errors, and if so, log a suggestion to explicitly
     * configure an appropriate ProxySourceAddress (Issue #213).
     */

    if (pr_netaddr_get_family(bind_addr) == pr_netaddr_get_family(remote_addr)) {
      if (netaddr_is_private(bind_addr) == TRUE) {
        if (netaddr_is_private(remote_addr) != TRUE) {
          (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
            "local address '%s' is a private network address, and remote "
            "address '%s' is a public address; consider using "
            "ProxySourceAddress directive to configure a public local address",
            pr_netaddr_get_ipstr(bind_addr), remote_ipstr);
        }

      } else {
        if (netaddr_is_private(remote_addr) == TRUE) {
          (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
            "local address '%s' is a public address, and remote address '%s' "
            "is a private network address; consider using ProxySourceAddress "
            "directive to configure a private local address",
            pr_netaddr_get_ipstr(bind_addr), remote_ipstr);
        }
      }
    }

    pr_timer_remove(proxy_sess->connect_timerno, &proxy_module);
    errno = xerrno;
    return NULL;
  }

  if (res == 0) {
    pr_netio_stream_t *nstrm;
    int connected = FALSE, nstrm_mode = PR_NETIO_IO_RD, use_tls;

    if ((proxy_opts & PROXY_OPT_USE_PROXY_PROTOCOL_V1) ||
        (proxy_opts & PROXY_OPT_USE_PROXY_PROTOCOL_V2)) {
      /* Rather than waiting for the stream to be readable (because the
       * other end sent us something), wait for the stream to be writable
       * so that we can send something to the other end).
       */
      nstrm_mode = PR_NETIO_IO_WR;
    }

    use_tls = proxy_tls_using_tls();
    if (use_tls == PROXY_TLS_ENGINE_IMPLICIT) {
      /* For implicit FTPS connections, we will be initiating the TLS
       * handshake, and thus we need to wait for the stream to be writable.
       */
      nstrm_mode = PR_NETIO_IO_WR;
    }

    /* Not yet connected. */
    nstrm = proxy_netio_open(p, PR_NETIO_STRM_OTHR, server_conn->listen_fd,
      nstrm_mode);
    if (nstrm == NULL) {
      int xerrno = errno;

      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error opening stream to %s#%u: %s", remote_ipstr, remote_port,
        strerror(xerrno));

      pr_timer_remove(proxy_sess->connect_timerno, &proxy_module);
      pr_inet_close(p, server_conn);

      errno = xerrno;
      return NULL;
    }

    proxy_netio_set_poll_interval(nstrm, 1);

    while (connected == FALSE) {
      int polled;

      pr_signals_handle();

      polled = proxy_netio_poll(nstrm);
      switch (polled) {
        case 1: {
          /* Aborted, timed out.  Note that we shouldn't reach here. */
          int xerrno = ETIMEDOUT;

          (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
            "error connecting to %s#%u: %s", remote_ipstr, remote_port,
            strerror(xerrno));
          pr_timer_remove(proxy_sess->connect_timerno, &proxy_module);
          proxy_netio_close(nstrm);
          pr_inet_close(p, server_conn);

          errno = xerrno;
          return NULL;
        }

        case -1: {
          /* Error */
          int xerrno = nstrm->strm_errno;

          if (xerrno == 0) {
            xerrno = errno;
          }

          if (xerrno == EINTR) {
            /* Treat this as a timeout. */
            xerrno = ETIMEDOUT;

          } else if (xerrno == EOF) {
            xerrno = ECONNREFUSED;
          }

          (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
            "error connecting to %s#%u: %s", remote_ipstr, remote_port,
            strerror(xerrno));

          pr_timer_remove(proxy_sess->connect_timerno, &proxy_module);
          proxy_netio_close(nstrm);
          pr_inet_close(p, server_conn);

          errno = xerrno;
          return NULL;
        }

        default: {
          /* Connected */
          server_conn->mode = CM_OPEN;
          pr_timer_remove(proxy_sess->connect_timerno, &proxy_module);
          pr_table_remove(session.notes, "mod_proxy.proxy-connect-addr", NULL);

          res = pr_inet_get_conn_info(server_conn, server_conn->listen_fd);
          if (res < 0) {
            int xerrno = errno;

            (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
              "error obtaining local socket info on fd %d: %s",
              server_conn->listen_fd, strerror(xerrno));

            proxy_netio_close(nstrm);
            pr_inet_close(p, server_conn);

            errno = xerrno;
            return NULL;
          }

          proxy_netio_reset_poll_interval(nstrm);
          connected = TRUE;
          break;
        }
      }
    }
  }

  pr_trace_msg(trace_channel, 5,
    "successfully connected to %s#%u from %s#%d", remote_ipstr, remote_port,
    pr_netaddr_get_ipstr(server_conn->local_addr),
    ntohs(pr_netaddr_get_port(server_conn->local_addr)));

  ctrl_conn = proxy_inet_openrw(p, server_conn, NULL, PR_NETIO_STRM_CTRL, -1,
    -1, -1, FALSE);
  if (ctrl_conn == NULL) {
    int xerrno = errno;

    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "unable to open control connection to %s#%u: %s", remote_ipstr,
      remote_port, strerror(xerrno));

    pr_inet_close(p, server_conn);

    errno = xerrno;
    return NULL;
  }

  /* Remember that pr_inet_openrw() makes a copy of the input connection;
   * we thus do not need server_conn now.
   */
  pr_inet_close(p, server_conn);

  pr_pool_tag(ctrl_conn->pool, "proxy backend ctrl conn pool");
  return ctrl_conn;
}

const char *proxy_conn_get_uri(const struct proxy_conn *pconn) {
  if (pconn == NULL) {
    errno = EINVAL;
    return NULL;
  }

  return pconn->pconn_uri;
}

int proxy_conn_send_proxy_v1(pool *p, conn_t *conn) {
  int res, src_port, dst_port;
  const char *proto, *src_ipstr, *dst_ipstr;
  pool *sub_pool = NULL;

  if (p == NULL ||
      conn == NULL) {
    errno = EINVAL;
    return -1;
  }

  /* "PROXY" "TCP4"|"TCP6"|"UNKNOWN"
   *   session.c->remote_addr session.c->local_addr
   *   session.c->remote_port, session.c->local_port "\r\n"
   */

  if (pr_netaddr_get_family(session.c->remote_addr) == AF_INET &&
      pr_netaddr_get_family(session.c->local_addr) == AF_INET) {
    proto = "TCP4";
    src_ipstr = pr_netaddr_get_ipstr(session.c->remote_addr);
    src_port = session.c->remote_port;
    dst_ipstr = pr_netaddr_get_ipstr(session.c->local_addr);
    dst_port = session.c->local_port;

  } else {
    proto = "TCP6";
    sub_pool = make_sub_pool(p);

    if (pr_netaddr_get_family(session.c->remote_addr) == AF_INET) {
      const char *ipstr;

      ipstr = pr_netaddr_get_ipstr(session.c->remote_addr);
      src_ipstr = pstrcat(sub_pool, "::ffff:", ipstr, NULL);

    } else {
      src_ipstr = pr_netaddr_get_ipstr(session.c->remote_addr);
    }

    src_port = session.c->remote_port;

    if (pr_netaddr_get_family(session.c->local_addr) == AF_INET) {
      const char *ipstr;

      ipstr = pr_netaddr_get_ipstr(session.c->local_addr);
      dst_ipstr = pstrcat(sub_pool, "::ffff:", ipstr, NULL);

    } else {
      dst_ipstr = pr_netaddr_get_ipstr(session.c->local_addr);
    }

    dst_port = session.c->local_port;

    /* What should we do if the entire frontend connection is IPv6, but the
     * backend server is IPv4?  Sending "PROXY TCP6" there may not work as
     * expected, e.g. the backend server may not want to handle IPv6 addresses
     * (even though it does not have to); should that be handled using
     * "PROXY UNKNOWN"?
     */
    if (pr_netaddr_get_family(conn->remote_addr) == AF_INET) {
      proto = "UNKNOWN";

      pr_trace_msg(trace_channel, 9,
        "client address '%s' and local address '%s' are both IPv6, "
        "but backend address '%s' is IPv4, using '%s' proto", src_ipstr,
        dst_ipstr, pr_netaddr_get_ipstr(conn->remote_addr), proto);
    }
  }

  pr_trace_msg(trace_channel, 9,
    "sending PROXY protocol V1 message: 'PROXY %s %s %s %d %d' to backend",
    proto, src_ipstr, dst_ipstr, src_port, dst_port);

  res = proxy_netio_printf(conn->outstrm, "PROXY %s %s %s %d %d\r\n",
    proto, src_ipstr, dst_ipstr, src_port, dst_port);

  if (sub_pool != NULL) {
    destroy_pool(sub_pool);
  }

  return res;
}

static int writev_conn(conn_t *conn, const struct iovec *iov, int iov_count) {
  int res, xerrno;

  if (pr_netio_poll(conn->outstrm) < 0) {
    return -1;
  }

  res = writev(conn->wfd, iov, iov_count);
  xerrno = errno;

  while (res <= 0) {
    if (res < 0) {
      if (xerrno == EINTR) {
        pr_signals_handle();

        if (pr_netio_poll(conn->outstrm) < 0) {
          return -1;
        }

        res = writev(conn->wfd, iov, iov_count);
        xerrno = errno;

        continue;
      }

      pr_trace_msg(trace_channel, 16,
        "error writing to client (fd %d): %s", conn->wfd, strerror(xerrno));
      errno = errno;
      return -1;
    }
  }

  session.total_raw_out += res;
  return res;
}

static uint16_t add_v2_tlv_alpn(pool *p, struct iovec *v2_iov,
    unsigned int *v2_niov) {
  uint8_t *tlv_type;
  uint16_t *tlv_len, total_len;
  const char *tlv_val;
  size_t tlv_valsz = 0;
  unsigned int niov;

  tlv_type = pcalloc(p, sizeof(uint8_t));
  *tlv_type = PROXY_PROTOCOL_V2_TLV_ALPN;

  tlv_val = pstrdup(p, pr_session_get_protocol(0));
  tlv_valsz = strlen(tlv_val);

  tlv_len = pcalloc(p, sizeof(uint16_t));
  *tlv_len = htons(tlv_valsz);

  niov = *v2_niov;

  v2_iov[niov].iov_base = (void *) tlv_type;
  v2_iov[niov].iov_len = sizeof(uint8_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_len;
  v2_iov[niov].iov_len = sizeof(uint16_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_val;
  v2_iov[niov].iov_len = tlv_valsz;

  /* Make sure to increment niov one more, for the next TLV. */
  *v2_niov = niov + 1;

  total_len = sizeof(uint8_t) + sizeof(uint16_t) + tlv_valsz;
  return total_len;
}

static uint16_t add_v2_tlv_authority(pool *p, struct iovec *v2_iov,
    unsigned int *v2_niov) {
  uint8_t *tlv_type;
  uint16_t *tlv_len, total_len;
  const char *tlv_val;
  size_t tlv_valsz = 0;
  unsigned int niov;
  const void *val = NULL;

  /* Only add the Authority TLV if the client sent an FTP HOST command, or
   * used TLS SNI.
   */

  val = pr_table_get(session.notes, "mod_core.host", NULL);
  if (val == NULL) {
    val = pr_table_get(session.notes, "mod_tls.sni", NULL);
  }

  if (val == NULL) {
    return 0;
  }

  tlv_type = pcalloc(p, sizeof(uint8_t));
  *tlv_type = PROXY_PROTOCOL_V2_TLV_AUTHORITY;

  tlv_val = pstrdup(p, val);
  tlv_valsz = strlen(tlv_val);

  tlv_len = pcalloc(p, sizeof(uint16_t));
  *tlv_len = htons(tlv_valsz);

  niov = *v2_niov;

  v2_iov[niov].iov_base = (void *) tlv_type;
  v2_iov[niov].iov_len = sizeof(uint8_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_len;
  v2_iov[niov].iov_len = sizeof(uint16_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_val;
  v2_iov[niov].iov_len = tlv_valsz;

  /* Make sure to increment niov one more, for the next TLV. */
  *v2_niov = niov + 1;

  total_len = sizeof(uint8_t) + sizeof(uint16_t) + tlv_valsz;
  return total_len;
}

static uint16_t add_v2_tlv_tls(pool *p, struct iovec *v2_iov,
    unsigned int *v2_niov) {
  uint8_t *tlv_type, client;
  uint16_t *tlv_len, total_len;
  uint32_t verify;
  char *tlv_ptr;
  void *tlv_val;
  size_t tlv_valsz = 0, valsz = 0;
  unsigned int niov;
  const char *proto, *tls_version, *tls_common_name, *tls_cipher, *tls_sig_algo, *tls_key_algo;

  /* Only add the SSL TLV if FTPS is in use. */
  proto = pr_session_get_protocol(0);
  if (strcmp(proto, "ftps") != 0) {
    return 0;
  }

  tlv_type = pcalloc(p, sizeof(uint8_t));
  *tlv_type = PROXY_PROTOCOL_V2_TLV_TLS;

  /* This is more complicated, due to the nested nature of TLS sub-TLVs. */

  tls_version = pr_table_get(session.notes, "TLS_PROTOCOL", NULL);
  tls_common_name = pr_table_get(session.notes, "TLS_CLIENT_S_DN_CN", NULL);
  tls_cipher = pr_table_get(session.notes, "TLS_CIPHER", NULL);
  tls_sig_algo = pr_table_get(session.notes, "TLS_CLIENT_A_SIG", NULL);
  tls_key_algo = pr_table_get(session.notes, "TLS_CLIENT_A_KEY", NULL);

  valsz = sizeof(client) + sizeof(verify);

  if (tls_version != NULL) {
    valsz += (sizeof(uint8_t) + sizeof(uint16_t) + strlen(tls_version));
  }

  if (tls_common_name != NULL) {
    valsz += (sizeof(uint8_t) + sizeof(uint16_t) + strlen(tls_common_name));
  }

  if (tls_cipher != NULL) {
    valsz += (sizeof(uint8_t) + sizeof(uint16_t) + strlen(tls_cipher));
  }

  if (tls_sig_algo != NULL) {
    valsz += (sizeof(uint8_t) + sizeof(uint16_t) + strlen(tls_sig_algo));
  }

  if (tls_key_algo != NULL) {
    valsz += (sizeof(uint8_t) + sizeof(uint16_t) + strlen(tls_key_algo));
  }

  tlv_ptr = tlv_val = pcalloc(p, valsz);
  tlv_valsz = valsz;

  /* Client field: 0x01 to indicate TLS was used, 0x02 when a client cert
   * was also presented.
   */
  client = 0x01;
  if (tls_common_name != NULL) {
    client = 0x02;
  }

  memcpy(tlv_ptr, &client, sizeof(client));
  tlv_ptr += sizeof(client);

  /* Verify field; 0 if a client cert was presented, otherwise non-zero. */
  verify = htonl(1);
  if (tls_common_name != NULL) {
    verify = 0;
  }

  memcpy(tlv_ptr, &verify, sizeof(verify));
  tlv_ptr += sizeof(verify);

  if (tls_version != NULL) {
    uint8_t tlv_subtype;
    uint16_t tlv_sublen;
    size_t tlv_subvalsz;

    tlv_subtype = PROXY_PROTOCOL_V2_TLV_TLS_VERSION;
    tlv_subvalsz = strlen(tls_version);
    tlv_sublen = htons(tlv_subvalsz);

    memcpy(tlv_ptr, &tlv_subtype, sizeof(tlv_subtype));
    tlv_ptr += sizeof(tlv_subtype);

    memcpy(tlv_ptr, &tlv_sublen, sizeof(tlv_sublen));
    tlv_ptr += sizeof(tlv_sublen);

    memcpy(tlv_ptr, tls_version, tlv_subvalsz);
    tlv_ptr += tlv_subvalsz;
  }

  if (tls_common_name != NULL) {
    uint8_t tlv_subtype;
    uint16_t tlv_sublen;
    size_t tlv_subvalsz;

    tlv_subtype = PROXY_PROTOCOL_V2_TLV_TLS_CN;
    tlv_subvalsz = strlen(tls_common_name);
    tlv_sublen = htons(tlv_subvalsz);

    memcpy(tlv_ptr, &tlv_subtype, sizeof(tlv_subtype));
    tlv_ptr += sizeof(tlv_subtype);

    memcpy(tlv_ptr, &tlv_sublen, sizeof(tlv_sublen));
    tlv_ptr += sizeof(tlv_sublen);

    memcpy(tlv_ptr, tls_common_name, tlv_subvalsz);
    tlv_ptr += tlv_subvalsz;
  }

  if (tls_cipher != NULL) {
    uint8_t tlv_subtype;
    uint16_t tlv_sublen;
    size_t tlv_subvalsz;

    tlv_subtype = PROXY_PROTOCOL_V2_TLV_TLS_CIPHER;
    tlv_subvalsz = strlen(tls_cipher);
    tlv_sublen = htons(tlv_subvalsz);

    memcpy(tlv_ptr, &tlv_subtype, sizeof(tlv_subtype));
    tlv_ptr += sizeof(tlv_subtype);

    memcpy(tlv_ptr, &tlv_sublen, sizeof(tlv_sublen));
    tlv_ptr += sizeof(tlv_sublen);

    memcpy(tlv_ptr, tls_cipher, tlv_subvalsz);
    tlv_ptr += tlv_subvalsz;
  }

  if (tls_sig_algo != NULL) {
    uint8_t tlv_subtype;
    uint16_t tlv_sublen;
    size_t tlv_subvalsz;

    tlv_subtype = PROXY_PROTOCOL_V2_TLV_TLS_SIG_ALGO;
    tlv_subvalsz = strlen(tls_sig_algo);
    tlv_sublen = htons(tlv_subvalsz);

    memcpy(tlv_ptr, &tlv_subtype, sizeof(tlv_subtype));
    tlv_ptr += sizeof(tlv_subtype);

    memcpy(tlv_ptr, &tlv_sublen, sizeof(tlv_sublen));
    tlv_ptr += sizeof(tlv_sublen);

    memcpy(tlv_ptr, tls_sig_algo, tlv_subvalsz);
    tlv_ptr += tlv_subvalsz;
  }

  if (tls_key_algo != NULL) {
    uint8_t tlv_subtype;
    uint16_t tlv_sublen;
    size_t tlv_subvalsz;

    tlv_subtype = PROXY_PROTOCOL_V2_TLV_TLS_KEY_ALGO;
    tlv_subvalsz = strlen(tls_sig_algo);
    tlv_sublen = htons(tlv_subvalsz);

    memcpy(tlv_ptr, &tlv_subtype, sizeof(tlv_subtype));
    tlv_ptr += sizeof(tlv_subtype);

    memcpy(tlv_ptr, &tlv_sublen, sizeof(tlv_sublen));
    tlv_ptr += sizeof(tlv_sublen);

    memcpy(tlv_ptr, tls_key_algo, tlv_subvalsz);
    tlv_ptr += tlv_subvalsz;
  }

  tlv_len = pcalloc(p, sizeof(uint16_t));
  *tlv_len = htons(tlv_valsz);

  niov = *v2_niov;

  v2_iov[niov].iov_base = (void *) tlv_type;
  v2_iov[niov].iov_len = sizeof(uint8_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_len;
  v2_iov[niov].iov_len = sizeof(uint16_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_val;
  v2_iov[niov].iov_len = tlv_valsz;

  /* Make sure to increment niov one more, for the next TLV. */
  *v2_niov = niov + 1;

  total_len = sizeof(uint8_t) + sizeof(uint16_t) + tlv_valsz;
  return total_len;
}

static uint16_t add_v2_tlv_unique_id(pool *p, struct iovec *v2_iov,
    unsigned int *v2_niov) {
  uint8_t *tlv_type;
  uint16_t *tlv_len, total_len;
  const char *tlv_val;
  size_t tlv_valsz = 0;
  unsigned int niov;
  const void *val = NULL;

  /* Only add the Unique ID TLV if mod_unique_id generated one. */
  val = pr_table_get(session.notes, "UNIQUE_ID", NULL);
  if (val == NULL) {
    return 0;
  }

  tlv_type = pcalloc(p, sizeof(uint8_t));
  *tlv_type = PROXY_PROTOCOL_V2_TLV_UNIQUE_ID;

  tlv_val = pstrdup(p, val);
  tlv_valsz = strlen(tlv_val);

  tlv_len = pcalloc(p, sizeof(uint16_t));
  *tlv_len = htons(tlv_valsz);

  niov = *v2_niov;

  v2_iov[niov].iov_base = (void *) tlv_type;
  v2_iov[niov].iov_len = sizeof(uint8_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_len;
  v2_iov[niov].iov_len = sizeof(uint16_t);

  niov++;
  v2_iov[niov].iov_base = (void *) tlv_val;
  v2_iov[niov].iov_len = tlv_valsz;

  /* Make sure to increment niov one more, for the next TLV. */
  *v2_niov = niov + 1;

  total_len = sizeof(uint8_t) + sizeof(uint16_t) + tlv_valsz;
  return total_len;
}

int proxy_conn_send_proxy_v2(pool *p, conn_t *conn) {
  int res, xerrno;
  uint8_t ver_cmd, trans_fam, src_ipv6[16], dst_ipv6[16];
  uint16_t v2_len, src_port, dst_port;
  uint32_t src_ipv4, dst_ipv4;
  struct iovec v2_iov[20];
  unsigned int v2_niov = 8;
  pool *sub_pool = NULL, *tlv_pool = NULL;
  char *proto;
  const pr_netaddr_t *src_addr = NULL, *dst_addr = NULL;

  if (p == NULL ||
      conn == NULL) {
    errno = EINVAL;
    return -1;
  }

  v2_iov[0].iov_base = (void *) proxy_protocol_v2_sig;
  v2_iov[0].iov_len = PROXY_PROTOCOL_V2_SIGLEN;

  /* PROXY protocol v2 + PROXY command */
  ver_cmd = (0x20|0x01);
  v2_iov[1].iov_base = (void *) &ver_cmd;
  v2_iov[1].iov_len = sizeof(ver_cmd);

  src_addr = session.c->remote_addr;
  dst_addr = session.c->local_addr;

  if (pr_netaddr_get_family(src_addr) == AF_INET &&
      pr_netaddr_get_family(dst_addr) == AF_INET) {
    struct sockaddr_in *saddr;

    proto = "TCP/IPv4";
    trans_fam = (PROXY_PROTOCOL_V2_TRANSPORT_STREAM|PROXY_PROTOCOL_V2_FAMILY_INET);
    v2_len = PROXY_PROTOCOL_V2_ADDRLEN_INET;

    saddr = (struct sockaddr_in *) pr_netaddr_get_sockaddr(src_addr);
    src_ipv4 = saddr->sin_addr.s_addr;
    v2_iov[4].iov_base = (void *) &src_ipv4;
    v2_iov[4].iov_len = sizeof(src_ipv4);

    saddr = (struct sockaddr_in *) pr_netaddr_get_sockaddr(dst_addr);
    dst_ipv4 = saddr->sin_addr.s_addr;
    v2_iov[5].iov_base = (void *) &dst_ipv4;
    v2_iov[5].iov_len = sizeof(dst_ipv4);

    /* Quell compiler warnings about unused variables. */
    (void) src_ipv6;
    (void) dst_ipv6;

  } else {
    struct sockaddr_in6 *saddr;

    proto = "TCP/IPv6";
    trans_fam = (PROXY_PROTOCOL_V2_TRANSPORT_STREAM|PROXY_PROTOCOL_V2_FAMILY_INET6);
    v2_len = PROXY_PROTOCOL_V2_ADDRLEN_INET6;

    sub_pool = make_sub_pool(p);

    if (pr_netaddr_get_family(src_addr) == AF_INET) {
      src_addr = pr_netaddr_v4tov6(sub_pool, src_addr);
    }

    saddr = (struct sockaddr_in6 *) pr_netaddr_get_sockaddr(src_addr);
    memcpy(&src_ipv6, &(saddr->sin6_addr), sizeof(src_ipv6));
    v2_iov[4].iov_base = (void *) &src_ipv6;
    v2_iov[4].iov_len = sizeof(src_ipv6);

    if (pr_netaddr_get_family(dst_addr) == AF_INET) {
      dst_addr = pr_netaddr_v4tov6(sub_pool, dst_addr);
    }

    saddr = (struct sockaddr_in6 *) pr_netaddr_get_sockaddr(dst_addr);
    memcpy(&dst_ipv6, &(saddr->sin6_addr), sizeof(dst_ipv6));
    v2_iov[5].iov_base = (void *) &dst_ipv6;
    v2_iov[5].iov_len = sizeof(dst_ipv6);

    /* Quell compiler warnings about unused variables. */
    (void) src_ipv4;
    (void) dst_ipv4;
  }

  v2_iov[2].iov_base = (void *) &trans_fam;
  v2_iov[2].iov_len = sizeof(trans_fam);

  if (proxy_opts & PROXY_OPT_USE_PROXY_PROTOCOL_V2_TLVS) {
    uint16_t tlv_len;

    tlv_pool = make_sub_pool(p);

    tlv_len = add_v2_tlv_alpn(tlv_pool, v2_iov, &v2_niov);
    if (tlv_len > 0) {
      v2_len += tlv_len;
    }

    tlv_len = add_v2_tlv_authority(tlv_pool, v2_iov, &v2_niov);
    if (tlv_len > 0) {
      v2_len += tlv_len;
    }

    tlv_len = add_v2_tlv_tls(tlv_pool, v2_iov, &v2_niov);
    if (tlv_len > 0) {
      v2_len += tlv_len;
    }

    tlv_len = add_v2_tlv_unique_id(tlv_pool, v2_iov, &v2_niov);
    if (tlv_len > 0) {
      v2_len += tlv_len;
    }
  }

  v2_len = htons(v2_len);
  v2_iov[3].iov_base = (void *) &v2_len;
  v2_iov[3].iov_len = sizeof(v2_len);

  src_port = htons(session.c->remote_port);
  v2_iov[6].iov_base = (void *) &src_port;
  v2_iov[6].iov_len = sizeof(src_port);

  dst_port = htons(session.c->local_port);
  v2_iov[7].iov_base = (void *) &dst_port;
  v2_iov[7].iov_len = sizeof(dst_port);

  pr_trace_msg(trace_channel, 9,
    "sending PROXY protocol V2 message for %s %s#%u %s#%u to backend",
    proto, pr_netaddr_get_ipstr(src_addr), (unsigned int) ntohs(src_port),
    pr_netaddr_get_ipstr(dst_addr), (unsigned int) ntohs(dst_port));

  res = writev_conn(conn, v2_iov, v2_niov);
  xerrno = errno;

  if (sub_pool != NULL) {
    destroy_pool(sub_pool);
  }

  if (tlv_pool != NULL) {
    destroy_pool(tlv_pool);
  }

  errno = xerrno;
  return res;
}