xref: /netbsd-src/lib/libutil/if_media.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: if_media.c,v 1.2 2008/04/28 20:23:03 martin Exp $	*/
2ece723d1Sdsl 
3ece723d1Sdsl /*-
4ece723d1Sdsl  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5ece723d1Sdsl  * All rights reserved.
6ece723d1Sdsl  *
7ece723d1Sdsl  * This code is derived from software contributed to The NetBSD Foundation
8ece723d1Sdsl  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9ece723d1Sdsl  * NASA Ames Research Center.
10ece723d1Sdsl  *
11ece723d1Sdsl  * Redistribution and use in source and binary forms, with or without
12ece723d1Sdsl  * modification, are permitted provided that the following conditions
13ece723d1Sdsl  * are met:
14ece723d1Sdsl  * 1. Redistributions of source code must retain the above copyright
15ece723d1Sdsl  *    notice, this list of conditions and the following disclaimer.
16ece723d1Sdsl  * 2. Redistributions in binary form must reproduce the above copyright
17ece723d1Sdsl  *    notice, this list of conditions and the following disclaimer in the
18ece723d1Sdsl  *    documentation and/or other materials provided with the distribution.
19ece723d1Sdsl  *
20ece723d1Sdsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21ece723d1Sdsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22ece723d1Sdsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23ece723d1Sdsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24ece723d1Sdsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25ece723d1Sdsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26ece723d1Sdsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27ece723d1Sdsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28ece723d1Sdsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ece723d1Sdsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ece723d1Sdsl  * POSSIBILITY OF SUCH DAMAGE.
31ece723d1Sdsl  */
32ece723d1Sdsl 
33ece723d1Sdsl #include <sys/cdefs.h>
34ece723d1Sdsl #if defined(LIBC_SCCS) && !defined(lint)
35*ce099b40Smartin __RCSID("$NetBSD: if_media.c,v 1.2 2008/04/28 20:23:03 martin Exp $");
36ece723d1Sdsl #endif
37ece723d1Sdsl 
38ece723d1Sdsl #include <stdio.h>
39ece723d1Sdsl #include <string.h>
40ece723d1Sdsl #include <stdlib.h>
41ece723d1Sdsl #include <sys/types.h>
42ece723d1Sdsl #include <net/if_media.h>
43ece723d1Sdsl 
44ece723d1Sdsl struct ifmedia_description ifm_mode_descriptions[] =
45ece723d1Sdsl     IFM_MODE_DESCRIPTIONS;
46ece723d1Sdsl 
47ece723d1Sdsl struct ifmedia_description ifm_type_descriptions[] =
48ece723d1Sdsl     IFM_TYPE_DESCRIPTIONS;
49ece723d1Sdsl 
50ece723d1Sdsl struct ifmedia_description ifm_subtype_descriptions[] =
51ece723d1Sdsl     IFM_SUBTYPE_DESCRIPTIONS;
52ece723d1Sdsl 
53ece723d1Sdsl struct ifmedia_description ifm_option_descriptions[] =
54ece723d1Sdsl     IFM_OPTION_DESCRIPTIONS;
55ece723d1Sdsl 
56ece723d1Sdsl const char *
get_media_type_string(int mword)57ece723d1Sdsl get_media_type_string(int mword)
58ece723d1Sdsl {
59ece723d1Sdsl 	struct ifmedia_description *desc;
60ece723d1Sdsl 
61ece723d1Sdsl 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) {
62ece723d1Sdsl 		if (IFM_TYPE(mword) == desc->ifmt_word)
63ece723d1Sdsl 			return (desc->ifmt_string);
64ece723d1Sdsl 	}
65ece723d1Sdsl 	return "<unknown type>";
66ece723d1Sdsl }
67ece723d1Sdsl 
68ece723d1Sdsl const char *
get_media_subtype_string(int mword)69ece723d1Sdsl get_media_subtype_string(int mword)
70ece723d1Sdsl {
71ece723d1Sdsl 	struct ifmedia_description *desc;
72ece723d1Sdsl 
73ece723d1Sdsl 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
74ece723d1Sdsl 	     desc++) {
75ece723d1Sdsl 		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
76ece723d1Sdsl 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
77ece723d1Sdsl 			return desc->ifmt_string;
78ece723d1Sdsl 	}
79ece723d1Sdsl 	return "<unknown subtype>";
80ece723d1Sdsl }
81ece723d1Sdsl 
82ece723d1Sdsl const char *
get_media_mode_string(int mword)83ece723d1Sdsl get_media_mode_string(int mword)
84ece723d1Sdsl {
85ece723d1Sdsl 	struct ifmedia_description *desc;
86ece723d1Sdsl 
87ece723d1Sdsl 	for (desc = ifm_mode_descriptions; desc->ifmt_string != NULL; desc++) {
88ece723d1Sdsl 		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
89ece723d1Sdsl 		    IFM_MODE(mword) == IFM_MODE(desc->ifmt_word))
90ece723d1Sdsl 			return desc->ifmt_string;
91ece723d1Sdsl 	}
92ece723d1Sdsl 	return NULL;
93ece723d1Sdsl }
94ece723d1Sdsl 
95ece723d1Sdsl const char *
get_media_option_string(int * mwordp)96ece723d1Sdsl get_media_option_string(int *mwordp)
97ece723d1Sdsl {
98ece723d1Sdsl 	struct ifmedia_description *desc;
99ece723d1Sdsl 	int mword = *mwordp;
100ece723d1Sdsl 
101ece723d1Sdsl 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
102ece723d1Sdsl 	     desc++) {
103ece723d1Sdsl 		if (!IFM_TYPE_MATCH(desc->ifmt_word, mword))
104ece723d1Sdsl 			continue;
105ece723d1Sdsl 		if (mword & IFM_OPTIONS(desc->ifmt_word)) {
106ece723d1Sdsl 			*mwordp = mword & ~IFM_OPTIONS(desc->ifmt_word);
107ece723d1Sdsl 			return desc->ifmt_string;
108ece723d1Sdsl 		}
109ece723d1Sdsl 	}
110ece723d1Sdsl 
111ece723d1Sdsl 	/* Historical behaviour is to ignore unknown option bits! */
112ece723d1Sdsl 	*mwordp = mword & ~IFM_OPTIONS(~0);
113ece723d1Sdsl 	return NULL;
114ece723d1Sdsl }
115ece723d1Sdsl 
116ece723d1Sdsl int
lookup_media_word(struct ifmedia_description * desc,int type,const char * val)117ece723d1Sdsl lookup_media_word(struct ifmedia_description *desc, int type, const char *val)
118ece723d1Sdsl {
119ece723d1Sdsl 
120ece723d1Sdsl 	for (; desc->ifmt_string != NULL; desc++) {
121ece723d1Sdsl 		if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
122ece723d1Sdsl 		    strcasecmp(desc->ifmt_string, val) == 0)
123ece723d1Sdsl 			return (desc->ifmt_word);
124ece723d1Sdsl 	}
125ece723d1Sdsl 	return -1;
126ece723d1Sdsl }
127ece723d1Sdsl 
128ece723d1Sdsl int
get_media_mode(int type,const char * val)129ece723d1Sdsl get_media_mode(int type, const char *val)
130ece723d1Sdsl {
131ece723d1Sdsl 
132ece723d1Sdsl 	return lookup_media_word(ifm_mode_descriptions, type, val);
133ece723d1Sdsl }
134ece723d1Sdsl 
135ece723d1Sdsl int
get_media_subtype(int type,const char * val)136ece723d1Sdsl get_media_subtype(int type, const char *val)
137ece723d1Sdsl {
138ece723d1Sdsl 
139ece723d1Sdsl 	return lookup_media_word(ifm_subtype_descriptions, type, val);
140ece723d1Sdsl }
141ece723d1Sdsl 
142ece723d1Sdsl int
get_media_options(int type,const char * val,char ** invalid)143ece723d1Sdsl get_media_options(int type, const char *val, char **invalid)
144ece723d1Sdsl {
145ece723d1Sdsl 	char *optlist, *str;
146ece723d1Sdsl 	int option, rval = 0;
147ece723d1Sdsl 
148ece723d1Sdsl 	/* We muck with the string, so copy it. */
149ece723d1Sdsl 	optlist = strdup(val);
150ece723d1Sdsl 	if (optlist == NULL) {
151ece723d1Sdsl 		if (invalid != NULL)
152ece723d1Sdsl 			*invalid = NULL;
153ece723d1Sdsl 		return -1;
154ece723d1Sdsl 	}
155ece723d1Sdsl 	str = optlist;
156ece723d1Sdsl 
157ece723d1Sdsl 	/*
158ece723d1Sdsl 	 * Look up the options in the user-provided comma-separated list.
159ece723d1Sdsl 	 */
160ece723d1Sdsl 	type = IFM_TYPE(type);
161ece723d1Sdsl 	for (; (str = strtok(str, ",")) != NULL; str = NULL) {
162ece723d1Sdsl 		option = lookup_media_word(ifm_option_descriptions, type, str);
163ece723d1Sdsl 		if (option != -1) {
164ece723d1Sdsl 			rval |= IFM_OPTIONS(option);
165ece723d1Sdsl 			continue;
166ece723d1Sdsl 		}
167ece723d1Sdsl 		rval = -1;
168ece723d1Sdsl 		if (invalid == NULL)
169ece723d1Sdsl 			break;
170ece723d1Sdsl 		/* Pass invalid option at start of malloced buffer */
171ece723d1Sdsl 		if (str != optlist)
172ece723d1Sdsl 			memmove(optlist, str, strlen(str) + 1);
173ece723d1Sdsl 		/* Caller should free() or exit() */
174ece723d1Sdsl 		*invalid = optlist;
175ece723d1Sdsl 		return rval;
176ece723d1Sdsl 	}
177ece723d1Sdsl 
178ece723d1Sdsl 	free(optlist);
179ece723d1Sdsl 	return (rval);
180ece723d1Sdsl }
181