OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [librpc/] [src/] [rpc/] [bindresvport.c] - Blame information for rev 173

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3
 * unrestricted use provided that this legend is included on all tape
4
 * media and as a part of the software program in whole or part.  Users
5
 * may copy or modify Sun RPC without charge, but are not authorized
6
 * to license or distribute it to anyone else except as part of a product or
7
 * program developed by the user.
8
 *
9
 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12
 *
13
 * Sun RPC is provided with no support and without any obligation on the
14
 * part of Sun Microsystems, Inc. to assist in its use, correction,
15
 * modification or enhancement.
16
 *
17
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19
 * OR ANY PART THEREOF.
20
 *
21
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22
 * or profits or other special, indirect and consequential damages, even if
23
 * Sun has been advised of the possibility of such damages.
24
 *
25
 * Sun Microsystems, Inc.
26
 * 2550 Garcia Avenue
27
 * Mountain View, California  94043
28
 */
29
 
30
#if defined(LIBC_SCCS) && !defined(lint)
31
/*static char *sccsid = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";*/
32
/*static char *sccsid = "from: @(#)bindresvport.c       2.2 88/07/29 4.0 RPCSRC";*/
33
/*from: OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp */
34
static char *rcsid = "$FreeBSD: src/lib/libc/rpc/bindresvport.c,v 1.12 2000/01/26 09:02:42 shin Exp $";
35
#endif
36
 
37
/*
38
 * Copyright (c) 1987 by Sun Microsystems, Inc.
39
 *
40
 * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
41
 */
42
 
43
#include <sys/types.h>
44
#include <sys/errno.h>
45
#include <sys/socket.h>
46
#include <netinet/in.h>
47
#include <unistd.h>
48
#include <string.h>
49
 
50
/*
51
 * Bind a socket to a privileged port for whatever protocol.
52
 */
53
int
54
bindresvport_sa(sd, sa)
55
        int sd;
56
        struct sockaddr *sa;
57
{
58
        int old, error, af;
59
        struct sockaddr myaddr;
60
        struct sockaddr_in *sin;
61
#if (defined(AF_INET6) && defined(IPPROTO_IPV6))
62
        struct sockaddr_in6 *sin6;
63
#endif
64
        int proto, portrange, portlow;
65
        u_int16_t port;
66
        int salen;
67
 
68
        if (sa == NULL) {
69
                salen = sizeof(myaddr);
70
                sa = (struct sockaddr *)&myaddr;
71
 
72
                if (getsockname(sd, sa, &salen) == -1)
73
                        return -1;      /* errno is correctly set */
74
 
75
                af = sa->sa_family;
76
                memset(&myaddr, 0, salen);
77
        } else
78
                af = sa->sa_family;
79
 
80
        if (af == AF_INET) {
81
                proto = IPPROTO_IP;
82
                portrange = IP_PORTRANGE;
83
                portlow = IP_PORTRANGE_LOW;
84
                sin = (struct sockaddr_in *)sa;
85
                salen = sizeof(struct sockaddr_in);
86
                port = sin->sin_port;
87
#if (defined(AF_INET6) && defined(IPPROTO_IPV6))
88
        } else if (af == AF_INET6) {
89
                proto = IPPROTO_IPV6;
90
                portrange = IPV6_PORTRANGE;
91
                portlow = IPV6_PORTRANGE_LOW;
92
                sin6 = (struct sockaddr_in6 *)sa;
93
                salen = sizeof(struct sockaddr_in6);
94
                port = sin6->sin6_port;
95
#endif
96
        } else {
97
                errno = EPFNOSUPPORT;
98
                return (-1);
99
        }
100
        sa->sa_family = af;
101
        sa->sa_len = salen;
102
 
103
        if (port == 0) {
104
                int oldlen = sizeof(old);
105
 
106
                error = getsockopt(sd, proto, portrange, &old, &oldlen);
107
                if (error < 0)
108
                        return (error);
109
 
110
                error = setsockopt(sd, proto, portrange, &portlow,
111
                    sizeof(portlow));
112
                if (error < 0)
113
                        return (error);
114
        }
115
 
116
        error = bind(sd, sa, salen);
117
 
118
        if (port == 0) {
119
                int saved_errno = errno;
120
 
121
                if (error) {
122
                        if (setsockopt(sd, proto, portrange, &old,
123
                            sizeof(old)) < 0)
124
                                errno = saved_errno;
125
                        return (error);
126
                }
127
 
128
                if (sa != (struct sockaddr *)&myaddr) {
129
                        /* Hmm, what did the kernel assign... */
130
                        if (getsockname(sd, sa, &salen) < 0)
131
                                errno = saved_errno;
132
                        return (error);
133
                }
134
        }
135
        return (error);
136
}
137
 
138
/*
139
 * Bind a socket to a privileged IP port
140
 */
141
int
142
bindresvport(sd, sin)
143
        int sd;
144
        struct sockaddr_in *sin;
145
{
146
        return bindresvport_sa(sd, (struct sockaddr *)sin);
147
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.