Back to index

opendkim  2.6.4
Classes | Defines | Functions
ar-socket.c File Reference
#include "build-config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include "ar-socket.h"

Go to the source code of this file.

Classes

struct  ar_socket_set

Defines

#define NULL   0
#define FALSE   0
#define TRUE   1
#define MINSETSIZE   16

Functions

AR_SOCKET_SET ar_socket_init (unsigned int initsz)
void ar_socket_free (AR_SOCKET_SET ss)
void ar_socket_reset (AR_SOCKET_SET ss)
int ar_socket_add (AR_SOCKET_SET ss, int fd, unsigned int events)
int ar_socket_check (AR_SOCKET_SET ss, int fd, unsigned int events)
int ar_socket_wait (AR_SOCKET_SET ss, int timeout)

Class Documentation

struct ar_socket_set

Definition at line 43 of file ar-socket.c.

Class Members
int arss_maxfd
fd_set arss_rfds
fd_set arss_wfds
fd_set arss_xfds

Define Documentation

#define FALSE   0

Definition at line 34 of file ar-socket.c.

#define MINSETSIZE   16

Definition at line 40 of file ar-socket.c.

#define NULL   0

Definition at line 31 of file ar-socket.c.

#define TRUE   1

Definition at line 37 of file ar-socket.c.


Function Documentation

int ar_socket_add ( AR_SOCKET_SET  ss,
int  fd,
unsigned int  events 
)

Definition at line 161 of file ar-socket.c.

{
#ifdef POLL
       int c;
#endif /* POLL */

       assert(ss != NULL);
       assert(fd >= 0);

#ifdef POLL
       /* if this one is alerady in the set, update the events bitmask */
       for (c = 0; c < ss->arss_num; c++)
       {
              if (ss->arss_poll[c].fd == fd)
              {
                     if ((events & AR_SOCKET_EVENT_READ) != 0)
                            ss->arss_poll[c].events |= POLLIN;
                     if ((events & AR_SOCKET_EVENT_WRITE) != 0)
                            ss->arss_poll[c].events |= POLLOUT;
                     if ((events & AR_SOCKET_EVENT_EXCEPTION) != 0)
                            ss->arss_poll[c].events |= (POLLERR|POLLHUP|POLLNVAL);

                     return 0;
              }
       }

       /* adding; resize poll array if needed */
       if (ss->arss_alloc == ss->arss_num)
       {
              unsigned int new;
              struct pollfd *newp;

              new = ss->arss_alloc * 2;
              newp = (struct pollfd *) realloc(ss->arss_poll,
                                               new * sizeof(struct pollfd));
              if (newp == NULL)
                     return -1;

              ss->arss_alloc = new;
              ss->arss_poll = newp;
       }

       ss->arss_poll[ss->arss_num].fd = fd;
       ss->arss_poll[ss->arss_num].events = 0;
       ss->arss_poll[ss->arss_num].revents = 0;
       if ((events & AR_SOCKET_EVENT_READ) != 0)
              ss->arss_poll[ss->arss_num].events |= POLLIN;
       if ((events & AR_SOCKET_EVENT_WRITE) != 0)
              ss->arss_poll[ss->arss_num].events |= POLLOUT;
       if ((events & AR_SOCKET_EVENT_EXCEPTION) != 0)
              ss->arss_poll[ss->arss_num].events |= (POLLERR|POLLHUP|POLLNVAL);
       ss->arss_num++;

       return 0;
#else /* POLL */
       if (fd >= FD_SETSIZE)
       {
              errno = EINVAL;
              return -1;
       }

       if ((events & AR_SOCKET_EVENT_READ) != 0)
              FD_SET(fd, &ss->arss_rfds);
       if ((events & AR_SOCKET_EVENT_WRITE) != 0)
              FD_SET(fd, &ss->arss_wfds);
       if ((events & AR_SOCKET_EVENT_EXCEPTION) != 0)
              FD_SET(fd, &ss->arss_xfds);

       if (fd > ss->arss_maxfd)
              ss->arss_maxfd = fd;

       return 0;
#endif /* POLL */
}

Here is the caller graph for this function:

int ar_socket_check ( AR_SOCKET_SET  ss,
int  fd,
unsigned int  events 
)

Definition at line 251 of file ar-socket.c.

{
#ifdef POLL
       unsigned int c;
#else /* POLL */
       int ret;
#endif /* POLL */

       assert(ss != NULL);
       assert(fd >= 0);

#ifdef POLL
       for (c = 0; c < ss->arss_num; c++)
       {
              if (ss->arss_poll[c].fd == fd)
              {
                     /* read */
                     if ((events & AR_SOCKET_EVENT_READ) != 0 &&
                         (ss->arss_poll[c].revents & POLLIN) != 0)
                            return 1;

                     /* write */
                     if ((events & AR_SOCKET_EVENT_WRITE) != 0 &&
                         (ss->arss_poll[c].revents & POLLOUT) != 0)
                            return 1;

                     /* exception */
                     if ((events & AR_SOCKET_EVENT_EXCEPTION) != 0 &&
                         (ss->arss_poll[c].revents & (POLLERR|POLLHUP|POLLNVAL)) != 0)
                            return 1;

                     return 0;
              }
       }

       return 0;
#else /* POLL */
       if (fd >= FD_SETSIZE)
       {
              errno = EINVAL;
              return -1;
       }

       ret = 0;
       if ((events & AR_SOCKET_EVENT_READ) != 0 &&
           FD_ISSET(fd, &ss->arss_rfds))
              ret = 1;
       if ((events & AR_SOCKET_EVENT_WRITE) != 0 &&
           FD_ISSET(fd, &ss->arss_wfds))
              ret = 1;
       if ((events & AR_SOCKET_EVENT_EXCEPTION) != 0 &&
           FD_ISSET(fd, &ss->arss_xfds))
              ret = 1;

       return ret;
#endif /* POLL */
}

Here is the caller graph for this function:

Definition at line 110 of file ar-socket.c.

{
       assert(ss != NULL);

#ifdef POLL
       if (ss->arss_poll != NULL)
              free(ss->arss_poll);
#endif /* POLL */

       free(ss);
}

Here is the caller graph for this function:

AR_SOCKET_SET ar_socket_init ( unsigned int  initsz)

Definition at line 68 of file ar-socket.c.

{
       struct ar_socket_set *new;

       new = (AR_SOCKET_SET) malloc(sizeof *new);
       if (new == NULL)
              return NULL;

       if (initsz < MINSETSIZE)
              initsz = MINSETSIZE;

#ifdef POLL
       new->arss_poll = (struct pollfd *) malloc(sizeof(struct pollfd) * initsz);
       if (new->arss_poll == NULL)
       {
              free(new);
              return NULL;
       }

       new->arss_num = 0;
       new->arss_alloc = initsz;
#else /* POLL */
       new->arss_maxfd = -1;
       FD_ZERO(&new->arss_rfds);
       FD_ZERO(&new->arss_wfds);
       FD_ZERO(&new->arss_xfds);
#endif /* POLL */

       return new;
}

Here is the caller graph for this function:

Definition at line 133 of file ar-socket.c.

{
       assert(ss != NULL);

#ifdef POLL
       ss->arss_num = 0;
#else /* POLL */
       ss->arss_maxfd = -1;
       FD_ZERO(&ss->arss_rfds);
       FD_ZERO(&ss->arss_wfds);
       FD_ZERO(&ss->arss_xfds);
#endif /* POLL */
}

Here is the caller graph for this function:

int ar_socket_wait ( AR_SOCKET_SET  ss,
int  timeout 
)

Definition at line 321 of file ar-socket.c.

{
#ifndef POLL
       struct timeval to;
#endif /* POLL */

       assert(ss != NULL);

#ifdef POLL
       return poll(ss->arss_poll, ss->arss_num, timeout);
#else /* POLL */
       if (timeout != -1)
       {
              to.tv_sec = timeout / 1000;
              to.tv_usec = (timeout % 1000) * 1000;
       }

       return select(ss->arss_maxfd + 1,
                     &ss->arss_rfds,
                     &ss->arss_wfds,
                     &ss->arss_xfds,
                     timeout == -1 ? NULL : &to);
#endif /* POLL */
}

Here is the caller graph for this function: