More changes for scan corrections and unit test coverage
[ric-plt/lib/rmr.git] / src / rmr / si / src / si95 / silisten.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 *
24 *  Mnemonic:    SIlistener
25 *  Abstract:    Open a port on which connection requests (TCP) or datagrams (UDP)
26 *                               can be received. SIlistener will open the ipv4/6 port based on
27 *                               the address buffer passed in. The listener() obsoletes SIopen()
28 *                               with regard to opening udp ports.
29 *                               Allows the user to open multiple secondary listening ports
30 *  Parms:       type - TCP_DEVICE or UDP_DEVICE
31 *                               abuf - buffer containing either 0.0.0.0;port or ::1;port
32 *
33 *  Returns:     The file descriptor of the port, <0 if error
34 *  Date:        26 March 1995 -- revised 13 Mar 2007 to support both ipv4 and 6
35 *  Author:      E. Scott Daniels
36 *
37 *  Modified:    10 May 1995 - To change SOCK_RAW to SOCK_DGRAM
38 *                               14 Mar 2007 - To enhance for ipv6
39 ******************************************************************************
40 */
41 #include "sisetup.h"
42 #include "sitransport.h"
43
44 extern int SIlistener( struct ginfo_blk *gptr, int type, char *abuf ) {
45         struct tp_blk *tpptr;                   //  pointer into tp list
46         int status = SI_ERROR;                  //  status of processing
47
48         if( PARANOID_CHECKS ) {
49                 if( gptr == NULL ) {
50                         return status;
51                 }
52                 if( gptr->magicnum != MAGICNUM )                        //  good cookie at the gptr address?
53                         return status;
54         }
55
56         tpptr = SIlisten_prep( type, abuf, 0 );
57
58         if( tpptr != NULL )                          //  established a fd bound to the port ok
59         {                                               //  enable connection reqs
60                 if( type == TCP_DEVICE )
61                 {
62                         if( (status = LISTEN( tpptr->fd, 1 )) < SI_OK )
63                                 return SI_ERROR;
64
65                         tpptr->flags |= TPF_LISTENFD;          //  flag it so we can search it out if needed
66                 }
67
68                 tpptr->next = gptr->tplist;             //  add to the list
69                 if( tpptr->next != NULL )
70                         tpptr->next->prev = tpptr;
71                 gptr->tplist = tpptr;
72                 status = tpptr->fd;                     //  return the fd of the listener
73         }
74
75         return status;
76 }