summaryrefslogtreecommitdiff
path: root/contrib/testclient.c
blob: a8dbf2e3791d9758b5aea9d088105c7ad9b87544 (plain) (blame)
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
/*
 *  tcpproxy
 *
 *  tcpproxy is a simple tcp connection proxy which combines the
 *  features of rinetd and 6tunnel. tcpproxy supports IPv4 and
 *  IPv6 and also supports connections from IPv6 to IPv4
 *  endpoints and vice versa.
 *
 *
 *  Copyright (C) 2010-2015 Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of tcpproxy.
 *
 *  tcpproxy is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  tcpproxy 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
  if(argc < 3) {
    fprintf(stderr, "Usage: %s <addr> <port>\n", argv[0]);
    return -1;
  }

  int c = socket(AF_INET, SOCK_STREAM, 0);
  if(!c) {
    perror("socket()");
    return -1;
  }

  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(atoi(argv[2]));
  int ret = inet_pton(AF_INET, argv[1], &(addr.sin_addr));
  if (ret <= 0) {
    if (ret == 0)
      fprintf(stderr, "inet_pton(): Not in presentation format\n");
    else
      perror("inet_pton()");
    return -1;
  }

  ret = connect(c, (struct sockaddr*)(&addr), sizeof(struct sockaddr_in));
  if(ret) {
    perror("connect()");
    return -1;
  }

  char buf[123456];
  unsigned int i;
  for(i = 0; i<sizeof(buf); ++i) {
    buf[i] = 'A' + i%62;
  }
  buf[sizeof(buf)-1] = '\n';

  int nbwritten = send(c, buf, sizeof(buf), 0);
  if(nbwritten <= 0) {
    if(nbwritten < 0)
      perror("send()");
    else
      fprintf(stderr, "nothing sent... aborting\n");
    return -1;
  }
  printf("%d bytes sent\n", nbwritten);
  if(nbwritten != sizeof(buf)) {
    fprintf(stderr, "to few bytes sent ... aborting!");
    return -1;
  }

  shutdown(c, SHUT_WR);

  int len = 0;
  for(;;) {
    int nbread = recv(c, &(buf[len]), sizeof(buf) - len, 0);
    if(nbread <= 0) {
      if(!nbread) {
        fprintf(stderr, "connection closed\n");
      } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
        perror("recv()");
      } else
        continue;

      return -1;
    }

    printf("%d bytes received", nbread);
    len += nbread;
    if(len == nbwritten) {
      printf(" .. finished\n");
      for(i = 0; i<nbwritten-1; ++i) {
        if(buf[i] != ('A' + i%62)) {
          fprintf(stderr, "error at index %d (expected 0x%02X, got 0x%02X)\n", i, 'A' + i%62, (unsigned int)(buf[i]));
          return -1;
        }
      }
      if(buf[nbwritten-1] != '\n') {
        fprintf(stderr, "string doesn't end with new-line\n");
        return -1;
      }
      break;
    }

    printf("\n");
  }

  while(recv(c, buf, 1, 0) > 0);

  return 0;
}