Debian Codegraph @

agent/mibgroup/tcp-mib/data_access/tcpConn_netbsd.c

raw
1 /*
2 * tcpConnTable MIB architecture support for NetBSD
3 */
4 #include <net-snmp/net-snmp-config.h>
5 #include <net-snmp/net-snmp-includes.h>
6
7 #include <net-snmp/agent/net-snmp-agent-includes.h>
8 #include <net-snmp/data_access/tcpConn.h>
9
10 #include "tcp-mib/tcpConnectionTable/tcpConnectionTable_constants.h"
11 #include "tcp-mib/data_access/tcpConn_private.h"
12
13 #include "mibII/mibII_common.h"
14
15 #ifdef HAVE_NETINET_TCP_H
16 #include <netinet/tcp.h>
17 #endif
18 #ifdef HAVE_NETINET_TCP_TIMER_H
19 #include <netinet/tcp_timer.h>
20 #endif
21 #ifdef HAVE_NETINET_TCPIP_H
22 #include <netinet/tcpip.h>
23 #endif
24 #ifdef HAVE_NETINET_TCP_VAR_H
25 #include <netinet/tcp_var.h>
26 #endif
27
28 static int _load(netsnmp_container *container, u_int flags, int var);
29
30 /*
31 * initialize arch specific storage
32 *
33 * @retval 0: success
34 * @retval <0: error
35 */
36 int
37 netsnmp_arch_tcpconn_entry_init(netsnmp_tcpconn_entry *entry)
38 {
39 /*
40 * init
41 */
42 return 0;
43 }
44
45 /*
46 * cleanup arch specific storage
47 */
48 void
49 netsnmp_arch_tcpconn_entry_cleanup(netsnmp_tcpconn_entry *entry)
50 {
51 /*
52 * cleanup
53 */
54 }
55
56 /*
57 * copy arch specific storage
58 */
59 int
60 netsnmp_arch_tcpconn_entry_copy(netsnmp_tcpconn_entry *lhs,
61 netsnmp_tcpconn_entry *rhs)
62 {
63 return 0;
64 }
65
66 /*
67 * delete an entry
68 */
69 int
70 netsnmp_arch_tcpconn_entry_delete(netsnmp_tcpconn_entry *entry)
71 {
72 if (NULL == entry)
73 return -1;
74 /** xxx-rks:9 tcpConn delete not implemented */
75 return -1;
76 }
77
78
79 /**
80 *
81 * @retval 0 no errors
82 * @retval !0 errors
83 */
84 int
85 netsnmp_arch_tcpconn_container_load(netsnmp_container *container,
86 u_int load_flags )
87 {
88 int rc = 0;
89
90 DEBUGMSGTL(("access:tcpconn:container",
91 "tcpconn_container_arch_load (flags %x)\n", load_flags));
92
93 if (NULL == container) {
94 snmp_log(LOG_ERR, "no container specified/found for access_tcpconn\n");
95 return -1;
96 }
97
98 rc = _load(container, load_flags, 4);
99 #if defined(NETSNMP_ENABLE_IPV6)
100 rc = _load(container, load_flags, 6);
101 #endif
102
103 return rc;
104 }
105
106
107 /**
108 *
109 * @retval 0 no errors
110 * @retval !0 errors
111 */
112 static int
113 _load(netsnmp_container *container, u_int load_flags, int ver)
114 {
115 const char *mibname;
116 int mib[8];
117 size_t mib_len;
118 struct kinfo_pcb *pcblist;
119 size_t pcb_len;
120 int StateMap[] = { 1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11 };
121 netsnmp_tcpconn_entry *entry;
122 int state;
123 int i, rc = 0;
124
125 /*
126 * Read in the buffer containing the TCP table data
127 */
128 switch (ver) {
129 case 4:
130 mibname = "net.inet.tcp.pcblist";
131 break;
132 case 6:
133 mibname = "net.inet6.tcp6.pcblist";
134 break;
135 default:
136 snmp_log(LOG_ERR, "tcp-mib:data_access:_load: bad version %d\n", ver);
137 return -1;
138 }
139
140 if (sysctlnametomib(mibname, mib, &mib_len) == -1) {
141 snmp_log(LOG_ERR, "tcp-mib:data_access:_load: cant resolve mib %s\n", mibname);
142 return -1;
143 }
144
145 if (sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &pcb_len, NULL, 0) == -1) {
146 snmp_log(LOG_ERR, "tcp-mib:data_access:_load: cant size mib %s\n", mibname);
147 return -1;
148 }
149
150 if ((pcblist = malloc(pcb_len)) == NULL) {
151 snmp_log(LOG_ERR, "tcp-mib:data_access:_load: cant allocate mib %s\n", mibname);
152 return -1;
153 }
154 memset(pcblist, 0, pcb_len);
155
156 mib[6] = sizeof(*pcblist);
157 mib[7] = pcb_len / sizeof(*pcblist);
158
159 if (sysctl(mib, sizeof(mib) / sizeof(*mib),
160 pcblist, &pcb_len, NULL, 0) == -1) {
161 snmp_log(LOG_ERR, "tcp-mib:data_access:_load: cant size mib %s\n", mibname);
162 return -1;
163 }
164
165 /*
166 * Unpick this into the constituent structures, and extract
167 * the 'inpcb' elements into a linked list (built in reverse)
168 */
169 for (i = 0; i < pcb_len / sizeof(*pcblist); i++) {
170 struct kinfo_pcb *pcb = pcblist+i;
171 state = StateMap[pcb->ki_tstate];
172
173 if (load_flags) {
174 if (state == TCPCONNECTIONSTATE_LISTEN) {
175 if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) {
176 DEBUGMSGT(("verbose:access:tcpconn:container",
177 " skipping listen\n"));
178 continue;
179 }
180 }
181 else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) {
182 DEBUGMSGT(("verbose:access:tcpconn:container",
183 " skipping non-listen\n"));
184 continue;
185 }
186 }
187
188 entry = netsnmp_access_tcpconn_entry_create();
189 if(NULL == entry) {
190 rc = -3;
191 break;
192 }
193
194 entry->tcpConnState = state;
195 entry->pid = 0;
196
197 if (ver == 6) {
198 struct sockaddr_in6 src, dst;
199 memcpy(&src, &pcb->ki_s, sizeof(src));
200 memcpy(&dst, &pcb->ki_d, sizeof(dst));
201 entry->loc_addr_len = entry->rmt_addr_len = 16;
202 memcpy(entry->loc_addr, &src.sin6_addr, 16);
203 memcpy(entry->rmt_addr, &dst.sin6_addr, 16);
204 entry->loc_port = ntohs(src.sin6_port);
205 entry->rmt_port = ntohs(dst.sin6_port);
206 }
207 else {
208 struct sockaddr_in src, dst;
209 memcpy(&src, &pcb->ki_s, sizeof(src));
210 memcpy(&dst, &pcb->ki_d, sizeof(dst));
211 entry->loc_addr_len = entry->rmt_addr_len = 4;
212 memcpy(entry->loc_addr, &src.sin_addr, 4);
213 memcpy(entry->rmt_addr, &dst.sin_addr, 4);
214 entry->loc_port = ntohs(src.sin_port);
215 entry->rmt_port = ntohs(dst.sin_port);
216 }
217
218 /*
219 * add entry to container
220 */
221 entry->arbitrary_index = CONTAINER_SIZE(container) + 1;
222 CONTAINER_INSERT(container, entry);
223 }
224
225 if(rc<0)
226 return rc;
227
228 return 0;
229 }