int tryscan int pPort int port pPort getaddrinfosuc port true addrinfo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int tryscan(int *pPort)
{
int port = *pPort;
getaddrinfosuc[port] = true;
addrinfo *pAddr = new addrinfo;
memcpy(pAddr, result, sizeof(addrinfo));
pAddr->ai_addr->sa_data[0] = port/256;
pAddr->ai_addr->sa_data[1] = port % 256;
praddr(pAddr->ai_addr);
char currPort[10];
memset(currPort, 0, sizeof(currPort));
itoa(port, currPort, 10);
SOCKET ConnSock = INVALID_SOCKET;
ConnSock = socket(pAddr->ai_family, pAddr->ai_socktype, pAddr->ai_protocol);
if(ConnSock == INVALID_SOCKET)
{
cout << "Error at socket(): " << WSAGetLastError() << '\n';
return port;
}
int iResult = connect(ConnSock, pAddr->ai_addr, (int)pAddr->ai_addrlen);
if(iResult == SOCKET_ERROR)
{
closesocket(ConnSock);
ConnSock = INVALID_SOCKET;
}
if(ConnSock == INVALID_SOCKET)
{
goto CLEANUP;
}
else
{
connsuc[port] = true;
}
iResult = shutdown(ConnSock, SD_SEND);
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", WSAGetLastError());
}
closesocket(ConnSock);
CLEANUP:
delete pAddr;
return port;
}