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
|
/* SA ID, for libreswan
*
* Copyright (C) 2019 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2000, 2001 Henry Spencer.
* Copyright (C) 2012 David McCullough <david_mccullough@mcafee.com>
*
* 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. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* 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.
*/
#include "ip_said.h"
#include "ip_info.h"
#include "jambuf.h"
const ip_said unset_said;
ip_said said_from_raw(where_t where UNUSED,
const struct ip_info *afi,
const struct ip_bytes dst,
const struct ip_protocol *protocol,
ipsec_spi_t spi)
{
ip_said said = {
.is_set = true,
.version = afi->ip_version,
.dst = dst,
.ipproto = protocol->ipproto,
.spi = spi,
};
return said;
}
ip_said said_from_address_protocol_spi(const ip_address address,
const struct ip_protocol *protocol,
ipsec_spi_t spi)
{
const struct ip_info *afi = address_info(address);
if (afi == NULL) {
/* NULL+unset+unknown */
return unset_said;
}
return said_from_raw(HERE, afi, address.bytes,
protocol, spi);
}
bool said_is_unset(const ip_said *said)
{
if (said == NULL) {
return true;
}
return !said->is_set;
}
/*
* convert SA to text "ah507@1.2.3.4"
*/
size_t jam_said(struct jambuf *buf, const ip_said *said)
{
if (!said->is_set) {
return jam_string(buf, "<unset-said>");
}
const struct ip_info *afi = said_type(said);
if (afi == NULL) {
return jam(buf, "<said-has-no-type");
}
const struct ip_protocol *proto = protocol_from_ipproto(said->ipproto);
if (proto == &ip_protocol_ipip/*TUN*/ &&
said->spi == PASSTHROUGHSPI &&
/* any zero */ thingeq(said->dst, unset_ip_bytes)) {
return jam_string(buf, (afi == &ipv4_info ? PASSTHROUGH4NAME :
afi == &ipv6_info ? PASSTHROUGH6NAME :
"<unknown-said-version>"));;
}
/* general case needed */
size_t s = 0;
s += jam_string(buf, proto->prefix != NULL ? proto->prefix : proto->name);
/* .SPI */
s += jam_char(buf, (afi == &ipv4_info ? '.' :
afi == &ipv6_info ? ':' :
'?'));
s += jam(buf, "%x", ntohl(said->spi));;
s += jam_char(buf, '@');
s += afi->jam.address(buf, afi, &said->dst);
return s;
}
const char *str_said(const ip_said *said, said_buf *buf)
{
struct jambuf b = ARRAY_AS_JAMBUF(buf->buf);
jam_said(&b, said);
return buf->buf;
}
const struct ip_info *said_type(const ip_said *said)
{
if (said == NULL) {
return NULL;
}
/* may return NULL */
return said_info(*said);
}
const struct ip_info *said_info(const ip_said said)
{
if (!said.is_set) {
return NULL;
}
/* may return NULL */
return ip_version_info(said.version);
}
ip_address said_address(const ip_said said)
{
const struct ip_info *afi = said_info(said);
if (afi == NULL) {
/* NULL+unset+unknown */
return unset_address; /* empty_address? */
}
return address_from_raw(HERE, afi, said.dst);
}
const struct ip_protocol *said_protocol(const ip_said said)
{
if (said_is_unset(&said)) {
return NULL;
}
return protocol_from_ipproto(said.ipproto);
}
|