]> git.saurik.com Git - apple/network_cmds.git/blame - mptcp_client/conn_lib.c
network_cmds-606.100.3.tar.gz
[apple/network_cmds.git] / mptcp_client / conn_lib.c
CommitLineData
342c141e
A
1/*
2 * Copyright (c) 2012-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29//
30// Created by Anumita Biswas on 10/30/12.
31//
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <string.h>
37#include <stdint.h>
38#include <sys/types.h>
39#include <sys/socket.h>
40#include <sys/ioctl.h>
41#include <errno.h>
42
43
44#include "conn_lib.h"
45
46int
89c4ed63 47copyassocids(int s, sae_associd_t **aidpp, uint32_t *cnt)
342c141e
A
48{
49 struct so_aidreq aidr;
89c4ed63 50 sae_associd_t *buf;
342c141e 51 int err;
d9520f62 52
342c141e
A
53 if (aidpp == NULL || cnt == NULL) {
54 errno = EINVAL;
55 return (-1);
56 }
57 *aidpp = NULL;
58 *cnt = 0;
d9520f62 59
342c141e 60 bzero(&aidr, sizeof (aidr));
d9520f62 61
342c141e
A
62 err = ioctl(s, SIOCGASSOCIDS, &aidr);
63 if (err != 0)
64 return (-1);
d9520f62 65
342c141e
A
66 /* none, just return */
67 if (aidr.sar_cnt == 0)
68 return (0);
d9520f62 69
89c4ed63 70 buf = calloc(aidr.sar_cnt, sizeof (sae_associd_t));
342c141e
A
71 if (buf == NULL)
72 return (-1);
d9520f62 73
342c141e
A
74 aidr.sar_aidp = buf;
75 err = ioctl(s, SIOCGASSOCIDS, &aidr);
76 if (err != 0) {
77 free(buf);
78 return (-1);
79 }
d9520f62 80
342c141e
A
81 *aidpp = buf;
82 *cnt = aidr.sar_cnt;
d9520f62 83
342c141e
A
84 return (0);
85}
86
87void
89c4ed63 88freeassocids(sae_associd_t *aidp)
342c141e
A
89{
90 free(aidp);
91}
92
93int
89c4ed63 94copyconnids(int s, sae_associd_t aid, sae_connid_t **cidp, uint32_t *cnt)
342c141e
A
95{
96 struct so_cidreq cidr;
89c4ed63 97 sae_connid_t *buf;
342c141e 98 int err;
d9520f62 99
342c141e
A
100 if (cidp == NULL || cnt == NULL) {
101 errno = EINVAL;
102 return (-1);
103 }
104 *cidp = NULL;
105 *cnt = 0;
d9520f62 106
342c141e 107 bzero(&cidr, sizeof (cidr));
d9520f62 108
342c141e
A
109 cidr.scr_aid = aid;
110 err = ioctl(s, SIOCGCONNIDS, &cidr);
111 if (err != 0)
112 return (-1);
d9520f62 113
342c141e
A
114 /* none, just return */
115 if (cidr.scr_cnt == 0)
116 return (0);
d9520f62 117
89c4ed63 118 buf = calloc(cidr.scr_cnt, sizeof (sae_connid_t));
342c141e
A
119 if (buf == NULL)
120 return (-1);
d9520f62 121
342c141e
A
122 cidr.scr_cidp = buf;
123 err = ioctl(s, SIOCGCONNIDS, &cidr);
124 if (err != 0) {
125 free(buf);
126 return (-1);
127 }
d9520f62 128
342c141e
A
129 *cidp = buf;
130 *cnt = cidr.scr_cnt;
d9520f62 131
342c141e
A
132 return (0);
133}
134
135void
89c4ed63 136freeconnids(sae_connid_t *cidp)
342c141e
A
137{
138 free(cidp);
139}
140
141int
89c4ed63 142copyconninfo(int s, sae_connid_t cid, conninfo_t **cfop)
342c141e
A
143{
144 struct sockaddr *src = NULL, *dst = NULL, *aux = NULL;
145 struct so_cinforeq scir;
146 conninfo_t *buf = NULL;
d9520f62 147
342c141e
A
148 if (cfop == NULL) {
149 errno = EINVAL;
150 goto error;
151 }
152 *cfop = NULL;
d9520f62 153
342c141e 154 bzero(&scir, sizeof (scir));
d9520f62 155
342c141e
A
156 scir.scir_cid = cid;
157 if (ioctl(s, SIOCGCONNINFO, &scir) != 0)
158 goto error;
d9520f62 159
342c141e
A
160 if (scir.scir_src_len != 0) {
161 src = calloc(1, scir.scir_src_len);
162 if (src == NULL)
163 goto error;
164 scir.scir_src = src;
165 }
166 if (scir.scir_dst_len != 0) {
167 dst = calloc(1, scir.scir_dst_len);
168 if (dst == NULL)
169 goto error;
170 scir.scir_dst = dst;
171 }
172 if (scir.scir_aux_len != 0) {
173 aux = calloc(1, scir.scir_aux_len);
174 if (aux == NULL)
175 goto error;
176 scir.scir_aux_data = aux;
177 }
d9520f62 178
342c141e
A
179 if (ioctl(s, SIOCGCONNINFO, &scir) != 0)
180 goto error;
d9520f62 181
342c141e
A
182 buf = calloc(1, sizeof (*buf));
183 if (buf == NULL)
184 goto error;
d9520f62 185
342c141e
A
186 // When we query for the length using the first ioctl call above, the kernel
187 // tells us the length of the aux structure so we know how much to allocate
188 // memory. There may not be any aux data, which will be indicated by the aux
189 // data length using the second ioctl call.
190 if (scir.scir_aux_len == 0 && aux != NULL) {
191 free(aux);
192 aux = NULL;
193 scir.scir_aux_data = NULL;
194 }
d9520f62 195
342c141e
A
196 buf->ci_flags = scir.scir_flags;
197 buf->ci_ifindex = scir.scir_ifindex;
198 buf->ci_src = src;
199 buf->ci_dst = dst;
200 buf->ci_error = scir.scir_error;
201 buf->ci_aux_type = scir.scir_aux_type;
202 buf->ci_aux_data = aux;
89c4ed63 203 *cfop = (conninfo_t*)buf;
d9520f62 204
342c141e 205 return (0);
d9520f62 206
342c141e
A
207error:
208 if (src != NULL)
209 free(src);
210 if (dst != NULL)
211 free(dst);
212 if (aux != NULL)
213 free(aux);
214 if (buf != NULL)
215 free(buf);
d9520f62 216
342c141e
A
217 return (-1);
218}
219
220void
221freeconninfo(conninfo_t *cfo)
222{
223 if (cfo->ci_src != NULL)
224 free(cfo->ci_src);
d9520f62 225
342c141e
A
226 if (cfo->ci_dst != NULL)
227 free(cfo->ci_dst);
d9520f62 228
342c141e
A
229 if (cfo->ci_aux_data != NULL)
230 free(cfo->ci_aux_data);
d9520f62 231
342c141e
A
232 free(cfo);
233}