xref: /illumos-gate/usr/src/cmd/fs.d/nfs/lib/replica.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * replica.c
247c478bd9Sstevel@tonic-gate  *
25*c72ac7bbSfrankho  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Parse replicated server lists of the form:
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  *	host1:/path1,host2,host3,host4:/path2,host5:/path3
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * into an array containing its constituent parts:
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *	host1	/path1
377c478bd9Sstevel@tonic-gate  *	host2	/path2
387c478bd9Sstevel@tonic-gate  *	host3	/path2
397c478bd9Sstevel@tonic-gate  *	host4	/path2
407c478bd9Sstevel@tonic-gate  *	host5	/path3
417c478bd9Sstevel@tonic-gate  * where a server could also be represented in form of literal address
427c478bd9Sstevel@tonic-gate  * and in case it is an IPv6 literal address it will be enclosed in
437c478bd9Sstevel@tonic-gate  * square brackets [IPv6 Literal address]
447c478bd9Sstevel@tonic-gate  * Problems indicated by null return; they will be memory allocation
457c478bd9Sstevel@tonic-gate  * errors worthy of an error message unless count == -1, which means
467c478bd9Sstevel@tonic-gate  * a parse error.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <malloc.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
52*c72ac7bbSfrankho #include <strings.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <errno.h>
557c478bd9Sstevel@tonic-gate #include "replica.h"
567c478bd9Sstevel@tonic-gate 
57*c72ac7bbSfrankho void
free_replica(struct replica * list,int count)58*c72ac7bbSfrankho free_replica(struct replica *list, int count)
59*c72ac7bbSfrankho {
60*c72ac7bbSfrankho 	int i;
61*c72ac7bbSfrankho 
62*c72ac7bbSfrankho 	for (i = 0; i < count; i++) {
63*c72ac7bbSfrankho 		if (list[i].host)
64*c72ac7bbSfrankho 			free(list[i].host);
65*c72ac7bbSfrankho 		if (list[i].path)
66*c72ac7bbSfrankho 			free(list[i].path);
67*c72ac7bbSfrankho 	}
68*c72ac7bbSfrankho 	free(list);
69*c72ac7bbSfrankho }
70*c72ac7bbSfrankho 
717c478bd9Sstevel@tonic-gate struct replica *
parse_replica(char * special,int * count)727c478bd9Sstevel@tonic-gate parse_replica(char *special, int *count)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	struct replica *list = NULL;
757c478bd9Sstevel@tonic-gate 	char *root, *special2;
767c478bd9Sstevel@tonic-gate 	char *proot, *x, *y;
777c478bd9Sstevel@tonic-gate 	int scount, v6addr, i;
787c478bd9Sstevel@tonic-gate 	int found_colon = 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	*count = 0;
817c478bd9Sstevel@tonic-gate 	scount = 0;
827c478bd9Sstevel@tonic-gate 	v6addr = 0;
837c478bd9Sstevel@tonic-gate 	root = special2 = strdup(special);
847c478bd9Sstevel@tonic-gate 	proot = root;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	while (root) {
877c478bd9Sstevel@tonic-gate 		switch (*root) {
887c478bd9Sstevel@tonic-gate 		case '[':
897c478bd9Sstevel@tonic-gate 			if ((root != special2) && (*(root -1) != ',')) {
907c478bd9Sstevel@tonic-gate 				root++;
917c478bd9Sstevel@tonic-gate 				break;
927c478bd9Sstevel@tonic-gate 			}
937c478bd9Sstevel@tonic-gate 			y = strchr(root, ']');
947c478bd9Sstevel@tonic-gate 			if (!y) {
957c478bd9Sstevel@tonic-gate 				root++;
967c478bd9Sstevel@tonic-gate 				break;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 			if ((*(y + 1) != ',') && (*(y + 1) != ':')) {
997c478bd9Sstevel@tonic-gate 				root = y + 1;
1007c478bd9Sstevel@tonic-gate 				break;
1017c478bd9Sstevel@tonic-gate 			}
1027c478bd9Sstevel@tonic-gate 			/*
1037c478bd9Sstevel@tonic-gate 			 * Found a v6 Literal Address, so set "v6addr"
1047c478bd9Sstevel@tonic-gate 			 * and grab the address and store it in the list
1057c478bd9Sstevel@tonic-gate 			 * under "host part".
1067c478bd9Sstevel@tonic-gate 			 */
1077c478bd9Sstevel@tonic-gate 			proot = root + 1;
1087c478bd9Sstevel@tonic-gate 			root = y + 1;
1097c478bd9Sstevel@tonic-gate 			v6addr = 1;
110*c72ac7bbSfrankho 			if ((list = realloc(list, (*count + 1) *
111*c72ac7bbSfrankho 			    sizeof (struct replica))) == NULL)
1127c478bd9Sstevel@tonic-gate 				goto bad;
113*c72ac7bbSfrankho 			bzero(&list[(*count)++], sizeof (struct replica));
1147c478bd9Sstevel@tonic-gate 			*y = '\0';
1157c478bd9Sstevel@tonic-gate 			list[*count-1].host = strdup(proot);
1167c478bd9Sstevel@tonic-gate 			if (!list[*count-1].host)
1177c478bd9Sstevel@tonic-gate 				goto bad;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case ':':
1207c478bd9Sstevel@tonic-gate 			*root = '\0';
1217c478bd9Sstevel@tonic-gate 			x = root + 1;
1227c478bd9Sstevel@tonic-gate 			/*
1237c478bd9Sstevel@tonic-gate 			 * Find comma (if present), which bounds the path.
1247c478bd9Sstevel@tonic-gate 			 * The comma implies that the user is trying to
1257c478bd9Sstevel@tonic-gate 			 * specify failover syntax if another colon follows.
1267c478bd9Sstevel@tonic-gate 			 */
1277c478bd9Sstevel@tonic-gate 			if (((y = strchr(x, ',')) != NULL) &&
1287c478bd9Sstevel@tonic-gate 			    (strchr((y + 1), ':'))) {
1297c478bd9Sstevel@tonic-gate 				root = y + 1;
1307c478bd9Sstevel@tonic-gate 				*y = '\0';
1317c478bd9Sstevel@tonic-gate 			} else {
1327c478bd9Sstevel@tonic-gate 				found_colon = 1;
1337c478bd9Sstevel@tonic-gate 				root = NULL;
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			/*
1367c478bd9Sstevel@tonic-gate 			 * If "v6addr" is set, unset it, and since the "host
1377c478bd9Sstevel@tonic-gate 			 * part" is already taken care of, skip to the "path
1387c478bd9Sstevel@tonic-gate 			 * path" part.
1397c478bd9Sstevel@tonic-gate 			 */
1407c478bd9Sstevel@tonic-gate 			if (v6addr == 1)
1417c478bd9Sstevel@tonic-gate 				v6addr = 0;
1427c478bd9Sstevel@tonic-gate 			else {
143*c72ac7bbSfrankho 				if ((list = realloc(list, (*count + 1) *
144*c72ac7bbSfrankho 				    sizeof (struct replica))) == NULL)
1457c478bd9Sstevel@tonic-gate 					goto bad;
146*c72ac7bbSfrankho 				bzero(&list[(*count)++],
147*c72ac7bbSfrankho 				    sizeof (struct replica));
1487c478bd9Sstevel@tonic-gate 				list[*count-1].host = strdup(proot);
1497c478bd9Sstevel@tonic-gate 				if (!list[*count-1].host)
1507c478bd9Sstevel@tonic-gate 					goto bad;
1517c478bd9Sstevel@tonic-gate 				proot = root;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			}
1547c478bd9Sstevel@tonic-gate 			for (i = scount; i < *count; i++) {
1557c478bd9Sstevel@tonic-gate 				list[i].path = strdup(x);
1567c478bd9Sstevel@tonic-gate 				if (!list[i].path)
1577c478bd9Sstevel@tonic-gate 					goto bad;
1587c478bd9Sstevel@tonic-gate 			}
1597c478bd9Sstevel@tonic-gate 			scount = i;
1607c478bd9Sstevel@tonic-gate 			proot = root;
1617c478bd9Sstevel@tonic-gate 			if (y)
1627c478bd9Sstevel@tonic-gate 				*y = ',';
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		case ',':
1657c478bd9Sstevel@tonic-gate 			/*
1667c478bd9Sstevel@tonic-gate 			 * If "v6addr" is set, unset it and continue
1677c478bd9Sstevel@tonic-gate 			 * else grab the address and store it in the list
1687c478bd9Sstevel@tonic-gate 			 * under "host part".
1697c478bd9Sstevel@tonic-gate 			 */
1707c478bd9Sstevel@tonic-gate 			if (v6addr == 1) {
1717c478bd9Sstevel@tonic-gate 				v6addr = 0;
1727c478bd9Sstevel@tonic-gate 				proot = ++root;
1737c478bd9Sstevel@tonic-gate 			} else {
1747c478bd9Sstevel@tonic-gate 				*root = '\0';
1757c478bd9Sstevel@tonic-gate 				root++;
176*c72ac7bbSfrankho 				if ((list = realloc(list, (*count + 1) *
177*c72ac7bbSfrankho 				    sizeof (struct replica))) == NULL)
1787c478bd9Sstevel@tonic-gate 					goto bad;
179*c72ac7bbSfrankho 				bzero(&list[(*count)++],
180*c72ac7bbSfrankho 				    sizeof (struct replica));
1817c478bd9Sstevel@tonic-gate 				list[*count-1].host = strdup(proot);
1827c478bd9Sstevel@tonic-gate 				if (!list[*count-1].host)
1837c478bd9Sstevel@tonic-gate 					goto bad;
1847c478bd9Sstevel@tonic-gate 				proot = root;
1857c478bd9Sstevel@tonic-gate 				*(root - 1) = ',';
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		default:
1897c478bd9Sstevel@tonic-gate 			if (*root == '\0')
1907c478bd9Sstevel@tonic-gate 				root = NULL;
1917c478bd9Sstevel@tonic-gate 			else
1927c478bd9Sstevel@tonic-gate 				root++;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	if (found_colon) {
1967c478bd9Sstevel@tonic-gate 		free(special2);
1977c478bd9Sstevel@tonic-gate 		return (list);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate bad:
200*c72ac7bbSfrankho 	if (list)
201*c72ac7bbSfrankho 		free_replica(list, *count);
2027c478bd9Sstevel@tonic-gate 	if (!found_colon)
2037c478bd9Sstevel@tonic-gate 		*count = -1;
2047c478bd9Sstevel@tonic-gate 	free(special2);
2057c478bd9Sstevel@tonic-gate 	return (NULL);
2067c478bd9Sstevel@tonic-gate }
207