Ejemplo n.º 1
0
/* Devuelve 1 si esta en la cache, completa ls con la info del sector y actualiza su timestamp en cache */
int cache_Search (cacheMem * ch, LogicalSector * ls, tSectorId sectorId) {
	cacheObject * p;

	p = cache_findSector (ch, sectorId);
	if (p == NULL) {
		cache_miss(ch);
		return 0;
	}
	sector_Copy(ls, &(p->sector));
	cache_updateObjectTime(p);
	cache_hit(ch);
	return 1;
}
Ejemplo n.º 2
0
static int
lookup_address( int url_num )
    {
    char* hostname;
    unsigned short port;
	int cached;
#ifdef USE_IPV6
    struct addrinfo hints;
    char portstr[10];
    int gaierr;
    struct addrinfo* ai;
    struct addrinfo* ai2;
    struct addrinfo* aiv4;
    struct addrinfo* aiv6;
#else /* USE_IPV6 */
    struct hostent *he;
#endif /* USE_IPV6 */
// printf("hel!\n");
    urls[url_num].sa_len = sizeof(urls[url_num].sa);
    (void) memset( (void*) &urls[url_num].sa, 0, urls[url_num].sa_len );

    if ( do_proxy )
	{
	hostname = proxy_hostname;
	port = proxy_port;
	}
    else
	{
	hostname = urls[url_num].hostname;
	port = urls[url_num].port;
	}

#ifdef USE_IPV6
//	printf("xxx\n");
    (void) memset( &hints, 0, sizeof(hints) );
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    (void) snprintf( portstr, sizeof(portstr), "%d", (int) port );
    if ( (gaierr = getaddrinfo( hostname, portstr, &hints, &ai )) != 0 )
	{
	(void) fprintf(
	    stderr, "%s: getaddrinfo %s - %s\n", argv0, hostname,
	    gai_strerror( gaierr ) );
//	exit( 1 );
	return 0;
	}

    /* Find the first IPv4 and IPv6 entries. */
    aiv4 = (struct addrinfo*) 0;
    aiv6 = (struct addrinfo*) 0;
    for ( ai2 = ai; ai2 != (struct addrinfo*) 0; ai2 = ai2->ai_next )
	{
	switch ( ai2->ai_family )
	    {
	    case AF_INET: 
	    if ( aiv4 == (struct addrinfo*) 0 )
		aiv4 = ai2;
	    break;
	    case AF_INET6:
	    if ( aiv6 == (struct addrinfo*) 0 )
		aiv6 = ai2;
	    break;
	    }
	}

    /* If there's an IPv4 address, use that, otherwise try IPv6. */
    if ( aiv4 != (struct addrinfo*) 0 )
	{
	if ( sizeof(urls[url_num].sa) < aiv4->ai_addrlen )
	    {
	    (void) fprintf(
		stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname,
		(unsigned long) sizeof(urls[url_num].sa),
		(unsigned long) aiv4->ai_addrlen );
	//    exit( 1 );
			return 0;
	    }
	urls[url_num].sock_family = aiv4->ai_family;
	urls[url_num].sock_type = aiv4->ai_socktype;
	urls[url_num].sock_protocol = aiv4->ai_protocol;
	urls[url_num].sa_len = aiv4->ai_addrlen;
	(void) memmove( &urls[url_num].sa, aiv4->ai_addr, aiv4->ai_addrlen );
	freeaddrinfo( ai );
	return 1;
	}
    if ( aiv6 != (struct addrinfo*) 0 )
	{
	if ( sizeof(urls[url_num].sa) < aiv6->ai_addrlen )
	    {
	    (void) fprintf(
		stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname,
		(unsigned long) sizeof(urls[url_num].sa),
		(unsigned long) aiv6->ai_addrlen );
	    exit( 1 );
	    }
	urls[url_num].sock_family = aiv6->ai_family;
	urls[url_num].sock_type = aiv6->ai_socktype;
	urls[url_num].sock_protocol = aiv6->ai_protocol;
	urls[url_num].sa_len = aiv6->ai_addrlen;
	(void) memmove( &urls[url_num].sa, aiv6->ai_addr, aiv6->ai_addrlen );
	freeaddrinfo( ai );
	return 1;
	}

    (void) fprintf(
	stderr, "%s: no valid address found for host %s\n", argv0, hostname );
    // exit( 1 );
	return 0;

#else /* USE_IPV6 */
	cached = cache_hit(hostname);
	if(cached >=0)
	{
//		printf("[Cache Hit: %s -> %d]\n", hostname, cached);
		urls[url_num].sock_family = urls[url_num].sa.sin_family = cached_urls[cached].sock_family;
		urls[url_num].sock_type = SOCK_STREAM;
		urls[url_num].sock_protocol = 0;
    		urls[url_num].sa_len = sizeof(urls[url_num].sa);
		 (void) memmove( &urls[url_num].sa.sin_addr, &cached_urls[cached].sa.sin_addr, cached_urls[cached].h_length );
		urls[url_num].sa.sin_port = htons( port );
	}
	else
	{
    		he = gethostbyname( hostname );
    		if ( he == (struct hostent*) 0 )
		{
			(void) fprintf( stderr, "%s: unknown host - %s\n", argv0, hostname );
		//	exit( 1 );
			return 0;
		}
    		urls[url_num].sock_family = urls[url_num].sa.sin_family = he->h_addrtype;
    		urls[url_num].sock_type = SOCK_STREAM;
    		urls[url_num].sock_protocol = 0;
    		urls[url_num].sa_len = sizeof(urls[url_num].sa);
    		(void) memmove( &urls[url_num].sa.sin_addr, he->h_addr, he->h_length );
    		urls[url_num].sa.sin_port = htons( port );
		//printf("Setting cache num:%d\n", cached_num);	
		cached_urls[cached_num].hostname = strdup_check( hostname );
		cached_urls[cached_num].sock_family = cached_urls[cached_num].sa.sin_family = he->h_addrtype;
                cached_urls[cached_num].sock_type = SOCK_STREAM;
                cached_urls[cached_num].sock_protocol = 0;
                cached_urls[cached_num].sa_len = sizeof(urls[url_num].sa);
                (void) memmove( &cached_urls[cached_num].sa.sin_addr, he->h_addr, he->h_length );
		cached_urls[cached_num].sa.sin_port = htons(port);
		cached_urls[cached_num].h_length = he->h_length;
		cached_num ++;
                // urls[url_num].sa.sin_port = htons( port );
	}
#endif /* USE_IPV6 */

	return 1;
    }