GithubHelp home page GithubHelp logo

mehrbod2002 / dns-lookup Goto Github PK

View Code? Open in Web Editor NEW

This project forked from keeperofdakeys/dns-lookup

0.0 1.0 0.0 124 KB

A small libc::getaddrinfo wrapper for Rust to perform dns lookups.

License: Apache License 2.0

Shell 1.41% Rust 98.59%

dns-lookup's Introduction

dns-lookup

Crates.io

A small wrapper for libc to perform simple DNS lookups.

You can use the lookup_host function to get a list of IP Addresses for a given hostname, and the lookup_name function to get the reverse dns entry for the given IP Address.

PS: If you only need a single result, consider ToSocketAddrs in libstd.

The library also includes a safe wrapper for getaddrinfo and getnameinfo.

Documentation

Usage

Simple API

use dns_lookup::{lookup_host, lookup_addr};

{
  let hostname = "localhost";
  let ips: Vec<std::net::IpAddr> = lookup_host(hostname).unwrap();
  assert!(ips.contains(&"127.0.0.1".parse().unwrap()));
}

{
  let ip: std::net::IpAddr = "127.0.0.1".parse().unwrap();
  let host = lookup_addr(&ip).unwrap();

  // The string "localhost" on unix, and the hostname on Windows.
}

libc API

{
  extern crate dns_lookup;

  use dns_lookup::{getaddrinfo, AddrInfoHints, SockType};

  fn main() {
    let hostname = "localhost";
    let service = "ssh";
    let hints = AddrInfoHints {
      socktype: SockType::Stream.into(),
      .. AddrInfoHints::default()
    };
    let sockets =
      getaddrinfo(Some(hostname), Some(service), Some(hints))
        .unwrap().collect::<std::io::Result<Vec<_>>>().unwrap();

    for socket in sockets {
      // Try connecting to socket
      println!("{:?}", socket);
    }
  }
}

{
  use dns_lookup::getnameinfo;
  use std::net::{IpAddr, SocketAddr};

  let ip: IpAddr = "127.0.0.1".parse().unwrap();
  let port = 22;
  let socket: SocketAddr = (ip, port).into();

  let (name, service) = match getnameinfo(&socket, 0) {
    Ok((n, s)) => (n, s),
    Err(e) => panic!("Failed to lookup socket {:?}", e),
  };

  println!("{:?} {:?}", name, service);
  let _ = (name, service);
}

{
  use dns_lookup::gethostname;

  let hostname = gethostname().unwrap();
}

Retrieve raw buffer as array

fn main(){
    use dns_lookup::Raw;
    
    // Use specefic DNS address -> Vec(u8)
    let buffer = Raw::set_dns("8.8.4.4")
    .build("google.com").unwrap();

    // Use default DNS address which is (8.8.8.8) -> Vec(u8)
    let buffer = Raw::build_default("google.com").unwrap();
}

dns-lookup's People

Contributors

keeperofdakeys avatar mehrbod2002 avatar coolreader18 avatar erickt avatar jcaesar avatar retep998 avatar rahulg avatar senden9 avatar fredszaq avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.