Home | History | Annotate | Line # | Download | only in mount_portal
pt_tcp.c revision 1.2
      1 /*
      2  * Copyright (c) 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software donated to Berkeley by
      7  * Jan-Simon Pendry.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     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. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	from: Id: pt_tcp.c,v 1.1 1992/05/25 21:43:09 jsp Exp jsp
     38  *	from: @(#)pt_tcp.c	8.1 (Berkeley) 6/5/93
     39  *	$Id: pt_tcp.c,v 1.2 1994/01/13 17:43:39 mycroft Exp $
     40  */
     41 
     42 #include <stdio.h>
     43 #include <unistd.h>
     44 #include <stdlib.h>
     45 #include <errno.h>
     46 #include <strings.h>
     47 #include <sys/types.h>
     48 #include <sys/param.h>
     49 #include <sys/syslog.h>
     50 #include <sys/socket.h>
     51 #include <netinet/in.h>
     52 #include <netdb.h>
     53 
     54 #include "portald.h"
     55 
     56 /*
     57  * Key will be tcp/host/port[/"priv"]
     58  * Create a TCP socket connected to the
     59  * requested host and port.
     60  * Some trailing suffix values have special meanings.
     61  * An unrecognised suffix is an error.
     62  */
     63 int portal_tcp(pcr, key, v, kso, fdp)
     64 struct portal_cred *pcr;
     65 char *key;
     66 char **v;
     67 int kso;
     68 int *fdp;
     69 {
     70 	char host[MAXHOSTNAMELEN];
     71 	char port[MAXHOSTNAMELEN];
     72 	char *p = key + (v[1] ? strlen(v[1]) : 0);
     73 	char *q;
     74 	struct hostent *hp;
     75 	struct servent *sp;
     76 	struct in_addr **ipp;
     77 	struct in_addr *ip[2];
     78 	struct in_addr ina;
     79 	int s_port;
     80 	struct sockaddr_in sain;
     81 
     82 	q = strchr(p, '/');
     83 	if (q == 0 || q - p >= sizeof(host))
     84 		return (EINVAL);
     85 	*q = '\0';
     86 	strcpy(host, p);
     87 	p = ++q;
     88 
     89 	q = strchr(p, '/');
     90 	if (q != 0 || strlen(p) >= sizeof(port))
     91 		return (EINVAL);
     92 	strcpy(port, p);
     93 
     94 	hp = gethostbyname(host);
     95 	if (hp != 0) {
     96 		ipp = (struct in_addr **) hp->h_addr_list;
     97 	} else {
     98 		ina.s_addr = inet_addr(host);
     99 		if (ina.s_addr == INADDR_NONE)
    100 			return (EINVAL);
    101 		ip[0] = &ina;
    102 		ip[1] = 0;
    103 		ipp = ip;
    104 	}
    105 
    106 	sp = getservbyname(port, "tcp");
    107 	if (sp != 0)
    108 		s_port = sp->s_port;
    109 	else {
    110 		s_port = atoi(port);
    111 		if (s_port == 0)
    112 			return (EINVAL);
    113 	}
    114 
    115 	bzero(&sain, sizeof(sain));
    116 	sain.sin_len = sizeof(sain);
    117 	sain.sin_family = AF_INET;
    118 	sain.sin_port = s_port;
    119 
    120 	while (ipp[0]) {
    121 		int so;
    122 
    123 		so = socket(AF_INET, SOCK_STREAM, 0);
    124 		if (so < 0) {
    125 			syslog(LOG_ERR, "socket: %m");
    126 			return (errno);
    127 		}
    128 
    129 		sain.sin_addr = *ipp[0];
    130 		if (connect(so, (struct sockaddr *) &sain, sizeof(sain)) == 0) {
    131 			*fdp = so;
    132 			return (0);
    133 		}
    134 		(void) close(so);
    135 
    136 		ipp++;
    137 	}
    138 
    139 	return (errno);
    140 }
    141