1. #define WIN32_LEAN_AND_MEAN
    2. #include <windows.h>
    3. #include <winsock2.h>
    4. #include <ws2tcpip.h>
    5. #include <stdlib.h>
    6. #include <stdio.h>
    7. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    8. #pragma comment (lib, "Ws2_32.lib")
    9. #pragma comment (lib, "Mswsock.lib")
    10. #pragma comment (lib, "AdvApi32.lib")
    11. #define DEFAULT_BUFLEN 512
    12. #define DEFAULT_PORT "27015"
    13. int Scan(char *ip,int port);
    14. int __cdecl main(int argc, char** argv)
    15. {
    16. WSADATA wsaData;
    17. int iResult;
    18. // Validate the parameters
    19. if (argc != 2) {
    20. printf("usage: %s server-name\n", argv[0]);
    21. return 1;
    22. }
    23. // Initialize Winsock
    24. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    25. if (iResult != 0) {
    26. printf("WSAStartup failed with error: %d\n", iResult);
    27. return 1;
    28. }
    29. for (int i = 0; i < 65535; i++)
    30. {
    31. Scan(argv[1], i);
    32. }
    33. }
    34. int Scan(char** ip,int port) {
    35. struct addrinfo* result = NULL,
    36. * ptr = NULL, hints;
    37. SOCKET ConnectSocket = INVALID_SOCKET;
    38. ZeroMemory(&hints, sizeof(hints));
    39. hints.ai_family = AF_UNSPEC;
    40. hints.ai_socktype = SOCK_STREAM;
    41. hints.ai_protocol = IPPROTO_TCP;
    42. int iResult;
    43. // Resolve the server address and port
    44. iResult = getaddrinfo(ip[1], (PCSTR)port, &hints, &result);
    45. if (iResult != 0) {
    46. printf("getaddrinfo failed with error: %d\n", iResult);
    47. WSACleanup();
    48. return 1;
    49. }
    50. // Attempt to connect to an address until one succeeds
    51. for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
    52. // Create a SOCKET for connecting to server
    53. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
    54. ptr->ai_protocol);
    55. if (ConnectSocket == INVALID_SOCKET) {
    56. printf("socket failed with error: %ld\n", WSAGetLastError());
    57. return 1;
    58. }
    59. // Connect to server.
    60. iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    61. if (iResult == SOCKET_ERROR) {
    62. closesocket(ConnectSocket);
    63. ConnectSocket = INVALID_SOCKET;
    64. continue;
    65. }
    66. break;
    67. }
    68. freeaddrinfo(result);
    69. if (ConnectSocket == INVALID_SOCKET) {
    70. printf("Unable to connect to server!\n");
    71. return 1;
    72. }
    73. printf("addr : %s|%d open",ip,port);
    74. // shutdown the connection since no more data will be sent
    75. iResult = shutdown(ConnectSocket, SD_SEND);
    76. if (iResult == SOCKET_ERROR) {
    77. printf("shutdown failed with error: %d\n", WSAGetLastError());
    78. closesocket(ConnectSocket);
    79. return 1;
    80. }
    81. // Receive until the peer closes the connection
    82. // cleanup
    83. closesocket(ConnectSocket);
    84. return 0;
    85. };