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