feature_tests.h: always use largefile interfaces
[unleashed.git] / usr / src / lib / libilb / common / ilb_nat.c
blob192d7b6d59525dd3c5774eb74f6a43cdeec8a0b4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <unistd.h>
30 #include <stddef.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include "libilb.h"
34 #include "libilb_impl.h"
36 enum which_tbl {
37 show_nat = 1,
38 show_persist
41 /* The common function to show kernel info. */
42 static ilb_status_t ilb_show_info(ilb_handle_t, char *, size_t *, boolean_t *,
43 enum which_tbl);
46 * To get the ILB NAT table.
48 * buf: The buffer to return the NAT table entries.
49 * num: The caller sets it to the number of ilb_nat_info_t entries buf can
50 * hold. On return, it contains the actual number of entries put in buf.
51 * end: The caller sets it to B_TRUE if it only wants at most num entries to
52 * be returned. The transaction to ilbd will be termianted when this
53 * call returns.
54 * The caller sets it to B_FALSE if it intends to get the whole table.
55 * If the whole table has more than num entries, the caller can call
56 * this function again to retrieve the rest of the table.
57 * On return, end is set to B_TRUE if end of table is reached; B_FALSE
58 * if there are still remaining entries.
60 ilb_status_t
61 ilb_show_nat(ilb_handle_t h, ilb_nat_info_t buf[], size_t *num,
62 boolean_t *end)
64 return (ilb_show_info(h, (char *)buf, num, end, show_nat));
68 * To get the ILB persistent entry table.
70 * buf: The buffer to return the persistent table entries.
71 * num: The caller sets it to the number of ilb_persist_info_t entries buf can
72 * hold. On return, it contains the actual number of entries put in buf.
73 * end: The caller sets it to B_TRUE if it only wants at most num entries to
74 * be returned. The transaction to ilbd will be termianted when this
75 * call returns.
76 * The caller sets it to B_FALSE if it intends to get the whole table.
77 * If the whole table has more than num entries, the caller can call
78 * this function again to retrieve the rest of the table.
79 * On return, end is set to B_TRUE if end of table is reached; B_FALSE
80 * if there are still remaining entries.
82 ilb_status_t
83 ilb_show_persist(ilb_handle_t h, ilb_persist_info_t buf[], size_t *num,
84 boolean_t *end)
86 return (ilb_show_info(h, (char *)buf, num, end, show_persist));
90 * The function doing the work... The tbl parameter determines whith table
91 * to show.
93 static ilb_status_t
94 ilb_show_info(ilb_handle_t h, char *buf, size_t *num, boolean_t *end,
95 enum which_tbl tbl)
97 ilb_comm_t *req, *rbuf;
98 ilb_show_info_t *req_si, *tmp_si;
99 size_t reqsz, rbufsz, tmp_rbufsz, cur_num;
100 size_t entry_sz;
101 ilb_status_t rc;
103 if (*num == 0)
104 return (ILB_STATUS_EINVAL);
106 reqsz = sizeof (ilb_comm_t) + sizeof (ilb_show_info_t);
107 if ((req = malloc(reqsz)) == NULL)
108 return (ILB_STATUS_ENOMEM);
109 req_si = (ilb_show_info_t *)&req->ic_data;
112 * Need to allocate a receive buffer and then copy the buffer
113 * content to the passed in buf. The reason is that the
114 * communication to ilbd is message based and the protocol
115 * includes a header in the reply. We need to remove this header
116 * from the message, hence the copying...
118 if (tbl == show_nat)
119 entry_sz = sizeof (ilb_nat_info_t);
120 else
121 entry_sz = sizeof (ilb_persist_info_t);
122 rbufsz = *num * entry_sz + sizeof (ilb_comm_t) +
123 sizeof (ilb_show_info_t);
124 if ((rbuf = malloc(rbufsz)) == NULL) {
125 free(req);
126 return (ILB_STATUS_ENOMEM);
129 if (tbl == show_nat)
130 req->ic_cmd = ILBD_SHOW_NAT;
131 else
132 req->ic_cmd = ILBD_SHOW_PERSIST;
133 req->ic_flags = 0;
134 req_si->sn_num = *num;
135 cur_num = 0;
137 do {
138 tmp_rbufsz = rbufsz;
139 rc = i_ilb_do_comm(h, req, reqsz, rbuf, &tmp_rbufsz);
140 if (rc != ILB_STATUS_OK)
141 goto out;
142 if (rbuf->ic_cmd != ILBD_CMD_OK) {
143 rc = *(ilb_status_t *)&rbuf->ic_data;
144 goto out;
147 tmp_si = (ilb_show_info_t *)&rbuf->ic_data;
149 cur_num += tmp_si->sn_num;
150 bcopy(&tmp_si->sn_data, buf, tmp_si->sn_num * entry_sz);
151 buf += tmp_si->sn_num * entry_sz;
154 * Buffer is filled, regardless of this is the end of table or
155 * not, we need to stop.
157 if (cur_num == *num)
158 break;
159 /* Try to fill in the rest. */
160 req_si->sn_num = *num - cur_num;
161 } while (!(rbuf->ic_flags & ILB_COMM_END));
163 *num = cur_num;
165 /* End of transaction, let the caller know. */
166 if (rbuf->ic_flags & ILB_COMM_END) {
167 *end = B_TRUE;
168 } else {
169 /* The user wants to terminate the transaction */
170 if (*end) {
171 req->ic_flags = ILB_COMM_END;
172 tmp_rbufsz = rbufsz;
173 rc = i_ilb_do_comm(h, req, reqsz, rbuf, &tmp_rbufsz);
176 out:
177 free(req);
178 free(rbuf);
179 return (rc);