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