summaryrefslogtreecommitdiff
path: root/lib/examples/client.c
blob: 2304e49eb5c3928458a89c8e3dfec2a92a1a7c4e (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
/* RADIUS client doing blocking i/o.  */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <event2/event.h>
#include "../libradsec.h"
#include "../debug.h"

#define SECRET "sikrit"
#define USER_NAME "bob"
#define USER_PW "hemligt"

struct rs_error *
rsx_client (const char *srvname, int srvport)
{
  struct rs_handle *h;
  struct rs_connection *conn;
  struct rs_peer *server;
  struct rs_packet *req;

  if (rs_context_create (&h, "/usr/share/freeradius/dictionary"))
    return NULL;

  if (rs_conn_create (h, &conn))
    return rs_conn_err_pop (conn);
  if (rs_conn_add_server (conn, &server, RS_CONN_TYPE_TCP, srvname, srvport))
    return rs_conn_err_pop (conn);
  rs_server_set_timeout (server, 10);
  rs_server_set_tries (server, 3);
  rs_server_set_secret (server, SECRET);

  if (rs_packet_create_acc_request (conn, &req, USER_NAME, USER_PW))
    return rs_conn_err_pop (conn);

  if (rs_packet_send (conn, req, NULL))
    return rs_conn_err_pop (conn);
  req = NULL;

  rs_conn_destroy (conn);
  rs_context_destroy (h);
  return 0;
}

int
main (int argc, char *argv[])
{
  struct rs_error *err;
  char *host;
  int port;

  host = strsep (argv + 1, ":");
  port = atoi (argv[1]);
  err = rsx_client (host, port);
  if (!err)
    return -1;
  fprintf (stderr, "%s\n", rs_err_msg (err, 0));
  return rs_err_code (err, 1);
}