b297124b8b0ae6c9b365debc38fd8e3e7cf5708e
[ric-plt/lib/rmr.git] / src / rmr / si / src / si95 / siinit.c
1 // vim: noet sw=4 ts=4:
2 /*
3 ==================================================================================
4     Copyright (c) 2020 Nokia
5     Copyright (c) 2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22 **************************************************************************
23 *  Mnemonic: SIinitialise
24 *  Abstract: Initialisation and other context management functions.
25 *          
26 *  Date:     26 March 1995
27 *  Author:   E. Scott Daniels
28 *
29 *  Mod:         17 FEB 2002 - To convert to a globally managed gpointer 
30 *                       09 Mar 2007 - To allow for ipv6 (added SIinitialise() to 
31 *                               replace SIinit())
32 **************************************************************************
33 */
34 #include  "sisetup.h"     //  get the setup stuff 
35
36 /*
37         Initialise the SI environment. Specifically:
38                 allocate the global info block (context)
39
40         Returns a pointer to the block or nil on failure.
41         On failure errno should indicate the problem.
42 */
43 extern struct ginfo_blk* SIinitialise( int opts )
44 {
45         struct ginfo_blk *gptr = NULL;  //  pointer at gen info blk 
46         int     status = SI_OK;                 //  status of internal processing 
47         struct  tp_blk *tpptr;          //  pointer at tp stuff 
48         struct  sigaction sact;                 //  signal action block 
49         int     i;                                                      //  loop index 
50         int     signals = SI_DEF_SIGS;          //  signals to be set in SIsetsig 
51
52         errno = ENOMEM;
53
54         if( (gptr = SInew( GI_BLK )) != NULL ) {                //  make our context
55                 gptr->rbuf = (char *) malloc( MAX_RBUF );   //  get rcv buffer
56                 gptr->rbuflen = MAX_RBUF;
57                 gptr->tp_map = (struct tp_blk **) malloc( sizeof( struct tp_blk *) * MAX_FDS );
58                 if( gptr->tp_map == NULL ) {
59                         fprintf( stderr, "SIinit: unable to initialise tp_map: no memory\n" );
60                         free( gptr );
61                         return NULL;
62                 }
63                 memset( gptr->tp_map, 0, sizeof( struct tp_blk *) * MAX_FDS );
64
65                 gptr->cbtab = (struct callback_blk *) malloc(
66                         (sizeof( struct callback_blk ) * MAX_CBS ) );
67                 if( gptr->cbtab != NULL ) {
68                         for( i = 0; i < MAX_CBS; i++ ) {     //  initialize callback table 
69                                 gptr->cbtab[i].cbdata = NULL;    //  no data and no functions 
70                                 gptr->cbtab[i].cbrtn = NULL;
71                         }
72                 } else {                 //  if call back table allocation failed - error off 
73                         SIshutdown( gptr );  //  clean up any open fds 
74                         free( gptr );
75                         gptr = NULL;       //  dont allow them to continue 
76                 }
77         }                     //  end if gen infor block allocated successfully 
78
79         
80         memset( &sact, 0, sizeof( sact ) );
81         sact.sa_handler = SIG_IGN;
82         sigaction( SIGPIPE, &sact, NULL );              // ignore pipe signals as for some bloody reason linux sets this off if write to closed socket
83
84         return gptr;            //  all's well that ends well 
85
86
87 /*
88         This will set all of the tcp oriented flags in mask (SI_TF_* constants).
89 */
90 extern void SIset_tflags( struct ginfo_blk* gp, int mask )  {
91         if( gp != NULL ) {
92                 gp->tcp_flags |= mask;
93         }
94 }
95
96 /*
97         This will clear all tcp oriented flags set in mask.
98 */
99 extern void SIclr_tflags( struct ginfo_blk* gp, int mask )  {
100         if( gp != NULL ) {
101                 gp->tcp_flags &= ~mask;
102         }
103 }
104
105 /*
106         Dump stats to stderr.
107
108         NOTE:  the receive stats are the number of times that wait popped for
109                 a file descriptor and NOT the actual number of RMR messages which
110                 were contained.  Thus it is VERY likely that the receive count
111                 reported will not match the number of actual messages sent. These
112                 counts should be used only to track activity on a socket.
113 */
114 extern void SItp_stats( void *vgp ) {
115         struct ginfo_blk* gp;
116         struct tp_blk* tp;
117
118         if( (gp = (struct ginfo_blk *) vgp) != NULL ) {
119                 for( tp = gp->tplist; tp != NULL; tp = tp->next ) {
120                         rmr_vlog( RMR_VL_DEBUG, "si95: tp: fd=%d sent=%lld rcvd=%lld qc=%lld\n", tp->fd, tp->sent, tp->rcvd, tp->qcount );
121                 }
122         }
123 }