[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.2

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

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