Low level connect and read working, kind of. At least TCP.
[radsecproxy.git] / lib / test-blocking.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <netdb.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "blocking.h"
8
9 int
10 f (const struct sockaddr *addr,
11    socklen_t addrlen,
12    int out_fd)
13 {
14   int fd = -1;
15   //struct rs_alloc_scheme as = { calloc, malloc, free, realloc };
16   struct rs_config ctx = { RS_CONN_TYPE_TCP, RS_CRED_NONE, NULL };
17   struct rs_packet *p = NULL;
18
19   fd = rs_connect (&ctx, addr, addrlen);
20   if (fd < 0)
21     {
22       perror ("rs_connect");
23       return -1;
24     }
25
26   p = next_packet (&ctx, fd);
27   if (p == NULL)
28     {
29       perror ("next_packet");
30       rs_disconnect (&ctx, fd);
31       return -1;
32     }
33   rs_disconnect (&ctx, fd);
34
35   if (send_packet (&ctx, out_fd, p))
36     {
37       rs_packet_free (&ctx, p);
38       perror ("send_packet");
39       return -1;
40     }
41
42     return 0;
43 }
44
45 int
46 main (int argc, char *argv[])
47 {
48   struct addrinfo *ai;
49   int rc;
50
51   rc = getaddrinfo (argv[1], argv[2], NULL, &ai);
52   if (rc)
53     {
54       if (rc == EAI_SYSTEM)
55         perror ("getaddrinfo");
56       else
57         fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (rc));
58       return -1;
59     }
60
61   return f (ai->ai_addr, ai->ai_addrlen, 1);
62 }