pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


createpersistenttcpportreservation_test1
Posted by Anonymous on Mon 25th Mar 2024 14:37
raw | new post
view followups (newest first): createpersistenttcpportreservation_test1 by Anonymous

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

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at