summaryrefslogtreecommitdiffstats
path: root/nanohttp/nanohttp-socket.c
blob: 1a9742bd7b8d8e2579258ac72f8fc69106989ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/******************************************************************
 *  $Id: nanohttp-socket.c,v 1.1 2003/12/11 14:51:04 snowdrop Exp $
 *
 * CSOAP Project:  A http client/server library in C
 * Copyright (C) 2003  Ferhat Ayaz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * 
 * Email: ayaz@jprogrammer.net
 ******************************************************************/
#include <nanohttp/nanohttp-socket.h>
#include <nanohttp/nanohttp-common.h>

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif 

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <stdio.h>


/*--------------------------------------------------
  FUNCTION: hsocket_module_init
----------------------------------------------------*/
int hsocket_module_init()
{
  /* nothing to init for unix sockets */
  return 0;
}

/*--------------------------------------------------
  FUNCTION: hsocket_module_destroy
----------------------------------------------------*/
void hsocket_module_destroy()
{
  /* nothing to destroy for unix sockets */
}


/*--------------------------------------------------
  FUNCTION: hsocket_init
----------------------------------------------------*/
int hsocket_init(hsocket_t *sock)
{
  /* nothing to init for unix sockets */
  /* just set the descriptor to -1 */
  *sock = -1;
  return 0;
}


/*--------------------------------------------------
  FUNCTION: hsocket_free
----------------------------------------------------*/
void hsocket_free(hsocket_t sock)
{
  /* nothing to free for unix sockets */
}


/*--------------------------------------------------
  FUNCTION: hsocket_open
----------------------------------------------------*/
int hsocket_open(hsocket_t *dsock, const char* hostname, int port)
{
  int sock;
  struct sockaddr_in address; 
  struct hostent* host;

  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock <= 0) return HSOCKET_CAN_NOT_CREATE_SOCKET;

  /* Get host data */
  host = gethostbyname(hostname);
  if (host == NULL) return HSOCKET_CAN_NOT_GET_HOSTNAME;

  /* set server addresss */
  address.sin_family = host->h_addrtype;
  address.sin_port = htons(port);
  memcpy((char*)&address.sin_addr.s_addr,
	 host->h_addr_list[0], host->h_length);

  /* connect to the server */
  if (connect(sock, (struct sockaddr*) &address, sizeof(address)) != 0)
    return HSOCKET_CAN_NOT_CONNECT;

  *dsock = sock;
  return HSOCKET_OK;
}


/*--------------------------------------------------
  FUNCTION: hsocket_close
----------------------------------------------------*/
void hsocket_close(hsocket_t sock)
{
  close(sock);
}


/*--------------------------------------------------
  FUNCTION: hsocket_send
----------------------------------------------------*/
int hsocket_send(hsocket_t sock, const char* buffer)
{
  int size;

  size = send((int)sock, buffer, strlen(buffer), 0);
  if (size == -1)
    return HSOCKET_CAN_NOT_SEND;

  return HSOCKET_OK;
}


/*--------------------------------------------------
  FUNCTION: hsocket_recv_limit (DON'T USE!)
----------------------------------------------------*/
int hsocket_recv_limit(hsocket_t sock, char** buffer, 
		       const char* delim, char **rest, 
		       int *totalBuffer, int *totalRest)
{
  ssize_t size;
  int chunk=1;
  char tmp[HSOCKET_MAX_BUFSIZE+1];
  int delimlen;
  int fsize;
  int i;

  *totalBuffer = 0;
  *totalRest = 0;
  delimlen = strlen(delim);

  /* calculate first size for realloc */
  if (*buffer) {
    fsize = strlen(*buffer);
  } else {
    fsize = 0;
  }

  do {
    
    size = recv(sock, tmp, HSOCKET_MAX_BUFSIZE, 0);

    if (size == -1) {
      log_error1("Error reading from socket\n");
      return HSOCKET_CAN_NOT_RECEIVE;
    }

    if (size == 0) {
      break;
    }

    /* section 1: find delimiter if exist */
    for (i=0;i<size-delimlen;i++) {
      if (!strncmp(&tmp[i],delim,delimlen)) {
	
	/* ** split to buffer and rest ** */
	
	/* fill buffer until i */
	*totalBuffer += i;
	if (*buffer) {
	  *buffer = (char*)realloc(*buffer, *totalBuffer+fsize+1+HSOCKET_MAX_BUFSIZE);
	  strncat(*buffer, tmp, i);
	  *buffer[*totalBuffer+fsize] = '\0';
	} else {
	  *buffer = (char*)realloc(NULL, *totalBuffer+1);
	  strncpy(*buffer, tmp, i);
	  (*buffer)[*totalBuffer] = '\0';
	}


	/* fill rest from i to size */
	if (size - (i+delimlen)+1 > 0) {
	  *rest = (char*)realloc(NULL, size - (i+delimlen)+1 + HSOCKET_MAX_BUFSIZE);
	  strcpy(*rest, &tmp[i+delimlen]);
	  *totalRest = size - (i+delimlen);
	  (*rest)[*totalRest] = '\0';
	} else {
	  *rest = NULL;
	}

	return HSOCKET_OK;
      }
    }
    
    /* section 2: no delimiter found. so add tmp to buffer */
    *totalBuffer += size;
    if (*buffer) {
      *buffer = (char*)realloc(*buffer, *totalBuffer+fsize+1);
      strcat(*buffer, tmp);
    } else {
      *buffer = (char*)realloc(NULL, *totalBuffer+1);
      strcpy(*buffer, tmp);
    }

    (*buffer)[*totalBuffer] = '\0';	
    
    chunk++;
  } while (size > 0);

  return HSOCKET_OK;  
}

/*--------------------------------------------------
  FUNCTION: hsocket_recv
----------------------------------------------------*/
int hsocket_recv(hsocket_t sock, char** buffer, int *totalSize)
{
  ssize_t size;
  int chunk=1;
  char tmp[HSOCKET_MAX_BUFSIZE+1];
  int fsize;

  *totalSize = 0;

  /* calculate first size for realloc */
  if (*buffer) {
    fsize = strlen(*buffer);
  } else {
    fsize = 0;
  }

  do {
    
    size = recv(sock, tmp, HSOCKET_MAX_BUFSIZE, 0);

    if (size == -1) {
      log_error1("Error reading from socket\n");
      return HSOCKET_CAN_NOT_RECEIVE;
    }

    if (size == 0) {
      break;
    }

    *totalSize += size;
    if (*buffer) {
      log_debug2("reallocation %d bytes",*totalSize+fsize+1);
      *buffer = (char*)realloc((char*)*buffer, (*totalSize)+fsize+HSOCKET_MAX_BUFSIZE);
      strcat(*buffer, tmp);
    } else {
      log_debug1("Allocating");
      *buffer = (char*)realloc(NULL, *totalSize+1);
      strcpy(*buffer, tmp);
    }

    log_debug1("Assigning");
    (*buffer)[*totalSize+fsize] = '\0';	
    chunk++;
  } while (size > 0);

  log_debug1("Returning");
  return HSOCKET_OK;
}



/*--------------------------------------------------
  FUNCTION: hsocket_recv
----------------------------------------------------*/
int hsocket_recv_cb(hsocket_t sock, 
		    hsocket_recv_callback cb, void *userdata)
{
  ssize_t size;
  char tmp[HSOCKET_MAX_BUFSIZE+1];

  do {
    
    size = recv(sock, tmp, HSOCKET_MAX_BUFSIZE, 0);

    if (size == -1) {
      log_error1("Error reading from socket\n");
      return HSOCKET_CAN_NOT_RECEIVE;
    }
    
    if (size == 0) {
      break;
    }

    tmp[size]='\0';
    if (!cb(sock, tmp, size, userdata)) {
      break;
    } 

  } while (size > 0);

  return HSOCKET_OK;
}