aboutsummaryrefslogtreecommitdiffstats
path: root/checkalias.c
blob: 1eca63daadc7c19409d3fc83ffd555fbc9d30911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * checkalias.c -- check to see if the IP addresses of two hosts are the same
 *
 * Copyright 1997 by Eric S. Raymond
 * For license terms, see the file COPYING in this directory.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "fetchmail.h"
 
int is_ip_alias(const char *name1,const char *name2)
/* Given two hostnames as arguments, returns TRUE if they
 * have at least one IP address in common.
 * It is meant to be called by the is_host_alias() function in driver.c
 * No check is done on errors returned by gethostbyname,
 * the calling function does them.
 */
{
    typedef unsigned char address_t[sizeof (struct in_addr)]; 
    typedef struct _address_e
    {
	struct _address_e *next;
	address_t address;
    } 
    address_e;
    address_e *host_a_addr, *host_b_addr,*dummy_addr;

    int i;
    struct hostent *hp;
    char **p;
 
    hp = gethostbyname(name1);
 
    dummy_addr = (address_e *)NULL;

    for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
    {
	struct in_addr in;
	(void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
	host_a_addr = (address_e *)xmalloc(sizeof( address_e));
	memset (host_a_addr,0, sizeof (address_e));
	host_a_addr->next = dummy_addr;
	(void) memcpy(&host_a_addr->address, *p, sizeof (in.s_addr));
	dummy_addr = host_a_addr;
    }


    hp = gethostbyname(name2);

    dummy_addr = (address_e *)NULL;

    for (i=0,p = hp->h_addr_list; *p != 0; i++,p++)
    {
	struct in_addr in;
	(void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
	host_b_addr = (address_e *)xmalloc(sizeof( address_e));
	memset (host_b_addr,0, sizeof (address_e));
	host_b_addr->next = dummy_addr;
	(void) memcpy(&host_b_addr->address, *p, sizeof (in.s_addr));
	dummy_addr = host_b_addr;
    }

    while (host_a_addr)
    {
	while (host_b_addr)
	{
	    if (!memcmp(host_b_addr->address,host_a_addr->address, sizeof (address_t)))
		return (TRUE);

	    host_b_addr = host_b_addr->next;
	}
	host_a_addr = host_a_addr->next;
    }
    return (FALSE);
}