Debian Codegraph @

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

raw
1 /*
2 * tcpConnTable MIB architecture support for FreeBSD/DragonFlyBSD
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);
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);
99
100 return rc;
101 }
102
103 #if defined(freebsd4) || defined(darwin)
104 #define NS_ELEM struct xtcpcb
105 #else
106 #define NS_ELEM struct xinpcb
107 #endif
108
109 /**
110 *
111 * @retval 0 no errors
112 * @retval !0 errors
113 */
114 static int
115 _load(netsnmp_container *container, u_int load_flags)
116 {
117 size_t len;
118 int sname[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_PCBLIST };
119 char *tcpcb_buf = NULL;
120 #if defined(dragonfly)
121 struct xinpcb *xig = NULL;
122 int StateMap[] = { 1, 1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11 };
123 int i, count;
124 #else
125 struct xinpgen *xig = NULL;
126 int StateMap[] = { 1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11 };
127 #endif
128 netsnmp_tcpconn_entry *entry;
129 int state;
130 int rc = 0;
131
132 /*
133 * Read in the buffer containing the TCP table data
134 */
135 len = 0;
136 if (sysctl(sname, 4, 0, &len, 0, 0) < 0 ||
137 (tcpcb_buf = malloc(len)) == NULL)
138 return -1;
139 if (sysctl(sname, 4, tcpcb_buf, &len, 0, 0) < 0) {
140 free(tcpcb_buf);
141 return -1;
142 }
143
144 /*
145 * Unpick this into the constituent 'xinpgen' structures, and extract
146 * the 'inpcb' elements into a linked list (built in reverse)
147 */
148 #if defined(dragonfly)
149 xig = (struct xinpcb *) tcpcb_buf;
150 count = len/sizeof(NS_ELEM);
151 #else
152 xig = (struct xinpgen *) tcpcb_buf;
153 xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
154 #endif
155
156 #if defined(dragonfly)
157 for (i = 0; i < count; i++)
158 #else
159 while (xig && (xig->xig_len > sizeof(struct xinpgen)))
160 #endif
161 {
162 NS_ELEM pcb = *((NS_ELEM *) xig);
163 #if defined(dragonfly)
164 xig = (struct xinpcb *) ((char *) xig + xig->xi_len);
165 #else
166 xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
167 #endif
168 #if __FreeBSD_version >= 1200026
169 state = StateMap[pcb.t_state];
170 #else
171 state = StateMap[pcb.xt_tp.t_state];
172 #endif
173
174 if (load_flags) {
175 if (state == TCPCONNECTIONSTATE_LISTEN) {
176 if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) {
177 DEBUGMSGT(("verbose:access:tcpconn:container",
178 " skipping listen\n"));
179 continue;
180 }
181 }
182 else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) {
183 DEBUGMSGT(("verbose:access:tcpconn:container",
184 " skipping non-listen\n"));
185 continue;
186 }
187 }
188
189 #if !defined(NETSNMP_ENABLE_IPV6)
190 #ifdef INP_ISIPV6
191 if (INP_ISIPV6(&pcb.xt_inp))
192 #else
193 if (pcb.xt_inp.inp_vflag & INP_IPV6)
194 #endif
195 continue;
196 #endif
197
198 entry = netsnmp_access_tcpconn_entry_create();
199 if(NULL == entry) {
200 rc = -3;
201 break;
202 }
203
204 /** oddly enough, these appear to already be in network order */
205 entry->loc_port = htons(pcb.xt_inp.inp_lport);
206 entry->rmt_port = htons(pcb.xt_inp.inp_fport);
207 entry->tcpConnState = state;
208 entry->pid = 0;
209
210 /** the addr string may need work */
211 #ifdef INP_ISIPV6
212 if (INP_ISIPV6(&pcb.xt_inp)) {
213 #else
214 if (pcb.xt_inp.inp_vflag & INP_IPV6) {
215 #endif
216 entry->loc_addr_len = entry->rmt_addr_len = 16;
217 memcpy(entry->loc_addr, &pcb.xt_inp.in6p_laddr, 16);
218 memcpy(entry->rmt_addr, &pcb.xt_inp.in6p_faddr, 16);
219 }
220 else {
221 entry->loc_addr_len = entry->rmt_addr_len = 4;
222 memcpy(entry->loc_addr, &pcb.xt_inp.inp_laddr, 4);
223 memcpy(entry->rmt_addr, &pcb.xt_inp.inp_faddr, 4);
224 }
225
226 /*
227 * add entry to container
228 */
229 entry->arbitrary_index = CONTAINER_SIZE(container) + 1;
230 CONTAINER_INSERT(container, entry);
231 }
232
233 free(tcpcb_buf);
234
235 if(rc<0)
236 return rc;
237
238 return 0;
239 }