1 | /*
|
---|
2 | * TINET (TCP/IP Protocol Stack)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering
|
---|
5 | * Tomakomai National College of Technology, JAPAN
|
---|
6 | *
|
---|
7 | * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
|
---|
8 | * によって公表されている GNU General Public License の Version 2 に記
|
---|
9 | * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
|
---|
10 | * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
|
---|
11 | * 利用と呼ぶ)することを無償で許諾する.
|
---|
12 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
13 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
14 | * スコード中に含まれていること.
|
---|
15 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
16 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
17 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
18 | * の無保証規定を掲載すること.
|
---|
19 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
20 | * 用できない形で再配布する場合には,次の条件を満たすこと.
|
---|
21 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
22 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | *
|
---|
26 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
27 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
|
---|
28 | * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
|
---|
29 | * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
|
---|
30 | *
|
---|
31 | * @(#) $Id: net_subr.c 315 2017-07-23 05:29:40Z coas-nagasima $
|
---|
32 | */
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Copyright (c) 1982, 1986, 1988, 1993
|
---|
36 | * The Regents of the University of California. All rights reserved.
|
---|
37 | *
|
---|
38 | * Redistribution and use in source and binary forms, with or without
|
---|
39 | * modification, are permitted provided that the following conditions
|
---|
40 | * are met:
|
---|
41 | * 1. Redistributions of source code must retain the above copyright
|
---|
42 | * notice, this list of conditions and the following disclaimer.
|
---|
43 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
44 | * notice, this list of conditions and the following disclaimer in the
|
---|
45 | * documentation and/or other materials provided with the distribution.
|
---|
46 | * 3. All advertising materials mentioning features or use of this software
|
---|
47 | * must display the following acknowledgement:
|
---|
48 | * This product includes software developed by the University of
|
---|
49 | * California, Berkeley and its contributors.
|
---|
50 | * 4. Neither the name of the University nor the names of its contributors
|
---|
51 | * may be used to endorse or promote products derived from this software
|
---|
52 | * without specific prior written permission.
|
---|
53 | *
|
---|
54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
64 | * SUCH DAMAGE.
|
---|
65 | */
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * ネットワーク汎用サポートルーチン
|
---|
69 | */
|
---|
70 |
|
---|
71 | #ifdef TARGET_KERNEL_ASP
|
---|
72 |
|
---|
73 | #include <kernel.h>
|
---|
74 | #include <sil.h>
|
---|
75 | #include "kernel_cfg.h"
|
---|
76 |
|
---|
77 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
78 |
|
---|
79 | #ifdef TARGET_KERNEL_JSP
|
---|
80 |
|
---|
81 | #include <s_services.h>
|
---|
82 | #include <t_services.h>
|
---|
83 | #include "kernel_id.h"
|
---|
84 |
|
---|
85 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
86 |
|
---|
87 | #include <tinet_defs.h>
|
---|
88 | #include <tinet_config.h>
|
---|
89 |
|
---|
90 | #include <net/if.h>
|
---|
91 | #include <net/if_loop.h>
|
---|
92 | #include <net/if_ppp.h>
|
---|
93 | #include <net/ethernet.h>
|
---|
94 | #include <net/net.h>
|
---|
95 | #include <net/net_endian.h>
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * 変数
|
---|
99 | */
|
---|
100 |
|
---|
101 | static uint32_t next = ULONG_C(1);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * net_rand -- 乱数を返す。
|
---|
105 | */
|
---|
106 |
|
---|
107 | uint32_t
|
---|
108 | net_rand (void)
|
---|
109 | {
|
---|
110 | next = (next * 99991 + 12345) & ULONG_C(0x7fffffff);
|
---|
111 | return next;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * srand -- 乱数を初期化する。
|
---|
116 | */
|
---|
117 |
|
---|
118 | void
|
---|
119 | net_srand (uint32_t seed)
|
---|
120 | {
|
---|
121 | SYSTIM now;
|
---|
122 |
|
---|
123 | syscall(get_tim(&now));
|
---|
124 | next += now + seed + IF_SRAND();
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * convert_hexdigit -- 16進数→文字列変換
|
---|
129 | */
|
---|
130 |
|
---|
131 | int_t
|
---|
132 | convert_hexdigit (char *buf, uint_t val, int_t radix, int_t width, char padchar)
|
---|
133 | {
|
---|
134 | static const char radhex[] = "0123456789abcdef";
|
---|
135 |
|
---|
136 | char digits[11], *start;
|
---|
137 | int_t ix, pad;
|
---|
138 |
|
---|
139 | ix = 0;
|
---|
140 | start = buf;
|
---|
141 | do {
|
---|
142 | digits[ix ++] = radhex[(val % radix) & 0x0f];
|
---|
143 | val /= radix;
|
---|
144 | } while (val != 0);
|
---|
145 | for (pad = ix; pad < width; pad ++)
|
---|
146 | *buf ++ = padchar;
|
---|
147 | while (ix -- > 0)
|
---|
148 | *buf ++ = digits[ix];
|
---|
149 | *buf = '\0';
|
---|
150 | return (int_t)(buf - start);
|
---|
151 | }
|
---|
152 |
|
---|
153 | #ifdef SUPPORT_ETHER
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * mac2str -- MAC アドレスを文字列に変換する。
|
---|
157 | */
|
---|
158 |
|
---|
159 | char *
|
---|
160 | mac2str (char *buf, uint8_t *macaddr)
|
---|
161 | {
|
---|
162 | static char addr_sbuf[NUM_MACADDR_STR_BUFF][sizeof("00:00:00:00:00:00")];
|
---|
163 | static int_t bix = NUM_MACADDR_STR_BUFF;
|
---|
164 |
|
---|
165 | char *start;
|
---|
166 |
|
---|
167 | if (buf == NULL) {
|
---|
168 | syscall(wai_sem(SEM_MAC2STR_BUFF_LOCK));
|
---|
169 | buf = addr_sbuf[-- bix];
|
---|
170 | if (bix <= 0)
|
---|
171 | bix = NUM_MACADDR_STR_BUFF;
|
---|
172 | syscall(sig_sem(SEM_MAC2STR_BUFF_LOCK));
|
---|
173 | }
|
---|
174 |
|
---|
175 | start = buf;
|
---|
176 | buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
|
---|
177 | *(buf ++) = ':';
|
---|
178 | buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
|
---|
179 | *(buf ++) = ':';
|
---|
180 | buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
|
---|
181 | *(buf ++) = ':';
|
---|
182 | buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
|
---|
183 | *(buf ++) = ':';
|
---|
184 | buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
|
---|
185 | *(buf ++) = ':';
|
---|
186 | buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
|
---|
187 | *buf = '\0';
|
---|
188 | return start;
|
---|
189 | }
|
---|
190 |
|
---|
191 | #endif /* of #ifdef SUPPORT_ETHER */
|
---|
192 |
|
---|
193 | #if _NET_CFG_BYTE_ORDER == _NET_CFG_LITTLE_ENDIAN
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * rev_memcpy_hword -- 反転メモリコピー(2 バイト)
|
---|
197 | *
|
---|
198 | * バイト単位にアクセスする事により、
|
---|
199 | * 境界へのアラインの問題を解決する。
|
---|
200 | */
|
---|
201 |
|
---|
202 | void
|
---|
203 | rev_memcpy_hword (void *dst, void *src)
|
---|
204 | {
|
---|
205 | *((uint8_t*)dst ) = *((uint8_t*)src + 1);
|
---|
206 | *((uint8_t*)dst + 1) = *((uint8_t*)src );
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * rev_memcpy_word -- 反転メモリコピー(4 バイト)
|
---|
211 | *
|
---|
212 | * バイト単位にアクセスする事により、
|
---|
213 | * 境界へのアラインの問題を解決する。
|
---|
214 | */
|
---|
215 |
|
---|
216 | void
|
---|
217 | rev_memcpy_word (void *dst, void *src)
|
---|
218 | {
|
---|
219 | *((uint8_t*)dst ) = *((uint8_t*)src + 3);
|
---|
220 | *((uint8_t*)dst + 1) = *((uint8_t*)src + 2);
|
---|
221 | *((uint8_t*)dst + 2) = *((uint8_t*)src + 1);
|
---|
222 | *((uint8_t*)dst + 3) = *((uint8_t*)src );
|
---|
223 | }
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * rev_memcmp_word -- 反転メモリ比較(4 バイト)
|
---|
227 | *
|
---|
228 | * バイト単位にアクセスする事により、
|
---|
229 | * 境界へのアラインの問題を解決する。
|
---|
230 | */
|
---|
231 |
|
---|
232 | int_t
|
---|
233 | rev_memcmp_word (void *data1, void *data2)
|
---|
234 | {
|
---|
235 | int_t ix, diff;
|
---|
236 |
|
---|
237 | for (ix = sizeof(uint32_t); ix -- > 0; ) {
|
---|
238 | diff = *((uint8_t*)data1 + ix) - *((uint8_t*)data2 + (3 - ix));
|
---|
239 | if (diff != 0)
|
---|
240 | return diff;
|
---|
241 | }
|
---|
242 | return 0;
|
---|
243 | }
|
---|
244 |
|
---|
245 | #endif /* of #if _NET_CFG_BYTE_ORDER == _NET_CFG_LITTLE_ENDIAN */
|
---|