Reduce worker client capacity to 50% in order to allow on each request open a new...
[MonkeyD.git] / src / socket.c
blobc11750a55a62e58454f0318867db5ea4a62d42d5
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2008-2009 Eduardo Silva P.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
33 #include "socket.h"
34 #include "memory.h"
36 /*
37 * Example from:
38 * http://www.baus.net/on-tcp_cork
40 int mk_socket_set_cork_flag(int fd, int state)
42 return setsockopt(fd, SOL_TCP, TCP_CORK, &state, sizeof(state));
45 int mk_socket_set_nonblocking(int sockfd)
47 if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) {
48 perror("fcntl");
49 return -1;
51 return 0;
54 int mk_socket_set_tcp_nodelay(int sockfd)
56 int on=1;
57 return setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &on, sizeof(on));
60 int mk_socket_get_ip(int socket, char *ipv4)
62 int ipv4_len = 16;
63 socklen_t len;
64 struct sockaddr_in m_addr;
66 len = sizeof(m_addr);
67 getpeername(socket, (struct sockaddr*)&m_addr, &len);
68 inet_ntop(PF_INET, &m_addr.sin_addr, ipv4, ipv4_len);
70 return 0;
73 int mk_socket_close(int socket)
75 return close(socket);
78 int mk_socket_timeout(int s, char *buf, int len,
79 int timeout, int recv_send)
81 fd_set fds;
82 time_t init_time, max_time;
83 int n=0, status;
84 struct timeval tv;
86 init_time=time(NULL);
87 max_time = init_time + timeout;
89 FD_ZERO(&fds);
90 FD_SET(s,&fds);
92 tv.tv_sec=timeout;
93 tv.tv_usec=0;
95 if(recv_send==ST_RECV)
96 n=select(s+1,&fds,NULL,NULL,&tv); // recv
97 else{
98 n=select(s+1,NULL,&fds,NULL,&tv); // send
101 switch(n){
102 case 0:
103 return -2;
104 break;
105 case -1:
106 //pthread_kill(pthread_self(), SIGPIPE);
107 return -1;
110 if(recv_send==ST_RECV){
111 status=recv(s,buf,len, 0);
113 else{
114 status=send(s,buf,len, 0);
117 if( status < 0 ){
118 if(time(NULL) >= max_time){
119 //pthread_kill(pthread_self(), SIGPIPE);
123 return status;
126 int mk_socket_create()
128 int sockfd;
130 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
131 perror("client: socket");
132 return -1;
135 return sockfd;
138 int mk_socket_connect(int sockfd, char *server, int port)
140 int res;
141 struct sockaddr_in *remote;
143 remote = (struct sockaddr_in *)
144 mk_mem_malloc_z(sizeof(struct sockaddr_in));
145 remote->sin_family = AF_INET;
147 res = inet_pton(AF_INET, server, (void *)(&(remote->sin_addr.s_addr)));
149 if(res < 0)
151 perror("Can't set remote->sin_addr.s_addr");
152 mk_mem_free(remote);
153 return -1;
155 else if(res == 0){
156 perror("Invalid IP address\n");
157 mk_mem_free(remote);
158 return -1;
161 remote->sin_port = htons(port);
162 if (connect(sockfd,
163 (struct sockaddr *)remote,
164 sizeof(struct sockaddr)) == -1)
166 close(sockfd);
167 perror("connect");
168 return -1;
170 mk_mem_free(remote);
171 return 0;
174 void mk_socket_reset(int socket)
176 int status=1;
178 if(setsockopt(socket,SOL_SOCKET,SO_REUSEADDR,&status,sizeof(int))==-1) {
179 perror("setsockopt");
180 exit(1);
184 /* Just IPv4 for now... */
185 int mk_socket_server(int port)
187 int fd;
188 struct sockaddr_in local_sockaddr_in;
190 /* Create server socket */
191 fd=socket(PF_INET,SOCK_STREAM,0);
192 mk_socket_set_tcp_nodelay(fd);
194 local_sockaddr_in.sin_family=AF_INET;
195 local_sockaddr_in.sin_port=htons(port);
196 local_sockaddr_in.sin_addr.s_addr=INADDR_ANY;
197 memset(&(local_sockaddr_in.sin_zero),'\0',8);
199 /* Avoid bind issues, reset socket */
200 mk_socket_reset(fd);
202 if(bind(fd,(struct sockaddr *)&local_sockaddr_in,
203 sizeof(struct sockaddr)) != 0)
205 perror("bind");
206 printf("Error: Port %i cannot be used\n", port);
207 exit(1);
210 /* Listen queue:
211 * The queue limit is given by /proc/sys/net/core/somaxconn
212 * we need to add a dynamic function to get that value on fly
214 if((listen(fd, 1024))!=0) {
215 perror("listen");
216 exit(1);
219 return fd;