1 /* $NetBSD: if_media.c,v 1.1 2004/11/11 20:36:28 dsl Exp $ */
4 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: if_media.c,v 1.1 2004/11/11 20:36:28 dsl Exp $");
41 #include <sys/types.h>
42 #include <net/if_media.h>
44 struct ifmedia_description ifm_mode_descriptions
[] =
45 IFM_MODE_DESCRIPTIONS
;
47 struct ifmedia_description ifm_type_descriptions
[] =
48 IFM_TYPE_DESCRIPTIONS
;
50 struct ifmedia_description ifm_subtype_descriptions
[] =
51 IFM_SUBTYPE_DESCRIPTIONS
;
53 struct ifmedia_description ifm_option_descriptions
[] =
54 IFM_OPTION_DESCRIPTIONS
;
57 get_media_type_string(int mword
)
59 struct ifmedia_description
*desc
;
61 for (desc
= ifm_type_descriptions
; desc
->ifmt_string
!= NULL
; desc
++) {
62 if (IFM_TYPE(mword
) == desc
->ifmt_word
)
63 return (desc
->ifmt_string
);
65 return "<unknown type>";
69 get_media_subtype_string(int mword
)
71 struct ifmedia_description
*desc
;
73 for (desc
= ifm_subtype_descriptions
; desc
->ifmt_string
!= NULL
;
75 if (IFM_TYPE_MATCH(desc
->ifmt_word
, mword
) &&
76 IFM_SUBTYPE(desc
->ifmt_word
) == IFM_SUBTYPE(mword
))
77 return desc
->ifmt_string
;
79 return "<unknown subtype>";
83 get_media_mode_string(int mword
)
85 struct ifmedia_description
*desc
;
87 for (desc
= ifm_mode_descriptions
; desc
->ifmt_string
!= NULL
; desc
++) {
88 if (IFM_TYPE_MATCH(desc
->ifmt_word
, mword
) &&
89 IFM_MODE(mword
) == IFM_MODE(desc
->ifmt_word
))
90 return desc
->ifmt_string
;
96 get_media_option_string(int *mwordp
)
98 struct ifmedia_description
*desc
;
101 for (desc
= ifm_option_descriptions
; desc
->ifmt_string
!= NULL
;
103 if (!IFM_TYPE_MATCH(desc
->ifmt_word
, mword
))
105 if (mword
& IFM_OPTIONS(desc
->ifmt_word
)) {
106 *mwordp
= mword
& ~IFM_OPTIONS(desc
->ifmt_word
);
107 return desc
->ifmt_string
;
111 /* Historical behaviour is to ignore unknown option bits! */
112 *mwordp
= mword
& ~IFM_OPTIONS(~0);
117 lookup_media_word(struct ifmedia_description
*desc
, int type
, const char *val
)
120 for (; desc
->ifmt_string
!= NULL
; desc
++) {
121 if (IFM_TYPE_MATCH(desc
->ifmt_word
, type
) &&
122 strcasecmp(desc
->ifmt_string
, val
) == 0)
123 return (desc
->ifmt_word
);
129 get_media_mode(int type
, const char *val
)
132 return lookup_media_word(ifm_mode_descriptions
, type
, val
);
136 get_media_subtype(int type
, const char *val
)
139 return lookup_media_word(ifm_subtype_descriptions
, type
, val
);
143 get_media_options(int type
, const char *val
, char **invalid
)
146 int option
, rval
= 0;
148 /* We muck with the string, so copy it. */
149 optlist
= strdup(val
);
150 if (optlist
== NULL
) {
158 * Look up the options in the user-provided comma-separated list.
160 type
= IFM_TYPE(type
);
161 for (; (str
= strtok(str
, ",")) != NULL
; str
= NULL
) {
162 option
= lookup_media_word(ifm_option_descriptions
, type
, str
);
164 rval
|= IFM_OPTIONS(option
);
170 /* Pass invalid option at start of malloced buffer */
172 memmove(optlist
, str
, strlen(str
) + 1);
173 /* Caller should free() or exit() */