Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions for managing transparent proxying with CTTPROXY. |
| 3 | * This file should be compiled only if CTTPROXY is enabled. |
| 4 | * |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 5 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
Willy Tarreau | 71e451c | 2012-10-05 22:18:07 +0200 | [diff] [blame] | 19 | #include <unistd.h> |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 20 | |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <common/compat.h> |
| 26 | #include <common/config.h> |
| 27 | #include <common/time.h> |
| 28 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 29 | #include <import/ip_tproxy.h> |
| 30 | |
| 31 | /* |
| 32 | * Checks that CTTPROXY is available and in the right version. |
| 33 | * Returns 0 if OK, -1 if wrong version, -2 if not available or other error. |
| 34 | */ |
| 35 | int check_cttproxy_version() { |
| 36 | struct in_tproxy itp1; |
Delta Yeh | af01c7c | 2010-05-03 22:08:33 +0800 | [diff] [blame] | 37 | int fd, ret; |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 38 | |
| 39 | memset(&itp1, 0, sizeof(itp1)); |
| 40 | |
| 41 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 42 | if (fd == -1) |
| 43 | return -2; |
| 44 | |
| 45 | itp1.op = TPROXY_VERSION; |
| 46 | itp1.v.version = 0x02000000; /* CTTPROXY version 2.0 expected */ |
Delta Yeh | af01c7c | 2010-05-03 22:08:33 +0800 | [diff] [blame] | 47 | |
| 48 | ret = 0; |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 49 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) == -1) { |
| 50 | if (errno == -EINVAL) |
Delta Yeh | af01c7c | 2010-05-03 22:08:33 +0800 | [diff] [blame] | 51 | ret = -1; /* wrong version */ |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 52 | else |
Delta Yeh | af01c7c | 2010-05-03 22:08:33 +0800 | [diff] [blame] | 53 | ret = -2; /* not supported or other error */ |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 54 | } |
Delta Yeh | af01c7c | 2010-05-03 22:08:33 +0800 | [diff] [blame] | 55 | close(fd); |
| 56 | return ret; |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * Local variables: |
| 62 | * c-indent-level: 8 |
| 63 | * c-basic-offset: 8 |
| 64 | * End: |
| 65 | */ |