Skip to content

Instantly share code, notes, and snippets.

@vincent99
Last active February 1, 2016 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincent99/32e029c995028a13328e to your computer and use it in GitHub Desktop.
Save vincent99/32e029c995028a13328e to your computer and use it in GitHub Desktop.
Examples of DNS resolution
// javac DNS.java
// java DNS blah
import java.net.*;
public class DNS {
public static void main(String[] args) {
java.security.Security.setProperty("networkaddress.cache.ttl", "0");
java.security.Security.setProperty("networkaddress.cache.negative.ttl", "0");
for ( int i = 0 ; i < 1000 ; i++ ) {
try {
InetAddress address = InetAddress.getByName(args[0]);
System.out.println(address.getHostAddress());
Thread.sleep(1000);
} catch ( UnknownHostException e ) {
System.out.println(e);
break;
} catch ( InterruptedException e ) {
System.out.println(e);
break;
}
}
}
}
<?php
// php dns.php blah
$name = $argv[1];
for ( $i = 0 ; $i < 1000; $i++ )
{
$ip = gethostbyname($name);
print_r($ip."\n");
sleep(1);
}
?>
<?php
// php dns2.php blah
$name = $argv[1];
for ( $i = 0 ; $i < 1000; $i++ )
{
$ips = gethostbynamel($name);
print_r(join(" ",$ips)."\n");
sleep(1);
}
?>
// gcc -o getaddrinfo getaddrinfo.c
// ./getaddrinfo blah
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i, j;
struct hostent *he;
struct in_addr **addr_list;
if (argc != 2) {
fprintf(stderr,"usage: %s hostname\n", argv[0]);
return 1;
}
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
for ( i = 0 ; i < 1000 ; i++ )
{
if ((rv = getaddrinfo(argv[1], "http", &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
printf("%s: ", argv[1]);
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ( p->ai_addr->sa_family == AF_INET ) {
struct sockaddr_in *addr;
addr =(struct sockaddr_in *)p->ai_addr;
printf("%s ", inet_ntoa(addr->sin_addr));
} else {
printf("Not IPv4");
}
}
printf("\n");
sleep(1);
}
freeaddrinfo(servinfo); // all done with this structure
return 0;
}
// gcc -o gethostbyname gethostbyname.c
// ./gethostbyname blah
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i, j;
struct hostent *he;
struct in_addr **addr_list;
if (argc != 2) {
fprintf(stderr,"usage: %s hostname\n", argv[0]);
return 1;
}
for ( i = 0 ; i < 1000 ; i++ )
{
if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
return 2;
}
// print information about this host:
printf("%s: ", he->h_name);
addr_list = (struct in_addr **)he->h_addr_list;
for(j = 0; addr_list[j] != NULL; j++) {
printf("%s ", inet_ntoa(*addr_list[j]));
}
printf("\n");
sleep(1);
}
return 0;
}
package main
// go build go-dns.go
// ./go-dns blah
import (
"net"
"flag"
"fmt"
"os"
"time"
)
func main() {
flag.Parse()
name := flag.Arg(0)
for i := 0 ; i < 1000 ; i++ {
ip, err := net.ResolveIPAddr("ip4", name)
if err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
}
fmt.Printf("%s: %s\n", name, ip.String())
time.Sleep(1000 * time.Millisecond)
}
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment