liblogging: beeplisten.c File Reference

Main Page | Compound List | File List | Compound Members | File Members | Related Pages

# beeplisten.c File Reference

Implementation of the (BEEP) listener Object. More...

`#include <assert.h>`
`#include "config.h"`
`#include "liblogging.h"`
`#include "sockets.h"`
`#include "beepsession.h"`
`#include "beepchannel.h"`
`#include "beepframe.h"`
`#include "beeplisten.h"`
`#include "namevaluetree.h"`
`#include "stringbuf.h"`
`#include "syslogmessage.h"`

## Functions
srRetVal | sbLstnOnFramRcvd (sbLstnObj *pThis, int *pbAbort, sbSessObj *pSess, sbFramObj *pFram)
| Lowest level event handler when a new frame arrived.
srRetVal | sbLstnBuildFrame (sbLstnObj *pThis, sbSessObj *pSess, char c, int *pbAbort)
| This method build a frame by processing one character at a time.
srRetVal | sbLstnDoIncomingData (sbLstnObj *pThis, sbSessObj *pSess)
| Receive all incoming data that is available.
srRetVal | sbSessAddActiveSession (sbLstnObj *pThis, sbSessObj *pSess)
| Add a session to the list of active sessions.
srRetVal | sbLstnSendFram (sbLstnObj *pThis, sbSessObj *pSess)
| Actually send a frame over the socket.
srRetVal | sbLstnServerLoop (sbLstnObj *pThis)
| This is the main server/IO handler.
sbLstnObj * | sbLstnConstruct (void)
| Construct a sbLstnObj.
void | sbLstnDestroy (sbLstnObj *pThis)
| Destructor for the beep listener object.
srRetVal | sbLstnInit (sbLstnObj *pThis)
| Initialize a listener.
srRetVal | sbLstnRun (sbLstnObj *pThis)
| THE listener process.
srRetVal | sbLstnExit (sbLstnObj *pThis)
| Exit the listener.
srRetVal | sbLstnAddProfile (sbLstnObj *pThis, sbProfObj *pProf)

## Detailed Description

Implementation of the (BEEP) listener Object.

Actually, this module implements all the listeners. The initial release did only care about BEEP, but starting with 0.6.0 additional listeners are supported. In 0.6.0 at least a standard UDP listener has been added. Other listeners (eventually not implemented by the time you read this) are the simple syslog over TCP (aka "SELP") and the local /var/log "listener" on *nix.

Please see architecture::c for more details.

Author:
  Rainer Gerhards <rgerhards@adiscon.com>

Date:
  2003-08-13 File initially created.

  2003-09-26 Changed the logical meaning of this module. It is no longer the BEEP listener, only. It is now *the* listener for all supported protocols. This allows us to keep all listeners on a single thread.

Copyright 2002-2003 Rainer Gerhards and Adiscon GmbH. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Adiscon GmbH or Rainer Gerhards nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## Function Documentation

srRetVal sbLstnBuildFrame | ( | sbLstnObj * | pThis,
|  | sbSessObj * | pSess,
|  | char | c,
|  | int * | pbAbort
| ) |

| This method build a frame by processing one character at a time. It operates on a continuous stream of data. It is a state machine. As soon as a full frame is detected, the frame handler is called which then should pass the data to his upper layer - or whatever he intends to do with it. This is not the business of this method. For each new frame, new memory is allocated, so the frame handler (or its upper layers) are responsible for deleting the frame. This method calls itself recursively if it needed to peek at a character and now needs to do the actual processing. Todo: improve!

sbLstnObj* sbLstnConstruct | ( | void |  | ) |

  | Construct a sbLstnObj. This MUST be the first call a server makes. After the server has obtained the sbLstnObj, it can set the parameters and THEN call sbLstnInit(). Return values:
  pointer | to constructed object or NULL, if error occurred.

srRetVal sbLstnDoIncomingData | ( | sbLstnObj * | pThis,
|  | sbSessObj * | pSess
| ) |

| Receive all incoming data that is available. This method reads all available incoming data and processes it. It will call receive state machine to form new frames. That machine, in turn, will fire off Receive events as full frames are completely received. Please note that it is possible for a single run of this method to receive no, one or more full frames. < actual number of bytes received < set by lower layer methods if a fatal error occured. in this case, the session should be aborted and such the full buffer needs not to be processed. < receive buffer - this could be any size, we have selected this size so that an ethernet frame fits. Feel free to tune.

srRetVal sbLstnExit | ( | sbLstnObj * | pThis | ) |

| Exit the listener. Will do all cleanup necessary.

srRetVal sbLstnInit | ( | sbLstnObj * | pThis | ) |

  | Initialize a listener. The listener socket is created, but it is not yet set to listen() to incoming calls. Parameters:
  pThis | {out] Pointer to a pointer to the object. On return, it contains a pointer to a new BEEP listener object or NULL, if an error occured. Consult the return value to learn more in the later case.

srRetVal sbLstnOnFramRcvd | ( | sbLstnObj * | pThis,
|  | int * | pbAbort,
|  | sbSessObj * | pSess,
|  | sbFramObj * | pFram
| ) |

  | Lowest level event handler when a new frame arrived. This handler must first verify if it is a good packet (checks like seqno sequence and thus) and if so, find the profile that it should send data to. SEQ frames are directly processed and channel 0 protocol is mapped to fixed handlers in this object itself. Please note: the message object created here is passed on to the upper layers. However, these upper layers MUST NOT destroy the Mesg Object - this will be done once this method finishes! Parameters:
  pSess | Pointer to the current session object.
  pFram | Pointer to the just received frame.

srRetVal sbLstnRun | ( | sbLstnObj * | pThis | ) |

| THE listener process. Todo: think of a smart way to handle iRet down here... For now, we just make sure BEEP is closed last, as this has the highest potential for errors.

srRetVal sbLstnSendFram | ( | sbLstnObj * | pThis,
|  | sbSessObj * | pSess
| ) |

| Actually send a frame over the socket. If the method would block, we could only send a partial frame. Todo: We need to upgrade either this method (or create another one so that it will send multiple frames if multiple frames are in the send queue. Right now, this is done by the select() loop anyhow, but it may be more efficient to do it that way (or it may be not, this needs some more thinking...). IMPORTANT: When sending SEQ frames, we do not use the "normal" window mechanism. Essentially, these SEQs are acknowledgements, so they are not to be considered as part of the data window. If we would count them, we would stall the process as the peer will abviously never acknowledge acknowledgements... ;) Todo: We need to see if race conditions can occur where pChan becomes invalid but is still stored with the message. It may be a better idea to use the "normal" channel lookup methods, though they are definitely slower... For now, we use an assert and will test this well. again, referencing the channel object as done below is a little dangerous...

srRetVal sbLstnServerLoop | ( | sbLstnObj * | pThis | ) |

| This is the main server/IO handler. This loop is executed until termination is flagged. This is the ONLY method that is responsible for scheduling IO. The loop has three phases: check if data is to be sent and, if so, try to send it wait for socket to become ready for read or write process pending socket reads We have some OS dependant code in here. The reason is that "original" BSD sockets need to have passed the highest numbered file descriptor. We can not properly emulate this in the socket layer. Even if we could, that would potentially cause a considerable performance hit. Thus, we implement it in this module and pass it on to the lower layer. If we run into a non BSD-sockets OS other than Windows, we might be not so happy with this decision, but lets try it... Please note that all send operations are only done in phase 1. Todo: think a little about the performance implications of the below - we may think about either doubly linking the list or having the previous element at hand (e.g. in the linked list object...). log this once we have a logging subsystem think a little about the performance implications of the below - we may think about either doubly linking the list or having the previous element at hand (e.g. in the linked list object...). check & act on return value!!!! (close session? most probably...)