- /*
- * gcc -Wall -g sioacquireportreservationtest1.c -lws2_32 -o sioacquireportreservationtest1
- */
- #ifndef UNICODE
- #define UNICODE
- #endif
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <Windows.h>
- #include <winsock2.h>
- #include <mstcpip.h>
- #include <ws2ipdef.h>
- #include <stdio.h>
- #include <stdlib.h>
- // Need to link with Ws2_32.lib for Winsock functions
- #pragma comment(lib, "ws2_32.lib")
- int main(int argc, char *argv[])
- {
- // Declare and initialize variables
- int startPort = 0; // host byte order
- int numPorts = 0;
- USHORT startPortns = 0; // Network byte order
- INET_PORT_RANGE portRange = { 0 };
- INET_PORT_RESERVATION_INSTANCE portRes = { 0 };
- unsigned long status = 0;
- WSADATA wsaData = { 0 };
- int iResult = 0;
- SOCKET sock = INVALID_SOCKET;
- int iFamily = AF_INET;
- int iType = 0;
- int iProtocol = 0;
- SOCKET sockRes = INVALID_SOCKET;
- DWORD bytesReturned = 0;
- // Note that the sockaddr_in struct works only with AF_INET not AF_INET6
- // An application needs to use the sockaddr_in6 for AF_INET6
- struct sockaddr_in service;
- struct sockaddr_in sockName;
- int nameLen = sizeof (sockName);
- // Validate the parameters
- if (argc != 6) {
- ("usage: %s <addressfamily> <type> <protocol> <StartingPort> <NumberOfPorts>\n",
- argv[0]);
- ("and then acquires a runtime port reservation for the protocol specified\n");
- printf(" where AF_INET=%d SOCK_DGRAM=%d IPPROTO_UDP=%d StartPort=5000 NumPorts=20\n", AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- printf(" where AF_INET=%d SOCK_STREAM=%d IPPROTO_TCP=%d StartPort=5000 NumPorts=20\n", AF_INET, SOCK_STREAM, IPPROTO_TCP);
- return 1;
- }
- if (startPort < 0 || startPort > 65535) {
- return 1;
- }
- startPortns = htons((USHORT) startPort);
- if (numPorts < 0) {
- return 1;
- }
- portRange.StartPort = startPortns;
- portRange.NumberOfPorts = (USHORT) numPorts;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- return 1;
- }
- sock = socket(iFamily, iType, iProtocol);
- if (sock == INVALID_SOCKET) {
- WSACleanup();
- return 1;
- } else {
- iResult =
- WSAIoctl(sock, SIO_ACQUIRE_PORT_RESERVATION, (LPVOID) & portRange,
- sizeof (INET_PORT_RANGE), (LPVOID) & portRes,
- sizeof (INET_PORT_RESERVATION_INSTANCE), &bytesReturned, NULL, NULL);
- if (iResult != 0) {
- WSAGetLastError());
- closesocket(sock);
- WSACleanup();
- return 1;
- } else {
- ("WSAIoctl(SIO_ACQUIRE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
- bytesReturned);
- htons(portRes.StartPort),
- portRes.NumberOfPorts, (long long)portRes.Token);
- sockRes = socket(iFamily, iType, iProtocol);
- if (sockRes == INVALID_SOCKET) {
- WSAGetLastError());
- closesocket(sock);
- WSACleanup();
- return 1;
- } else {
- iResult =
- WSAIoctl(sock, SIO_ASSOCIATE_PORT_RESERVATION,
- (LPVOID) & portRes.Token, sizeof (ULONG64), NULL, 0,
- &bytesReturned, NULL, NULL);
- if (iResult != 0) {
- ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) failed with error = %d\n",
- WSAGetLastError());
- } else {
- ("WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
- bytesReturned);
- service.sin_family = (ADDRESS_FAMILY) iFamily;
- service.sin_addr.s_addr = INADDR_ANY;
- service.sin_port = 0;
- iResult = bind(sock, (SOCKADDR *) & service, sizeof (service));
- if (iResult == SOCKET_ERROR)
- else {
- iResult = getsockname(sock, (SOCKADDR *) & sockName, &nameLen);
- if (iResult == SOCKET_ERROR)
- WSAGetLastError());
- else {
- ntohs(sockName.sin_port));
- }
- }
- }
- }
- // comment out this block of code if you don't want to delete the reservation just created
- iResult =
- WSAIoctl(sock, SIO_RELEASE_PORT_RESERVATION, (LPVOID) & portRes.Token,
- sizeof (ULONG64), NULL, 0, &bytesReturned, NULL, NULL);
- if (iResult != 0) {
- ("WSAIoctl(SIO_RELEASE_PORT_RESERVATION) failed with error = %d\n",
- WSAGetLastError());
- } else {
- ("WSAIoctl(SIO_RELEASE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
- bytesReturned);
- }
- }
- if (sockRes != INVALID_SOCKET) {
- iResult = closesocket(sockRes);
- if (iResult == SOCKET_ERROR) {
- WSAGetLastError());
- }
- }
- if (sock != INVALID_SOCKET) {
- iResult = closesocket(sock);
- if (iResult == SOCKET_ERROR) {
- WSAGetLastError());
- }
- }
- }
- WSACleanup();
- return 0;
- }
sioacquireportreservationtest1.c
Posted by Anonymous on Mon 25th Mar 2024 15:06
raw | new post
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.