blob: 0c778eb7aac63644afc5f5e3c03f061247178ca7 [file] [log] [blame]
Willy Tarreau8f72d482021-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>
17#include <import/xxhash.h>
18
19__attribute__((noreturn)) void die(int code, const char *format, ...)
20{
21 va_list args;
22
23 va_start(args, format);
24 vfprintf(stderr, format, args);
25 va_end(args);
26 exit(code);
27}
28
29int main(int argc, char **argv)
30{
31 size_t key_len;
32 int addr_len;
33 char *buf;
34 int port;
35
36 if (argc < 4)
37 die(1, "Usage: %s <key> <ip> <port>\n", argv[0]);
38
39 key_len = strlen(argv[1]);
40 buf = realloc(strdup(argv[1]), key_len + 16 + 4);
41 if (!buf)
42 die(2, "Not enough memory\n");
43
44 if (inet_pton(AF_INET, argv[2], buf + key_len) > 0)
45 addr_len = 4;
46 else if (inet_pton(AF_INET6, argv[2], buf + key_len) > 0)
47 addr_len = 16;
48 else
49 die(3, "Cannot parse address <%s> as IPv4/IPv6\n", argv[2]);
50
51 port = htonl(atoi(argv[3]));
52 memcpy(buf + key_len + addr_len, &port, 4);
53 printf("%016llx\n", (long long)XXH64(buf, key_len + addr_len + 4, 0));
54 return 0;
55}