blob: 24b3199397be01ae7d1a8c8e458824080bd32e67 [file] [log] [blame]
Timo Teräsdafa05e2017-01-19 17:27:01 +02001#include "zebra.h"
2#include "hash.h"
3#include "nhrpd.h"
4
5static unsigned int nhrp_reqid_key(void *data)
6{
7 struct nhrp_reqid *r = data;
8 return r->request_id;
9}
10
11static int nhrp_reqid_cmp(const void *data, const void *key)
12{
13 const struct nhrp_reqid *a = data, *b = key;
14 return a->request_id == b->request_id;
15}
16
17uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *p, struct nhrp_reqid *r, void (*cb)(struct nhrp_reqid *, void *))
18{
19 if (!p->reqid_hash) {
20 p->reqid_hash = hash_create(nhrp_reqid_key, nhrp_reqid_cmp);
21 p->next_request_id = 1;
22 }
23
24 if (r->cb != cb) {
25 r->request_id = p->next_request_id;
26 if (++p->next_request_id == 0) p->next_request_id = 1;
27 r->cb = cb;
28 hash_get(p->reqid_hash, r, hash_alloc_intern);
29 }
30 return r->request_id;
31}
32
33void nhrp_reqid_free(struct nhrp_reqid_pool *p, struct nhrp_reqid *r)
34{
35 if (r->cb) {
36 hash_release(p->reqid_hash, r);
37 r->cb = NULL;
38 }
39}
40
41struct nhrp_reqid *nhrp_reqid_lookup(struct nhrp_reqid_pool *p, uint32_t reqid)
42{
43 struct nhrp_reqid key;
44 if (!p->reqid_hash) return 0;
45 key.request_id = reqid;
46 return hash_lookup(p->reqid_hash, &key);
47}
48
49