--------------------- PatchSet 1391 Date: 2005/05/20 07:07:21 Author: rmartinez Branch: squid3-ipv6 Tag: (none) Log: Restoring harmony. We have to modify only that regarding IP addresses, not ASN numbers Members: src/asn.cc:1.16.6.4->1.16.6.5 Index: squid3/src/asn.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/asn.cc,v retrieving revision 1.16.6.4 retrieving revision 1.16.6.5 diff -u -r1.16.6.4 -r1.16.6.5 --- squid3/src/asn.cc 19 May 2005 14:25:52 -0000 1.16.6.4 +++ squid3/src/asn.cc 20 May 2005 07:07:21 -0000 1.16.6.5 @@ -1,6 +1,6 @@ /* - * $Id: asn.cc,v 1.16.6.4 2005/05/19 14:25:52 rmartinez Exp $ + * $Id: asn.cc,v 1.16.6.5 2005/05/20 07:07:21 rmartinez Exp $ * * DEBUG: section 53 AS Number handling * AUTHOR: Duane Wessels, Kostas Anagnostakis @@ -48,32 +48,15 @@ #define WHOIS_PORT 43 #define AS_REQBUF_SZ 4096 + /* BEGIN of definitions for radix tree entries */ -/* 32/128 bits (IPv4/IPv6) "integer" in memory with length */ -/* Always in _network_ oder */ -typedef u_char m_squidip_int_t[1 + sizeof(squidip_int_t)]; -/* - * FIXME: - * Warning: A very weak macro. - * This macro must be called with: - - i beeing a non-paremeter variable (see C-calling convention on arrays, underlying the squidip_int_t for IPv6) - - i[0],...,i[n] in _network_ order - * Note that this was not the usual way on 2.5 version - * - */ -#define store_m_squidip_int_t(i, m) \ - (m[0] = sizeof(m_squidip_int_t), xmemcpy(m+1, &i, sizeof(squidip_int_t))) -/* - * FIXME: - * Warning: A very weak macro. - * This macro must be called with: - - i beeing a non-paremeter variable (see C-calling convention on arrays, underlying the squidip_int_t for IPv6) - - i[0],...,i[n] ready to held values in _network_ order - * Note that this was not the usual way on 2.5 version - */ -#define get_m_squidip_int_t(i, m) \ - xmemcpy(&i, m+1, sizeof(squidip_int_t)) +/* 32/128 bits address in memory with length */ +typedef u_char m_IN_ADDR[1 + sizeof(struct IN_ADDR)]; +#define store_m_IN_ADDR(i, m) \ + (m[0] = sizeof(struct IN_ADDR), xmemcpy(m+1, &i, sizeof(struct IN_ADDR))) +#define get_m_IN_ADDR(i, m) \ + xmemcpy(&i, m+1, sizeof(struct IN_ADDR)) /* END of definitions for radix tree entries */ @@ -83,7 +66,7 @@ /* explicit instantiation required for some systems */ -template cbdata_type List +template cbdata_type List ::CBDATA_List; /* @@ -94,7 +77,7 @@ struct _as_info { - List *as_number; + List *as_number; time_t expires; /* NOTUSED */ }; @@ -103,7 +86,7 @@ StoreEntry *entry; store_client *sc; HttpRequest *request; - squidip_int_t as_number; + ASN as_number; off_t offset; int reqofs; char reqbuf[AS_REQBUF_SZ]; @@ -121,15 +104,15 @@ struct squid_radix_node e_nodes[2]; as_info *e_info; - m_squidip_int_t e_addr; - m_squidip_int_t e_mask; + m_IN_ADDR e_addr; + m_IN_ADDR e_mask; }; typedef struct _rtentry rtentry_t; -static int asnAddNet(char *, squidip_int_t); +static int asnAddNet(char *, ASN ); -static void asnCacheStart(u_char as[16]); +static void asnCacheStart(ASN as); static STCB asHandleReply; @@ -148,14 +131,14 @@ /* PUBLIC */ int -asnMatchIp(List *data, struct IN_ADDR addr) +asnMatchIp(List *data, struct IN_ADDR addr) { struct squid_radix_node *rn; as_info *e; - m_squidip_int_t m_addr; - List *a = NULL; - List *b = NULL; + m_IN_ADDR m_addr; + List *a = NULL; + List *b = NULL; #if 0 unsigned long lh; @@ -174,11 +157,7 @@ if (ADDR_IS_ANYADDR(addr)) return 0; -#ifdef INET6 - store_m_squidip_int_t(addr.s6_addr, m_addr); -#else - store_m_squidip_int_t(addr.s_addr, m_addr); /* FIXME : store requires network order */ -#endif + store_m_IN_ADDR(addr, m_addr); rn = squid_rn_match(m_addr, AS_tree_head); @@ -193,7 +172,7 @@ for (a = data; a; a = a->next) for (b = e->as_number; b; b = b->next) - if (memcmp(&a->element,&b->element,sizeof(squidip_int_t)) == 0) { /* A hack to check 128 bits integers */ + if (a->element == b->element) { debug(53, 5) ("asnMatchIp: Found a match!\n"); return 1; } @@ -205,7 +184,7 @@ void ACLASN::prepareForUse() { - for (List *i = data; i; i = i-> + for (List *i = data; i; i = i-> next) asnCacheStart(i->element); } @@ -248,7 +227,7 @@ /* PRIVATE */ static void -asnCacheStart(squidip_int_t as) +asnCacheStart(ASN as) { LOCAL_ARRAY(char, asres, 4096); StoreEntry *e; @@ -256,17 +235,9 @@ ASState *asState; asState = cbdataAlloc(ASState); asState->dataRead = 0; -#ifdef INET6 - /* FIXME: is this format kept rigth for whois protocol's URL in IPv6 ? */ - /* FIXME : Check whois.cc */ - debug(53, 3) ("asnCacheStart: AS %d,%d,%d,%d\n", as[0],as[4],as[8],as[12]); - snprintf(asres, 4096, "whois://%s/!gAS%d,%d,%d,%d", Config.as_whois_server, as[0],as[4],as[8],as[12]); - memcpy(asState->as_number, as, sizeof(squidip_int_t)); -#else debug(53, 3) ("asnCacheStart: AS %d\n", as); snprintf(asres, 4096, "whois://%s/!gAS%d", Config.as_whois_server, as); asState->as_number = as; -#endif req = urlParse(METHOD_GET, asres); assert(NULL != req); asState->request = requestLink(req); @@ -320,13 +291,8 @@ asStateFree(asState); return; } else if (HTTP_OK != e->getReply()->sline.status) { -#ifdef INET6 - debug(53, 1) ("WARNING: AS %d,%d,%d,%d whois request failed\n", - asState->as_number[0],asState->as_number[4],asState->as_number[8],asState->as_number[12]); -#else debug(53, 1) ("WARNING: AS %d whois request failed\n", asState->as_number); -#endif asStateFree(asState); return; } @@ -352,11 +318,7 @@ } *t = '\0'; -#ifdef INET6 - debug(53, 3) ("asHandleReply: AS# %s (%d,%d,%d,%d)\n", s, asState->as_number[0],asState->as_number[4],asState->as_number[8],asState->as_number[12]); -#else debug(53, 3) ("asHandleReply: AS# %s (%d)\n", s, asState->as_number); -#endif asnAddNet(s, asState->as_number); s = t + 1; asState->dataRead = 1; @@ -426,19 +388,19 @@ * number */ static int -asnAddNet(char *as_string, squidip_int_t as_number) +asnAddNet(char *as_string, ASN as_number) { rtentry_t *e = (rtentry_t *)xmalloc(sizeof(rtentry_t)); struct squid_radix_node *rn; char dbg1[MAXIPSTRLEN], dbg2[MAXIPSTRLEN]; - List **Tail = NULL; - List *q = NULL; + List **Tail = NULL; + List *q = NULL; as_info *asinfo = NULL; struct IN_ADDR in_a, in_m; - squidip_int_t mask,addr; + struct IN_ADDR mask,addr; char *t; int bitl; @@ -464,23 +426,24 @@ #ifdef INET6 u_int bp; - memset(&mask, 0xff, sizeof(squidip_int_t)); - for (bp = bitl/8; bp < sizeof(squidip_int_t); bp++) { + char *ptr=(char *)&mask; + memset(ptr, 0xff, sizeof(struct IN_ADDR)); + for (bp = bitl/8; bp < sizeof(struct IN_ADDR); bp++) { int clearbits = (bp+1)*8 - bitl; if (clearbits > 0) { if (clearbits >= 8) - mask[bp] = 0; + *(ptr + bp) = 0; else - mask[bp] = 0xff << clearbits; + *(ptr + bp) = 0xff << clearbits; } } #else mask = bitl ? 0xfffffffful << (32 - bitl) : 0; /* Could be assumed by the above branch */ #endif - memcpy(&in_a,&addr,sizeof(squidip_int_t)); + memcpy(&in_a,&addr,sizeof(struct IN_ADDR)); - memcpy(&in_m,&mask,sizeof(squidip_int_t)); + memcpy(&in_m,&mask,sizeof(struct IN_ADDR)); xstrncpy(dbg1, INET_NTOA(in_a), MAXIPSTRLEN); @@ -496,38 +459,32 @@ memset(e, '\0', sizeof(rtentry_t)); - store_m_squidip_int_t(addr, e->e_addr); + store_m_IN_ADDR(addr, e->e_addr); - store_m_squidip_int_t(mask, e->e_mask); + store_m_IN_ADDR(mask, e->e_mask); rn = squid_rn_lookup(e->e_addr, e->e_mask, AS_tree_head); if (rn != NULL) { asinfo = ((rtentry_t *) rn)->e_info; -#ifdef INET6 - if (asinfo->as_number->find((const void *)as_number,sizeof(squidip_int_t))) { - debug(53, 3) ("asnAddNet: Ignoring repeated network '%s/%d' for AS %d,%d,%d,%d\n", - dbg1, bitl, as_number[0], as_number[4], as_number[8], as_number[12]); -#else if (asinfo->as_number->find(as_number)) { debug(53, 3) ("asnAddNet: Ignoring repeated network '%s/%d' for AS %d\n", dbg1, bitl, as_number); -#endif } else { debug(53, 3) ("asnAddNet: Warning: Found a network with multiple AS numbers!\n"); for (Tail = &asinfo->as_number; *Tail; Tail = &(*Tail)->next) ; - q = new List ((const void *)as_number,sizeof(squidip_int_t)); + q = new List (as_number); *(Tail) = q; e->e_info = asinfo; } } else { - q = new List ((const void *) as_number,sizeof(squidip_int_t)); + q = new List (as_number); asinfo = (as_info *)xmalloc(sizeof(asinfo)); asinfo->as_number = q; rn = squid_rn_addroute(e->e_addr, e->e_mask, AS_tree_head, e->e_nodes); @@ -573,8 +530,8 @@ static void destroyRadixNodeInfo(as_info * e_info) { - List *prev = NULL; - List *data = e_info->as_number; + List *prev = NULL; + List *data = e_info->as_number; while (data) { prev = data; @@ -585,30 +542,19 @@ delete data; } -/* - * Pre: mask_len must be call - * with mask in network order. - * - Most Significant Byte first-. - * - */ static int -mask_len(squidip_int_t mask) +mask_len(struct IN_ADDR mask) { u_char shift,byte; char bit,caught; int len = 0; - char *ptr; - - /* Hack to bypass the C calling's convention for arrays */ - /* Depending on IPv4/IPv6, mask is u_int32_t o u_char[16] */ - /* So, we do not want to process (char **) ptr on IPv6 */ - ptr = (AF_FAMILY==AF_INET6)? (char *)mask: (char *)&mask; + char *ptr= (char *)&mask; /* Let's scan all the bits from Most Significant to Least */ /* Until we find an "0" bit. Then, we return */ - for (shift=0;shift *q; + List *q; as_info *asinfo; struct IN_ADDR addr; @@ -639,27 +585,19 @@ struct IN_ADDR mask; assert(e); assert(e->e_info); + (void) get_m_IN_ADDR(addr, e->e_addr); + (void) get_m_IN_ADDR(mask, e->e_mask); #ifdef INET6 - (void) get_m_squidip_int_t(addr.s6_addr, e->e_addr); - (void) get_m_squidip_int_t(mask.s6_addr, e->e_mask); storeAppendPrintf(sentry, "%45s/%d\t", - INET_NTOA(addr), mask_len(mask.s6_addr)); #else - (void) get_m_squidip_int_t(addr.s_addr, e->e_addr); - (void) get_m_squidip_int_t(mask.s_addr, e->e_mask); storeAppendPrintf(sentry, "%15s/%d\t", - INET_NTOA(addr), mask_len(mask.s_addr)); #endif + INET_NTOA(addr), mask_len(mask)); asinfo = e->e_info; assert(asinfo->as_number); for (q = asinfo->as_number; q; q = q->next) -#ifdef INET6 - /* FIXME: Is this fomat kept on StoreEntry ? */ - storeAppendPrintf(sentry, " %d,%d,%d,%d", q->element[0],q->element[4],q->element[8],q->element[12]); -#else storeAppendPrintf(sentry, " %d", q->element); -#endif storeAppendPrintf(sentry, "\n"); @@ -684,14 +622,10 @@ { wordlist *W = NULL; char buf[32]; /* FIXME: Is this an arbitrary length ? */ - List *ldata = data; + List *ldata = data; while (ldata != NULL) { -#ifdef INET6 - snprintf(buf, sizeof(buf), "%d,%d,%d,%d", ldata->element[0],ldata->element[4],ldata->element[8],ldata->element[12]); -#else snprintf(buf, sizeof(buf), "%d", ldata->element); -#endif wordlistAdd(&W, buf); ldata = ldata->next; } @@ -702,21 +636,16 @@ void ACLASN::parse() { - List **curlist = &data; - List **Tail; - List *q = NULL; + List **curlist = &data; + List **Tail; + List *q = NULL; char *t = NULL; for (Tail = curlist; *Tail; Tail = &((*Tail)->next)) ; while ((t = strtokFile())) { -#ifdef INET6 - /* FIXME: URGENT. What is the parsing convention for file */ - q = new List ((const void *)t,sizeof(squidip_int_t)); -#else - q = new List (atoi(t)); -#endif + q = new List (atoi(t)); *(Tail) = q; Tail = &q->next; }