1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
16 *     contributors may be used to endorse or promote products derived from
17 *     this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Portions of this software have been released under the following terms:
31 *
32 * (c) Copyright 1989-1993 OPEN SOFTWARE FOUNDATION, INC.
33 * (c) Copyright 1989-1993 HEWLETT-PACKARD COMPANY
34 * (c) Copyright 1989-1993 DIGITAL EQUIPMENT CORPORATION
35 *
36 * To anyone who acknowledges that this file is provided "AS IS"
37 * without any express or implied warranty:
38 * permission to use, copy, modify, and distribute this file for any
39 * purpose is hereby granted without fee, provided that the above
40 * copyright notices and this notice appears in all source code copies,
41 * and that none of the names of Open Software Foundation, Inc., Hewlett-
42 * Packard Company or Digital Equipment Corporation be used
43 * in advertising or publicity pertaining to distribution of the software
44 * without specific, written prior permission.  Neither Open Software
45 * Foundation, Inc., Hewlett-Packard Company nor Digital
46 * Equipment Corporation makes any representations about the suitability
47 * of this software for any purpose.
48 *
49 * Copyright (c) 2007, Novell, Inc. All rights reserved.
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 *
54 * 1.  Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 * 2.  Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in the
58 *     documentation and/or other materials provided with the distribution.
59 * 3.  Neither the name of Novell Inc. nor the names of its contributors
60 *     may be used to endorse or promote products derived from this
61 *     this software without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 *
74 * @APPLE_LICENSE_HEADER_END@
75 */
76
77/*
78**
79**  NAME
80**
81**      cnassm.h
82**
83**  FACILITY:
84**
85**      Remote Procedure Call (RPC)
86**
87**  ABSTRACT:
88**
89**  Definitions of types/constants internal to the NCA Connection
90**  RPC Protocol Service association state machine.
91**
92**
93*/
94
95#ifndef _CNASSM_H
96#define _CNASSM_H	1
97
98/*
99 * The default fragment size all implementations of the NCA
100 * Connection protocol must be able to receive as defined in the NCA
101 * Architecture spec.
102 */
103#define RPC_C_ASSOC_MUST_RECV_FRAG_SIZE         1432
104
105
106/***********************************************************************/
107/*
108 * R P C _ C N _ A S S O C _ C H E C K _ S T
109 *
110 * This macro will check the given status. If not rpc_s_ok a local
111 * error event will be inserted into the association event list.
112 */
113
114#define RPC_CN_ASSOC_CHECK_ST(assoc, st)\
115{\
116    rpc_cn_sm_event_entry_t     _event;\
117\
118    if (*(st) != rpc_s_ok)\
119    {\
120        (assoc)->assoc_local_status = *(st);\
121        _event.event_id = RPC_C_ASSOC_LOCAL_ERROR;\
122        RPC_CN_ASSOC_INSERT_EVENT ((assoc), &_event);\
123        return (*(st));\
124    }\
125}
126
127
128/***********************************************************************/
129/*
130 * R P C _ C N _ A S S O C _ S M _ T R C
131 */
132#ifdef DEBUG
133#define RPC_CN_ASSOC_SM_TRC(assoc, event_id)\
134{\
135    if ((assoc)->assoc_flags & RPC_C_CN_ASSOC_CLIENT)\
136    {\
137        RPC_DBG_PRINTF (rpc_e_dbg_cn_state, RPC_C_CN_DBG_ASSOC_SM_TRACE, \
138                        ("STATE CLIENT ASSOC: %p state->%s event->%s\n",\
139                         assoc,\
140                         rpc_g_cn_assoc_client_states[(assoc)->assoc_state.cur_state-RPC_C_CN_STATEBASE],\
141                         rpc_g_cn_assoc_client_events[event_id-RPC_C_CN_STATEBASE]));\
142    }\
143    else\
144    {\
145        RPC_DBG_PRINTF (rpc_e_dbg_cn_state, RPC_C_CN_DBG_ASSOC_SM_TRACE, \
146                        ("STATE SERVER ASSOC: %p state->%s event->%s\n",\
147                         assoc,\
148                         rpc_g_cn_assoc_server_states[(assoc)->assoc_state.cur_state-RPC_C_CN_STATEBASE],\
149                         rpc_g_cn_assoc_server_events[event_id-RPC_C_CN_STATEBASE]));\
150    }\
151}
152#else
153#define RPC_CN_ASSOC_SM_TRC(assoc, event_id)
154#endif /* DEBUG */
155
156
157/***********************************************************************/
158/*
159 * R P C _ C N _ A S S O C _ S M _ T R C _ S T A T E
160 */
161#ifdef DEBUG
162#define RPC_CN_ASSOC_SM_TRC_STATE(assoc)\
163{\
164    if ((assoc)->assoc_flags & RPC_C_CN_ASSOC_CLIENT)\
165    {\
166        RPC_DBG_PRINTF (rpc_e_dbg_cn_state, RPC_C_CN_DBG_ASSOC_SM_TRACE, \
167                        ("STATE CLIENT ASSOC: %p new state->%s\n",\
168                         assoc, \
169                         rpc_g_cn_assoc_client_states[(assoc)->assoc_state.cur_state-RPC_C_CN_STATEBASE])); \
170    }\
171    else\
172    {\
173        RPC_DBG_PRINTF (rpc_e_dbg_cn_state, RPC_C_CN_DBG_ASSOC_SM_TRACE, \
174                        ("STATE SERVER ASSOC: %p new state->%s\n",\
175                         assoc, \
176                         rpc_g_cn_assoc_server_states[(assoc)->assoc_state.cur_state-RPC_C_CN_STATEBASE])); \
177    }\
178}
179#else
180#define RPC_CN_ASSOC_SM_TRC_STATE(assoc)
181#endif /* DEBUG */
182
183
184/***********************************************************************/
185/*
186 * R P C _ C N _ A S S O C _ E V A L _ N E T W O R K _ E V E N T
187 *
188 * This macro will be called by the network receiver thread when an
189 * association network event is detected. The "scanned" bit in
190 * the association is turned off. This bit is used in finding
191 * associations to reclaim. The fragbuf is freed if provided as an
192 * event parameter.
193 */
194#define RPC_CN_ASSOC_EVAL_NETWORK_EVENT(assoc, event_id, fragbuf, st)\
195{\
196    RPC_CN_ASSOC_SM_TRC (assoc, event_id);\
197    st = rpc__cn_sm_eval_event ((event_id),\
198                                (dce_pointer_t) (fragbuf),\
199                                (dce_pointer_t) (assoc),\
200                                &((assoc)->assoc_state));\
201    assoc->assoc_flags &= ~RPC_C_CN_ASSOC_SCANNED;\
202    if ((fragbuf) != NULL)\
203    {\
204        (*(fragbuf)->fragbuf_dealloc)((fragbuf));\
205    }\
206    RPC_CN_ASSOC_SM_TRC_STATE (assoc); \
207}
208
209
210/***********************************************************************/
211/*
212 * R P C _ C N _ A S S O C _ E V A L _ U S E R _ E V E N T
213 *
214 * This macro will be called when user level events are detected. If
215 * the association status is bad then don't evaluate the user event.
216 * The "scanned" bit in the association is turned off.
217 */
218#define RPC_CN_ASSOC_EVAL_USER_EVENT(assoc, event_id, event_param, st)\
219{\
220    RPC_CN_ASSOC_SM_TRC (assoc, event_id);\
221    st = assoc->assoc_status;\
222    if (st == rpc_s_ok)\
223    {\
224        st = rpc__cn_sm_eval_event ((event_id),\
225                                    (dce_pointer_t) (event_param),\
226                                    (dce_pointer_t) (assoc),\
227                                    &((assoc)->assoc_state));\
228        assoc->assoc_flags &= ~RPC_C_CN_ASSOC_SCANNED;\
229    }\
230    RPC_CN_ASSOC_SM_TRC_STATE (assoc); \
231}
232
233
234/***********************************************************************/
235/*
236 * R P C _ C N _ A S S O C _ I N S E R T _ E V E N T
237 *
238 * This macro will be called when an event is generated inside an
239 * action routine of the association state machine.
240 */
241#define RPC_CN_ASSOC_INSERT_EVENT(assoc, event)\
242{\
243    RPC_DBG_PRINTF (rpc_e_dbg_cn_state, RPC_C_CN_DBG_ASSOC_SM_TRACE, \
244                    ("STATE INSERT EVENT ")); \
245    RPC_CN_ASSOC_SM_TRC ((assoc), (event)->event_id);\
246    rpc__cn_sm_insert_event ((event),\
247                             &((assoc)->assoc_state));\
248}
249
250
251/***********************************************************************/
252/*
253 * A S S O C   E V E N T S
254 */
255
256/*
257 * Events common to both client and server state machines and a
258 * comment as to who generated them: the user of the association
259 * services or the network.
260 *
261 * Note: local_error is not defined in the architecture. It is an
262 * event indicating a fatal error has occured while processing an
263 * event in the state machine.
264 *
265 * State values are incremented by 100 to distinguish them from
266 * action routine indexes which are all < 100.  This was done as
267 * an efficiency measure to the engine, rpc__cn_sm_eval_event().
268 */
269#define RPC_C_ASSOC_ABORT_REQ  		101  /* user         */
270#define RPC_C_ASSOC_NO_CONN_IND         104  /* network      */
271#define RPC_C_ASSOC_ALLOCATE_REQ        109  /* user         */
272#define RPC_C_ASSOC_DEALLOCATE_REQ      110  /* user         */
273#define RPC_C_ASSOC_LOCAL_ERROR         112  /* internal     */
274#define RPC_C_ASSOC_SHUTDOWN_REQ        111   /* user */
275
276/*
277 * Events only applicable to client state machine
278 *
279 * Note: calls_done is 12 in the architecture. I'm
280 * making it 13 here so local_error will be 12 and therefore the same
281 * as the server local_error event.
282 *
283 * Note: shutdown_ind is 11 in the architecture. I'm
284 * making it 14 here so shutdown_req will be 11 and therefore the same
285 * as the server shutdown_req event.
286 */
287#define RPC_C_ASSOC_REQ		        100  /* user         */
288#define RPC_C_ASSOC_REQUEST_CONN_ACK    102  /* network      */
289#define RPC_C_ASSOC_REQUEST_CONN_NACK   103  /* network      */
290#define RPC_C_ASSOC_ACCEPT_CONF         105  /* network      */
291#define RPC_C_ASSOC_REJECT_CONF         106  /* network      */
292#define RPC_C_ASSOC_ALTER_CONTEXT_REQ   107  /* user         */
293#define RPC_C_ASSOC_ALTER_CONTEXT_CONF  108  /* network      */
294#define RPC_C_ASSOC_CALLS_DONE          113  /* user         */
295#define RPC_C_ASSOC_SHUTDOWN_IND        114  /* network      */
296
297/*
298 * Events only applicable to server state machine
299 */
300/*
301 * Note: alter_context_resp is 4 in the architecture. I'm
302 * making it 5 here so no_conn_ind will be 4 and therefore the same
303 * as the client no_conn_ind event.
304 *
305 * Note: accept_resp is 1 in the architecture. I'm making
306 * it 13 here so abort_req will be 1 and therefore the same as the
307 * client abort_req event.
308 */
309#define RPC_C_ASSOC_IND           	100  /* network      */
310#define RPC_C_ASSOC_REJECT_RESP         102  /* user         */
311#define RPC_C_ASSOC_ALTER_CONTEXT_IND   103  /* network      */
312#define RPC_C_ASSOC_ALTER_CONTEXT_RESP  105  /* user         */
313#define RPC_C_ASSOC_AUTH3_IND           106  /* network      */
314#define RPC_C_ASSOC_AUTH3_ACK           107  /* user         */
315#define RPC_C_ASSOC_AUTH3_NACK          108  /* user         */
316#define RPC_C_ASSOC_ACCEPT_RESP         113   /* user         */
317#define RPC_C_ASSOC_ASSOC_COMPLETE_RESP 114   /* user	      */
318
319
320/***********************************************************************/
321/*
322 * C L I E N T   A S S O C   S T A T E S
323 */
324
325#define	RPC_C_CLIENT_ASSOC_CLOSED	        100
326#define RPC_C_CLIENT_ASSOC_CONNECT_WAIT         101
327#define RPC_C_CLIENT_ASSOC_INIT_WAIT            102
328#define RPC_C_CLIENT_ASSOC_OPEN                 103
329#define RPC_C_CLIENT_ASSOC_ACTIVE               104
330#define RPC_C_CLIENT_ASSOC_CALL_DONE_WAIT       105
331#define RPC_C_CLIENT_ASSOC_STATES	        106
332
333/***********************************************************************/
334/*
335 * C L I E N T   A S S O C   T A B L E S
336 */
337EXTERNAL rpc_cn_sm_state_entry_p_t rpc_g_cn_client_assoc_sm [];
338EXTERNAL rpc_cn_sm_action_fn_t     rpc_g_cn_client_assoc_act_tbl [];
339
340EXTERNAL const char   *rpc_g_cn_assoc_client_events [];
341EXTERNAL const char   *rpc_g_cn_assoc_client_states [];
342
343
344/***********************************************************************/
345/*
346 * S E R V E R   A S S O C   S T A T E S
347 */
348#define RPC_C_SERVER_ASSOC_CLOSED               100
349#define RPC_C_SERVER_ASSOC_REQUESTED            101
350#define RPC_C_SERVER_ASSOC_AUTH3_WAIT           102
351#define RPC_C_SERVER_ASSOC_AUTH3                103
352#define RPC_C_SERVER_ASSOC_OPEN                 104
353#define RPC_C_SERVER_ASSOC_WAIT			105
354#define RPC_C_SERVER_ASSOC_STATES               106
355
356/***********************************************************************/
357/*
358 * S E R V E R   A S S O C   T A B L E S
359 */
360EXTERNAL rpc_cn_sm_state_entry_p_t rpc_g_cn_server_assoc_sm [];
361EXTERNAL rpc_cn_sm_action_fn_t     rpc_g_cn_server_assoc_act_tbl [];
362
363EXTERNAL const char   *rpc_g_cn_assoc_server_events [];
364EXTERNAL const char   *rpc_g_cn_assoc_server_states [];
365
366#endif /* _CNASSM_H */
367