OpenCoverage

bindresvport.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/openbsd-compat/bindresvport.c
Source codeSwitch to Preprocessed file
LineSourceCount
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 */-
52int-
53bindresvport_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 if (sa == NULL) {
sa == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
65 memset(&myaddr, 0, sizeof(myaddr));-
66 sa = (struct sockaddr *)&myaddr;-
67 salen = sizeof(myaddr);-
68-
69 if (getsockname(sd, sa, &salen) == -1)
getsockname(sd... &salen) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
70 return -1; /* errno is correctly set */
never executed: return -1;
0
71-
72 af = sa->sa_family;-
73 memset(&myaddr, 0, salen);-
74 } else
never executed: end of block
0
75 af = sa->sa_family;
never executed: af = sa->sa_family;
0
76-
77 if (af == AF_INET) {
af == 2Description
TRUEnever evaluated
FALSEnever evaluated
0
78 in = (struct sockaddr_in *)sa;-
79 salen = sizeof(struct sockaddr_in);-
80 portp = &in->sin_port;-
81 } else if (af == AF_INET6) {
never executed: end of block
af == 10Description
TRUEnever evaluated
FALSEnever evaluated
0
82 in6 = (struct sockaddr_in6 *)sa;-
83 salen = sizeof(struct sockaddr_in6);-
84 portp = &in6->sin6_port;-
85 } else {
never executed: end of block
0
86 errno = EPFNOSUPPORT;-
87 return (-1);
never executed: return (-1);
0
88 }-
89 sa->sa_family = af;-
90-
91 port = ntohs(*portp);
never executed: __v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8)));
never executed: __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) : "cc");
__builtin_constant_p (__x)Description
TRUEnever evaluated
FALSEnever evaluated
0
92 if (port == 0)
port == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
93 port = arc4random_uniform(NPORTS) + STARTPORT;
never executed: port = arc4random_uniform((( 1024 - 1) - 600 + 1)) + 600;
0
94-
95 /* Avoid warning */-
96 error = -1;-
97-
98 for(i = 0; i < NPORTS; i++) {
i < (( 1024 - 1) - 600 + 1)Description
TRUEnever evaluated
FALSEnever evaluated
0
99 *portp = htons(port);
never executed: __v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8)));
never executed: __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) : "cc");
__builtin_constant_p (__x)Description
TRUEnever evaluated
FALSEnever evaluated
0
100 -
101 error = bind(sd, sa, salen);-
102-
103 /* Terminate on success */-
104 if (error == 0)
error == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
105 break;
never executed: break;
0
106 -
107 /* Terminate on errors, except "address already in use" */-
108 if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
(error < 0)Description
TRUEnever evaluated
FALSEnever evaluated
( (*__errno_lo...on ()) == 98 )Description
TRUEnever evaluated
FALSEnever evaluated
( (*__errno_lo...on ()) == 22 )Description
TRUEnever evaluated
FALSEnever evaluated
0
109 break;
never executed: break;
0
110 -
111 port++;-
112 if (port > ENDPORT)
port > ( 1024 - 1)Description
TRUEnever evaluated
FALSEnever evaluated
0
113 port = STARTPORT;
never executed: port = 600;
0
114 }
never executed: end of block
0
115-
116 return (error);
never executed: return (error);
0
117}-
118-
119#endif /* HAVE_BINDRESVPORT_SA */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2