b84bc981118d6d9b18d986a6759cfdcd9f8abc00
[openjpeg.git] / applications / jpip / libopenjpip / sock_manager.c
1 /*
2  * $Id$
3  *
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
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #ifdef _WIN32
32 #include <windows.h>
33 #else
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <arpa/inet.h>
37 #endif
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include "sock_manager.h"
44
45 #ifdef SERVER
46 #include "fcgi_stdio.h"
47 #define logstream FCGI_stdout
48 #else
49 #define FCGI_stdout stdout
50 #define FCGI_stderr stderr
51 #define logstream stderr
52 #endif //SERVER
53
54 SOCKET open_listeningsocket( int port)
55 {
56   SOCKET listening_socket;
57   struct sockaddr_in sin;
58   int sock_optval = 1;
59   
60   listening_socket = socket(AF_INET, SOCK_STREAM, 0);
61   if ( listening_socket == -1 ){
62     perror("socket");
63     exit(1);
64   }
65   
66   if ( setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&sock_optval, sizeof(sock_optval)) == -1 ){
67     perror("setsockopt");
68     exit(1);
69   }
70
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);
75
76   if ( bind(listening_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){
77     perror("bind");
78     close_socket(listening_socket);
79     exit(1);
80   }
81
82   if( listen(listening_socket, SOMAXCONN) == -1){
83     perror("listen");
84     close_socket(listening_socket);
85     exit(1);
86   }
87   fprintf( FCGI_stderr, "port %d is listened\n", port);
88
89   return listening_socket;
90 }
91
92 SOCKET accept_socket( SOCKET listening_socket)
93 {
94   struct sockaddr_in peer_sin;
95   unsigned int addrlen = sizeof(peer_sin);
96
97   return accept( listening_socket, (struct sockaddr *)&peer_sin, &addrlen);
98 }
99
100 void send_stream( SOCKET connected_socket, void *stream, int length)
101 {
102   void *ptr = stream;
103   int remlen = length;
104
105   while( remlen > 0){
106     int sentlen = send( connected_socket, ptr, remlen, 0);
107     if( sentlen == -1){
108       fprintf( FCGI_stderr, "sending stream error\n");
109       break;
110     }
111     remlen = remlen - sentlen;
112     ptr = ptr + sentlen;
113   }
114 }
115
116 void * receive_stream( SOCKET connected_socket, int length)
117 {
118   void *stream, *ptr;
119   int remlen, redlen;
120
121   ptr = stream = malloc( length);
122   remlen = length;
123
124   while( remlen > 0){
125     redlen = recv( connected_socket, ptr, remlen, 0);
126     if( redlen == -1){
127       fprintf( FCGI_stderr, "receive stream error\n");
128       free( stream);
129       stream = NULL;
130       break;
131     }
132     remlen -= redlen;
133     ptr = ptr + redlen;
134   }
135   return stream;
136 }
137
138 int receive_line(SOCKET connected_socket, char *p)
139 {
140   int len = 0;
141   while (1){
142     int ret;
143     ret = recv( connected_socket, p, 1, 0);
144     if ( ret == -1 ){
145       perror("receive");
146       exit(1);
147     } else if ( ret == 0 ){
148       break;
149     }
150     if ( *p == '\n' )
151       break;
152     p++;
153     len++;
154   }
155   *p = '\0';
156
157   if( len == 0)
158     fprintf( FCGI_stderr, "Header receive error\n");
159
160   return len;
161 }
162
163 char * receive_string( SOCKET connected_socket)
164 {
165   char buf[BUF_LEN];
166   
167   receive_line( connected_socket, buf);
168     
169   return strdup(buf);
170 }
171
172 int close_socket( SOCKET sock)
173 {
174 #ifdef _WIN32
175   return closesocket( sock);
176 #else
177   return close( sock);
178 #endif
179 }