summaryrefslogtreecommitdiff
path: root/lib/peer.c
blob: b6ec16733b65dbd82ec18c3beb76c04947f6be96 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Copyright 2010,2011,2013 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->base_.active_peer)
    conn->base_.active_peer = conn->base_.active_peer->next; /* Next.  */
  if (!conn->base_.active_peer)
    conn->base_.active_peer = conn->base_.peers; /* From the top.  */

  return conn->base_.active_peer;
}

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

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

int
peer_create_for_connbase (struct rs_conn_base *connbase,
                          struct rs_peer **peer_out)
{
  struct rs_peer *peer;

  peer = peer_create (connbase->ctx, &connbase->peers);
  if (peer == NULL)
    return rs_err_connbase_push_fl (connbase, RSE_NOMEM, __FILE__, __LINE__,
                                    NULL);
  peer->connbase = connbase;
  peer->realm = connbase->realm;

  if (*peer_out)
    *peer_out = peer;
  return RSE_OK;
}

/* Public functions.  */
int
rs_peer_create_for_conn (struct rs_connection *conn, struct rs_peer **peer_out)
{
  return peer_create_for_connbase (TO_BASE_CONN (conn), peer_out);
}

int
rs_peer_create_for_listener (struct rs_listener *listener,
                             struct rs_peer **peer_out)
{
  return peer_create_for_connbase (TO_BASE_CONN (listener), peer_out);
}

int
rs_peer_set_address (struct rs_peer *peer,
                     const char *hostname,
                     const char *service)
{
  assert (peer);
  assert (peer->connbase);
  assert (peer->connbase->ctx);

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

  return RSE_OK;
}

void
rs_peer_free_address (struct rs_peer *peer)
{
  assert (peer);
  assert (peer->connbase);
  assert (peer->connbase->ctx);

  if (peer->hostname)
    rs_free (peer->connbase->ctx, peer->hostname);
  peer->hostname = NULL;
  if (peer->service)
    rs_free (peer->connbase->ctx, peer->service);
  peer->service = NULL;
}

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)
{
  assert (peer);
  assert (peer->connbase);
  assert (peer->connbase->ctx);

  rs_peer_free_secret (peer);
  peer->secret = rs_calloc (peer->connbase->ctx, 1, strlen(secret) + 1);
  if (!peer->secret)
    return rs_err_connbase_push (peer->connbase, RSE_NOMEM, NULL);
  strcpy (peer->secret, secret);
  return RSE_OK;
}

void
rs_peer_free_secret (struct rs_peer *peer)
{
  assert (peer);
  assert (peer->connbase);
  assert (peer->connbase->ctx);

  if (peer->secret)
    rs_free (peer->connbase->ctx, peer->secret);
  peer->secret = NULL;
}