CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / ppp / pppd / plugins / pppoe / pppoehash.c
blob0c846423a78a38269846912e8083458ac6052548
1 /* PPPoE support library "libpppoe"
3 * Copyright 2000 Michal Ostrowski <mostrows@styx.uwaterloo.ca>,
4 * Jamal Hadi Salim <hadi@cyberus.ca>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 #include <pppoe.h>
14 #define PPPOE_HASH_SIZE 16
17 static inline int keycmp(char *a, char *b, int x, int y){
18 return x==y && !memcmp(a,b,x);
21 static int hash_con(int key_len, char* key)
23 int i = 0;
24 char hash[sizeof(int)]={0,};
26 for (i = 0; i < key_len ; ++i)
27 hash[i% sizeof(int)] = hash[i%sizeof(int)] ^ key[i];
29 i = (*((int*)hash)) ;
30 i &= PPPOE_HASH_SIZE - 1;
32 return i;
35 static struct pppoe_con *con_ht[PPPOE_HASH_SIZE] = { 0, };
37 struct pppoe_con *get_con(int len, char *key)
39 int hash = hash_con(len, key);
40 struct pppoe_con *ret;
42 ret = con_ht[hash];
44 while (ret && !keycmp(ret->key,key, ret->key_len, len))
45 ret = ret->next;
47 return ret;
50 int store_con(struct pppoe_con *pc)
52 int hash = hash_con(pc->key_len, pc->key);
53 struct pppoe_con *ret;
55 ret = con_ht[hash];
56 while (ret) {
57 if (!keycmp(ret->key, pc->key, ret->key_len, pc->key_len))
58 return -EALREADY;
60 ret = ret->next;
63 if (!ret) {
64 pc->next = con_ht[hash];
65 con_ht[hash] = pc;
68 return 0;
71 struct pppoe_con *delete_con(unsigned long len, char *key)
73 int hash = hash_con(len, key);
74 struct pppoe_con *ret, **src;
76 ret = con_ht[hash];
77 src = &con_ht[hash];
79 while (ret) {
80 if (keycmp(ret->key,key, ret->key_len, len)) {
81 *src = ret->next;
82 break;
85 src = &ret->next;
86 ret = ret->next;
89 return ret;