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