Difference between
modified post 11c4e by Anonymous on Mon 25th Mar 2024 14:40
original post 6c180 by Anonymous on Mon 25th Mar 2024 14:37
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | ||
| 2 | 2 | /* | |
| 3 | 3 | * | |
| 4 | 4 | * $ gcc -g createpersistenttcpportreservation_test1.c -liphlpapi -lws2_32 | |
| 5 | 5 | * -o createpersistenttcpportreservation_test1 | |
| 6 | 6 | */ | |
| 7 | 7 | ||
| 8 | 8 | #ifndef UNICODE | |
| 9 | 9 | #define UNICODE | |
| 10 | 10 | #endif | |
| 11 | 11 | ||
| 12 | 12 | #ifndef WIN32_LEAN_AND_MEAN | |
| 13 | 13 | #define WIN32_LEAN_AND_MEAN | |
| 14 | 14 | #endif | |
| 15 | 15 | ||
| 16 | 16 | #include <Windows.h> | |
| 17 | 17 | #include <winsock2.h> | |
| 18 | 18 | #include <mstcpip.h> | |
| 19 | 19 | #include <ws2ipdef.h> | |
| 20 | 20 | #include <iphlpapi.h> | |
| 21 | 21 | #include <stdio.h> | |
| 22 | 22 | #include <stdlib.h> | |
| 23 | 23 | #include <wchar.h> | |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | 26 | // Need to link with iphlpapi.lib | |
| 27 | 27 | #pragma comment(lib, "iphlpapi.lib") | |
| 28 | 28 | ||
| 29 | 29 | // Need to link with Ws2_32.lib for Winsock functions | |
| 30 | 30 | #pragma comment(lib, "ws2_32.lib") | |
| 31 | 31 | ||
| 32 | 32 | int main(int argc, char *argv[]) | |
| 33 | 33 | { | |
| 34 | 34 | ||
| 35 | 35 | // Declare and initialize variables | |
| 36 | 36 | ||
| 37 | 37 | int startPort = 0; // host byte order | |
| 38 | 38 | int numPorts = 0; | |
| 39 | 39 | USHORT startPortns = 0; // Network byte order | |
| 40 | 40 | ULONG64 resToken = { 0 }; | |
| 41 | 41 | ||
| 42 | 42 | unsigned long status = 0; | |
| 43 | 43 | ||
| 44 | 44 | WSADATA wsaData = { 0 }; | |
| 45 | 45 | int iResult = 0; | |
| 46 | 46 | ||
| 47 | 47 | SOCKET sock = INVALID_SOCKET; | |
| 48 | 48 | int iFamily = AF_INET; | |
| 49 | 49 | int iType = SOCK_STREAM; | |
| 50 | 50 | int iProtocol = IPPROTO_TCP; | |
| 51 | 51 | ||
| 52 | 52 | DWORD bytesReturned = 0; | |
| 53 | 53 | ||
| 54 | 54 | ||
| 55 | 55 | // Note that the sockaddr_in struct works only with AF_INET not AF_INET6 | |
| 56 | 56 | // An application needs to use the sockaddr_in6 for AF_INET6 | |
| 57 | 57 | struct sockaddr_in service; | |
| 58 | 58 | struct sockaddr_in sockName; | |
| 59 | 59 | int nameLen = sizeof(sockName); | |
| 60 | 60 | ||
| 61 | 61 | // Validate the parameters | |
| 62 | 62 | if (argc != 3) { | |
| 63 | 63 | printf("usage: %s <Starting Port> <Number of Ports>\n", | |
| 64 | 64 | argv[0]); | |
| 65 | 65 | printf("Creates a persistent TCP port reservation\n"); | |
| 66 | 66 | printf("Example usage:\n"); | |
| 67 | 67 | printf(" %s 5000 20\n", argv[0]); | |
| 68 | 68 | printf(" where StartPort=5000 NumPorts=20"); | |
| 69 | 69 | return 1; | |
| 70 | 70 | } | |
| 71 | 71 | ||
| 72 | 72 | startPort = atoi(argv[1]); | |
| 73 | 73 | if (startPort < 0 || startPort > 65535) { | |
| 74 | 74 | printf("Starting point must be either 0 or between 1 and 65,535\n"); | |
| 75 | 75 | return 1; | |
| 76 | 76 | } | |
| 77 | 77 | startPortns = htons((USHORT) startPort); | |
| 78 | 78 | ||
| 79 | 79 | numPorts = atoi(argv[2]); | |
| 80 | 80 | if (numPorts < 0) { | |
| 81 | 81 | printf("Number of ports must be a positive number\n"); | |
| 82 | 82 | return 1; | |
| 83 | 83 | ||
| 84 | 84 | } | |
| 85 | 85 | ||
| 86 | 86 | status = | |
| 87 | 87 | CreatePersistentTcpPortReservation((USHORT) startPortns, (USHORT) numPorts, | |
| 88 | 88 | &resToken); | |
| 89 | 89 | if (status != NO_ERROR) { | |
| 90 | 90 | printf("CreatePersistentTcpPortReservation returned error: %ld\n", status); | |
| 91 | 91 | return 1; | |
| 92 | 92 | } | |
| 93 | 93 | ||
| 94 | 94 | printf("CreatePersistentTcpPortReservation call succeeded\n"); | |
| 95 | - | printf(" Token = %I64d\n", resToken); | |
| 95 | + | printf(" Token = %llx\n", (long long)resToken); | |
| 96 | 96 | ||
| 97 | 97 | // Comment out this block if you don't want to create a socket and associate it with the | |
| 98 | 98 | // persistent reservation | |
| 99 | 99 | ||
| 100 | 100 | // Initialize Winsock | |
| 101 | 101 | iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
| 102 | 102 | if (iResult != 0) { | |
| 103 | 103 | printf("WSAStartup failed with error = %d\n", iResult); | |
| 104 | 104 | // return 1; | |
| 105 | 105 | } | |
| 106 | 106 | ||
| 107 | 107 | sock = socket(iFamily, iType, iProtocol); | |
| 108 | 108 | if (sock == INVALID_SOCKET) | |
| 109 | 109 | printf("socket function failed with error = %d\n", WSAGetLastError()); | |
| 110 | 110 | else { | |
| 111 | 111 | printf("socket function succeeded\n"); | |
| 112 | 112 | ||
| 113 | 113 | iResult = | |
| 114 | 114 | ||
| 115 | 115 | WSAIoctl(sock, SIO_ASSOCIATE_PORT_RESERVATION, (LPVOID) & resToken, | |
| 116 | 116 | sizeof (ULONG64), NULL, 0, &bytesReturned, NULL, NULL); | |
| 117 | 117 | if (iResult != 0) { | |
| 118 | 118 | printf | |
| 119 | 119 | ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) failed with error = %d\n", | |
| 120 | 120 | WSAGetLastError()); | |
| 121 | 121 | } else { | |
| 122 | 122 | printf("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, bytesReturned = %u\n", | |
| 123 | 123 | bytesReturned); | |
| 124 | 124 | ||
| 125 | 125 | service.sin_family = AF_INET; | |
| 126 | 126 | service.sin_addr.s_addr = INADDR_ANY; | |
| 127 | 127 | service.sin_port = 0; | |
| 128 | 128 | ||
| 129 | 129 | iResult = bind(sock, (SOCKADDR*) &service, sizeof(service) ); | |
| 130 | 130 | if (iResult == SOCKET_ERROR) | |
| 131 | 131 | printf("bind failed with error = %d\n", WSAGetLastError()); | |
| 132 | 132 | else { | |
| 133 | 133 | printf("bind succeeded\n"); | |
| 134 | 134 | iResult = getsockname(sock, (SOCKADDR*) &sockName, &nameLen); | |
| 135 | 135 | if (iResult == SOCKET_ERROR) | |
| 136 | 136 | printf("getsockname failed with error = %d\n", WSAGetLastError() ); | |
| 137 | 137 | else { | |
| 138 | 138 | printf("getsockname succeeded\n"); | |
| 139 | 139 | printf("Port number allocated = %u\n", ntohs(sockName.sin_port) ); | |
| 140 | 140 | } | |
| 141 | 141 | } | |
| 142 | 142 | } | |
| 143 | 143 | ||
| 144 | 144 | if (sock != INVALID_SOCKET) { | |
| 145 | 145 | iResult = closesocket(sock); | |
| 146 | 146 | if (iResult == SOCKET_ERROR) { | |
| 147 | 147 | printf("closesocket failed with error = %d\n", WSAGetLastError()); | |
| 148 | 148 | WSACleanup(); | |
| 149 | 149 | } | |
| 150 | 150 | } | |
| 151 | 151 | } | |
| 152 | 152 | ||
| 153 | 153 | // comment out this block of code if you don't want to delete the reservation just created | |
| 154 | 154 | status = DeletePersistentTcpPortReservation((USHORT) startPortns, (USHORT) numPorts); | |
| 155 | 155 | if (status != NO_ERROR) { | |
| 156 | 156 | printf("DeletePersistentTcpPortReservation returned error: %ld\n", status); | |
| 157 | 157 | return 1; | |
| 158 | 158 | } | |
| 159 | 159 | printf("DeletePersistentTcpPortReservation call succeeded\n"); | |
| 160 | 160 | ||
| 161 | 161 | return 0; | |
| 162 | 162 | } | |
| 163 | 163 | ||
| 164 | 164 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
nrubsig.kpaste.net RSS