如何解码监听端口162(Snmp Trap)的结果? [英] how to decode the result of listenning to port 162 (Snmp Trap)?
问题描述
我想使用自己的traplistener获取snmp陷阱.实际上,我使用了在Internet上找到的代码,我对其进行了一些修改,现在可以正常工作了.我可以通过端口162进行监听.
I want to get the snmp trap by using my own traplistener. In fact, I used a code found in internet I added some modifications and now it is working. I can listen through the port 162.
#include "stdio.h"
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
#define SNMP_TRAP_PORT 162
#define MAX_MSG 400
static void init(void)
{
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err < 0)
{
puts("WSAStartup failed !");
exit(EXIT_FAILURE);
}
}
static void end(void)
{
WSACleanup();
}
int main(int argc, char *argv[]) {
int sd, rc, n, cliLen;
struct sockaddr_in cliAddr, servAddr;
char msg[MAX_MSG];
/* socket creation */
init();
sd= socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0) {
printf("can't open socket \n");
exit(1);
}
/* bind local server port */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(SNMP_TRAP_PORT);
rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
if(rc<0) {
printf("can't bind port number %d \n", SNMP_TRAP_PORT);
exit(1);
}
printf("waiting for SNMP Traps on UDP port %d\n", SNMP_TRAP_PORT);
/* server infinite loop */
while(1) {
/* init buffer */
memset(msg,0x0,MAX_MSG);
/* receive message */
cliLen = sizeof(cliAddr);
n = recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);
if(n<0) {
printf("%s: cannot receive data \n",argv[0]);
continue;
}
/*message is encoded with ASN1 and should be decoded*/
/* print received message */
printf("SNMP Trap received from %s : %o\n", inet_ntoa(cliAddr.sin_addr),msg);
}/* end of server infinite loop */
end();
return 0;
}
当我收到陷阱时,该代码现在可以正常工作了,我收到一个号码.通常,我应该使用ASN1(十六进制或bin)捕获陷阱,但我得到的只是: Traplistner结果. 我想知道12175440是什么意思. 谢谢
The code works fine now when I get traps I receive a number. Normally, I should get the trap in ASN1 (hex or bin) but I get just this: Traplistner result. I was wondering what does the 12175440 means. Thx
推荐答案
简短答案;它是用八进制写的msg
的内存地址.
Short answer; it is the memory address of msg
written in octal.
如果您是在具有32位int
和64位指针的小端计算机上运行此代码,则很可能是msg
地址的低32位.
If you are running this code on a little-endian machine with 32-bit int
and 64-bit pointers, it is most likely the lower 32 bits of the address of msg
.
这是由于两个薄片的组合造成的:
This is due to a combination of two thins:
-
printf
格式说明符%o
将参数数据中的下一个字节解释为整数,并将其打印为八进制.
The
printf
format specifier%o
interprets the next bytes in the argument data as an integer and prints it in octal.
将数组作为参数传递给函数将转换为将指针传递给第一个元素.因此,这些语句是等效的:
Passing an array as an argument to a function is converted into passing a pointer to the first element. So these statements are equivalent:
printf("%p\n", msg);
printf("%p\n", &msg[0]);
要实际打印您收到的数据,请添加以下内容:
To actually print the data you received, add the following:
for (int i = 0; i < n; ++i) {
printf("%02x ", (unsigned char)msg[i]);
}
printf("\n");
这篇关于如何解码监听端口162(Snmp Trap)的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!