summaryrefslogtreecommitdiff
path: root/lib/peer.c
blob: decc64bb281cf1d9b240974573fe0f5fc88e019a (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
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
/* Copyright 2010-2012 NORDUnet A/S. All rights reserved.
   See LICENSE for licensing information. */

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

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <radsec/radsec.h>
#include <radsec/radsec-impl.h>
#include "err.h"
#include "peer.h"
#include "util.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;
}

struct rs_peer *
peer_create (struct rs_context *ctx, struct rs_peer **rootp)
{
  struct rs_peer *p;

  p = (struct rs_peer *) rs_malloc (ctx, sizeof(*p));
  if (p)
    {
      memset (p, 0, sizeof(struct rs_peer));
      if (*rootp)
	{
	  p->next = (*rootp)->next;
	  (*rootp)->next = p;
	}
      else
	*rootp = p;
    }
  return p;
}

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

  peer = peer_create (conn->ctx, &conn->peers);
  if (peer)
    {
      peer->conn = conn;
      peer->realm->timeout = 2;	/* FIXME: Why?  */
      peer->realm->retries = 2;	/* FIXME: Why?  */
    }
  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)
{
  assert (peer);
  assert (peer->conn);
  assert (peer->conn->ctx);

  peer->hostname = rs_strdup (peer->conn->ctx, hostname);
  peer->service = rs_strdup (peer->conn->ctx, service);
  if (peer->hostname == NULL || peer->service == NULL)
    return RSE_NOMEM;

  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;
}