source: azure_iot_hub/trunk/asp3_dcre/tinet/netinet/udpn_input.c@ 388

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

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 8.9 KB
Line 
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 * 上記著作権者
8は,以下の (1)~(4) の条件か,Free Software Foundation
9 * によってå…
10¬è¡¨ã•ã‚Œã¦ã„ã‚‹ GNU General Public License の Version 2 に記
11 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12 * を改変したものを含む.以下同じ)を使用・複製・改変・再é…
13å¸ƒï¼ˆä»¥ä¸‹ï¼Œ
14 * 利用と呼ぶ)することを無償で許諾する.
15 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
16 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
17 * スコード中に含まれていること.
18 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
19 * 用できる形で再é…
20å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œå†é…
21å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨
22 * 者
23マニュアルなど)に,上記の著作権表示,この利用条件および下記
24 * の無保証規定を掲載すること.
25 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
26 * 用できない形で再é…
27å¸ƒã™ã‚‹å ´åˆã«ã¯ï¼Œæ¬¡ã®æ¡ä»¶ã‚’満たすこと.
28 * (a) 再é…
29å¸ƒã«ä¼´ã†ãƒ‰ã‚­ãƒ¥ãƒ¡ãƒ³ãƒˆï¼ˆåˆ©ç”¨è€…
30マニュアルなど)に,上記の著
31 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
32 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
33 * 害からも,上記著作権者
34およびTOPPERSプロジェクトをå…
35è²¬ã™ã‚‹ã“と.
36 *
37 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者
38お
39 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
40 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
41 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
42 *
43 * @(#) $Id: udpn_input.c 388 2019-05-22 11:25:18Z coas-nagasima $
44 */
45
46/*
47 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
48 * The Regents of the University of California. All rights reserved.
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. All advertising materials mentioning features or use of this software
59 * must display the following acknowledgement:
60 * This product includes software developed by the University of
61 * California, Berkeley and its contributors.
62 * 4. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
65 *
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * SUCH DAMAGE.
77 */
78
79#if (API_PROTO == API_PROTO_IPV6) || (TNUM_UDP4_CEPID > 0)
80
81/*
82 * udp_input_select -- UDP のå…
83¥åŠ›å‡¦ç†ã®é¸æŠž
84 */
85
86void
87UDP_INPUT_SELECT (T_UDP_CEP *cep, T_NET_BUF *input, uint_t off)
88{
89 T_UDP_HDR *udph;
90 T_UDP_RCV_DAT_PARA para;
91
92 udph = (T_UDP_HDR *)(input->buf + off);
93 para.len = (uint_t)(ntohs(udph->ulen) - UDP_HDR_SIZE);
94
95 if (cep->rcv_tskid != TA_NULL) { /* 非ノンブロッキングコールでペンディング中 */
96 if (psnd_dtq(cep->rcvqid, (intptr_t)input) != E_OK) {
97 NET_COUNT_UDP(net_count_udp.in_err_packets, 1);
98 NET_COUNT_MIB(udp_stats.udpInErrors, 1);
99 syscall(rel_net_buf(input));
100 }
101 }
102
103#ifdef UDP_CFG_NON_BLOCKING
104
105 else if (cep->rcv_p_dstaddr != NULL) { /* ノンブロッキングコールでペンディング中 */
106
107 /* p_dstaddr を設定する。*/
108 cep->rcv_p_dstaddr->portno = ntohs(udph->sport);
109 IN_COPY_TO_HOST(&cep->rcv_p_dstaddr->ipaddr, input);
110
111 /* データをバッファに移す。*/
112 memcpy(cep->rcv_data, GET_UDP_SDU(input, off),
113 (size_t)(para.len < cep->rcv_len ? para.len : cep->rcv_len));
114 syscall(rel_net_buf(input));
115
116 if (IS_PTR_DEFINED(cep->callback))
117
118#ifdef TCP_CFG_NON_BLOCKING_COMPAT14
119 (*cep->callback)(GET_UDP_CEPID(cep), TFN_UDP_RCV_DAT, (void*)(uint32_t)para.len);
120#else
121 (*cep->callback)(GET_UDP_CEPID(cep), TFN_UDP_RCV_DAT, (void*)&para.len);
122#endif
123 else
124 syslog(LOG_WARNING, "[UDP] no call back, CEP: %d.", GET_UDP_CEPID(cep));
125 cep->rcv_p_dstaddr = NULL;
126 }
127
128#endif/* of #ifdef UDP_CFG_NON_BLOCKING */
129
130 else if (IS_PTR_DEFINED(cep->callback)) {
131
132 /* コールバック関数を呼び出す。*/
133 cep->cb_netbuf = input;
134
135#ifdef TCP_CFG_NON_BLOCKING_COMPAT14
136 (*cep->callback)(GET_UDP_CEPID(cep), TEV_UDP_RCV_DAT, (void*)(uint32_t)para.len);
137#else
138 para.input = input;
139 para.off = off;
140#if API_PROTO == API_PROTO_IPV4
141 para.rep4.portno = ntohs(udph->sport);
142 IN_COPY_TO_HOST(&para.rep4.ipaddr, input);
143#else
144 para.rep6.portno = ntohs(udph->sport);
145 IN_COPY_TO_HOST(&para.rep6.ipaddr, input);
146#endif
147 (*cep->callback)(GET_UDP_CEPID(cep), TEV_UDP_RCV_DAT, (void*)&para.len);
148#endif
149 /*
150 * ネットワークバッファがそのままであれば、コールバック関数内
151で
152 * データを読み出さなかったことになるので、捨てる。
153 */
154 if (cep->cb_netbuf != NULL) {
155 if ((input->flags & NB_FLG_NOREL_IFOUT) == 0)
156 syscall(rel_net_buf(cep->cb_netbuf));
157 cep->cb_netbuf = NULL;
158 }
159 }
160 else {
161 NET_COUNT_UDP(net_count_udp.in_err_packets, 1);
162 NET_COUNT_MIB(udp_stats.udpInErrors, 1);
163 syscall(rel_net_buf(input));
164 }
165
166 }
167
168#endif /* of #if (API_PROTO == API_PROTO_IPV6) || (TNUM_UDP4_CEPID > 0) */
169
170/*
171 * udp_input -- UDP のå…
172¥åŠ›é–¢æ•°
173 */
174
175uint_t
176UDP_INPUT (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp)
177{
178 T_NET_BUF *input = *inputp;
179 T_UDP_CEP *cep;
180 T_UDP_HDR *udph;
181 uint_t hlen, ulen;
182
183#if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4
184 T_UDP6_CEP *cep6;
185#endif
186
187 hlen = (uint_t)GET_IF_IP_HDR_SIZE(input);
188
189 NET_COUNT_UDP(net_count_udp.in_octets, input->len - hlen);
190 NET_COUNT_UDP(net_count_udp.in_packets, 1);
191 NET_COUNT_MIB(udp_stats.udpInDatagrams, 1);
192
193 /* ヘッダ長をチェックする。*/
194 if (input->len < hlen + UDP_HDR_SIZE)
195 goto buf_rel;
196
197 udph = (T_UDP_HDR *)(input->buf + *offp);
198
199 /* データグラム長をチェックする */
200 ulen = ntohs(udph->ulen);
201 if (ulen != input->len - hlen)
202 goto buf_rel;
203
204 /* 宛å…
205ˆãƒãƒ¼ãƒˆãŒ 0 のデータグラムは破棄する。RFC768 */
206 if (udph->dport == 0)
207 goto buf_rel;
208
209#ifdef UDP_CFG_IN_CHECKSUM
210
211 /* チェックサムをチェックする */
212 if (udph->sum && IN_CKSUM(input, IPPROTO_UDP, *offp, ulen) != 0)
213 goto buf_rel;
214
215#endif /* of #ifdef UDP_CFG_IN_CHECKSUM */
216
217#if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4
218
219#if TNUM_UDP4_CEPID > 0
220
221 /* UDP 通信端点を得る。*/
222 cep = udp4_find_cep(input, *offp);
223
224 if (cep != NULL)
225 udp4_input_select(cep, input, *offp);
226 else if ((cep6 = udp6_find_cep(input, *offp)) != NULL)
227 udp6_input_select(cep6, input, *offp);
228 else
229 udp4_reply_no_port(input, *offp);
230
231 return IPPROTO_DONE;
232
233#else /* of #if TNUM_UDP4_CEPID > 0 */
234
235 cep = 0; /* warning 回避用のダミー */
236 if ((cep6 = udp6_find_cep(input, *offp)) != NULL)
237 udp6_input_select(cep6, input, *offp);
238 else
239 udp4_reply_no_port(input, *offp);
240
241 return IPPROTO_DONE;
242
243#endif /* of #if TNUM_UDP4_CEPID > 0 */
244
245#else /* of #if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4 */
246
247 /* UDP 通信端点を得る。*/
248 cep = UDP_FIND_CEP(input, *offp);
249
250 if (cep != NULL)
251 UDP_INPUT_SELECT(cep, input, *offp);
252 else
253 UDP_REPLY_NO_PORT(input, *offp);
254
255 return IPPROTO_DONE;
256
257#endif /* of #if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4 */
258
259buf_rel:
260 NET_COUNT_UDP(net_count_udp.in_err_packets, 1);
261 NET_COUNT_MIB(udp_stats.udpInErrors, 1);
262 syscall(rel_net_buf(input));
263 return IPPROTO_DONE;
264 }
Note: See TracBrowser for help on using the repository browser.