login-proxy-state.c revision 7dc4bf28c0c4c673a198070edd1ed54e14ae39ae
585N/A/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
2362N/A
585N/A#include "lib.h"
585N/A#include "network.h"
585N/A#include "hash.h"
585N/A#include "login-proxy-state.h"
2362N/A
585N/Astruct login_proxy_state {
2362N/A struct hash_table *hash;
585N/A pool_t pool;
585N/A};
585N/A
585N/Astatic unsigned int login_proxy_record_hash(const void *p)
585N/A{
585N/A const struct login_proxy_record *rec = p;
585N/A
585N/A return net_ip_hash(&rec->ip) ^ rec->port;
585N/A}
585N/A
585N/Astatic int login_proxy_record_cmp(const void *p1, const void *p2)
2362N/A{
2362N/A const struct login_proxy_record *rec1 = p1, *rec2 = p2;
2362N/A
585N/A if (!net_ip_compare(&rec1->ip, &rec2->ip))
585N/A return 1;
585N/A
585N/A return (int)rec1->port - (int)rec2->port;
585N/A}
585N/A
585N/Astruct login_proxy_state *login_proxy_state_init(void)
585N/A{
585N/A struct login_proxy_state *state;
585N/A
585N/A state = i_new(struct login_proxy_state, 1);
585N/A state->pool = pool_alloconly_create("login proxy state", 1024);
585N/A state->hash = hash_table_create(default_pool, state->pool, 0,
585N/A login_proxy_record_hash,
585N/A login_proxy_record_cmp);
585N/A return state;
585N/A}
585N/A
585N/Avoid login_proxy_state_deinit(struct login_proxy_state **_state)
585N/A{
585N/A struct login_proxy_state *state = *_state;
585N/A
585N/A *_state = NULL;
585N/A hash_table_destroy(&state->hash);
585N/A pool_unref(&state->pool);
585N/A i_free(state);
585N/A}
585N/A
585N/Astruct login_proxy_record *
585N/Alogin_proxy_state_get(struct login_proxy_state *state,
585N/A const struct ip_addr *ip, unsigned int port)
585N/A{
585N/A struct login_proxy_record *rec, key;
585N/A
585N/A memset(&key, 0, sizeof(key));
585N/A key.ip = *ip;
585N/A key.port = port;
585N/A
585N/A rec = hash_table_lookup(state->hash, &key);
585N/A if (rec == NULL) {
585N/A rec = p_new(state->pool, struct login_proxy_record, 1);
585N/A rec->ip = *ip;
585N/A rec->port = port;
585N/A hash_table_insert(state->hash, rec, rec);
585N/A }
585N/A return rec;
585N/A}
585N/A