xref: /titanic_44/usr/src/cmd/getent/dogetnetmask.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1994-1996, by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include "getent.h"
38 
39 extern char *inet_nettoa(struct in_addr in);
40 
41 static int
42 putnetmask(const struct in_addr key, const struct in_addr netmask, FILE *fp)
43 {
44 	char **p;
45 	int rc = 0;
46 	struct in_addr net;
47 
48 	net.s_addr = ntohl(key.s_addr);
49 	if (fprintf(fp, "%-20s", inet_nettoa(net)) == EOF)
50 		rc = 1;
51 	if (fprintf(fp, " %s", inet_ntoa(netmask)) == EOF)
52 		rc = 1;
53 	if (putc('\n', fp) == EOF)
54 		rc = 1;
55 	return (rc);
56 }
57 
58 /*
59  * getnetmaskbyaddr - get entries from network database
60  */
61 int
62 dogetnetmask(const char **list)
63 {
64 	int rc = EXC_SUCCESS;
65 	struct in_addr addr, netmask;
66 
67 	if (list == NULL || *list == NULL)
68 		return (EXC_ENUM_NOT_SUPPORTED);
69 
70 	for (; *list != NULL; list++) {
71 		addr.s_addr = htonl(inet_network(*list));
72 		if (addr.s_addr != -1) {
73 			if (getnetmaskbyaddr(addr, &netmask) == 0) {
74 				(void) putnetmask(addr, netmask, stdout);
75 			} else {
76 				rc = EXC_NAME_NOT_FOUND;
77 			}
78 		} else {
79 			rc = EXC_NAME_NOT_FOUND;
80 		}
81 	}
82 
83 	return (rc);
84 }
85