xref: /openbsd/regress/usr.sbin/snmpd/transport.c (revision 4e63e8f9)
1 #include <sys/socket.h>
2 
3 #include <ber.h>
4 #include <err.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 
10 #include "regress.h"
11 
12 #define MIB_SUBAGENT_TRANSPORT_TCP MIB_SUBAGENT_TRANSPORT, 1
13 #define MIB_TRANSPORT_TCP MIB_TRANSPORT, 1
14 
15 void
transport_tcp_get(void)16 transport_tcp_get(void)
17 {
18 	struct sockaddr_storage ss;
19 	struct sockaddr *sa = (struct sockaddr *)&ss;
20 	socklen_t salen;
21 	int snmp_s, ax_s;
22 	uint32_t sessionid;
23 	struct varbind varbind = {
24 		.type = TYPE_NULL,
25 		.name = OID_STRUCT(MIB_TRANSPORT_TCP, 1, 0),
26 		.data.int32 = 1
27 	};
28 	int32_t requestid;
29 	char buf[1024];
30 	size_t n;
31 
32 	ax_s = agentx_connect(axsocket);
33 	sessionid = agentx_open(ax_s, 0, 0,
34 	    OID_ARG(MIB_SUBAGENT_TRANSPORT_TCP, 1), __func__);
35 	agentx_register(ax_s, sessionid, 0, 0, 127, 0,
36 	    OID_ARG(MIB_TRANSPORT_TCP, 1), 0);
37 
38 	salen = snmp_resolve(SOCK_STREAM, hostname, servname, sa);
39 	snmp_s = snmp_connect(SOCK_STREAM, sa, salen);
40 	requestid = snmpv2_get(snmp_s, community, 0, &varbind, 1);
41 
42 	n = agentx_read(ax_s, buf, sizeof(buf), 1000);
43 	agentx_get_handle(__func__, buf, n, 0, sessionid, &varbind, 1);
44 
45 	varbind.type = TYPE_INTEGER;
46 	agentx_response(ax_s, buf, NOERROR, 0, &varbind, 1);
47 
48 	snmpv2_response_validate(snmp_s, 1000, community, requestid, 0, 0,
49 	    &varbind, 1);
50 }
51 
52 void
transport_tcp_disconnect(void)53 transport_tcp_disconnect(void)
54 {
55 	struct sockaddr_storage ss;
56 	struct sockaddr *sa = (struct sockaddr *)&ss;
57 	socklen_t salen;
58 	int snmp_s, ax_s;
59 	uint32_t sessionid;
60 	struct varbind varbind = {
61 		.type = TYPE_NULL,
62 		.name = OID_STRUCT(MIB_TRANSPORT_TCP, 2, 0),
63 		.data.int32 = 1
64 	};
65 	int32_t requestid;
66 	char buf[1024];
67 	size_t n;
68 
69 	ax_s = agentx_connect(axsocket);
70 	sessionid = agentx_open(ax_s, 0, 0,
71 	    OID_ARG(MIB_SUBAGENT_TRANSPORT_TCP, 2), __func__);
72 	agentx_register(ax_s, sessionid, 0, 0, 127, 0,
73 	    OID_ARG(MIB_TRANSPORT_TCP, 2), 0);
74 
75 	salen = snmp_resolve(SOCK_STREAM, hostname, servname, sa);
76 	snmp_s = snmp_connect(SOCK_STREAM, sa, salen);
77 	requestid = snmpv2_get(snmp_s, community, 0, &varbind, 1);
78 
79 	close(snmp_s);
80 
81 	n = agentx_read(ax_s, buf, sizeof(buf), 1000);
82 	agentx_get_handle(__func__, buf, n, 0, sessionid, &varbind, 1);
83 
84 	varbind.type = TYPE_INTEGER;
85 	agentx_response(ax_s, buf, NOERROR, 0, &varbind, 1);
86 }
87 
88 void
transport_tcp_double_get_disconnect(void)89 transport_tcp_double_get_disconnect(void)
90 {
91 	struct sockaddr_storage ss;
92 	struct sockaddr *sa = (struct sockaddr *)&ss;
93 	socklen_t salen;
94 	int snmp_s, ax_s;
95 	uint32_t sessionid;
96 	struct varbind varbind = {
97 		.type = TYPE_NULL,
98 		.name = OID_STRUCT(MIB_TRANSPORT_TCP, 2, 0),
99 		.data.int32 = 1
100 	};
101 	struct ber_element *message;
102 	struct ber ber = {};
103 	char buf[1024];
104 	ssize_t buflen, writelen;
105 	void *ptr;
106 	size_t n;
107 
108 	ax_s = agentx_connect(axsocket);
109 	sessionid = agentx_open(ax_s, 0, 0,
110 	    OID_ARG(MIB_SUBAGENT_TRANSPORT_TCP, 2), __func__);
111 	agentx_register(ax_s, sessionid, 0, 0, 127, 0,
112 	    OID_ARG(MIB_TRANSPORT_TCP, 2), 0);
113 
114 	salen = snmp_resolve(SOCK_STREAM, hostname, servname, sa);
115 	snmp_s = snmp_connect(SOCK_STREAM, sa, salen);
116 
117 	message = snmpv2_build(community, REQUEST_GET, 0, 0, 0, &varbind, 1);
118 
119 	if (ober_write_elements(&ber, message) == -1)
120 		err(1, "ober_write_elements");
121 
122 	buflen = ober_get_writebuf(&ber, &ptr);
123 	/* send two copies in a single window */
124 	memcpy(buf, ptr, buflen);
125 	memcpy(buf + buflen, ptr, buflen);
126 
127 	if ((writelen = write(snmp_s, buf, buflen * 2)) == -1)
128 		err(1, "write");
129 	if (writelen != buflen * 2)
130 		errx(1, "write: short write");
131 
132 	if (verbose) {
133 		printf("SNMP send(%d):\n", snmp_s);
134 		smi_debug_elements(message);
135 	}
136 	ober_free_elements(message);
137 	ober_free(&ber);
138 
139 	close(snmp_s);
140 
141 	n = agentx_read(ax_s, buf, sizeof(buf), 1000);
142 	agentx_get_handle(__func__, buf, n, 0, sessionid, &varbind, 1);
143 
144 	varbind.type = TYPE_INTEGER;
145 	agentx_response(ax_s, buf, NOERROR, 0, &varbind, 1);
146 
147 	n = agentx_read(ax_s, buf, sizeof(buf), 1000);
148 	agentx_get_handle(__func__, buf, n, 0, sessionid, &varbind, 1);
149 	agentx_response(ax_s, buf, NOERROR, 0, &varbind, 1);
150 }
151