FreeCalypso > hg > fc-selenite
comparison src/g23m-fad/tcpip/rnet/rnet_rt/rnet_rt_api_new.c @ 1:d393cd9bb723
src/g23m-*: initial import from Magnetite
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 15 Jul 2018 04:40:46 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:b6a5e36de839 | 1:d393cd9bb723 |
---|---|
1 /** | |
2 * @file rnet_rt_api_new.c | |
3 * | |
4 * RNET_RT API | |
5 * | |
6 * @author Regis Feneon | |
7 * @version 0.1 | |
8 */ | |
9 | |
10 /* | |
11 * $Id: rnet_rt_api_new.c,v 1.4 2002/10/30 15:23:34 rf Exp $ | |
12 * $Name: ti_20021030 $ | |
13 * | |
14 * History: | |
15 * | |
16 * Date Author Modification | |
17 * -------------------------------------------------- | |
18 * 3/22/2002 Regis Feneon Create | |
19 * 6/24/2002 Regis Feneon RNET_SEND_CFM renamed RNET_SEND_RDY | |
20 * send this message when a connection | |
21 * is accepted | |
22 * (C) Copyright 2002 by TI, All Rights Reserved | |
23 * | |
24 */ | |
25 | |
26 #include "rnet_cfg.h" | |
27 #ifdef RNET_CFG_REAL_TRANSPORT | |
28 | |
29 #include "rnet_rt_i.h" | |
30 #include "rnet_rt_env.h" | |
31 #include "rnet_message.h" | |
32 | |
33 | |
34 /* | |
35 * Send a message back to the user using socket's return path | |
36 */ | |
37 static int socket_send_msg( NGsock *so, T_RV_HDR *msg) | |
38 { | |
39 if( ((T_RNET_RT_SOCK *) so)->return_path.callback_func != NULL) { | |
40 /* Send the message using callback function. */ | |
41 ((T_RNET_RT_SOCK *) so)->return_path.callback_func( msg); | |
42 return( RV_OK); | |
43 } | |
44 else { | |
45 /* Send the message using mailbox. */ | |
46 return( rvf_send_msg( ((T_RNET_RT_SOCK *) so)->return_path.addr_id, msg)); | |
47 } | |
48 } | |
49 | |
50 NGsock *cach_so = NULL ; | |
51 | |
52 //pinghua add two function to avoid abormal sock close. OMAPS00173156 patch fix 05122008 | |
53 void cach_sock_message(NGsock *so) | |
54 { | |
55 | |
56 cach_so = so ; | |
57 return ; | |
58 | |
59 } | |
60 | |
61 void Send_cach_message(void ) | |
62 { | |
63 | |
64 union { | |
65 T_RNET_ERROR_IND eind; | |
66 T_RNET_CONNECT_IND cind; | |
67 T_RNET_CONNECT_CFM ccfm; | |
68 T_RNET_SEND_RDY srdy; | |
69 T_RNET_RECV_IND rind; | |
70 } *msg; | |
71 | |
72 if(cach_so==NULL) | |
73 return ; | |
74 if(cach_so->so_rcv_cc!=0) | |
75 return ; | |
76 | |
77 RNET_RT_SEND_TRACE("RNET_RT: Send_cach_message close! ",RV_TRACE_LEVEL_ERROR); | |
78 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_ERROR_IND), | |
79 RNET_ERROR_IND, (T_RV_HDR **) &msg) == RVF_RED) { | |
80 return ; | |
81 } | |
82 msg->eind.desc = (T_RNET_DESC *) cach_so; | |
83 msg->eind.error = RNET_CONN_CLOSED; | |
84 socket_send_msg( cach_so, (T_RV_HDR *) msg); | |
85 ((T_RNET_RT_SOCK *) cach_so)->flags &= ~RNET_RT_SOCKF_NOTIFY_CLOSED; | |
86 | |
87 cach_so= NULL; | |
88 | |
89 } | |
90 // end | |
91 /* | |
92 * Socket callback function. | |
93 * The socket events are translated to RNET messages and sent the application. | |
94 * | |
95 */ | |
96 static void socket_callback( NGsock *so, void *data, int flags) | |
97 { | |
98 NGsock *nso; | |
99 NGsockaddr addr; | |
100 NGbuf *bufp; | |
101 char tmp[25]; | |
102 union { | |
103 T_RNET_ERROR_IND eind; | |
104 T_RNET_CONNECT_IND cind; | |
105 T_RNET_CONNECT_CFM ccfm; | |
106 T_RNET_SEND_RDY srdy; | |
107 T_RNET_RECV_IND rind; | |
108 } *msg; | |
109 | |
110 | |
111 sprintf(tmp,"recv data len %d", so->so_rcv_cc); | |
112 RNET_RT_SEND_TRACE(tmp,RV_TRACE_LEVEL_ERROR); | |
113 | |
114 msg = NULL; | |
115 if( so->so_error != 0) { | |
116 /* send RNET_ERROR_IND msg */ | |
117 RNET_RT_SEND_TRACE("RNET_RT: RNET_ERROR_IND ",RV_TRACE_LEVEL_ERROR); | |
118 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_ERROR_IND), | |
119 RNET_ERROR_IND, (T_RV_HDR **) &msg) == RVF_RED) { | |
120 goto getbuf_error; | |
121 } | |
122 msg->eind.desc = (T_RNET_DESC *) so; | |
123 msg->eind.error = rnet_rt_ngip_error( so->so_error); | |
124 socket_send_msg( so, (T_RV_HDR *) msg); | |
125 so->so_error = 0; | |
126 return; | |
127 } | |
128 | |
129 if( flags & NG_SAIO_READ) { | |
130 if( so->so_state & NG_SS_CANTRCVMORE) { | |
131 /* connection has been closed/aborted */ | |
132 //pinghua add one condiction to avoid close abnormal 20080505 | |
133 | |
134 if( ((T_RNET_RT_SOCK *) so)->flags & RNET_RT_SOCKF_NOTIFY_CLOSED && ( so->so_rcv_cc==0 ) ) { | |
135 /* connection is closed */ | |
136 /* send RNET_ERROR_IND/RNET_CONN_CLOSED msg */ | |
137 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_ERROR_IND), | |
138 RNET_ERROR_IND, (T_RV_HDR **) &msg) == RVF_RED) { | |
139 goto getbuf_error; | |
140 } | |
141 msg->eind.desc = (T_RNET_DESC *) so; | |
142 msg->eind.error = RNET_CONN_CLOSED; | |
143 socket_send_msg( so, (T_RV_HDR *) msg); | |
144 ((T_RNET_RT_SOCK *) so)->flags &= ~RNET_RT_SOCKF_NOTIFY_CLOSED; | |
145 } | |
146 else { //buffer this close FIN messgae | |
147 | |
148 cach_sock_message(so); | |
149 | |
150 } | |
151 | |
152 | |
153 } | |
154 else if( so->so_options & NG_SO_ACCEPTCONN) { | |
155 | |
156 | |
157 /* accept a new connection */ | |
158 if( ngSAIOAccept( so, &addr, 0, &nso) == NG_EOK) { | |
159 /* initialise rnet part of socket */ | |
160 ((T_RNET_RT_SOCK *) nso)->return_path = ((T_RNET_RT_SOCK *) so)->return_path; | |
161 ((T_RNET_RT_SOCK *) nso)->flags = | |
162 RNET_RT_SOCKF_NOTIFY_RECV|RNET_RT_SOCKF_NOTIFY_CLOSED; | |
163 ((T_RNET_RT_SOCK *) nso)->user_data = NULL; | |
164 /* install callback */ | |
165 ngSAIOSetCallback( nso, socket_callback, NULL); | |
166 /* send RNET_CONNECT_IND msg */ | |
167 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_CONNECT_IND), | |
168 RNET_CONNECT_IND, (T_RV_HDR **) &msg) == RVF_RED) { | |
169 ngSAIOClose( nso, 0); | |
170 goto getbuf_error; | |
171 } | |
172 msg->cind.new_desc = (T_RNET_DESC *) nso; | |
173 msg->cind.listen_desc = (T_RNET_DESC *) so; | |
174 msg->cind.peer_addr = ngNTOHL( addr.sin_addr); | |
175 msg->cind.peer_port = ngNTOHS( addr.sin_port); | |
176 socket_send_msg( so, (T_RV_HDR *) msg); | |
177 /* send first RNET_SEND_RDY message */ | |
178 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_SEND_RDY), | |
179 RNET_SEND_RDY, (T_RV_HDR **) &msg) == RVF_RED) { | |
180 goto getbuf_error; | |
181 } | |
182 msg->srdy.desc = (T_RNET_DESC *) nso; | |
183 socket_send_msg( so, (T_RV_HDR *) msg); | |
184 } | |
185 return; | |
186 } | |
187 else { | |
188 if( ((T_RNET_RT_SOCK *) so)->flags & RNET_RT_SOCKF_NOTIFY_CONNECT) { | |
189 /* active connection is done */ | |
190 /* send RNET_CONNECT_CFM msg */ | |
191 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_CONNECT_CFM), | |
192 RNET_CONNECT_CFM, (T_RV_HDR **) &msg) == RVF_RED) { | |
193 goto getbuf_error; | |
194 } | |
195 msg->ccfm.desc = (T_RNET_DESC *) so; | |
196 socket_send_msg( so, (T_RV_HDR *) msg); | |
197 ((T_RNET_RT_SOCK *) so)->flags &= ~RNET_RT_SOCKF_NOTIFY_CONNECT; | |
198 ((T_RNET_RT_SOCK *) so)->flags |= RNET_RT_SOCKF_NOTIFY_CLOSED; | |
199 ((T_RNET_RT_SOCK *) so)->flags |= RNET_RT_SOCKF_NOTIFY_RECV ; | |
200 } | |
201 if( (((T_RNET_RT_SOCK *) so)->flags & RNET_RT_SOCKF_NOTIFY_RECV) && | |
202 (so->so_rcv_cc >= so->so_rcv_lowat)) { | |
203 /* data has been received */ | |
204 /* send RNET_RECV_IND msg */ | |
205 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_RECV_IND), | |
206 RNET_RECV_IND, (T_RV_HDR **) &msg) == RVF_RED) { | |
207 goto getbuf_error; | |
208 } | |
209 msg->rind.desc = (T_RNET_DESC *) so; | |
210 /* fillin sender's address */ | |
211 if( so->so_state & NG_SS_ISCONNECTED) { | |
212 /* socket is connected, get peer address in socket control block */ | |
213 msg->rind.peer_addr = ngNTOHL( so->so_faddr); | |
214 msg->rind.peer_port = ngNTOHS( so->so_fport); | |
215 } | |
216 else if( (so->so_proto->pr_flags & NG_PR_ADDR) && | |
217 ((bufp = (NGbuf *) so->so_rcv_q.qu_tail) != NULL)) { | |
218 /* get address from last queued buffer */ | |
219 ngMemCpy( &addr, bufp->buf_iov, sizeof( NGsockaddr)); | |
220 msg->rind.peer_addr = ngNTOHL( addr.sin_addr); | |
221 msg->rind.peer_port = ngNTOHS( addr.sin_port); | |
222 } | |
223 else { | |
224 /* cannot get address, this should not happen... */ | |
225 msg->rind.peer_addr = 0; | |
226 msg->rind.peer_port = 0; | |
227 } | |
228 socket_send_msg( so, (T_RV_HDR *) msg); | |
229 ((T_RNET_RT_SOCK *) so)->flags &= ~RNET_RT_SOCKF_NOTIFY_RECV; | |
230 } | |
231 } | |
232 } | |
233 | |
234 | |
235 if( flags & NG_SAIO_WRITE) { | |
236 if( !(so->so_state & NG_SS_CANTSENDMORE) && | |
237 (((T_RNET_RT_SOCK *) so)->flags & RNET_RT_SOCKF_NOTIFY_SEND) && | |
238 ((so->so_snd_hiwat - so->so_snd_cc) >= so->so_snd_lowat)) { | |
239 /* send data has been acknowledged by peer */ | |
240 /* send RNET_SEND_RDY msg */ | |
241 if( rvf_get_msg_buf( rnet_rt_env_ctrl_blk_p->mb_id, sizeof(T_RNET_SEND_RDY), | |
242 RNET_SEND_RDY, (T_RV_HDR **) &msg) == RVF_RED) { | |
243 goto getbuf_error; | |
244 } | |
245 msg->srdy.desc = (T_RNET_DESC *) so; | |
246 socket_send_msg( so, (T_RV_HDR *) msg); | |
247 ((T_RNET_RT_SOCK *) so)->flags &= ~RNET_RT_SOCKF_NOTIFY_SEND; | |
248 } | |
249 } | |
250 return; | |
251 | |
252 getbuf_error: | |
253 RNET_RT_SEND_TRACE("RNET_RT: cannot allocate MSG ",RV_TRACE_LEVEL_ERROR); | |
254 return; | |
255 } | |
256 | |
257 /** | |
258 * Creates a new connection identifier (T_RNET_DESC). | |
259 * | |
260 * The connection ID is not active until it has either been bound | |
261 * to a local address or connected to a remote address. | |
262 * | |
263 * @param proto Protocol that should be used [IN]. | |
264 * @param desc Connection identifier created [OUT]. | |
265 * @param return_path Return path that should be used to send | |
266 * the messages like accept, connect, etc [IN]. | |
267 * @return RNET_MEMORY_ERR No available memory to create the new connection id. | |
268 * RNET_NOT_INITIALIZED NET subsystem not initialized (internal error). | |
269 * RNET_INTERNAL_ERR Network subsystem failed. | |
270 * or No more socket descriptors available. | |
271 * RNET_NOT_READY Still processing a callback function. | |
272 * RNET_NOT_SUPPORTED Specified protocol not supported. | |
273 * RNET_OK Connection ID successfully created. | |
274 */ | |
275 | |
276 T_RNET_RET rnet_rt_new (T_RNET_IPPROTO proto, | |
277 T_RNET_DESC ** desc, | |
278 T_RV_RETURN_PATH return_path) | |
279 { | |
280 NGsock *so; | |
281 int type, err; | |
282 | |
283 /* check if SW entity has been initialised */ | |
284 if( rnet_rt_env_ctrl_blk_p == NULL) { | |
285 return( RNET_NOT_INITIALIZED); | |
286 } | |
287 | |
288 /* select type of socket, only TCP and UDP allowed */ | |
289 switch( proto) { | |
290 case RNET_IPPROTO_TCP: | |
291 type = NG_SOCK_STREAM; | |
292 break; | |
293 case RNET_IPPROTO_UDP: | |
294 type = NG_SOCK_DGRAM; | |
295 break; | |
296 default: | |
297 /* invalid protocol */ | |
298 /* could create raw IP socket... */ | |
299 return( RNET_NOT_SUPPORTED); | |
300 } | |
301 | |
302 /* create ngip socket */ | |
303 | |
304 rvf_lock_mutex( &rnet_rt_env_ctrl_blk_p->mutex); | |
305 err = ngSAIOCreate( &so, NG_AF_INET, type, 0, NG_O_NONBLOCK); | |
306 | |
307 if( err == NG_EOK) { | |
308 /* initialise rnet part of socket */ | |
309 ((T_RNET_RT_SOCK *) so)->return_path = return_path; | |
310 #if 0 /* RNET_RT bug!? [ni 2003-12-05 */ | |
311 ((T_RNET_RT_SOCK *) so)->flags = RNET_RT_SOCKF_NOTIFY_RECV | | |
312 RNET_RT_SOCKF_NOTIFY_SEND; | |
313 #else | |
314 if (proto == RNET_IPPROTO_UDP) { | |
315 /* A UDP socket is ready for communication right after it has been | |
316 * created, ... */ | |
317 ((T_RNET_RT_SOCK *) so)->flags = | |
318 RNET_RT_SOCKF_NOTIFY_RECV | RNET_RT_SOCKF_NOTIFY_SEND; | |
319 } else { | |
320 /* ... whereas a TCP socket has to be connected first. */ | |
321 ((T_RNET_RT_SOCK *) so)->flags = 0 ; | |
322 } | |
323 #endif /* 0 */ | |
324 ((T_RNET_RT_SOCK *) so)->user_data = NULL; | |
325 | |
326 | |
327 /* install callback */ | |
328 ngSAIOSetCallback( so, socket_callback, NULL); | |
329 | |
330 | |
331 } | |
332 rvf_unlock_mutex( &rnet_rt_env_ctrl_blk_p->mutex); | |
333 | |
334 /* returns new socket */ | |
335 if( err == NG_EOK) { | |
336 *desc = (T_RNET_DESC *) so; | |
337 return( RNET_OK); | |
338 } | |
339 | |
340 /* convert error code */ | |
341 return( rnet_rt_ngip_error( err)); | |
342 } | |
343 | |
344 #endif /* ifdef RNET_CFG_REAL_TRANSPORT */ | |
345 |