source: EcnlProtoTool/trunk/asp3_dcre/tinet/net/net_subr.c@ 321

Last change on this file since 321 was 321, checked in by coas-nagasima, 7 years ago

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 7.1 KB
Line 
1/*
2 * TINET (TCP/IP Protocol Stack)
3 *
4 * Copyright (C) 2001-2009 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$
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/net.h>
91
92/*
93 * 変数
94 */
95
96static uint32_t next = ULONG_C(1);
97
98/*
99 * net_rand -- 乱数を返す。
100 */
101
102uint32_t
103net_rand (void)
104{
105 next = (next * 1103515245 + 12345) % (ULONG_C(0x7fffffff) + 1);
106 return next;
107 }
108
109/*
110 * srand -- 乱数を初期化する。
111 */
112
113void
114net_srand (uint32_t seed)
115{
116 next += seed;
117 }
118
119/*
120 * convert_hexdigit -- 16進数→文字列変換
121 */
122
123int_t
124convert_hexdigit (char *buf, uint_t val, int_t radix, int_t width, char padchar)
125{
126 static const char radhex[] = "0123456789abcdef";
127
128 char digits[11], *start;
129 int_t ix, pad;
130
131 ix = 0;
132 start = buf;
133 do {
134 digits[ix ++] = radhex[(val % radix) & 0x0f];
135 val /= radix;
136 } while (val != 0);
137 for (pad = ix; pad < width; pad ++)
138 *buf ++ = padchar;
139 while (ix -- > 0)
140 *buf ++ = digits[ix];
141 *buf = '\0';
142 return (int_t)(buf - start);
143 }
144
145#ifdef SUPPORT_ETHER
146
147/*
148 * mac2str -- MAC アドレスを文字列に変換する。
149 */
150
151char *
152mac2str (char *buf, uint8_t *macaddr)
153{
154 static char addr_sbuf[NUM_MACADDR_STR_BUFF][sizeof("00:00:00:00:00:00")];
155 static int_t bix = NUM_MACADDR_STR_BUFF;
156
157 char *start;
158
159 if (buf == NULL) {
160 syscall(wai_sem(SEM_MAC2STR_BUFF_LOCK));
161 buf = addr_sbuf[-- bix];
162 if (bix <= 0)
163 bix = NUM_MACADDR_STR_BUFF;
164 syscall(sig_sem(SEM_MAC2STR_BUFF_LOCK));
165 }
166
167 start = buf;
168 buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
169 *(buf ++) = ':';
170 buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
171 *(buf ++) = ':';
172 buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
173 *(buf ++) = ':';
174 buf += convert_hexdigit(buf, (uint_t)(*macaddr ++), 16, 2, '0');
175 *(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 = '\0';
180 return start;
181 }
182
183#endif /* of #ifdef SUPPORT_ETHER */
184
185#if _NET_CFG_BYTE_ORDER == _NET_CFG_LITTLE_ENDIAN
186
187/*
188 * rev_memcpy_word -- 反転メモリコピー(4 バイト)
189 *
190 * バイト単位にアクセスする事により、
191 * 境界へのアラインの問題を解決する。
192 */
193
194void
195rev_memcpy_word (uint8_t *dst, uint8_t *src)
196{
197 *(dst ) = *(src + 3);
198 *(dst + 1) = *(src + 2);
199 *(dst + 2) = *(src + 1);
200 *(dst + 3) = *(src );
201 }
202
203/*
204 * rev_memcmp_word -- 反転メモリ比較(4 バイト)
205 *
206 * バイト単位にアクセスする事により、
207 * 境界へのアラインの問題を解決する。
208 */
209
210int_t
211rev_memcmp_word (uint8_t *data1, uint8_t *data2)
212{
213 int_t ix, diff;
214
215 for (ix = 4; ix -- > 0; ) {
216 diff = *(data1 + ix) - *(data2 + (3 - ix));
217 if (diff != 0)
218 return diff;
219 }
220 return 0;
221 }
222
223#endif /* of #if _NET_CFG_BYTE_ORDER == _NET_CFG_LITTLE_ENDIAN */
Note: See TracBrowser for help on using the repository browser.