c++ - Windows Server 2012 上的 SNMP Extension Agent 无法连接到它需要数据的端口

标签 c++ winsock snmp

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

这是我的场景:

  1. 我们的 SNMP Extension Agent 创建一个 Windows 套接字来连接我们的一个 特定端口号上的代理以获取一些配置 我们传播到 MIB 浏览器的信息。
  2. 当我们全新安装我们的应用程序然后安装 Windows Server 2012 上的 SNMP,一切运行良好。
  3. 重启后,对我们的 Extension Agent 的任何 SNMP 请求都会超时 通过 MIB 浏览器。我从 Extension Agent 调试并发现 一些如何在 connect() 调用中我们得到一个“WSAEACCES (10013) Permission denied error”。看下面代码中的注释。
  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();

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

请让我知道我还能尝试什么,或者如果您需要更多信息。

感谢和问候 阿迪亚

最佳答案

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

Any SNMP Extension agent that attempts to perform any UDP or TCP network communication on Windows Server 2012 or Windows 8 will fail. The socket connect() request will fail with the following NT status code: 0xC0000022 = STATUS_ACCESS_DENIED {Access Denied}. A process has requested access to an object, but has not been granted those access rights.

因此,为了解决这个问题,我们需要运行我发现的 power shell 脚本 here为 SNMP 服务添加入站和出站规则以与我们的代理端口号通信。

关于c++ - Windows Server 2012 上的 SNMP Extension Agent 无法连接到它需要数据的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419192/

相关文章:

c++ - 如何捕获 QML 绘图缓冲区

c++ - 虚表如何处理纯虚函数

c++ - 在同一端口上发送和接收 udp 数据包

java - SNMP4J.jar - java.lang.ClassNotFoundException

SNMP 解释 RMON2-MIB 文件的正确方法是什么?

java - 当 net-snmp get 返回响应事件时,为什么 snmp 代理不接受 snmp4j get 上的请求

c++ - 如何重载下标运算符以允许角色扮演角色数组?

C++ 部分方法特化

c - Mingw 未定义对 libws2_32 的引用 ('getnameinfo' , 'getaddrinfo' , ...)

windows - 什么类型的网络流量不使用 Windows 中的 Winsock?