xref: /illumos-gate/usr/src/lib/librstp/common/base.h (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1 /************************************************************************
2  * RSTP library - Rapid Spanning Tree (802.1t, 802.1w)
3  * Copyright (C) 2001-2003 Optical Access
4  * Author: Alex Rozin
5  *
6  * This file is part of RSTP library.
7  *
8  * RSTP library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; version 2.1
11  *
12  * RSTP library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with RSTP library; see the file COPYING.  If not, write to the Free
19  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20  * 02111-1307, USA.
21  **********************************************************************/
22 
23 /* Mutual RSTP definitions */
24 
25 #ifndef _STP_BASE_H__
26 #define _STP_BASE_H__
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #define STP_DBG 1
32 
33 #if defined(__LINUX__) || defined(__SUN__)
34 #  include <stddef.h>
35 #  include <stdio.h>
36 #  include <netinet/in.h>
37 #  include "uid_stp.h"
38 #else
39 #  include <psos.h>
40 #  include "comdef.h"
41 #  include "comdef.x"
42 #  include "Bitmap/bitmap.h"
43 #  include "Bitmap/bitmap.x"
44 #  include "Ui/uid_stp.h"
45 #endif
46 
47 #ifndef INOUT
48 #  define IN      /* consider as comments near 'input' parameters */
49 #  define OUT     /* consider as comments near 'output' parameters */
50 #  define INOUT   /* consider as comments near 'input/output' parameters */
51 #endif
52 
53 #ifndef Zero
54 #  define Zero        0
55 #  define One         1
56 #endif
57 
58 #ifndef Bool
59 #  define Bool        int
60 #  define False       0
61 #  define True        1
62 #endif
63 
64 #include "stp_bpdu.h"
65 #include "vector.h"
66 #include "times.h"
67 
68 #define RSTP_ERRORS { \
69   CHOOSE(STP_OK),                                       \
70   CHOOSE(STP_Cannot_Find_Vlan),         \
71   CHOOSE(STP_Implicit_Instance_Create_Failed),          \
72   CHOOSE(STP_Small_Bridge_Priority),                    \
73   CHOOSE(STP_Large_Bridge_Priority),                    \
74   CHOOSE(STP_Small_Hello_Time),                         \
75   CHOOSE(STP_Large_Hello_Time),                         \
76   CHOOSE(STP_Small_Max_Age),                            \
77   CHOOSE(STP_Large_Max_Age),                            \
78   CHOOSE(STP_Small_Forward_Delay),                      \
79   CHOOSE(STP_Large_Forward_Delay),                      \
80   CHOOSE(STP_Forward_Delay_And_Max_Age_Are_Inconsistent),\
81   CHOOSE(STP_Hello_Time_And_Max_Age_Are_Inconsistent),  \
82   CHOOSE(STP_Vlan_Had_Not_Yet_Been_Created),            \
83   CHOOSE(STP_Port_Is_Absent_In_The_Vlan),               \
84   CHOOSE(STP_Big_len8023_Format),                       \
85   CHOOSE(STP_Small_len8023_Format),                     \
86   CHOOSE(STP_len8023_Format_Gt_Len),                    \
87   CHOOSE(STP_Not_Proper_802_3_Packet),                  \
88   CHOOSE(STP_Invalid_Protocol),                         \
89   CHOOSE(STP_Invalid_Version),                          \
90   CHOOSE(STP_Had_Not_Yet_Been_Enabled_On_The_Vlan),     \
91   CHOOSE(STP_Cannot_Create_Instance_For_Vlan),          \
92   CHOOSE(STP_Cannot_Create_Instance_For_Port),          \
93   CHOOSE(STP_Invalid_Bridge_Priority),                  \
94   CHOOSE(STP_There_Are_No_Ports),                       \
95   CHOOSE(STP_Cannot_Compute_Bridge_Prio),               \
96   CHOOSE(STP_Another_Error),                            \
97   CHOOSE(STP_Nothing_To_Do),                            \
98   CHOOSE(STP_No_Such_State_Machine),                    \
99   CHOOSE(STP_LAST_DUMMY)                                \
100 }
101 
102 #define CHOOSE(a) a
103 typedef enum RSTP_ERRORS RSTP_ERRORS_T;
104 #undef CHOOSE
105 
106 #if !defined(__LINUX__) && !defined(__SUN__)
107 extern char* strdup (const char *s);
108 
109 extern USHORT Ntohs (USHORT n);
110 extern ULONG Htonl (ULONG h);
111 extern USHORT Htons (USHORT h);
112 extern ULONG Ntohl (ULONG n);
113 
114 #define htonl Htonl
115 #define htons Htons
116 #define ntohl Ntohl
117 #define ntohs Ntohs
118 
119 #endif
120 
121 #if defined(__LINUX__) || defined(__SUN__)
122 #ifdef STP_DBG
123 #define STP_FATAL(TXT, MSG, EXCOD)                      \
124       {stp_trace ("FATAL:%s failed: %s:%d", TXT, MSG, EXCOD);  \
125       exit (EXCOD);}
126 #else
127 #define STP_FATAL(TXT, MSG, EXCOD)                      \
128       abort();
129 #endif
130 #else
131 #define STP_FATAL(TXT, MSG, EXCOD)                      \
132       printf("FATAL: %s code %s:%d\n", TXT, MSG, EXCOD)
133 #endif
134 
135 #define STP_MALLOC(PTR, TYPE, MSG)              \
136   {                                             \
137     PTR = (TYPE*) calloc (1, sizeof (TYPE));    \
138     if (! PTR) {                                \
139       STP_FATAL("malloc", MSG, -6);             \
140     }                                           \
141   }
142 
143 #define STP_FREE(PTR, MSG)              \
144   {                                     \
145     if (! PTR) {                        \
146       STP_FATAL("free", MSG, -66);      \
147     }                                   \
148     free (PTR);                         \
149     PTR = NULL;                         \
150   }
151 
152 #define STP_STRDUP(PTR, SRC, MSG)       \
153   {                                     \
154     PTR = strdup (SRC);                 \
155     if (! PTR) {                        \
156       STP_FATAL("strdup", MSG, -7);     \
157     }                                   \
158   }
159 
160 #define STP_NEW_IN_LIST(WHAT, TYPE, LIST, MSG)  \
161   {                                             \
162     STP_MALLOC(WHAT, TYPE, MSG);                \
163     WHAT->next = LIST;                          \
164     LIST = WHAT;                                \
165   }
166 
167 /* for debug trace messages */
168 
169 #ifdef STP_DBG
170 #if defined(__LINUX__)
171 extern char* sprint_time_stump (void);
172 #define stp_trace(F, B...) printf("%s:" F "\n", sprint_time_stump(), ##B)
173 #elif defined(__SUN__)
174 #define	stp_trace	(*stp_vectors->trace)
175 #else
176 extern ULONG stp_trace (const char* fmt, ...);
177 #endif
178 #else /* !STP_DBG */
179 #define stp_trace(F, B...) ((void)0)
180 #endif /* STP_DBG */
181 
182 
183 /* Inner usage definitions & functions */
184 
185 #if defined(__LINUX__) || defined(__SUN__)
186 #  define RSTP_INIT_CRITICAL_PATH_PROTECTIO
187 #  define RSTP_CRITICAL_PATH_START
188 #  define RSTP_CRITICAL_PATH_END
189 #else
190 #  define RSTP_INIT_CRITICAL_PATH_PROTECTIO STP_OUT_psos_init_semaphore ()
191 #  define RSTP_CRITICAL_PATH_START          STP_OUT_psos_close_semaphore ()
192 #  define RSTP_CRITICAL_PATH_END            STP_OUT_psos_open_semaphore ()
193    extern void STP_OUT_psos_init_semaphore (void);
194    extern void STP_OUT_psos_close_semaphore (void);
195    extern void STP_OUT_psos_open_semaphore (void);
196 #endif
197 
198 #endif /*  _STP_BASE_H__ */
199