net/SocketAddress: add method GetPort()
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2012-2015 Max Kellermann <max.kellermann@gmail.com>
|
* Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -27,12 +27,43 @@
|
|||||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "SocketAddress.hxx"
|
#include "SocketAddress.hxx"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_TCP
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#else
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
bool
|
bool
|
||||||
SocketAddress::operator==(SocketAddress other) const noexcept
|
SocketAddress::operator==(SocketAddress other) const noexcept
|
||||||
{
|
{
|
||||||
return size == other.size && memcmp(address, other.address, size) == 0;
|
return size == other.size && memcmp(address, other.address, size) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_TCP
|
||||||
|
|
||||||
|
unsigned
|
||||||
|
SocketAddress::GetPort() const
|
||||||
|
{
|
||||||
|
if (IsNull())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
switch (GetFamily()) {
|
||||||
|
case AF_INET:
|
||||||
|
return ntohs(((const struct sockaddr_in *)(const void *)address)->sin_port);
|
||||||
|
|
||||||
|
case AF_INET6:
|
||||||
|
return ntohs(((const struct sockaddr_in6 *)(const void *)address)->sin6_port);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2012-2015 Max Kellermann <max.kellermann@gmail.com>
|
* Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
#ifndef SOCKET_ADDRESS_HXX
|
#ifndef SOCKET_ADDRESS_HXX
|
||||||
#define SOCKET_ADDRESS_HXX
|
#define SOCKET_ADDRESS_HXX
|
||||||
|
|
||||||
|
#include "Features.hxx"
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@@ -92,6 +93,14 @@ public:
|
|||||||
return GetFamily() != AF_UNSPEC;
|
return GetFamily() != AF_UNSPEC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_TCP
|
||||||
|
/**
|
||||||
|
* Extract the port number. Returns 0 if not applicable.
|
||||||
|
*/
|
||||||
|
gcc_pure
|
||||||
|
unsigned GetPort() const;
|
||||||
|
#endif
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
bool operator==(const SocketAddress other) const noexcept;
|
bool operator==(const SocketAddress other) const noexcept;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user