b7a76e32f15c058386f3ad46cfba5f1c2993b172
[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  * $DragonFly: src/lib/libncp/ncpl_net.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/errno.h>
40 #include <sys/syscall.h>
41 #include <ctype.h>
42 #include <netinet/in.h>
43 #include <netipx/ipx.h>
44 #include <netdb.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <unistd.h>
48
49 #include "ipxsap.h"
50 #include <netncp/ncp_lib.h>
51 #include "ncp_mod.h"
52
53 static int ncp_find_server_in(struct ncp_conn_loginfo *li, int type, char *server_name);
54
55 static int
56 ncp_find_server_ipx(struct ncp_conn_loginfo *li, int type) {
57         char server[NCP_BINDERY_NAME_LEN + 1];
58         int error;
59         char nearest[NCP_BINDERY_NAME_LEN + 1];
60         struct nw_property prop;
61         struct ipx_addr *n_addr = (struct ipx_addr *) &prop;
62 /*      struct ncp_conn_loginfo ltmp;*/
63         int connid;
64
65         bzero(server, sizeof(server));
66         bzero(nearest, sizeof(nearest));
67
68         strcpy(server, li->server);
69         ncp_str_upper(server);
70
71         if ((error = sap_find_nearest(type, &li->addr.ipxaddr, nearest)) != 0) {
72                 return error;
73         }
74         /* if no server specified return info about nearest */
75         if (!li->server[0]) {
76                 strcpy(li->server, nearest);
77                 return 0;
78         }
79 /*      printf("%s\n",ipx_ntoa(li->addr.ipxaddr.sipx_addr));*/
80         if (strcmp(server, nearest) == 0) {
81                 return 0;
82         }
83         /* We have to ask the nearest server for our wanted server */
84         li->opt=0;
85         if ((error = ncp_connect(li, &connid)) != 0) {
86                 return error;
87         }
88         if (ncp_read_property_value(connid, type, server, 1, "NET_ADDRESS", &prop) != 0) {
89                 ncp_disconnect(connid);
90                 return EHOSTUNREACH;
91         }
92         if ((error = ncp_disconnect(connid)) != 0) {
93                 return error;
94         }
95         li->addr.ipxaddr.sipx_family = AF_IPX;
96         li->addr.ipxaddr.sipx_addr.x_net = n_addr->x_net;
97         li->addr.ipxaddr.sipx_port = n_addr->x_port;
98         li->addr.ipxaddr.sipx_addr.x_host = n_addr->x_host;
99         return 0;
100 }
101
102 static int
103 ncp_find_server_in(struct ncp_conn_loginfo *li, int type, char *server_name) {
104         struct hostent* h;
105         int l;
106
107         h = gethostbyname(server_name);
108         if (!h) {
109                 fprintf(stderr, "Get host address `%s': ", server_name);
110                 herror(NULL);
111                 return 1;
112         }
113         if (h->h_addrtype != AF_INET) {
114                 fprintf(stderr, "Get host address `%s': Not AF_INET\n", server_name);
115                 return 1;
116         }
117         if (h->h_length != 4) {
118                 fprintf(stderr, "Get host address `%s': Bad address length\n", server_name);
119                 return 1;
120         }
121         l = sizeof(struct sockaddr_in);
122         bzero(&li->addr.inaddr, l);
123         li->addr.inaddr.sin_len = l;
124         li->addr.inaddr.sin_family = h->h_addrtype;
125         memcpy(&li->addr.inaddr.sin_addr.s_addr, h->h_addr, 4);
126         li->addr.inaddr.sin_port = htons(524); /* ncp */
127         return 0;
128 }
129
130 int 
131 ncp_find_server(struct ncp_conn_loginfo *li, int type, int af, char *name) {
132         int error = EHOSTUNREACH;
133
134         switch(af) {
135             case AF_IPX:
136                 error = ncp_find_server_ipx(li, type);
137                 break;
138             case AF_INET:
139                 if (name)
140                         error = ncp_find_server_in(li, type, name);
141                 break;
142             default:
143                 error = EPROTONOSUPPORT;
144         }
145         return error;
146 }
147
148 int
149 ncp_find_fileserver(struct ncp_conn_loginfo *li, int af, char *name) {
150         return ncp_find_server(li, NCP_BINDERY_FSERVER, af, name);
151 }