4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <arpa/inet.h>
43 #include "sock_manager.h"
46 #include "fcgi_stdio.h"
47 #define logstream FCGI_stdout
49 #define FCGI_stdout stdout
50 #define FCGI_stderr stderr
51 #define logstream stderr
54 SOCKET open_listeningsocket( uint16_t port)
56 SOCKET listening_socket;
57 struct sockaddr_in sin;
60 listening_socket = socket(AF_INET, SOCK_STREAM, 0);
61 if ( listening_socket == -1 ){
66 if ( setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&sock_optval, sizeof(sock_optval)) == -1 ){
71 memset(&sin, 0, sizeof(sin));
72 sin.sin_family = AF_INET;
73 sin.sin_port = htons(port);
74 sin.sin_addr.s_addr = htonl(INADDR_ANY);
76 if ( bind(listening_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){
78 close_socket(listening_socket);
82 if( listen(listening_socket, SOMAXCONN) == -1){
84 close_socket(listening_socket);
87 fprintf( FCGI_stderr, "port %d is listened\n", port);
89 return listening_socket;
92 SOCKET accept_socket( SOCKET listening_socket)
94 struct sockaddr_in peer_sin;
95 unsigned int addrlen = sizeof(peer_sin);
97 return accept( listening_socket, (struct sockaddr *)&peer_sin, &addrlen);
100 void send_stream( SOCKET connected_socket, const void *stream, OPJ_SIZE_T length)
102 char *ptr = (char*)stream;
103 OPJ_SIZE_T remlen = length;
106 ssize_t sentlen = send( connected_socket, ptr, remlen, 0);
108 fprintf( FCGI_stderr, "sending stream error\n");
111 remlen = remlen - (OPJ_SIZE_T)sentlen;
116 void * receive_stream( SOCKET connected_socket, OPJ_SIZE_T length)
121 ptr = stream = malloc( length);
125 ssize_t redlen = recv( connected_socket, ptr, remlen, 0);
127 fprintf( FCGI_stderr, "receive stream error\n");
132 remlen -= (OPJ_SIZE_T)redlen;
138 OPJ_SIZE_T receive_line(SOCKET connected_socket, char *p)
143 ret = recv( connected_socket, p, 1, 0);
147 } else if ( ret == 0 ){
158 fprintf( FCGI_stderr, "Header receive error\n");
163 char * receive_string( SOCKET connected_socket)
167 /* MM FIXME: there is a nasty bug here, size of buf if BUF_LEN which is never
168 indicated to downstream receive_line */
169 receive_line( connected_socket, buf);
174 int close_socket( SOCKET sock)
177 return closesocket( sock);