From 690d234351445432fbdbe5d94cb1d9e762dd18f4 Mon Sep 17 00:00:00 2001 From: SASANO Takayoshi Date: Tue, 6 Apr 2021 05:15:07 +0900 Subject: [PATCH] add CUdpSocket::Open() to address family selection Open(uint16 uiPort) -> Open(uint16 uiPort, int af = AF_UNSPEC) --- src/cudpsocket.cpp | 10 ++++++---- src/cudpsocket.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cudpsocket.cpp b/src/cudpsocket.cpp index 9ebab14..742cef9 100755 --- a/src/cudpsocket.cpp +++ b/src/cudpsocket.cpp @@ -50,7 +50,7 @@ CUdpSocket::~CUdpSocket() //////////////////////////////////////////////////////////////////////////////////////// // open & close -bool CUdpSocket::Open(uint16 uiPort) +bool CUdpSocket::Open(uint16 uiPort, int af) { bool open = false; struct sockaddr_storage *ss; @@ -61,9 +61,11 @@ bool CUdpSocket::Open(uint16 uiPort) m_Ip[i] = CIp(g_Reflector.GetListenIp(i), uiPort); ss = m_Ip[i].GetSockAddr(ss_len); - // create socket - // (avoid INADDR_ANY on secondary and later IP address) - m_Socket[i] = ( i != 0 && g_Reflector.GetListenIp(i) == CIp() ) ? + // create socket, avoid two cases: + // 1. address family is specified and not matched + // 2. INADDR_ANY (0.0.0.0) on secondary and later IP address + m_Socket[i] = ( ( af != AF_UNSPEC && af != ss->ss_family ) || + ( i != 0 && g_Reflector.GetListenIp(i) == CIp() ) ) ? -1 : socket(ss->ss_family, SOCK_DGRAM, 0); if ( m_Socket[i] != -1 ) { diff --git a/src/cudpsocket.h b/src/cudpsocket.h index 0256145..ae8418c 100755 --- a/src/cudpsocket.h +++ b/src/cudpsocket.h @@ -57,7 +57,7 @@ public: ~CUdpSocket(); // open & close - bool Open(uint16); + bool Open(uint16, int = AF_UNSPEC); void Close(void); int GetSocket(const CIp &);