2 * $Id: imgsock_manager.c 54 2011-05-10 13:22:47Z kaori $
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.
33 #define strcasecmp _stricmp
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <sys/param.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
49 #include "imgsock_manager.h"
53 SOCKET open_listeningsocket()
55 SOCKET listening_socket;
56 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,
67 &sock_optval, sizeof(sock_optval)) == -1 ){
72 memset(&sin, 0, sizeof(sin));
73 sin.sin_family = AF_INET;
74 sin.sin_port = htons(port);
75 sin.sin_addr.s_addr = htonl(INADDR_ANY);
77 if ( bind(listening_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){
79 close_socket(listening_socket);
83 if( listen(listening_socket, SOMAXCONN) == -1){
85 close_socket(listening_socket);
88 printf("port %d is listened\n", port);
90 return listening_socket;
93 SOCKET accept_socket( SOCKET listening_socket)
95 struct sockaddr_in peer_sin;
96 unsigned int addrlen = sizeof(peer_sin);
98 return accept( listening_socket, (struct sockaddr *)&peer_sin, &addrlen);
101 msgtype_t identify_clientmsg( SOCKET connected_socket)
105 char *magicid[] = { "JPIP-stream", "PNM request", "XML request", "TID request", "CID request", "CID destroy", "JP2 save", "QUIT"};
108 receive_size = receive_line( connected_socket, buf);
110 if( receive_size == 0){
111 fprintf( stderr, "Error to receive the header of client message\n");
115 for( i=0; i<NUM_OF_MSGTYPES; i++){
116 if( strncasecmp( magicid[i], buf, strlen(magicid[i])) == 0){
117 printf("%s\n", magicid[i]);
122 fprintf( stderr, "Cannot identify client message type %s\n", buf);
126 Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, int *streamlen)
128 Byte_t *jpipstream=NULL, *ptr;
129 char buf[BUF_LEN], versionstring[] = "version 1.2";
130 int linelen, redlen, remlen;
132 *target = *cid = *tid = NULL;
134 if((linelen = receive_line( connected_socket, buf)) == 0)
136 if( strncmp( versionstring, buf, strlen(versionstring))!=0){
137 fprintf( stderr, "Wrong format\n");
141 if((linelen = receive_line( connected_socket, buf)) == 0)
144 if( strstr( buf, "jp2")){
145 // register cid option
146 *target = strdup( buf);
148 if((linelen = receive_line( connected_socket, buf)) == 0)
150 if( strcmp( buf, "0") != 0)
153 if((linelen = receive_line( connected_socket, buf)) == 0)
155 if( strcmp( buf, "0") != 0)
158 if((linelen = receive_line( connected_socket, buf)) == 0)
162 *streamlen = atoi( buf);
163 fprintf( stderr, "Receive Data: %d Bytes\n", *streamlen);
165 jpipstream = (unsigned char *)malloc( (*streamlen));
167 remlen = (*streamlen);
169 redlen = recv( connected_socket, ptr, remlen, 0);
171 fprintf( stderr, "receive JPT- JPP- stream error\n");
181 void send_stream( SOCKET connected_socket, void *stream, int length);
183 void send_XMLstream( SOCKET connected_socket, Byte_t *xmlstream, int length)
190 header[3] = (length >> 8) & 0xff;
191 header[4] = length & 0xff;
193 send_stream( connected_socket, header, 5);
194 send_stream( connected_socket, xmlstream, length);
197 void send_IDstream( SOCKET connected_socket, char *id, int idlen, char *label);
199 void send_CIDstream( SOCKET connected_socket, char *cid, int cidlen)
201 send_IDstream( connected_socket, cid, cidlen, "CID");
204 void send_TIDstream( SOCKET connected_socket, char *tid, int tidlen)
206 send_IDstream( connected_socket, tid, tidlen, "TID");
209 void send_IDstream( SOCKET connected_socket, char *id, int idlen, char *label)
213 header[0] = label[0];
214 header[1] = label[1];
215 header[2] = label[2];
216 header[3] = idlen & 0xff;
218 send_stream( connected_socket, header, 4);
219 send_stream( connected_socket, id, idlen);
222 void send_PNMstream( SOCKET connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval)
227 pnmlen = width*height*numofcomp;
230 header[1] = numofcomp==3 ? 6:5;
231 header[2] = (width >> 8) & 0xff;
232 header[3] = width & 0xff;
233 header[4] = (height >> 8) & 0xff;
234 header[5] = height & 0xff;
237 send_stream( connected_socket, header, 7);
238 send_stream( connected_socket, pnmstream, pnmlen);
241 void send_stream( SOCKET connected_socket, void *stream, int length)
247 int sentlen = send( connected_socket, ptr, remlen, 0);
249 fprintf( stderr, "sending stream error\n");
252 remlen = remlen - sentlen;
257 int receive_line(SOCKET connected_socket, char *p)
262 ret = recv( connected_socket, p, 1, 0);
266 } else if ( ret == 0 ){
277 fprintf( stderr, "Header receive error\n");
282 char * receive_string( SOCKET connected_socket)
286 receive_line( connected_socket, buf);
291 void response_signal( SOCKET connected_socket, bool succeed)
300 if( send( connected_socket, &code, 1, 0) != 1)
301 fprintf( stderr, "Response signalling error\n");
304 int close_socket( SOCKET sock)
307 return closesocket( sock);