Good day! It was necessary to write a program that implements SNMP snmpget. So far I started just by sending the package. Before sending it, I of course made a snmpget and dumped the package. Then, accordingly recoded it and began to send. So, I am sending a packet to the 161 port locale for the host, but I do not receive anything in return. What could be the problem?

#ifndef CONFIG_H #define CONFIG_H #include <fcntl.h> #include <netinet/in.h> #include <linux/limits.h> #include <netdb.h> #include <math.h> #include <memory.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #define ASN1_INTEGER 0x02 #define ASN1_OID 0x06 #define ASN1_OCTET_STRING 0x04 #define ASN1_SEQUINCE 0x30 #define ASN1_NULL 0x05 #define ASN1_GET_REQUEST_PDU 0xA0 #define ASN1_GET_RESPONCE_PDU 0xA2 #define LEFT_BIT 0x80 #define SRV_HOST "localhost" #define SRV_PORT 161 #define CLNT_PORT 1610 #define BUF_SIZE 64 #define ALLOCATE_ARRAY(ptr, type, n) \ ptr = ( type* ) calloc( n, sizeof(type) ) #endif octet_t translate_char(char C) { switch(C) { case '0': return 0x0; case '1': return 0x1; case '2': return 0x2; case '3': return 0x3; case '4': return 0x4; case '5': return 0x5; case '6': return 0x6; case '7': return 0x7; case '8': return 0x8; case '9': return 0x9; case 'A': return 0xA; case 'B': return 0xB; case 'C': return 0xC; case 'D': return 0xD; case 'E': return 0xE; case 'F': return 0xF; default: return 0x0; } } octet_t fill_octet(char *str) { /* printf("%02X\n", translate_char(str[0])); printf("%02X\n", translate_char(str[1])); char first = translate_char(str[0]); first = first << 4; */ return (translate_char(str[0]) << 4) | translate_char(str[1]); } octets_t get_raw_packet(char *str) { octets_t pck; ALLOCATE_ARRAY(pck, octet_t, strlen(str)); size_t i, j = 0; for(i = 0; i < strlen(str); i+=2) { pck[j] = fill_octet(&str[i]); j++; } return pck; } int main(int argc, char *argv[]) { int s; int from_len; char buf[BUF_SIZE]; struct hostent *hp; struct sockaddr_in clnt_sin, srv_sin; write(1, "\n\n\n", 3); s = socket (PF_INET, SOCK_DGRAM, IPPROTO_IP); memset ((char *)&clnt_sin, '\0', sizeof(clnt_sin)); clnt_sin.sin_family = AF_INET; clnt_sin.sin_addr.s_addr = INADDR_ANY; clnt_sin.sin_port = htons(CLNT_PORT); bind (s, (struct sockaddr *)&clnt_sin, sizeof(clnt_sin)); octets_t buf1 = get_raw_packet("302A02010104067075626C6963A11D020443F4A0E8020100020100300F300D06092B06010201190105000500"); char *inbuf; ALLOCATE_ARRAY(inbuf, char, BUFSIZ); memset ((char *)&srv_sin, '\0', sizeof(srv_sin)); hp = gethostbyname (SRV_HOST); srv_sin.sin_family = AF_INET; memcpy ((char *)&srv_sin.sin_addr,hp->h_addr,hp->h_length); srv_sin.sin_port = htons(SRV_PORT); socklen_t *addrlen; ALLOCATE_ARRAY(addrlen, socklen_t, 1); sendto(s, buf1, strlen(buf1), 0, (struct sockaddr *) &srv_sin, sizeof(srv_sin)); fd_set rfds, afds; int nfds=getdtablesize(); FD_ZERO(&afds); FD_SET(s,&afds); while(1) { memcpy(&rfds, &afds, sizeof(rfds)); select(nfds, &rfds, NULL,NULL,NULL); if ( FD_ISSET(s, &rfds) ) { from_len = recvfrom(s, inbuf, BUFSIZ, 0, (struct sockaddr *) &clnt_sin, addrlen);/*recv (s, buf, BUF_SIZE, 0);*/ write (1, inbuf, from_len); } } from_len = recvfrom(s, inbuf, BUFSIZ, 0, (struct sockaddr *) &clnt_sin, addrlen);/*recv (s, buf, BUF_SIZE, 0);*/ write (1, inbuf, from_len); close (s); exit (0); } 
  • Why did you decide that something went? You do not have a survey as sendto completed. - 0xdb
  • Added select, but he still doesn’t want to work. Maybe something was wrong? - Sergey Samoylov
  • And at what here? result = sendto(...) ; if (result<0) { perror("sendto"); } result = sendto(...) ; if (result<0) { perror("sendto"); } - 0xdb
  • ahem, well, how would strace show that the send was: sendto (3, "0 * \ 2 \ 1 \ 1 \ 4 \ 6public \ 241 \ 35 \ 2 \ 4C \ 364 \ 240 \ 350 \ 2 \ 1", 23 , 0, {sa_family = AF_INET, sin_port = htons (161), sin_addr = inet_addr ("127.0.0.1")}, 16) = 23 - Sergey Samoylov
  • Then see who is sitting on port 161, Put him debug and see the logs. - 0xdb

0