pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


Windows |bindresvport_()| prototype
Posted by Anonymous on Tue 26th Mar 2024 11:29
raw | new post
view followups (newest first): Windows |bindresvport_()| prototype by Anonymous

  1. diff --git a/libtirpc/src/bindresvport.c b/libtirpc/src/bindresvport.c
  2. index 01d1921..fd11dc3 100644
  3. --- a/libtirpc/src/bindresvport.c
  4. +++ b/libtirpc/src/bindresvport.c
  5. @@ -1,5 +1,6 @@
  6.  /*
  7.   * Copyright (c) 2009, Sun Microsystems, Inc.
  8. + * Copyright (c) 2024, Roland Mainz <roland.mainz@nrubsig.org>
  9.   * All rights reserved.
  10.   *
  11.   * Redistribution and use in source and binary forms, with or without
  12. @@ -32,20 +33,30 @@
  13.   * Copyright (c) 1987 by Sun Microsystems, Inc.
  14.   *
  15.   * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
  16. + * Portions Copyright(C) 2024, Roland Mainz <roland.mainz@nrubsig.org>
  17.   */
  18.  
  19.  #include <wintirpc.h>
  20.  #include <sys/types.h>
  21. -//#include <sys/socket.h>
  22. -
  23. -//#include <netinet/in.h>
  24. +#ifndef _WIN32
  25. +#include <sys/socket.h>
  26. +#include <netinet/in.h>
  27. +#endif
  28.  
  29.  #include <errno.h>
  30.  #include <string.h>
  31. -//#include <unistd.h>
  32. +#ifndef _WIN32
  33. +#include <unistd.h>
  34. +#endif
  35.  
  36.  #include <rpc/rpc.h>
  37.  
  38. +#ifdef _WIN32
  39. +#include <winsock2.h>
  40. +#include <mstcpip.h>
  41. +#include <ws2ipdef.h>
  42. +#endif
  43. +
  44.  /*
  45.   * Bind a socket to a privileged IP port
  46.   */
  47. @@ -139,18 +150,93 @@ bindresvport_sa(sd, sa)
  48.          return (res);
  49.  }
  50.  
  51. -#else
  52. -/*----------------------
  53. -#if defined(_WIN32)
  54. +#elif defined(_WIN32)
  55. +
  56. +#define STARTPORT 600
  57. +#define LOWPORT 512
  58. +#define ENDPORT (IPPORT_RESERVED - 1)
  59. +#define NPORTS  (ENDPORT - STARTPORT + 1)
  60.  
  61.  int
  62.  bindresvport_sa(int sd, struct sockaddr *sa)
  63.  {
  64. -       fprintf(stderr, "Do-nothing bindresvport_sa!\n");
  65. +       int ioctlres;
  66. +        int lasterr;
  67. +       SOCKET sd_sock;
  68. +       int currport;
  69. +
  70. +       INET_PORT_RANGE portRange;
  71. +       INET_PORT_RESERVATION_INSTANCE portRes;
  72. +       DWORD bytesReturned;
  73. +
  74. +       sd_sock = _get_osfhandle(sd);
  75. +
  76. +       for (currport = 600 ; currport < ENDPORT ; currport++) {
  77. +               (void)memset(&portRange, 0, sizeof(portRange));
  78. +               portRange.StartPort = htons(currport);
  79. +               portRange.NumberOfPorts = 1;
  80. +
  81. +               (void)memset(&portRes, 0, sizeof(portRes));
  82. +               bytesReturned = 0;
  83. +
  84. +               (void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
  85. +                       "trying port=%d ...\n",
  86. +                       sd, sa, (int)ntohs(portRange.StartPort));
  87. +                ioctlres = WSAIoctl(sd_sock, SIO_ACQUIRE_PORT_RESERVATION,
  88. +                       (LPVOID)&portRange, sizeof(INET_PORT_RANGE),
  89. +                       (LPVOID)&portRes, sizeof(INET_PORT_RESERVATION_INSTANCE),
  90. +                       &bytesReturned, NULL, NULL);
  91. +                lasterr = WSAGetLastError();
  92. +
  93. +               if ((ioctlres != 0) && (lasterr == WSAEADDRINUSE)) {
  94. +                       (void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
  95. +                               "port=%d in use, trying next port...\n",
  96. +                               sd, sa, currport);
  97. +                       continue;
  98. +               }
  99. +
  100. +               if (ioctlres != 0) {
  101. +                       warnx("bindresvport_sa(sd=%d,sa=0x%p): "
  102. +                               "SIO_ACQUIRE_PORT_RESERVATION failed with "
  103. +                               "error = %d\n",
  104. +                               sd, sa, lasterr);
  105. +                       return 1;
  106. +               }
  107. +
  108. +               /* Success */
  109. +               break;
  110. +       }
  111. +
  112. +        /* fixme: What if the loop completed ? */
  113. +
  114. +       (void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
  115. +               "SIO_ACQUIRE_PORT_RESERVATION succeeded, "
  116. +               "bytesReturned = %u, StartPort=%d, NumberOfPorts=%d, "
  117. +               "Token=%llx\n",
  118. +               sd, sa, bytesReturned, (int)ntohs(portRes.StartPort),
  119. +               portRes.NumberOfPorts, (long long)portRes.Token);
  120. +
  121. +       bytesReturned = 0;
  122. +       ioctlres = WSAIoctl(sd_sock, SIO_ASSOCIATE_PORT_RESERVATION,
  123. +               (LPVOID)&portRes.Token, sizeof(ULONG64), NULL, 0,
  124. +               &bytesReturned, NULL, NULL);
  125. +       lasterr = WSAGetLastError();
  126. +       if (ioctlres != 0) {
  127. +               (void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
  128. +                       "WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) "
  129. +                       "failed with error = %d\n",
  130. +                       sd, sa, lasterr);
  131. +               return 1;
  132. +       }
  133. +
  134. +       (void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
  135. +               "WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
  136. +               sd, sa, bytesReturned);
  137. +
  138.         return 0;
  139.  }
  140.  #else
  141. --------------------------*/
  142. +
  143.  #define IP_PORTRANGE 19
  144.  #define IP_PORTRANGE_LOW 2
  145.  
  146. diff --git a/libtirpc/src/clnt_dg.c b/libtirpc/src/clnt_dg.c
  147. index de318e6..801cf20 100644
  148. --- a/libtirpc/src/clnt_dg.c
  149. +++ b/libtirpc/src/clnt_dg.c
  150. @@ -275,7 +275,7 @@ clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
  151.         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
  152.  
  153.         /* XXX fvdl - do we still want this? */
  154. -#if 0
  155. +#if 1
  156.         (void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
  157.  #endif
  158.  #ifdef IP_RECVERR

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