2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software donated to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)pt_tcp.c 8.5 (Berkeley) 4/28/95
35 * $FreeBSD: src/sbin/mount_portal/pt_tcp.c,v 1.9 1999/08/28 00:13:38 peter Exp $
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/syslog.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
53 * Key will be tcp/host/port[/"priv"]
54 * Create a TCP socket connected to the
55 * requested host and port.
56 * Some trailing suffix values have special meanings.
57 * An unrecognized suffix is an error.
60 portal_tcp(struct portal_cred
*pcr
, char *key
, char **v
, int kso __unused
,
63 char host
[MAXHOSTNAMELEN
];
64 char port
[MAXHOSTNAMELEN
];
65 char *p
= key
+ (v
[1] ? strlen(v
[1]) : 0);
70 struct in_addr
*ip
[2];
74 struct sockaddr_in sain
;
77 if (q
== NULL
|| (size_t)(q
- p
) >= sizeof(host
))
86 if (strlen(p
) >= sizeof(port
))
91 if (strcmp(p
, "priv") == 0) {
92 if (pcr
->pcr_uid
== 0)
101 hp
= gethostbyname(host
);
103 ipp
= (struct in_addr
**) hp
->h_addr_list
;
105 ina
.s_addr
= inet_addr(host
);
106 if (ina
.s_addr
== INADDR_NONE
)
113 printf ("inet address for %s is %s\n", host
, inet_ntoa(*ipp
[0]));
116 sp
= getservbyname(port
, "tcp");
118 s_port
= (u_short
)sp
->s_port
;
120 s_port
= strtoul(port
, &p
, 0);
121 if (s_port
== 0 || *p
!= '\0')
123 s_port
= htons(s_port
);
126 printf ("port number for %s is %d\n", port
, (int)ntohs(s_port
));
129 memset(&sain
, 0, sizeof(sain
));
130 sain
.sin_len
= sizeof(sain
);
131 sain
.sin_family
= AF_INET
;
132 sain
.sin_port
= s_port
;
138 so
= rresvport(NULL
);
140 so
= socket(AF_INET
, SOCK_STREAM
, 0);
142 syslog(LOG_ERR
, "socket: %m");
146 sain
.sin_addr
= *ipp
[0];
147 if (connect(so
, (struct sockaddr
*) &sain
, sizeof(sain
)) == 0) {