Windows Server 2012上的SNMP扩展代理无法连接到需要数据的端口

SNMP Extension Agent on Windows Server 2012 unable to connect to a port from which it needs data

本文关键字:数据 连接 上的 2012 Server SNMP 扩展 代理 Windows      更新时间:2023-10-16

更新:因此,在Windows Server 2012上,如果我手动从WindowsSystem32调用snmp.exe以"以管理员身份运行",问题就会消失。在Windows Server 2012上,我无法以管理员身份强制SNMP服务启动。

下面是我的场景:

  1. 我们的SNMP扩展代理创建一个windows套接字来连接到我们的代理在特定端口号上获取一些配置我们向MIB浏览器传播的信息。
  2. 当我们重新安装我们的应用程序,然后安装SNMP在Windows Server 2012,一切工作得很好。
  3. 重启后,对扩展代理的任何SNMP请求都超时MIB browser。我从扩展代理中调试并发现在connect()调用中,我们得到了一个"WSAEACCES"(10013)权限被拒绝错误"。请看下面代码中的注释:
  4. 同样的事情在Windows Server 2008上运行良好。
下面是代码片段:
struct sockaddr_in dest;
int sockfd;
char buffer;
int bytes_read;
char portNumberStr[10];
int iResult;
struct addrinfo *result = NULL, *ptr = NULL, hints;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
iResult = WSAStartup(wVersionRequested, &wsaData);
if (iResult != 0) 
{
    int WSAError = WSAGetLastError();       
    return SOAP_NO_SOCKET_ERROR;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
sprintf(portNumberStr, "%d", port_number);
iResult = getaddrinfo("127.0.0.1", portNumberStr, &hints, &result);
if (iResult != 0)
{
    int WSAError = WSAGetLastError();
    WSACleanup();
    return SOAP_NO_SOCKET_ERROR;
}
// Loop through the results addrinfo structure
bool connectionSuccess = false;
for(ptr = result; ptr != NULL; ptr = result->ai_next)
{
    // Create a socket
    sockfd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if (INVALID_SOCKET == sockfd)
    {
        int WSAError = WSAGetLastError();
        continue;           
    }
    iResult = connect(sockfd, ptr->ai_addr, (int)ptr->ai_addrlen);  // This is the call where I get a WSAEACCES (10013) error.
    if (iResult == SOCKET_ERROR) 
    {
        int WSAError = WSAGetLastError();
        closesocket(sockfd);
        continue;
    }
    connectionSuccess = true;
    break;
}
// Clean up
freeaddrinfo(result);
if(false == connectionSuccess)
{
    return SOAP_ERROR;
}
// Form the Request
*localRequest = "Request goes in here"

// Send the message to the agent through a TCP socket.
send(sockfd,localRequest->c_str(),(int)localRequest->length(), 0);

// Clear out the request string so we can use it to hold a response.
*localRequest = "";
// Keep getting bytes from server until finished.
do
{
    bytes_read = recv(sockfd, &buffer, sizeof(buffer), 0);
    if ( bytes_read > 0 )
    {
        localRequest->append(1,buffer);
    }
}
while ( bytes_read > 0 );
closesocket(sockfd);
WSACleanup();

与独立应用程序相同的代码能够与我们的代理通信并获得所需的数据。

请让我知道我还可以尝试什么,或者如果你需要更多的信息。

谢谢Aditya

这篇KB文章描述了我们的SNMP扩展代理在Windows 8/2012上面临的问题。本文中描述的根本原因如下:

任何试图在Windows Server 2012或Windows 8上执行任何UDP或TCP网络通信的SNMP扩展代理将失败。socket connect()请求将失败,状态码如下:0xC0000022 = STATUS_ACCESS_DENIED {Access Denied}。进程请求访问对象,但未被授予访问权限。

因此,为了解决这个问题,我们需要运行power shell脚本,我在这里找到它来添加SNMP服务的入站和出站规则,以便与我们的代理端口号对话。

connect()文档说明:

WSAEACCES
试图连接数据报套接字到广播地址失败,因为setsockopt选项SO_BROADCAST未启用。

…对于无连接套接字,名称可以表示任何有效地址,包括广播地址。然而,要连接到广播地址,套接字必须使用setsockopt来启用SO_BROADCAST选项。否则,连接将失败,错误码为WSAEACCES。