[BACK]Return to ox_getport.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_socket

Annotation of OpenXM/src/ox_socket/ox_getport.c, Revision 1.3

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.3     ! maekawa    26:  *     $OpenXM: OpenXM/src/ox_socket/ox_getport.c,v 1.2 2000/12/01 08:09:41 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:
                     41: #ifndef INET6
                     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))
        !            48: #else /* HAVE_SOCKADDR_LEN */
1.1       maekawa    49: #define        _SS_PAD1SIZE    (_SS_ALIGNSIZE - sizeof(sa_family_t))
1.3     ! maekawa    50: #endif /* HAVE_SOCKADDR_LEN */
1.1       maekawa    51: #define        _SS_PAD2SIZE    (_SS_MAXSIZE - sizeof(sa_family_t)
                     52:                         + _SS_PAD1SIZE + _SS_ALIGNSIZE)
                     53:
                     54: struct sockaddr_storage {
1.3     ! maekawa    55: #ifdef HAVE_SOCKADDR_LEN
        !            56:        uint8_t         __ss_len;
        !            57: #endif /* HAVE_SOCKADDR_LEN
1.1       maekawa    58:        sa_family_t     __ss_family;
                     59:
                     60:        char            __ss_pad1[_SS_PAD1SIZE];
                     61:        int64_t         __ss_align;
                     62:        char            __ss_pad2[_SS_PAD2SIZE];
                     63: };
                     64: #endif /* INET6 */
                     65:
                     66: int
                     67: ox_getport(int sock)
                     68: {
                     69:        struct sockaddr_storage ss;
                     70:        int port;
                     71:
                     72:        if (getsockname(sock, (struct sockaddr *)&ss, NULL) < 0) {
                     73:                warn("ox_getport(): getsockname");
                     74:                return (-1);
                     75:        }
                     76:
                     77:        switch (ss.__ss_family) {
                     78:        case AF_INET:
                     79:        {
                     80:                struct sockaddr_in *sin;
                     81:
                     82:                sin = (struct sockaddr_in *)&ss;
                     83:                port = ntohs(sin->sin_port);
                     84:
                     85:                break;
                     86:        }
                     87: #ifdef INET6
                     88:        case AF_INET6:
                     89:        {
                     90:                struct sockaddr_in6 *sin6;
                     91:
                     92:                sin6 = (struct sockaddr_in6 *)&ss;
                     93:                port = ntohs(sin6->sin6_port);
                     94:
                     95:                break;
                     96:        }
                     97: #endif /* INET6 */
                     98:        default:
                     99:                /* not supported */
                    100:                port = -1;
                    101:        }
                    102:
                    103:        return (port);
                    104: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>