File: saddr.c

package info (click to toggle)
i2util 1.6-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 748 kB
  • ctags: 473
  • sloc: ansic: 5,290; perl: 1,151; makefile: 74; sh: 17
file content (347 lines) | stat: -rw-r--r-- 7,655 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 *      $Id$
 */
/************************************************************************
*									*
*			     Copyright (C)  2002			*
*				Internet2				*
*			     All Rights Reserved			*
*									*
************************************************************************/
/*
 *	File:		saddr.c
 *
 *	Author:		Jeff W. Boote
 *			Internet2
 *
 *	Date:		Tue Nov 26 07:45:48 MST 2002
 *
 *	Description:	
 *
 *	Generic socket functions used here to abstract away addr family
 *	differences.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
#include <assert.h>
#include <I2util/saddr.h>

#include <string.h>

/*
 * These first functions are used to copy sockaddr structs in and out of
 * a union designed to make the rest of the code more ISO C99 compliant.
 * Specifically - type punning (aliasing) is much more limited than it
 * used to be to allow for more optimization. (Of course it specifically
 * de-optimizes my code since I now have to memcpy things instead of
 * modifying them in place. YUCK!)
 */

/*
 * Function:    I2SockAddrToSockUnion
 *
 * Description:    
 *
 * In Args:    
 *
 * Out Args:    
 *
 * Scope:    
 * Returns:    
 * Side Effect:    
 */
I2SockUnion *
I2SockAddrToSockUnion(
        const struct sockaddr   *sa,
        socklen_t               sa_len,
        I2SockUnion             *sau_mem
        )
{
    assert(sau_mem);
    assert(sa);

    memset(sau_mem,0,sizeof(I2SockUnion));

    switch(sa->sa_family){
#ifdef	AF_INET6
        case AF_INET6:
            if(sa_len < sizeof(struct sockaddr_in6))
                return NULL;

            memcpy(&sau_mem->sin6,sa,sa_len);

            break;
#endif
        case AF_INET:
            if(sa_len < sizeof(struct sockaddr_in))
                return NULL;

            memcpy(&sau_mem->sin,sa,sa_len);

            break;

        default:
            return NULL;
    }

    return sau_mem;
}

/*
 * Function:    I2SockUnionToSockAddr
 *
 * Description:    
 *
 * In Args:    
 *
 * Out Args:    
 *
 * Scope:    
 * Returns:    
 * Side Effect:    
 */
struct sockaddr *
I2SockUnionToSockAddr(
        const I2SockUnion   *sau,
        socklen_t           *sa_len_in_out,
        struct sockaddr     *sa_mem
        )
{
    assert(sau);
    assert(sa_mem);

    switch(sau->sa.sa_family){
#ifdef	AF_INET6
        case AF_INET6:
            if(*sa_len_in_out < sizeof(struct sockaddr_in6))
                return NULL;
            *sa_len_in_out = sizeof(struct sockaddr_in6);


            break;
#endif
        case AF_INET:
            if(*sa_len_in_out < sizeof(struct sockaddr_in))
                return NULL;
            *sa_len_in_out = sizeof(struct sockaddr_in);

            break;

        default:
            return NULL;
    }

    memcpy(sa_mem,&sau->sas,*sa_len_in_out);

    return sa_mem;

}

/*
 * Function:	I2SockAddrEqual
 *
 * Description:	
 *
 * In Args:	
 *
 * Out Args:	
 *
 * Scope:	
 * Returns:	
 * 	<0	: error/unsupported addr family
 * 	0	: false
 * 	>0	: true
 * Side Effect:	
 */
int
I2SockAddrEqual(
	const struct sockaddr	*sa1,
	socklen_t		sa1_len,
	const struct sockaddr	*sa2,
	socklen_t		sa2_len,
	uint32_t		chk_what
	)
{
    I2SockUnion sau1_mem, sau2_mem;
    I2SockUnion *sau1, *sau2;

	/*
	 * If the lengths are not equal - or the families are not the
	 * same - check if the v6 address is really an encoded v4 address.
	 * If it is - then re-call ourselves with the v4 version directly.
	 */
	if((sa1_len != sa2_len) || (sa1->sa_family != sa2->sa_family)){
#ifdef	AF_INET6
		struct sockaddr_in  v4rec;

		/*
		 * check if sa1 is a mapped addr.
		 */

                /* copy sa1 into the union */
                if( !(sau1 = I2SockAddrToSockUnion(sa1,sa1_len,&sau1_mem))){
                    return -1;
                }

		if((sau1->sa.sa_family==AF_INET6) &&
				IN6_IS_ADDR_V4MAPPED(&sau1->sin6.sin6_addr)){
			memset(&v4rec,0,sizeof(v4rec));
#ifdef	HAVE_STRUCT_SOCKADDR_SA_LEN
			v4rec.sin_len = sizeof(v4rec);
#endif
			v4rec.sin_family = AF_INET;
			memcpy(&v4rec.sin_addr.s_addr,
					&sau1->sin6.sin6_addr.s6_addr[12],4);
                        v4rec.sin_port = sau1->sin6.sin6_port;
			return I2SockAddrEqual((struct sockaddr*)&v4rec,
					sizeof(v4rec),
					sa2,sa2_len,chk_what);
		}

                /* copy sa1 into the union */
                if( !(sau2 = I2SockAddrToSockUnion(sa2,sa2_len,&sau2_mem))){
                    return -1;
                }

		if((sau2->sa.sa_family==AF_INET6) &&
				IN6_IS_ADDR_V4MAPPED(&sau2->sin6.sin6_addr)){
			memset(&v4rec,0,sizeof(v4rec));
#ifdef	HAVE_STRUCT_SOCKADDR_SA_LEN
			v4rec.sin_len = sizeof(v4rec);
#endif
			v4rec.sin_family = AF_INET;
			memcpy(&v4rec.sin_addr.s_addr,
					&sau2->sin6.sin6_addr.s6_addr[12],4);
                        v4rec.sin_port = sau2->sin6.sin6_port;
			return I2SockAddrEqual(sa1,sa1_len,
					(struct sockaddr*)&v4rec,
					sizeof(v4rec),chk_what);
		}
#endif
		return 0;
	}

        /* copy to union to enable punning */
        if( !(sau1 = I2SockAddrToSockUnion(sa1,sa1_len,&sau1_mem)) ||
                !(sau2 = I2SockAddrToSockUnion(sa2,sa2_len,&sau2_mem))){
            return -1;
        }

	switch(sau1->sa.sa_family){
#ifdef	AF_INET6
	case AF_INET6:
		if((chk_what & I2SADDR_ADDR) &&
			(memcmp(&sau1->sin6.sin6_addr,&sau2->sin6.sin6_addr,
				sizeof(struct in6_addr)) != 0)){
			return 0;
		}

		if((chk_what & I2SADDR_PORT) &&
				(sau1->sin6.sin6_port != sau2->sin6.sin6_port)){
			return 0;
		}

		/*
		 * TODO:Do I need to check scope? Won't for now...
		 */
		return 1;

		break;
#endif
	case AF_INET:
		if((chk_what & I2SADDR_ADDR) &&
				(memcmp(&sau1->sin.sin_addr,&sau2->sin.sin_addr,
				sizeof(struct in_addr)) != 0)){
			return 0;
		}

		if((chk_what & I2SADDR_PORT) &&
				(sau1->sin.sin_port != sau2->sin.sin_port)){
			return 0;
		}

		return 1;

		break;

	default:
		return -1;
	}

	return -1;
}

/*
 * Function:	I2SockAddrIsLoopback
 *
 * Description:	
 *
 * In Args:	
 *
 * Out Args:	
 *
 * Scope:	
 * Returns:	
 * 	<0	: error/unsupported addr family
 * 	0	: false
 * 	>0	: true
 * Side Effect:	
 */
int
I2SockAddrIsLoopback(
	const struct sockaddr	*sa,
	socklen_t		sa_len
	)
{
    I2SockUnion sau_mem;
    I2SockUnion *sau;

    if( !(sau = I2SockAddrToSockUnion(sa,sa_len,&sau_mem))){
        return -1;
    }


    switch(sau->sa.sa_family){
#ifdef	AF_INET6
        case AF_INET6:
            if (IN6_IS_ADDR_V4MAPPED(&sau->sin6.sin6_addr)){
                struct sockaddr_in  v4rec;

                memset(&v4rec,0,sizeof(v4rec));
#ifdef	HAVE_STRUCT_SOCKADDR_SA_LEN
                v4rec.sin_len = sizeof(v4rec);
#endif
                v4rec.sin_family = AF_INET;
                memcpy(&v4rec.sin_addr.s_addr,
                        &sau->sin6.sin6_addr.s6_addr[12],4);
                v4rec.sin_port = sau->sin6.sin6_port;

                return I2SockAddrIsLoopback(&sau->sa,sizeof(v4rec));
            }

            return IN6_IS_ADDR_LOOPBACK(&sau->sin6.sin6_addr);

            break;
#endif
        case AF_INET:
            return (sau->sin.sin_addr.s_addr == htonl(INADDR_LOOPBACK));

            break;

        default:
            return -1;
    }

    return 0;
}