32 #include "stunclient.txtcode" 33 #include "stunclient_lite.txtcode" 77 const char* psz = fSummaryUsage ? stunclient_lite_text : stunclient_text;
96 uint16_t localport = 0;
97 uint16_t remoteport = 0;
109 socketconfig.
family = AF_INET;
120 int optvalue = atoi(args.
strFamily.c_str());
123 case 4: socketconfig.
family = AF_INET;
break;
124 case 6: socketconfig.
family = AF_INET6;
break;
146 socketconfig.
socktype = SOCK_STREAM;
160 remoteport = (uint16_t)(
unsigned int)nPort;
197 localport = (uint16_t)(
unsigned int)nPort;
212 if (socketconfig.
family == AF_INET6)
214 sockaddr_in6 addr6 = {};
215 addr6.sin6_family = AF_INET6;
233 else if (args.
strMode ==
"full")
238 else if (args.
strMode ==
"behavior")
242 else if (args.
strMode ==
"filtering")
263 std::string& str = *pStr;
272 default:
ASSERT(
false); str =
"";
break;
278 std::string& str = *pStr;
287 default:
ASSERT(
false); str =
"";
break;
294 const int buffersize = 100;
295 std::string strResult;
342 size_t bytes_sent, bytes_recv;
343 size_t bytes_to_send, max_bytes_recv, remaining;
399 bytes_to_send = (int)(spMsg->GetSize());
402 pData = spMsg->GetData();
403 while (bytes_sent < bytes_to_send)
405 ret = ::send(sock, pData+bytes_sent, bytes_to_send-bytes_sent, 0);
421 pData = spMsg->GetData();
423 max_bytes_recv = spMsg->GetAllocatedSize();
424 remaining = max_bytes_recv;
426 while (remaining > 0)
435 if (readsize > remaining)
442 ret = ::recv(sock, pData+bytes_recv, readsize, 0);
457 reader.
AddBytes(pData+bytes_recv, ret);
459 remaining = max_bytes_recv - bytes_recv;
460 spMsg->SetSize(bytes_recv);
495 std::string strAddrLocal;
532 ASSERT(spMsg->GetSize() > 0);
571 ret = select(sock+1, &
set, NULL, NULL, &tv);
574 ret =
::recvfromex(sock, spMsg->GetData(), spMsg->GetAllocatedSize(), MSG_DONTWAIT, &addrRemote, &addrLocal);
578 addrRemote.ToString(&strAddr);
580 Logging::LogMsg(
LL_DEBUG,
"Got response (%d bytes) from %s on interface %s", ret, strAddr.c_str(), strAddrLocal.c_str());
604 int main(
int argc,
char** argv)
669 if (socketconfig.
socktype == SOCK_STREAM)
const uint32_t MAX_STUN_MESSAGE_SIZE
void DumpResults(StunClientLogicConfig &config, StunClientResults &results)
bool fBehaviorTestSuccess
void TcpClientLoop(StunClientLogicConfig &config, ClientSocketConfig &socketconfig)
CDataStream & GetStream()
HRESULT Initialize(StunClientLogicConfig &config)
#define E_STUNCLIENT_STILL_WAITING
std::string strRemoteServer
socklen_t GetSockAddrLength() const
int ValidateNumberString(const char *psz, int nMinValue, int nMaxValue, int *pnResult)
#define E_STUNCLIENT_RESULTS_READY
void DumpConfig(StunClientLogicConfig &config, ClientSocketConfig &socketConfig)
HRESULT ResolveHostName(const char *pszHostName, int family, bool fNumericOnly, CSocketAddress *pAddr)
ssize_t recvfromex(int sockfd, void *buf, size_t len, int flags, CSocketAddress *pSrcAddr, CSocketAddress *pDstAddr)
void LogMsg(uint32_t level, const char *pszFormat,...)
HRESULT CreateConfigFromCommandLine(ClientCmdLineArgs &args, StunClientLogicConfig *pConfig, ClientSocketConfig *pSocketConfig)
HRESULT GetNextMessage(CRefCountedBuffer &spMsg, CSocketAddress *pAddrDest, uint32_t timeCurrentMilliseconds)
bool fFilteringTestSuccess
uint32_t GetMillisecondCounter()
bool IsNullOrEmpty(const char *psz)
ReaderParseState AddBytes(const uint8_t *pData, uint32_t size)
HRESULT GetSocketAddressForAdapter(int family, const char *pszAdapterName, uint16_t port, CSocketAddress *pSocketAddr)
void NatBehaviorToString(NatBehavior behavior, std::string *pStr)
int GetSocketHandle() const
void SetLogLevel(uint32_t level)
void ToLower(std::string &str)
CSocketAddress addrMapped
const uint16_t DEFAULT_STUN_PORT
HRESULT UdpClientLoop(StunClientLogicConfig &config, const ClientSocketConfig &socketconfig)
HRESULT ParseCommandLine(int argc, char **argv, int startindex, bool *fParseError)
const sockaddr * GetSockAddr() const
void ToString(std::string *pStr) const
HRESULT EnablePktInfoOption(bool fEnable)
void PrintUsage(bool fSummaryUsage)
int main(int argc, char **argv)
HRESULT ToStringBuffer(char *pszAddrBytes, size_t length) const
HRESULT ProcessResponse(CRefCountedBuffer &spMsg, CSocketAddress &addrRemote, CSocketAddress &addrLocal)
HRESULT GetResults(StunClientResults *pResults)
HRESULT AddNonOption(std::string *pStrResult)
std::string strRemotePort
HRESULT UDPInit(const CSocketAddress &local, SocketRole role)
HRESULT AddOption(const char *pszName, int has_arg, std::string *pStrResult)
HRESULT TCPInit(const CSocketAddress &local, SocketRole role, bool fSetReuseFlag)
boost::shared_ptr< CBuffer > CRefCountedBuffer
void Attach(CRefCountedBuffer &buffer, bool fForWriting)
uint16_t HowManyBytesNeeded()
#define ChkIfA(expr, hrerror)
void PrettyPrint(const char *pszInput, size_t width)
void NatFilteringToString(NatFiltering filtering, std::string *pStr)
CSocketAddress addrServer
const CSocketAddress & GetLocalAddress() const