summaryrefslogtreecommitdiff
path: root/lib/tests/test-blocking.c
blob: aab76e3fbc4d62c795c3f25d8d2def720064dec4 (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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include "blocking.h"

int
f (const struct sockaddr *addr,
   socklen_t addrlen,
   int out_fd)
{
  int fd = -1;
  //struct rs_alloc_scheme as = { calloc, malloc, free, realloc };
  struct rs_config ctx = { RS_CONN_TYPE_TCP,
			   { RS_CRED_NONE, NULL, NULL },
			   { NULL, NULL, NULL, NULL } };
  struct rs_packet *p = NULL;

  fd = rs_connect (&ctx, addr, addrlen);
  if (fd < 0)
    {
      perror ("rs_connect");
      return -1;
    }

  p = next_packet (&ctx, fd);
  if (p == NULL)
    {
      perror ("next_packet");
      rs_disconnect (&ctx, fd);
      return -1;
    }
  rs_disconnect (&ctx, fd);

  if (send_packet (&ctx, out_fd, p))
    {
      rs_packet_free (&ctx, &p);
      perror ("send_packet");
      return -1;
    }

    return 0;
}

int
main (int argc, char *argv[])
{
  struct addrinfo *ai;
  int rc;

  rc = getaddrinfo (argv[1], argv[2], NULL, &ai);
  if (rc)
    {
      if (rc == EAI_SYSTEM)
	perror ("getaddrinfo");
      else
	fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (rc));
      return -1;
    }

  return f (ai->ai_addr, ai->ai_addrlen, 1);
}