Skip to content

Commit aade1b9

Browse files
fix: handle NaN and float values in reverse DNS resolution (#18)
* fix: handle NaN and float values in reverse DNS resolution * refactor: use set for unresolvable IP tracking
1 parent 5a4a98e commit aade1b9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

parse.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
else:
3535
sys.exit("This script requires *nix.")
3636

37-
unresolvable_ips = [] # List to keep track of unresolvable IP addresses
37+
unresolvable_ips = set() # keep track of unresolvable IP addresses
3838

3939
def main():
4040
# Parse the command line arguments
@@ -108,22 +108,22 @@ def update_ip_hostname_mappings(df, ip_shelve):
108108
for _, row in sni_df.iterrows():
109109
ip_shelve[row['ip.dst']] = row['tls.handshake.extensions_server_name']
110110

111-
df['src_hostname'] = df['ip.src'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x)) if x else ''))
112-
df['dst_hostname'] = df['ip.dst'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x)) if x else ''))
111+
df['src_hostname'] = df['ip.src'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x))) if pd.notna(x) else '')
112+
df['dst_hostname'] = df['ip.dst'].map(lambda x: ip_shelve.get(str(x), reverse_dns(str(x))) if pd.notna(x) else '')
113113
df.drop(['dns.qry.name', 'dns.a', 'tls.handshake.extensions_server_name'], axis=1, inplace=True)
114114

115115
def reverse_dns(ip_address):
116116
"""
117117
Attempts to resolve an IP address to a hostname using a reverse DNS lookup;
118118
This function is used as a fallback mechanism in the event that an IP address does not have a corresponding hostname entry in the shelve database.
119119
"""
120-
if not ip_address or not isinstance(ip_address, str):
120+
if not ip_address or not isinstance(ip_address, str) or ip_address.lower() == 'nan':
121121
return ''
122122
try:
123123
hostname = socket.gethostbyaddr(ip_address)[0]
124124
return hostname
125125
except (socket.herror, socket.gaierror):
126-
unresolvable_ips.append(ip_address)
126+
unresolvable_ips.add(ip_address)
127127
return ''
128128

129129
if __name__ == "__main__":

0 commit comments

Comments
 (0)