Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libncp / ncpl_net.c
1 /*
2  * Copyright (c) 1999, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/lib/libncp/ncpl_net.c,v 1.1 1999/10/12 11:56:40 bp Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/errno.h>
39 #include <sys/syscall.h>
40 #include <ctype.h>
41 #include <netinet/in.h>
42 #include <netipx/ipx.h>
43 #include <netdb.h>
44 #include <string.h>
45 #include <stdio.h>
46 #include <unistd.h>
47
48 #include "ipxsap.h"
49 #include <netncp/ncp_lib.h>
50 #include "ncp_mod.h"
51
52 static int ncp_find_server_in(struct ncp_conn_loginfo *li, int type, char *server_name);
53
54 static int
55 ncp_find_server_ipx(struct ncp_conn_loginfo *li, int type) {
56         char server[NCP_BINDERY_NAME_LEN + 1];
57         int error;
58         char nearest[NCP_BINDERY_NAME_LEN + 1];
59         struct nw_property prop;
60         struct ipx_addr *n_addr = (struct ipx_addr *) &prop;
61 /*      struct ncp_conn_loginfo ltmp;*/
62         int connid;
63
64         bzero(server, sizeof(server));
65         bzero(nearest, sizeof(nearest));
66
67         strcpy(server, li->server);
68         ncp_str_upper(server);
69
70         if ((error = sap_find_nearest(type, &li->ipxaddr, nearest)) != 0) {
71                 return error;
72         }
73         /* if no server specified return info about nearest */
74         if (!li->server[0]) {
75                 strcpy(li->server, nearest);
76                 return 0;
77         }
78 /*      printf("%s\n",ipx_ntoa(li->ipxaddr.sipx_addr));*/
79         if (strcmp(server, nearest) == 0) {
80                 return 0;
81         }
82         /* We have to ask the nearest server for our wanted server */
83         li->opt=0;
84         if ((error = ncp_connect(li, &connid)) != 0) {
85                 return error;
86         }
87         if (ncp_read_property_value(connid, type, server, 1, "NET_ADDRESS", &prop) != 0) {
88                 ncp_disconnect(connid);
89                 return EHOSTUNREACH;
90         }
91         if ((error = ncp_disconnect(connid)) != 0) {
92                 return error;
93         }
94         li->ipxaddr.sipx_family = AF_IPX;
95         li->ipxaddr.sipx_addr.x_net = n_addr->x_net;
96         li->ipxaddr.sipx_port = n_addr->x_port;
97         li->ipxaddr.sipx_addr.x_host = n_addr->x_host;
98         return 0;
99 }
100
101 static int
102 ncp_find_server_in(struct ncp_conn_loginfo *li, int type, char *server_name) {
103         struct hostent* h;
104         int l;
105
106         h = gethostbyname(server_name);
107         if (!h) {
108                 fprintf(stderr, "Get host address `%s': ", server_name);
109                 herror(NULL);
110                 return 1;
111         }
112         if (h->h_addrtype != AF_INET) {
113                 fprintf(stderr, "Get host address `%s': Not AF_INET\n", server_name);
114                 return 1;
115         }
116         if (h->h_length != 4) {
117                 fprintf(stderr, "Get host address `%s': Bad address length\n", server_name);
118                 return 1;
119         }
120         l = sizeof(struct sockaddr_in);
121         bzero(&li->inaddr, l);
122         li->inaddr.sin_len = l;
123         li->inaddr.sin_family = h->h_addrtype;
124         memcpy(&li->inaddr.sin_addr.s_addr, h->h_addr, 4);
125         li->inaddr.sin_port = htons(524); /* ncp */
126         return 0;
127 }
128
129 int 
130 ncp_find_server(struct ncp_conn_loginfo *li, int type, int af, char *name) {
131         int error = EHOSTUNREACH;
132
133         switch(af) {
134             case AF_IPX:
135                 error = ncp_find_server_ipx(li, type);
136                 break;
137             case AF_INET:
138                 if (name)
139                         error = ncp_find_server_in(li, type, name);
140                 break;
141             default:
142                 error = EPROTONOSUPPORT;
143         }
144         return error;
145 }
146
147 int
148 ncp_find_fileserver(struct ncp_conn_loginfo *li, int af, char *name) {
149         return ncp_find_server(li, NCP_BINDERY_FSERVER, af, name);
150 }