76 struct xfrm_usersa_info xsinfo = {};
78 xsinfo.sel.family = AF_INET;
79 xsinfo.sel.saddr.a4 = INADDR_ANY;
80 xsinfo.sel.daddr.a4 = inet_addr(dest_address.c_str());
81 xsinfo.sel.prefixlen_d = 32;
83 xsinfo.id.daddr.a4 = inet_addr(dest_address.c_str());
84 xsinfo.id.spi = htonl(spi);
85 xsinfo.id.proto = IPPROTO_ESP;
87 xsinfo.saddr.a4 = INADDR_ANY;
89 xsinfo.lft.soft_byte_limit = XFRM_INF;
90 xsinfo.lft.hard_byte_limit = XFRM_INF;
91 xsinfo.lft.soft_packet_limit = XFRM_INF;
92 xsinfo.lft.hard_packet_limit = XFRM_INF;
95 xsinfo.family = AF_INET;
96 xsinfo.mode = XFRM_MODE_TRANSPORT;
98 std::vector<char> algo_buf(
sizeof(
struct xfrm_algo) + 512, 0);
99 auto* algo =
reinterpret_cast<struct xfrm_algo*
>(algo_buf.data());
101 std::vector<char> binary_key;
102 for (
unsigned int i = 0; i < key.length(); i += 2) {
103 binary_key.emplace_back((
char)strtol(key.substr(i, 2).c_str(),
nullptr, 16));
105 if (binary_key.size() > 512) {
106 throw std::runtime_error(
"Key is too long");
108 strcpy(algo->alg_name,
"aes");
109 algo->alg_key_len = binary_key.size() * 8;
110 memcpy(algo->alg_key, &binary_key[0], binary_key.size());
112 msg = nlmsg_alloc_simple(XFRM_MSG_NEWSA, 0);
113 nlmsg_append(msg, &xsinfo,
sizeof(xsinfo), NLMSG_ALIGNTO);
114 nla_put(msg, XFRMA_ALG_CRYPT, algo_buf.size(), algo);
116 sk = nl_socket_alloc();
117 nl_connect(sk, NETLINK_XFRM);
118 nl_send_auto(sk, msg);