xref: /aosp_15_r20/external/ltp/utils/sctp/lib/addrs.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* SCTP kernel Implementation: User API extensions.
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  * addrs.c
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Distributed under the terms of the LGPL v2.1 as described in
6*49cdfc7eSAndroid Build Coastguard Worker  *    http://www.gnu.org/copyleft/lesser.txt
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This file is part of the user library that offers support for the
9*49cdfc7eSAndroid Build Coastguard Worker  * SCTP kernel Implementation. The main purpose of this
10*49cdfc7eSAndroid Build Coastguard Worker  * code is to provide the SCTP Socket API mappings for user
11*49cdfc7eSAndroid Build Coastguard Worker  * application to interface with the SCTP in kernel.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * This implementation is based on the Socket API Extensions for SCTP
14*49cdfc7eSAndroid Build Coastguard Worker  * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * (C) Copyright IBM Corp. 2003
17*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2001-2002 Intel Corp.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * Written or modified by:
20*49cdfc7eSAndroid Build Coastguard Worker  *  Ardelle Fan     <[email protected]>
21*49cdfc7eSAndroid Build Coastguard Worker  *  Sridhar Samudrala <[email protected]>
22*49cdfc7eSAndroid Build Coastguard Worker  *  Ivan Skytte Jørgensen <[email protected]>
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include <malloc.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/sctp.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker /*
32*49cdfc7eSAndroid Build Coastguard Worker  * Common getsockopt() layer
33*49cdfc7eSAndroid Build Coastguard Worker  * If the NEW getsockopt() API fails this function will fall back to using
34*49cdfc7eSAndroid Build Coastguard Worker  * the old API
35*49cdfc7eSAndroid Build Coastguard Worker  */
36*49cdfc7eSAndroid Build Coastguard Worker static int
sctp_getaddrs(int sd,sctp_assoc_t id,int optname_new,struct sockaddr ** addrs)37*49cdfc7eSAndroid Build Coastguard Worker sctp_getaddrs(int sd, sctp_assoc_t id, int optname_new,
38*49cdfc7eSAndroid Build Coastguard Worker 	      struct sockaddr **addrs)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	int cnt, err;
41*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t len;
42*49cdfc7eSAndroid Build Coastguard Worker 	size_t bufsize = 4096; /*enough for most cases*/
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	struct sctp_getaddrs *getaddrs = (struct sctp_getaddrs*)malloc(bufsize);
45*49cdfc7eSAndroid Build Coastguard Worker 	if(!getaddrs)
46*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	for(;;) {
49*49cdfc7eSAndroid Build Coastguard Worker 		char *new_buf;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 		len = bufsize;
52*49cdfc7eSAndroid Build Coastguard Worker 		getaddrs->assoc_id = id;
53*49cdfc7eSAndroid Build Coastguard Worker 		err = getsockopt(sd, SOL_SCTP, optname_new, getaddrs, &len);
54*49cdfc7eSAndroid Build Coastguard Worker 		if (err == 0) {
55*49cdfc7eSAndroid Build Coastguard Worker 			/*got it*/
56*49cdfc7eSAndroid Build Coastguard Worker 			break;
57*49cdfc7eSAndroid Build Coastguard Worker 		}
58*49cdfc7eSAndroid Build Coastguard Worker 		if (errno != ENOMEM ) {
59*49cdfc7eSAndroid Build Coastguard Worker 			/*unknown error*/
60*49cdfc7eSAndroid Build Coastguard Worker 			free(getaddrs);
61*49cdfc7eSAndroid Build Coastguard Worker 			return -1;
62*49cdfc7eSAndroid Build Coastguard Worker 		}
63*49cdfc7eSAndroid Build Coastguard Worker 		/*expand buffer*/
64*49cdfc7eSAndroid Build Coastguard Worker 		if (bufsize > 128*1024) {
65*49cdfc7eSAndroid Build Coastguard Worker 			/*this is getting ridiculous*/
66*49cdfc7eSAndroid Build Coastguard Worker 			free(getaddrs);
67*49cdfc7eSAndroid Build Coastguard Worker 			errno = ENOBUFS;
68*49cdfc7eSAndroid Build Coastguard Worker 			return -1;
69*49cdfc7eSAndroid Build Coastguard Worker 		}
70*49cdfc7eSAndroid Build Coastguard Worker 		new_buf = realloc(getaddrs, bufsize+4096);
71*49cdfc7eSAndroid Build Coastguard Worker 		if (!new_buf) {
72*49cdfc7eSAndroid Build Coastguard Worker 			free(getaddrs);
73*49cdfc7eSAndroid Build Coastguard Worker 			return -1;
74*49cdfc7eSAndroid Build Coastguard Worker 		}
75*49cdfc7eSAndroid Build Coastguard Worker 		bufsize += 4096;
76*49cdfc7eSAndroid Build Coastguard Worker 		getaddrs = (struct sctp_getaddrs*)new_buf;
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	/* we skip traversing the list, allocating a new buffer etc. and enjoy
80*49cdfc7eSAndroid Build Coastguard Worker 	 * a simple hack*/
81*49cdfc7eSAndroid Build Coastguard Worker 	cnt = getaddrs->addr_num;
82*49cdfc7eSAndroid Build Coastguard Worker 	memmove(getaddrs, getaddrs + 1, len);
83*49cdfc7eSAndroid Build Coastguard Worker 	*addrs = (struct sockaddr*)getaddrs;
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	return cnt;
86*49cdfc7eSAndroid Build Coastguard Worker } /* sctp_getaddrs() */
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker /* Get all peer address on a socket.  This is a new SCTP API
89*49cdfc7eSAndroid Build Coastguard Worker  * described in the section 8.3 of the Sockets API Extensions for SCTP.
90*49cdfc7eSAndroid Build Coastguard Worker  * This is implemented using the getsockopt() interface.
91*49cdfc7eSAndroid Build Coastguard Worker  */
92*49cdfc7eSAndroid Build Coastguard Worker int
sctp_getpaddrs(int sd,sctp_assoc_t id,struct sockaddr ** addrs)93*49cdfc7eSAndroid Build Coastguard Worker sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **addrs)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker 	return sctp_getaddrs(sd, id,
96*49cdfc7eSAndroid Build Coastguard Worker 			     SCTP_GET_PEER_ADDRS,
97*49cdfc7eSAndroid Build Coastguard Worker 			     addrs);
98*49cdfc7eSAndroid Build Coastguard Worker } /* sctp_getpaddrs() */
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker /* Frees all resources allocated by sctp_getpaddrs().  This is a new SCTP API
101*49cdfc7eSAndroid Build Coastguard Worker  * described in the section 8.4 of the Sockets API Extensions for SCTP.
102*49cdfc7eSAndroid Build Coastguard Worker  */
103*49cdfc7eSAndroid Build Coastguard Worker int
sctp_freepaddrs(struct sockaddr * addrs)104*49cdfc7eSAndroid Build Coastguard Worker sctp_freepaddrs(struct sockaddr *addrs)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker 	free(addrs);
107*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker } /* sctp_freepaddrs() */
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker /* Get all locally bound address on a socket.  This is a new SCTP API
112*49cdfc7eSAndroid Build Coastguard Worker  * described in the section 8.5 of the Sockets API Extensions for SCTP.
113*49cdfc7eSAndroid Build Coastguard Worker  * This is implemented using the getsockopt() interface.
114*49cdfc7eSAndroid Build Coastguard Worker  */
115*49cdfc7eSAndroid Build Coastguard Worker int
sctp_getladdrs(int sd,sctp_assoc_t id,struct sockaddr ** addrs)116*49cdfc7eSAndroid Build Coastguard Worker sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **addrs)
117*49cdfc7eSAndroid Build Coastguard Worker {
118*49cdfc7eSAndroid Build Coastguard Worker 	return sctp_getaddrs(sd, id,
119*49cdfc7eSAndroid Build Coastguard Worker 			     SCTP_GET_LOCAL_ADDRS,
120*49cdfc7eSAndroid Build Coastguard Worker 			     addrs);
121*49cdfc7eSAndroid Build Coastguard Worker } /* sctp_getladdrs() */
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker /* Frees all resources allocated by sctp_getladdrs().  This is a new SCTP API
124*49cdfc7eSAndroid Build Coastguard Worker  * described in the section 8.6 of the Sockets API Extensions for SCTP.
125*49cdfc7eSAndroid Build Coastguard Worker  */
126*49cdfc7eSAndroid Build Coastguard Worker int
sctp_freeladdrs(struct sockaddr * addrs)127*49cdfc7eSAndroid Build Coastguard Worker sctp_freeladdrs(struct sockaddr *addrs)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker 	free(addrs);
130*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker } /* sctp_freeladdrs() */
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker int
sctp_getaddrlen(sa_family_t family)135*49cdfc7eSAndroid Build Coastguard Worker sctp_getaddrlen(sa_family_t family)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker 	/* We could call into the kernel to see what it thinks the size should
138*49cdfc7eSAndroid Build Coastguard Worker 	 * be, but hardcoding the address families here is: (a) faster,
139*49cdfc7eSAndroid Build Coastguard Worker 	 * (b) easier, and (c) probably good enough for forseeable future.
140*49cdfc7eSAndroid Build Coastguard Worker 	 */
141*49cdfc7eSAndroid Build Coastguard Worker 	switch(family) {
142*49cdfc7eSAndroid Build Coastguard Worker 	case AF_INET:
143*49cdfc7eSAndroid Build Coastguard Worker 		return sizeof(struct sockaddr_in);
144*49cdfc7eSAndroid Build Coastguard Worker 	case AF_INET6:
145*49cdfc7eSAndroid Build Coastguard Worker 		return sizeof(struct sockaddr_in6);
146*49cdfc7eSAndroid Build Coastguard Worker 	default:
147*49cdfc7eSAndroid Build Coastguard Worker 		/* Currently there is no defined error handling in
148*49cdfc7eSAndroid Build Coastguard Worker 		 * draft-ietf-tsvwg-sctpsocket-13.txt.
149*49cdfc7eSAndroid Build Coastguard Worker 		 * -1 might cause the application to overwrite buffer
150*49cdfc7eSAndroid Build Coastguard Worker 		 * or misinterpret data. 0 is more likely to cause
151*49cdfc7eSAndroid Build Coastguard Worker 		 * an endless loop.
152*49cdfc7eSAndroid Build Coastguard Worker 		 */
153*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
154*49cdfc7eSAndroid Build Coastguard Worker 	}
155*49cdfc7eSAndroid Build Coastguard Worker }
156