Annotation of OpenXM/src/ox_socket/ox_getport.c, Revision 1.8
1.1 maekawa 1: /*-
2: * Copyright (c) 2000 MAEKAWA Masahide <maekawa@math.sci.kobe-u.ac.jp>
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24: * SUCH DAMAGE.
25: *
1.8 ! maekawa 26: * $OpenXM: OpenXM/src/ox_socket/ox_getport.c,v 1.7 2000/12/01 17:37:53 maekawa Exp $
1.1 maekawa 27: */
28:
29: #include <sys/types.h>
30: #include <sys/socket.h>
31:
32: #include <netinet/in.h>
33:
34: #include <stdio.h>
35: #include <unistd.h>
36: #include <err.h>
37: #include <netdb.h>
38:
39: #include "ox_socket.h"
40:
1.8 ! maekawa 41: #ifndef HAVE_SOCKADDR_STORAGE
1.1 maekawa 42: /* Derived from RFC2553 */
43: #define _SS_MAXSIZE 128
44: #define _SS_ALIGNSIZE (sizeof(int64_t))
1.3 maekawa 45:
46: #ifdef HAVE_SOCKADDR_LEN
47: #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(uint8_t) - sizeof(sa_family_t))
1.7 maekawa 48: #define _SS_PAD2SIZE (_SS_ALIGNSIZE - sizeof(uint8_t) - sizeof(sa_family_t) \
49: - _SS_PAD1SIZE - _SS_ALIGNSIZE)
1.3 maekawa 50: #else /* HAVE_SOCKADDR_LEN */
1.1 maekawa 51: #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
1.5 maekawa 52: #define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(sa_family_t) \
1.6 maekawa 53: - _SS_PAD1SIZE - _SS_ALIGNSIZE)
1.7 maekawa 54: #endif /* HAVE_SOCKADDR_LEN */
1.1 maekawa 55:
56: struct sockaddr_storage {
1.3 maekawa 57: #ifdef HAVE_SOCKADDR_LEN
1.7 maekawa 58: uint8_t ss_len;
1.4 maekawa 59: #endif /* HAVE_SOCKADDR_LEN */
1.7 maekawa 60: sa_family_t ss_family;
1.1 maekawa 61:
62: char __ss_pad1[_SS_PAD1SIZE];
63: int64_t __ss_align;
64: char __ss_pad2[_SS_PAD2SIZE];
65: };
1.8 ! maekawa 66: #endif /* HAVE_SOCKADDR_STORAGE */
1.1 maekawa 67:
68: int
69: ox_getport(int sock)
70: {
71: struct sockaddr_storage ss;
72: int port;
73:
74: if (getsockname(sock, (struct sockaddr *)&ss, NULL) < 0) {
75: warn("ox_getport(): getsockname");
76: return (-1);
77: }
78:
1.7 maekawa 79: switch (ss.ss_family) {
1.1 maekawa 80: case AF_INET:
81: {
82: struct sockaddr_in *sin;
83:
84: sin = (struct sockaddr_in *)&ss;
85: port = ntohs(sin->sin_port);
86:
87: break;
88: }
89: #ifdef INET6
90: case AF_INET6:
91: {
92: struct sockaddr_in6 *sin6;
93:
94: sin6 = (struct sockaddr_in6 *)&ss;
95: port = ntohs(sin6->sin6_port);
96:
97: break;
98: }
99: #endif /* INET6 */
100: default:
101: /* not supported */
102: port = -1;
103: }
104:
105: return (port);
106: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>