login-proxy-state.c revision 088cb24027234024aff2c1ce5b6870c5a308a44b
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen#include "lib.h"
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen#include "network.h"
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen#include "hash.h"
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen#include "login-proxy-state.h"
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstruct login_proxy_state {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen struct hash_table *hash;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen pool_t pool;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen};
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstatic unsigned int ip_addr_hash(const void *p)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen const struct ip_addr *ip = p;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen return net_ip_hash(ip);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen}
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstatic int ip_addr_cmp(const void *p1, const void *p2)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen const struct ip_addr *ip1 = p1, *ip2 = p2;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen return net_ip_compare(ip1, ip2) ? 0 : 1;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen}
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstruct login_proxy_state *login_proxy_state_init(void)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen struct login_proxy_state *state;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen state = i_new(struct login_proxy_state, 1);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen state->pool = pool_alloconly_create("login proxy state", 1024);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen state->hash = hash_table_create(default_pool, state->pool, 0,
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen ip_addr_hash, ip_addr_cmp);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen return state;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen}
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenvoid login_proxy_state_deinit(struct login_proxy_state **_state)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen struct login_proxy_state *state = *_state;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen *_state = NULL;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen hash_table_destroy(&state->hash);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen pool_unref(&state->pool);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen i_free(state);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen}
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenstruct login_proxy_record *
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainenlogin_proxy_state_get(struct login_proxy_state *state,
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen const struct ip_addr *ip)
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen{
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen struct login_proxy_record *rec;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen struct ip_addr *new_ip;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen rec = hash_table_lookup(state->hash, ip);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen if (rec == NULL) {
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen new_ip = p_new(state->pool, struct ip_addr, 1);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen *new_ip = *ip;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen rec = p_new(state->pool, struct login_proxy_record, 1);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen hash_table_insert(state->hash, new_ip, rec);
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen }
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen return rec;
088cb24027234024aff2c1ce5b6870c5a308a44bTimo Sirainen}