LCOV - code coverage report
Current view: top level - openssh-6.6p1/openbsd-compat - bindresvport.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 0 30 0.0 %
Date: 2014-08-01 Functions: 0 1 0.0 %
Branches: 0 24 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* This file has be substantially modified from the original OpenBSD source */
       2                 :            : 
       3                 :            : /*      $OpenBSD: bindresvport.c,v 1.17 2005/12/21 01:40:22 millert Exp $       */
       4                 :            : 
       5                 :            : /*
       6                 :            :  * Copyright 1996, Jason Downs.  All rights reserved.
       7                 :            :  * Copyright 1998, Theo de Raadt.  All rights reserved.
       8                 :            :  * Copyright 2000, Damien Miller.  All rights reserved.
       9                 :            :  *
      10                 :            :  * Redistribution and use in source and binary forms, with or without
      11                 :            :  * modification, are permitted provided that the following conditions
      12                 :            :  * are met:
      13                 :            :  * 1. Redistributions of source code must retain the above copyright
      14                 :            :  *    notice, this list of conditions and the following disclaimer.
      15                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      16                 :            :  *    notice, this list of conditions and the following disclaimer in the
      17                 :            :  *    documentation and/or other materials provided with the distribution.
      18                 :            :  *
      19                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      20                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      21                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      22                 :            :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
      23                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      24                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      25                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      26                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      27                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      28                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 :            :  */
      30                 :            : 
      31                 :            : /* OPENBSD ORIGINAL: lib/libc/rpc/bindresvport.c */
      32                 :            : 
      33                 :            : #include "includes.h"
      34                 :            : 
      35                 :            : #ifndef HAVE_BINDRESVPORT_SA
      36                 :            : #include <sys/types.h>
      37                 :            : #include <sys/socket.h>
      38                 :            : 
      39                 :            : #include <netinet/in.h>
      40                 :            : #include <arpa/inet.h>
      41                 :            : 
      42                 :            : #include <errno.h>
      43                 :            : #include <string.h>
      44                 :            : 
      45                 :            : #define STARTPORT 600
      46                 :            : #define ENDPORT (IPPORT_RESERVED - 1)
      47                 :            : #define NPORTS  (ENDPORT - STARTPORT + 1)
      48                 :            : 
      49                 :            : /*
      50                 :            :  * Bind a socket to a privileged IP port
      51                 :            :  */
      52                 :            : int
      53                 :          0 : bindresvport_sa(int sd, struct sockaddr *sa)
      54                 :            : {
      55                 :            :         int error, af;
      56                 :            :         struct sockaddr_storage myaddr;
      57                 :            :         struct sockaddr_in *in;
      58                 :            :         struct sockaddr_in6 *in6;
      59                 :            :         u_int16_t *portp;
      60                 :            :         u_int16_t port;
      61                 :            :         socklen_t salen;
      62                 :            :         int i;
      63                 :            : 
      64         [ #  # ]:          0 :         if (sa == NULL) {
      65                 :            :                 memset(&myaddr, 0, sizeof(myaddr));
      66                 :          0 :                 sa = (struct sockaddr *)&myaddr;
      67                 :            : 
      68         [ #  # ]:          0 :                 if (getsockname(sd, sa, &salen) == -1)
      69                 :            :                         return -1;      /* errno is correctly set */
      70                 :            : 
      71                 :          0 :                 af = sa->sa_family;
      72                 :          0 :                 memset(&myaddr, 0, salen);
      73                 :            :         } else
      74                 :          0 :                 af = sa->sa_family;
      75                 :            : 
      76         [ #  # ]:          0 :         if (af == AF_INET) {
      77                 :          0 :                 in = (struct sockaddr_in *)sa;
      78                 :          0 :                 salen = sizeof(struct sockaddr_in);
      79                 :          0 :                 portp = &in->sin_port;
      80         [ #  # ]:          0 :         } else if (af == AF_INET6) {
      81                 :          0 :                 in6 = (struct sockaddr_in6 *)sa;
      82                 :          0 :                 salen = sizeof(struct sockaddr_in6);
      83                 :          0 :                 portp = &in6->sin6_port;
      84                 :            :         } else {
      85                 :          0 :                 errno = EPFNOSUPPORT;
      86                 :          0 :                 return (-1);
      87                 :            :         }
      88                 :          0 :         sa->sa_family = af;
      89                 :            : 
      90         [ #  # ]:          0 :         port = ntohs(*portp);
      91         [ #  # ]:          0 :         if (port == 0)
      92                 :          0 :                 port = arc4random_uniform(NPORTS) + STARTPORT;
      93                 :            : 
      94                 :            :         /* Avoid warning */
      95                 :            :         error = -1;
      96                 :            : 
      97         [ #  # ]:          0 :         for(i = 0; i < NPORTS; i++) {
      98         [ #  # ]:          0 :                 *portp = htons(port);
      99                 :            :                 
     100                 :          0 :                 error = bind(sd, sa, salen);
     101                 :            : 
     102                 :            :                 /* Terminate on success */
     103         [ #  # ]:          0 :                 if (error == 0)
     104                 :            :                         break;
     105                 :            :                         
     106                 :            :                 /* Terminate on errors, except "address already in use" */
     107 [ #  # ][ #  # ]:          0 :                 if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
     108                 :            :                         break;
     109                 :            :                         
     110                 :          0 :                 port++;
     111         [ #  # ]:          0 :                 if (port > ENDPORT)
     112                 :          0 :                         port = STARTPORT;
     113                 :            :         }
     114                 :            : 
     115                 :          0 :         return (error);
     116                 :            : }
     117                 :            : 
     118                 :            : #endif /* HAVE_BINDRESVPORT_SA */

Generated by: LCOV version 1.9