~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to tests/server/sockfilt.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: sockfilt.c,v 1.51 2008-03-05 18:27:31 yangtse Exp $
 
21
 * $Id: sockfilt.c,v 1.55 2008-10-01 17:34:25 danf Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/* Purpose
119
119
#define DEFAULT_LOGFILE "log/sockfilt.log"
120
120
#endif
121
121
 
122
 
const char *serverlogfile = (char *)DEFAULT_LOGFILE;
 
122
const char *serverlogfile = DEFAULT_LOGFILE;
123
123
 
124
 
bool verbose = FALSE;
125
 
bool use_ipv6 = FALSE;
126
 
unsigned short port = DEFAULT_PORT;
127
 
unsigned short connectport = 0; /* if non-zero, we activate this mode */
 
124
static bool verbose = FALSE;
 
125
#ifdef ENABLE_IPV6
 
126
static bool use_ipv6 = FALSE;
 
127
#endif
 
128
static const char *ipv_inuse = "IPv4";
 
129
static unsigned short port = DEFAULT_PORT;
 
130
static unsigned short connectport = 0; /* if non-zero, we activate this mode */
128
131
 
129
132
enum sockmode {
130
133
  PASSIVE_LISTEN,    /* as a server waiting for connections */
178
181
#ifdef SIGHUP
179
182
  /* ignore SIGHUP signal */
180
183
  if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
181
 
    logmsg("cannot install SIGHUP handler: 5s", strerror(ERRNO));
 
184
    logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
182
185
#endif
183
186
#ifdef SIGPIPE
184
187
  /* ignore SIGPIPE signal */
185
188
  if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
186
 
    logmsg("cannot install SIGPIPE handler: 5s", strerror(ERRNO));
 
189
    logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
187
190
#endif
188
191
#ifdef SIGALRM
189
192
  /* ignore SIGALRM signal */
190
193
  if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
191
 
    logmsg("cannot install SIGALRM handler: 5s", strerror(ERRNO));
 
194
    logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
192
195
#endif
193
196
#ifdef SIGINT
194
197
  /* handle SIGINT signal with our exit_signal_handler */
195
198
  if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
196
 
    logmsg("cannot install SIGINT handler: 5s", strerror(ERRNO));
 
199
    logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
197
200
  else
198
201
    siginterrupt(SIGINT, 1);
199
202
#endif
200
203
#ifdef SIGTERM
201
204
  /* handle SIGTERM signal with our exit_signal_handler */
202
205
  if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
203
 
    logmsg("cannot install SIGTERM handler: 5s", strerror(ERRNO));
 
206
    logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
204
207
  else
205
208
    siginterrupt(SIGTERM, 1);
206
209
#endif
269
272
  } while((size_t)nread < nbytes);
270
273
 
271
274
  if(verbose)
272
 
    logmsg("read %ld bytes", (long)nread);
 
275
    logmsg("read %zd bytes", nread);
273
276
 
274
277
  return nread;
275
278
}
313
316
  } while((size_t)nwrite < nbytes);
314
317
 
315
318
  if(verbose)
316
 
    logmsg("wrote %ld bytes", (long)nwrite);
 
319
    logmsg("wrote %zd bytes", nwrite);
317
320
 
318
321
  return nwrite;
319
322
}
403
406
  fd_set fds_read;
404
407
  fd_set fds_write;
405
408
  fd_set fds_err;
406
 
  curl_socket_t sockfd;
407
 
  curl_socket_t maxfd;
 
409
  curl_socket_t sockfd = CURL_SOCKET_BAD;
 
410
  curl_socket_t maxfd = CURL_SOCKET_BAD;
408
411
  ssize_t rc;
409
412
  ssize_t nread_socket;
410
413
  ssize_t bytes_written;
411
414
  ssize_t buffer_len;
412
 
  int error;
 
415
  int error = 0;
413
416
 
414
417
 /* 'buffer' is this excessively large only to be able to support things like
415
418
    test 1003 which tests exceedingly large server response lines */
542
545
    else if(!memcmp("PORT", buffer, 4)) {
543
546
      /* Question asking us what PORT number we are listening to.
544
547
         Replies to PORT with "IPv[num]/[port]" */
545
 
      sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, (int)port);
 
548
      sprintf((char *)buffer, "%s/%d\n", ipv_inuse, (int)port);
546
549
      buffer_len = (ssize_t)strlen((char *)buffer);
547
550
      snprintf(data, sizeof(data), "PORT\n%04x\n", buffer_len);
548
551
      if(!write_stdout(data, 10))
565
568
 
566
569
      buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
567
570
      if (buffer_len > (ssize_t)sizeof(buffer)) {
568
 
        logmsg("ERROR: Buffer size (%ld bytes) too small for data size "
569
 
               "(%ld bytes)", (long)sizeof(buffer), (long)buffer_len);
 
571
        logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
 
572
               "(%zd bytes)", sizeof(buffer), buffer_len);
570
573
        return FALSE;
571
574
      }
572
 
      logmsg("> %d bytes data, server => client", buffer_len);
 
575
      logmsg("> %zd bytes data, server => client", buffer_len);
573
576
 
574
577
      if(!read_stdin(buffer, buffer_len))
575
578
        return FALSE;
585
588
        /* send away on the socket */
586
589
        bytes_written = swrite(sockfd, buffer, buffer_len);
587
590
        if(bytes_written != buffer_len) {
588
 
          logmsg("Not all data was sent. Bytes to send: %d sent: %d", 
 
591
          logmsg("Not all data was sent. Bytes to send: %zd sent: %zd",
589
592
                 buffer_len, bytes_written);
590
593
        }
591
594
      }
650
653
    if(!write_stdout(buffer, nread_socket))
651
654
      return FALSE;
652
655
 
653
 
    logmsg("< %d bytes data, client => server", nread_socket);
 
656
    logmsg("< %zd bytes data, client => server", nread_socket);
654
657
    lograw(buffer, nread_socket);
655
658
  }
656
659
 
805
808
    }
806
809
    else if(!strcmp("--ipv6", argv[arg])) {
807
810
#ifdef ENABLE_IPV6
808
 
      use_ipv6=TRUE;
 
811
      ipv_inuse = "IPv6";
 
812
      use_ipv6 = TRUE;
809
813
#endif
810
814
      arg++;
811
815
    }
812
816
    else if(!strcmp("--ipv4", argv[arg])) {
813
817
      /* for completeness, we support this option as well */
814
 
      use_ipv6=FALSE;
 
818
#ifdef ENABLE_IPV6
 
819
      ipv_inuse = "IPv4";
 
820
      use_ipv6 = FALSE;
 
821
#endif
815
822
      arg++;
816
823
    }
817
824
    else if(!strcmp("--port", argv[arg])) {
906
913
#endif /* ENABLE_IPV6 */
907
914
    if(rc) {
908
915
      error = SOCKERRNO;
909
 
      logmsg("Error connecting to port %d: (%d) %s",
 
916
      logmsg("Error connecting to port %hu: (%d) %s",
910
917
             connectport, error, strerror(error));
911
918
      goto sockfilt_cleanup;
912
919
    }
921
928
    msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
922
929
  }
923
930
 
924
 
  logmsg("Running IPv%d version",
925
 
         (use_ipv6?6:4));
 
931
  logmsg("Running %s version", ipv_inuse);
926
932
 
927
933
  if(connectport)
928
 
    logmsg("Connected to port %d", connectport);
 
934
    logmsg("Connected to port %hu", connectport);
929
935
  else
930
 
    logmsg("Listening on port %d", port);
 
936
    logmsg("Listening on port %hu", port);
931
937
 
932
938
  wrotepidfile = write_pidfile(pidname);
933
939
  if(!wrotepidfile)