blob: 21a024ab4b459f84ce1ed2713200d9a074ce422e [file] [log] [blame]
Willy Tarreau0d06df62020-08-28 15:10:11 +02001/*
2 * AF_INET/AF_INET6 socket management
3 *
4 * Copyright 2000-2020 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 <string.h>
14
15#include <sys/param.h>
16#include <sys/socket.h>
17#include <sys/types.h>
18
19#include <netinet/tcp.h>
20#include <netinet/in.h>
21
22#include <haproxy/api.h>
23#include <haproxy/sock_inet.h>
24#include <haproxy/tools.h>
25
26
27/* PLEASE NOTE for function below:
28 * - sock_inet4_* is solely for AF_INET (IPv4)
29 * - sock_inet6_* is solely for AF_INET6 (IPv6)
30 * - sock_inet_* is for either
31 *
32 * The address family SHOULD always be checked. In some cases a function will
33 * be used in a situation where the address family is guaranteed (e.g. protocol
34 * definitions), so the test may be avoided. This special case must then be
35 * mentioned in the comment before the function definition.
36 */
37
38
39/* Compares two AF_INET sockaddr addresses. Returns 0 if they match or non-zero
40 * if they do not match.
41 */
42int sock_inet4_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
43{
44 const struct sockaddr_in *a4 = (const struct sockaddr_in *)a;
45 const struct sockaddr_in *b4 = (const struct sockaddr_in *)b;
46
47 if (a->ss_family != b->ss_family)
48 return -1;
49
50 if (a->ss_family != AF_INET)
51 return -1;
52
53 if (a4->sin_port != b4->sin_port)
54 return -1;
55
56 return memcmp(&a4->sin_addr, &b4->sin_addr, sizeof(a4->sin_addr));
57}
58
59/* Compares two AF_INET6 sockaddr addresses. Returns 0 if they match or
60 * non-zero if they do not match.
61 */
62int sock_inet6_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
63{
64 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)a;
65 const struct sockaddr_in6 *b6 = (const struct sockaddr_in6 *)b;
66
67 if (a->ss_family != b->ss_family)
68 return -1;
69
70 if (a->ss_family != AF_INET6)
71 return -1;
72
73 if (a6->sin6_port != b6->sin6_port)
74 return -1;
75
76 return memcmp(&a6->sin6_addr, &b6->sin6_addr, sizeof(a6->sin6_addr));
77}