summaryrefslogtreecommitdiff
path: root/lib/peer.c
blob: 0a1d2ec42d7b69b93582ebcbf0f7b8dbe1a341c1 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
   See the file COPYING for licensing information.  */

#if defined HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <radsec/radsec.h>
#include <radsec/radsec-impl.h>
#include "peer.h"

struct rs_peer *
peer_pick_peer (struct rs_connection *conn)
{
  assert (conn);

  if (conn->active_peer)
    conn->active_peer = conn->active_peer->next; /* Next.  */
  if (!conn->active_peer)
    conn->active_peer = conn->peers; /* From the top.  */

  return conn->active_peer;
}

/* Public functions.  */
int
rs_peer_create (struct rs_connection *conn, struct rs_peer **peer_out)
{
  struct rs_peer *peer;

  peer = _rs_peer_create (conn->ctx, &conn->peers);
  if (peer)
    {
      peer->conn = conn;
      peer->realm->timeout = 2;
      peer->realm->retries = 2;
    }
  else
    return rs_err_conn_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
  if (*peer_out)
    *peer_out = peer;
  return RSE_OK;
}

int
rs_peer_set_address (struct rs_peer *peer, const char *hostname,
		       const char *service)
{
  struct rs_error *err;

  assert (peer);
  assert (peer->realm);

  err = _rs_resolv (&peer->addr, peer->realm->type, hostname, service);
  if (err)
    return _rs_err_conn_push_err (peer->conn, err);
  return RSE_OK;
}

void
rs_peer_set_timeout (struct rs_peer *peer, int timeout)
{
  assert (peer);
  assert (peer->realm);
  peer->realm->timeout = timeout;
}
void
rs_peer_set_retries (struct rs_peer *peer, int retries)
{
  assert (peer);
  assert (peer->realm);
  peer->realm->retries = retries;
}

int
rs_peer_set_secret (struct rs_peer *peer, const char *secret)
{
  if (peer->secret)
    free (peer->secret);
  peer->secret = (char *) malloc (strlen(secret) + 1);
  if (!peer->secret)
    return rs_err_conn_push (peer->conn, RSE_NOMEM, NULL);
  strcpy (peer->secret, secret);
  return RSE_OK;
}