pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


sioacquireportreservationtest1.c
Posted by Anonymous on Mon 25th Mar 2024 15:06
raw | new post

  1.  
  2. /*
  3.  * gcc -Wall -g sioacquireportreservationtest1.c -lws2_32 -o sioacquireportreservationtest1
  4.  */
  5. #ifndef UNICODE
  6. #define UNICODE
  7. #endif
  8.  
  9. #ifndef WIN32_LEAN_AND_MEAN
  10. #define WIN32_LEAN_AND_MEAN
  11. #endif
  12.  
  13. #include <Windows.h>
  14. #include <winsock2.h>
  15. #include <mstcpip.h>
  16. #include <ws2ipdef.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. // Need to link with Ws2_32.lib for Winsock functions
  21. #pragma comment(lib, "ws2_32.lib")
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.  
  26.     // Declare and initialize variables
  27.  
  28.     int startPort = 0;          // host byte order
  29.     int numPorts = 0;
  30.     USHORT startPortns = 0;     // Network byte order
  31.  
  32.     INET_PORT_RANGE portRange = { 0 };
  33.     INET_PORT_RESERVATION_INSTANCE portRes = { 0 };
  34.  
  35.     unsigned long status = 0;
  36.  
  37.     WSADATA wsaData = { 0 };
  38.     int iResult = 0;
  39.  
  40.     SOCKET sock = INVALID_SOCKET;
  41.     int iFamily = AF_INET;
  42.     int iType = 0;
  43.     int iProtocol = 0;
  44.  
  45.     SOCKET sockRes = INVALID_SOCKET;
  46.  
  47.     DWORD bytesReturned = 0;
  48.  
  49.     // Note that the sockaddr_in struct works only with AF_INET not AF_INET6
  50.     // An application needs to use the sockaddr_in6 for AF_INET6
  51.     struct sockaddr_in service;
  52.     struct sockaddr_in sockName;
  53.     int nameLen = sizeof (sockName);
  54.  
  55.     // Validate the parameters
  56.     if (argc != 6) {
  57.         printf
  58.             ("usage: %s <addressfamily> <type> <protocol> <StartingPort> <NumberOfPorts>\n",
  59.              argv[0]);
  60.         printf("Opens a socket for the specified family, type, & protocol\n");
  61.         printf
  62.             ("and then acquires a runtime port reservation for the protocol specified\n");
  63.         printf("%s example usage\n", argv[0]);
  64.         printf("   %s 2 2 17 5000 20\n", argv[0]);
  65.         printf("   where AF_INET=%d SOCK_DGRAM=%d IPPROTO_UDP=%d StartPort=5000 NumPorts=20\n", AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  66.         printf("   where AF_INET=%d SOCK_STREAM=%d IPPROTO_TCP=%d StartPort=5000 NumPorts=20\n", AF_INET, SOCK_STREAM, IPPROTO_TCP);
  67.  
  68.         return 1;
  69.     }
  70.  
  71.     iFamily = atoi(argv[1]);
  72.     iType = atoi(argv[2]);
  73.     iProtocol = atoi(argv[3]);
  74.  
  75.     startPort = atoi(argv[4]);
  76.     if (startPort < 0 || startPort > 65535) {
  77.         printf("Starting point must be either 0 or between 1 and 65,535\n");
  78.         return 1;
  79.     }
  80.     startPortns = htons((USHORT) startPort);
  81.  
  82.     numPorts = atoi(argv[5]);
  83.     if (numPorts < 0) {
  84.         printf("Number of ports must be a positive number\n");
  85.         return 1;
  86.     }
  87.  
  88.     portRange.StartPort = startPortns;
  89.     portRange.NumberOfPorts = (USHORT) numPorts;
  90.  
  91.     // Initialize Winsock
  92.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  93.     if (iResult != 0) {
  94.         printf("WSAStartup failed with error = %d\n", iResult);
  95.         return 1;
  96.     }
  97.  
  98.     sock = socket(iFamily, iType, iProtocol);
  99.     if (sock == INVALID_SOCKET) {
  100.         printf("socket function failed with error = %d\n", WSAGetLastError());
  101.         WSACleanup();
  102.         return 1;
  103.     } else {
  104.         printf("socket function succeeded\n");
  105.  
  106.         iResult =
  107.             WSAIoctl(sock, SIO_ACQUIRE_PORT_RESERVATION, (LPVOID) & portRange,
  108.                      sizeof (INET_PORT_RANGE), (LPVOID) & portRes,
  109.                      sizeof (INET_PORT_RESERVATION_INSTANCE), &bytesReturned, NULL, NULL);
  110.         if (iResult != 0) {
  111.             printf("WSAIoctl(SIO_ACQUIRE_PORT_RESERVATION) failed with error = %d\n",
  112.                     WSAGetLastError());
  113.             closesocket(sock);
  114.             WSACleanup();
  115.             return 1;
  116.         } else {
  117.             printf
  118.                 ("WSAIoctl(SIO_ACQUIRE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
  119.                  bytesReturned);
  120.             printf("  Starting port=%d,  Number of Ports=%d, Token=%llx\n",
  121.                     htons(portRes.StartPort),
  122.                     portRes.NumberOfPorts, (long long)portRes.Token);
  123.  
  124.             sockRes = socket(iFamily, iType, iProtocol);
  125.             if (sockRes == INVALID_SOCKET) {
  126.                 printf("socket function for second socket failed with error = %d\n",
  127.                         WSAGetLastError());
  128.                 closesocket(sock);
  129.                 WSACleanup();
  130.                 return 1;
  131.             } else {
  132.                 printf("socket function for second socket succeeded\n");
  133.  
  134.                 iResult =
  135.                     WSAIoctl(sock, SIO_ASSOCIATE_PORT_RESERVATION,
  136.                              (LPVOID) & portRes.Token, sizeof (ULONG64), NULL, 0,
  137.                              &bytesReturned, NULL, NULL);
  138.                 if (iResult != 0) {
  139.                     printf
  140.                         ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) failed with error = %d\n",
  141.                          WSAGetLastError());
  142.                 } else {
  143.                     printf
  144.                         ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
  145.                          bytesReturned);
  146.  
  147.                     service.sin_family = (ADDRESS_FAMILY) iFamily;
  148.                     service.sin_addr.s_addr = INADDR_ANY;
  149.                     service.sin_port = 0;
  150.  
  151.                     iResult = bind(sock, (SOCKADDR *) & service, sizeof (service));
  152.                     if (iResult == SOCKET_ERROR)
  153.                         printf("bind failed with error = %d\n", WSAGetLastError());
  154.                     else {
  155.                         printf("bind succeeded\n");
  156.                         iResult = getsockname(sock, (SOCKADDR *) & sockName, &nameLen);
  157.                         if (iResult == SOCKET_ERROR)
  158.                             printf("getsockname failed with error = %d\n",
  159.                                     WSAGetLastError());
  160.                         else {
  161.                             printf("getsockname succeeded\n");
  162.                             printf("Port number allocated = %u\n",
  163.                                     ntohs(sockName.sin_port));
  164.                         }
  165.                     }
  166.                 }
  167.             }
  168.  
  169.             // comment out this block of code if you don't want to delete the reservation just created
  170.             iResult =
  171.                 WSAIoctl(sock, SIO_RELEASE_PORT_RESERVATION, (LPVOID) & portRes.Token,
  172.                          sizeof (ULONG64), NULL, 0, &bytesReturned, NULL, NULL);
  173.             if (iResult != 0) {
  174.                 printf
  175.                     ("WSAIoctl(SIO_RELEASE_PORT_RESERVATION) failed with error = %d\n",
  176.                      WSAGetLastError());
  177.             } else {
  178.                 printf
  179.                     ("WSAIoctl(SIO_RELEASE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
  180.                      bytesReturned);
  181.             }
  182.         }
  183.  
  184.         if (sockRes != INVALID_SOCKET) {
  185.             iResult = closesocket(sockRes);
  186.             if (iResult == SOCKET_ERROR) {
  187.                 printf("closesocket for second socket failed with error = %d\n",
  188.                         WSAGetLastError());
  189.             }
  190.         }
  191.         if (sock != INVALID_SOCKET) {
  192.             iResult = closesocket(sock);
  193.             if (iResult == SOCKET_ERROR) {
  194.                 printf("closesocket for first socket failed with error = %d\n",
  195.                         WSAGetLastError());
  196.             }
  197.         }
  198.     }
  199.  
  200.     WSACleanup();
  201.  
  202.     return 0;
  203. }

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