taskrambler  v0.1.8
Web server and task management solution.
handle_accept.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include <openssl/ssl.h>
28 
29 #include "http/worker.h"
30 #include "server.h"
31 #include "class.h"
32 #include "logger.h"
33 #include "stream.h"
34 
35 int
36 serverHandleAccept(Server this, unsigned int i)
37 {
38  char remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
39  Sock acc = NULL;
40  Stream st;
41 
42  if (this->nfds >= this->max_fds) {
43  return -1;
44  }
45 
46  acc = socketAccept((0 == i)? this->sock : this->sockSSL, &remoteAddr);
47 
48  if (NULL != acc && -1 != acc->handle) {
49  socketNonblock(acc);
50 
51  switch(i) {
52  case 0:
53  // no SSL
54  st = new(Stream, STREAM_FD, acc->handle);
55  break;
56 
57  case 1:
58  // SSL
59  {
60  SSL * ssl = SSL_new(this->ctx);
61  SSL_set_fd(ssl, acc->handle);
62  SSL_accept(ssl);
63  st = new(Stream, STREAM_SSL, ssl);
64  }
65  break;
66 
67  default:
68  st = NULL;
69  break;
70  }
71 
72  // save the socket handle
73  (this->conns)[acc->handle].sock = acc;
74 
75  // clone worker
76  (this->conns)[acc->handle].worker = clone(this->worker);
77  (this->conns)[acc->handle].stream = st;
78 
79  (this->fds)[this->nfds].fd = acc->handle;
80  (this->fds)[this->nfds].events = POLLIN;
81  this->nfds++;
82  } else {
83  delete(acc);
84 
85  switch(errno) {
86  case EAGAIN|EWOULDBLOCK:
87  case EINTR:
88  loggerLog(this->logger,
90  "server accept blocks");
91  return -1;
92  break;
93 
94  default:
95  loggerLog(this->logger,
97  "server accept error");
98  return -2;
99  break;
100  }
101  }
102 
103  if (0 == this->nfds%200) {
104  loggerLog(this->logger,
105  LOGGER_DEBUG, "paralel connections: %lu", this->nfds);
106  }
107 
108  return acc->handle;
109 }
110 
111 // vim: set ts=4 sw=4:
int serverHandleAccept(Server this, unsigned int i)
Definition: handle_accept.c:36
Logger logger
Definition: taskrambler.c:66
Sock socketAccept(Sock this, char(*remoteAddr)[16])
Definition: accept.c:32
void socketNonblock(Sock this)
Definition: nonblock.c:32
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
#define clone(object)
Definition: server.h:39