mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-23 22:43:48 +00:00
Fix sockaddr comparison (#575)
This may fix #565 It is not recommended to compare two structs via memcmp although they have been memset to 0. Added two helper functions to compare sockaddr_storage, one for address only and the other one takes port into account. These two functions are based on Unbound version in util/net_help.c, and comform needs of shadowsocks-libev. Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
@@ -23,8 +23,34 @@
|
||||
#ifndef _NETUTILS_H
|
||||
#define _NETUTILS_H
|
||||
|
||||
/** byte size of ip4 address */
|
||||
#define INET_SIZE 4
|
||||
/** byte size of ip6 address */
|
||||
#define INET6_SIZE 16
|
||||
|
||||
size_t get_sockaddr_len(struct sockaddr *addr);
|
||||
ssize_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, int block);
|
||||
int set_reuseport(int socket);
|
||||
|
||||
/**
|
||||
* Compare two sockaddrs. Imposes an ordering on the addresses.
|
||||
* Compares address and port.
|
||||
* @param addr1: address 1.
|
||||
* @param addr2: address 2.
|
||||
* @param len: lengths of addr.
|
||||
* @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
|
||||
*/
|
||||
int sockaddr_cmp(struct sockaddr_storage* addr1,
|
||||
struct sockaddr_storage* addr2, socklen_t len);
|
||||
|
||||
/**
|
||||
* Compare two sockaddrs. Compares address, not the port.
|
||||
* @param addr1: address 1.
|
||||
* @param addr2: address 2.
|
||||
* @param len: lengths of addr.
|
||||
* @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
|
||||
*/
|
||||
int sockaddr_cmp_addr(struct sockaddr_storage* addr1,
|
||||
struct sockaddr_storage* addr2, socklen_t len);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user