Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. ipx.c
  5. Abstract:
  6. Contains routines to manipulate ipx addresses.
  7. SnmpSvcAddrToSocket
  8. Environment:
  9. User Mode - Win32
  10. Revision History:
  11. --*/
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // //
  14. // Include files //
  15. // //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #include <snmp.h>
  18. #include <snmputil.h>
  19. #include <winsock.h>
  20. #include <wsipx.h>
  21. #define bcopy(slp, dlp, size) (void)memcpy(dlp, slp, size)
  22. BOOL isHex(LPSTR str, int strLen)
  23. {
  24. int ii;
  25. for (ii=0; ii < strLen; ii++)
  26. if (isxdigit(*str))
  27. str++;
  28. else
  29. return FALSE;
  30. return TRUE;
  31. }
  32. unsigned int toHex(unsigned char x)
  33. {
  34. if (x >= '0' && x <= '9')
  35. return x - '0';
  36. else if (x >= 'A' && x <= 'F')
  37. return x - 'A' + 10;
  38. else if (x >= 'a' && x <= 'f')
  39. return x - 'a' + 10;
  40. else
  41. return 0;
  42. }
  43. // convert str to hex number of NumDigits (must be even) into pNum
  44. void atohex(IN LPSTR str, IN int NumDigits, OUT unsigned char *pNum)
  45. {
  46. int i, j;
  47. j=0;
  48. for (i=0; i < (NumDigits>>1) ; i++)
  49. {
  50. pNum[i] = (toHex(str[j]) << 4) + toHex(str[j+1]);
  51. j+=2;
  52. }
  53. }
  54. // return true if addrText is of the form 123456789ABC or
  55. // 000001.123456789abc
  56. // if pNetNum is not null, upon successful return, pNetNum = network number
  57. // if pNodeNum is not null, upon successful return, pNodeNum = node number
  58. BOOL
  59. SNMP_FUNC_TYPE
  60. SnmpSvcAddrIsIpx(
  61. IN LPSTR addrText,
  62. OUT char pNetNum[4],
  63. OUT char pNodeNum[6])
  64. {
  65. int addrTextLen;
  66. addrTextLen = strlen(addrText);
  67. if (addrTextLen == 12 && isHex(addrText, 12))
  68. {
  69. if (pNetNum)
  70. *((UNALIGNED unsigned long *) pNetNum) = 0L;
  71. if (pNodeNum)
  72. atohex(addrText, 12, pNodeNum);
  73. return TRUE;
  74. }
  75. else if (addrTextLen == 21 && addrText[8] == '.' && isHex(addrText, 8) &&
  76. isHex(addrText+9, 12))
  77. {
  78. if (pNetNum)
  79. atohex(addrText, 8, pNetNum);
  80. if (pNodeNum)
  81. atohex(addrText+9, 12, pNodeNum);
  82. return TRUE;
  83. }
  84. else
  85. return FALSE;
  86. }
  87. BOOL
  88. SNMP_FUNC_TYPE
  89. SnmpSvcAddrToSocket(
  90. LPSTR addrText,
  91. struct sockaddr *addrEncoding
  92. )
  93. {
  94. struct hostent * hp;
  95. struct sockaddr_in * pAddr_in = (struct sockaddr_in *)addrEncoding;
  96. struct sockaddr_ipx * pAddr_ipx = (struct sockaddr_ipx *)addrEncoding;
  97. unsigned long addr;
  98. // check for ipx addr
  99. if (SnmpSvcAddrIsIpx(
  100. addrText,
  101. pAddr_ipx->sa_netnum,
  102. pAddr_ipx->sa_nodenum
  103. )) {
  104. // see if ip host name which looks like ipx
  105. if ((hp = gethostbyname(addrText)) == NULL) {
  106. // host really is ipx machine
  107. pAddr_ipx->sa_family = AF_IPX;
  108. pAddr_ipx->sa_socket = htons(DEFAULT_SNMPTRAP_PORT_IPX);
  109. // address transferred above...
  110. } else {
  111. // host is really ip machine
  112. pAddr_in->sin_family = AF_INET;
  113. pAddr_in->sin_port = htons(DEFAULT_SNMPTRAP_PORT_UDP);
  114. pAddr_in->sin_addr.s_addr = *(unsigned long *)hp->h_addr;
  115. }
  116. } else if (strncmp(addrText, "255.255.255.255", 15) == 0) {
  117. // host is a broadcast address
  118. pAddr_in->sin_family = AF_INET;
  119. pAddr_in->sin_port = htons(DEFAULT_SNMPTRAP_PORT_UDP);
  120. pAddr_in->sin_addr.s_addr = 0xffffffff;
  121. } else if ((long)(addr = inet_addr(addrText)) != -1) {
  122. // host is ip machine
  123. struct servent * pServEnt = NULL;
  124. // attempt to get server information
  125. pServEnt = getservbyname("snmptrap","udp");
  126. pAddr_in->sin_family = AF_INET;
  127. pAddr_in->sin_port = (pServEnt != NULL)
  128. ? (SHORT)pServEnt->s_port
  129. : htons(DEFAULT_SNMPTRAP_PORT_UDP)
  130. ;
  131. pAddr_in->sin_addr.s_addr = addr;
  132. } else if ((hp = gethostbyname(addrText)) != NULL) {
  133. // host is really ip machine
  134. struct servent * pServEnt = NULL;
  135. // attempt to get server information
  136. pServEnt = getservbyname("snmptrap","udp");
  137. pAddr_in->sin_family = AF_INET;
  138. pAddr_in->sin_port = (pServEnt != NULL)
  139. ? (SHORT)pServEnt->s_port
  140. : htons(DEFAULT_SNMPTRAP_PORT_UDP)
  141. ;
  142. pAddr_in->sin_addr.s_addr = *(unsigned long *)hp->h_addr;
  143. } else {
  144. SNMPDBG((
  145. SNMP_LOG_ERROR,
  146. "SNMP: API: could not convert %s to socket.\n",
  147. addrText
  148. ));
  149. // failure
  150. return FALSE;
  151. }
  152. // success
  153. return TRUE;
  154. }