]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/io.c
Validate "ServerName" variable.
[ngircd-alex.git] / src / ngircd / io.c
index b5ec13e2abdea92f681f4e1349615e223bfe48b5..36b854ab10cdc45c72a0d1ee682fcf8fb477268d 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: io.c,v 1.13 2006/05/09 17:02:40 fw Exp $";
+static char UNUSED id[] = "$Id: io.c,v 1.15 2006/07/12 19:27:12 fw Exp $";
 
 #include <assert.h>
 #include <stdlib.h>
@@ -31,7 +31,6 @@ static char UNUSED id[] = "$Id: io.c,v 1.13 2006/05/09 17:02:40 fw Exp $";
 
 typedef struct {
  void (*callback)(int, short);
- int fd;
  short what;
 } io_event;
 
@@ -88,9 +87,12 @@ static io_event *
 io_event_get(int fd)
 {
        io_event *i;
+
        assert(fd >= 0);
-       i = (io_event *) array_get(&io_events, sizeof(io_event), fd);
-       assert(i);
+
+       i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
+
+       assert(i != NULL);
 
        return i;
 }
@@ -114,7 +116,7 @@ io_library_init(unsigned int eventsize)
                eventsize = FD_SETSIZE - 1;
 #endif
 #endif
-       if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
+       if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
                eventsize = 0;
 
 #ifdef IO_USE_EPOLL
@@ -133,7 +135,7 @@ io_library_init(unsigned int eventsize)
        FD_ZERO(&readers);
        FD_ZERO(&writers);
 #ifdef FD_SETSIZE
-       if (Conf_MaxConnections >= FD_SETSIZE) {
+       if (Conf_MaxConnections >= (int)FD_SETSIZE) {
                Log(LOG_WARNING,
                    "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
                    Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
@@ -209,7 +211,7 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
 #endif                         /* FD_SETSIZE */
 #endif                         /* IO_USE_SELECT */
 
-       i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
+       i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
        if (!i) {
                Log(LOG_WARNING,
                    "array_alloc failed: could not allocate space for %d io_event structures",
@@ -217,7 +219,6 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
                return false;
        }
 
-       i->fd = fd;
        i->callback = cbfunc;
        i->what = 0;
 #ifdef IO_USE_EPOLL
@@ -270,7 +271,7 @@ io_event_kqueue_commit_cache(void)
 
        events = array_start(&io_evcache);
 
-       assert(events);
+       assert(events != NULL);
 
        ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
        if (ret)
@@ -311,8 +312,6 @@ io_event_add(int fd, short what)
 {
        io_event *i = io_event_get(fd);
 
-       assert(i != NULL);
-
        if (!i) return false;
        if (i->what == what) return true;
 #ifdef DEBUG_IO
@@ -369,7 +368,7 @@ io_close(int fd)
                while (select_maxfd>0) {
                        --select_maxfd; /* find largest fd */  
                        i = io_event_get(select_maxfd);
-                       if (i && (i->fd >= 0)) break;
+                       if (i && i->callback) break;
                }       
        }       
 #endif
@@ -390,8 +389,8 @@ io_close(int fd)
        io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
 #endif
        if (i) {
-               memset(i, 0, sizeof(io_event));
-               i->fd = -1;
+               i->callback = NULL;
+               i->what = 0;
        }
        return close(fd) == 0;
 }
@@ -404,7 +403,6 @@ io_event_del(int fd, short what)
 #ifdef DEBUG_IO
        Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
 #endif
-       assert(i != NULL);
        if (!i) return false;
 
        i->what &= ~what;
@@ -418,10 +416,10 @@ io_event_del(int fd, short what)
 #endif
 #ifdef IO_USE_SELECT
        if (what & IO_WANTWRITE)
-               FD_CLR(i->fd, &writers);
+               FD_CLR(fd, &writers);
 
        if (what & IO_WANTREAD)
-               FD_CLR(i->fd, &readers);
+               FD_CLR(fd, &readers);
 
        return true;
 #endif
@@ -525,7 +523,7 @@ io_dispatch_kqueue(struct timeval *tv)
                        newevents_len = 0;
 #ifdef DEBUG
                if (newevents_len)
-                       assert(newevents);
+                       assert(newevents != NULL);
 #endif
                ret = kevent(io_masterfd, newevents, newevents_len, kev,
                             100, &ts);
@@ -537,12 +535,21 @@ io_dispatch_kqueue(struct timeval *tv)
                        return total;
 
                for (i = 0; i < ret; i++) {
+                       if (kev[i].flags & EV_EOF) {
+#ifdef DEBUG
+                               LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
+                                       kev[i].filter, kev[i].ident);
+#endif                         
+                               io_docallback((int)kev[i].ident, IO_ERROR);
+                               continue;
+                       }       
+
                        switch (kev[i].filter) {
                                case EVFILT_READ:
-                                       io_docallback(kev[i].ident, IO_WANTREAD);
+                                       io_docallback((int)kev[i].ident, IO_WANTREAD);
                                        break;
                                case EVFILT_WRITE:
-                                       io_docallback(kev[i].ident, IO_WANTWRITE);
+                                       io_docallback((int)kev[i].ident, IO_WANTWRITE);
                                        break;
                                default:
 #ifdef DEBUG
@@ -550,19 +557,9 @@ io_dispatch_kqueue(struct timeval *tv)
                                                kev[i].filter, kev[i].ident); /* Fall through */
 #endif
                                case EV_ERROR:
-                                       io_docallback(kev[i].ident, IO_ERROR);
+                                       io_docallback((int)kev[i].ident, IO_ERROR);
                                        break;
-                                               
                        }
-                       if (kev[i].flags & EV_EOF) {
-#ifdef DEBUG
-                               LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
-                                       kev[i].filter, kev[i].ident); /* Fall through */
-
-#endif                         
-                               io_docallback(kev[i].ident, IO_ERROR);
-
-                       }       
                }
                ts.tv_sec = 0;
                ts.tv_nsec = 0;
@@ -597,9 +594,10 @@ io_docallback(int fd, short what)
        Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
 #endif
        i = io_event_get(fd);
-       assert(i);
 
-       if (i->callback)        /* callback might be 0 if previous callback function called io_close on this fd */
+       if (i->callback) {      /* callback might be NULL if a previous callback function 
+                                  called io_close on this fd */
                i->callback(fd, (what & IO_ERROR) ? i->what : what);
-       /* if error indicator is set, we return the event(s) the app asked for */
+       }       
+       /* if error indicator is set, we return the event(s) that were registered */
 }