net/StaticSocketAddress: add method SetPort()
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
|
||||||
@@ -34,6 +34,14 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_TCP
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#else
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
StaticSocketAddress &
|
StaticSocketAddress &
|
||||||
StaticSocketAddress::operator=(SocketAddress other) noexcept
|
StaticSocketAddress::operator=(SocketAddress other) noexcept
|
||||||
{
|
{
|
||||||
@@ -41,3 +49,29 @@ StaticSocketAddress::operator=(SocketAddress other) noexcept
|
|||||||
memcpy(&address, other.GetAddress(), size);
|
memcpy(&address, other.GetAddress(), size);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_TCP
|
||||||
|
|
||||||
|
bool
|
||||||
|
StaticSocketAddress::SetPort(unsigned port)
|
||||||
|
{
|
||||||
|
switch (GetFamily()) {
|
||||||
|
case AF_INET:
|
||||||
|
{
|
||||||
|
auto &a = (struct sockaddr_in &)address;
|
||||||
|
a.sin_port = htons(port);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case AF_INET6:
|
||||||
|
{
|
||||||
|
auto &a = (struct sockaddr_in6 &)address;
|
||||||
|
a.sin6_port = htons(port);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#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
|
||||||
@@ -91,6 +91,22 @@ public:
|
|||||||
address.ss_family = AF_UNSPEC;
|
address.ss_family = AF_UNSPEC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_TCP
|
||||||
|
/**
|
||||||
|
* Extract the port number. Returns 0 if not applicable.
|
||||||
|
*/
|
||||||
|
gcc_pure
|
||||||
|
unsigned GetPort() const {
|
||||||
|
return ((SocketAddress)*this).GetPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true on success, false if this address cannot have
|
||||||
|
* a port number
|
||||||
|
*/
|
||||||
|
bool SetPort(unsigned port);
|
||||||
|
#endif
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
bool operator==(SocketAddress other) const {
|
bool operator==(SocketAddress other) const {
|
||||||
return (SocketAddress)*this == other;
|
return (SocketAddress)*this == other;
|
||||||
|
Reference in New Issue
Block a user