blob: ddb71a748134d1b2bbb3acb13aad177a3c583eef [file] [log] [blame]
Willy Tarreau6807c7f2021-08-11 13:54:52 +02001/*
2 * Dynamic server cookie calculator
3 *
4 * Copyright 2021 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <stdarg.h>
16#include <arpa/inet.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020017
18#include <haproxy/xxhash.h>
Willy Tarreau6807c7f2021-08-11 13:54:52 +020019
20__attribute__((noreturn)) void die(int code, const char *format, ...)
21{
22 va_list args;
23
24 va_start(args, format);
25 vfprintf(stderr, format, args);
26 va_end(args);
27 exit(code);
28}
29
30int main(int argc, char **argv)
31{
32 size_t key_len;
33 int addr_len;
34 char *buf;
35 int port;
36
37 if (argc < 4)
38 die(1, "Usage: %s <key> <ip> <port>\n", argv[0]);
39
40 key_len = strlen(argv[1]);
41 buf = realloc(strdup(argv[1]), key_len + 16 + 4);
42 if (!buf)
43 die(2, "Not enough memory\n");
44
45 if (inet_pton(AF_INET, argv[2], buf + key_len) > 0)
46 addr_len = 4;
47 else if (inet_pton(AF_INET6, argv[2], buf + key_len) > 0)
48 addr_len = 16;
49 else
50 die(3, "Cannot parse address <%s> as IPv4/IPv6\n", argv[2]);
51
52 port = htonl(atoi(argv[3]));
53 memcpy(buf + key_len + addr_len, &port, 4);
54 printf("%016llx\n", (long long)XXH64(buf, key_len + addr_len + 4, 0));
55 return 0;
56}