-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup.c
More file actions
61 lines (45 loc) · 1.45 KB
/
lookup.c
File metadata and controls
61 lines (45 loc) · 1.45 KB
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
/*
* name: lookup.c
* this file is part of the "Net-Injector" project. It defines the lookup() function
* Copyright (C) 2002 by Jean Philippe GUILLEMIN <jp.guillemin@free.fr>
* license: This software is under GPL license
* date: 04 17 2003
* rev: 0.8
*/
#include "neti.h"
int lookup(u_int32_t *inet_ip, char *ip, char *name) {
struct hostent *hostnt;
struct in_addr ip_addr;
hostnt = malloc(HOSTENT_SIZE);
/* call gethostbyname() returns a pointer to a hostent struct or NULL. */
hostnt = gethostbyname(name);
if (!hostnt) {
fprintf(stderr, "Error : %s was not resolved !\n",name);
exit(EXIT_FAILURE);
}
ip_addr = *(struct in_addr *)(hostnt->h_addr);
*inet_ip = ip_addr.s_addr;
strncpy(ip,inet_ntoa(ip_addr), 32);
/*fprintf(stdout,"%s resolved to %s\n",name, myhdr->dst_ip);*/
return EXIT_SUCCESS;
}
int reverse_lookup(char *name, char *ip) {
struct hostent *hostnt;
struct in_addr ip_addr;
hostnt = malloc(HOSTENT_SIZE);
if (strncmp (last_address, ip, 32) == 0) {
strncpy(name, last_name, 1024);
return EXIT_SUCCESS;
}
/* call to gethostbyaddr() returns a pointer to a hostent struct or NULL. */
inet_aton(ip, &ip_addr);
hostnt = gethostbyaddr((char*)&ip_addr, 4, AF_INET);
if (!hostnt) {
strncpy(name, ip, 1024);
}else{
strncpy(name, hostnt->h_name, 1024);
}
strncpy(last_name, name, 1024);
strncpy(last_address, ip, 32);
return EXIT_SUCCESS;
}