Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

javacomplements by liaoxiuli


									DNS: Domain Name System

People: many identifiers:           Domain Name System:
          SSN, name, passport #    ❒  distributed database
Internet hosts, routers:               implemented in hierarchy of
                                       many name servers
          IP address (32 bit) -
                                    ❒  application-layer protocol
           used for addressing
                                       host, routers, name servers to
                                       communicate to resolve names
          “name”, e.g.,               (address/name translation)
  - used by
                                          note: core Internet
                                           function, implemented as
Q: map between IP                          application-layer protocol
  addresses and name ?                    complexity at network’s

H. Fauconnier                                           M2-Internet     1
DNS services                       Why not centralize DNS?
❒  hostname to IP                  ❒  single point of failure
   address translation             ❒  traffic volume
❒  host aliasing                   ❒  distant centralized
         Canonical, alias names      database
❒  mail server aliasing            ❒  maintenance
❒  load distribution
      replicated Web servers:     doesn’t scale!
       set of IP addresses for
       one canonical name

 H. Fauconnier                                       M2-Internet   2
Distributed, Hierarchical Database
                          Root DNS Servers

    com DNS servers       org DNS servers     edu DNS servers

                             DNS servers     DNS serversDNS servers
DNS servers DNS servers

Client wants IP for; 1st approx:
❒  client queries a root server to find com DNS server
❒  client queries com DNS server to get
   DNS server
❒  client queries DNS server to get IP
   address for

H. Fauconnier                                         M2-Internet     3
      DNS: Root name servers
     ❒  contacted by local name server that can not resolve name
     ❒  root name server:
                   contacts authoritative name server if name mapping not known
                   gets mapping
                   returns mapping to local name server
                                    a Verisign, Dulles, VA
                                    c Cogent, Herndon, VA (also LA)
                                    d U Maryland College Park, MD     k RIPE London (also 16 other locations)
                                    g US DoD Vienna, VA
                                    h ARL Aberdeen, MD                  i Autonomica, Stockholm (plus
                                    j Verisign, ( 21 locations)                    28 other locations)
e NASA Mt View, CA                                                                            m WIDE Tokyo (also Seoul,
f Internet Software C. Palo Alto,                                                             Paris, SF)
CA (and 36 other locations)

                                                                                                           13 root name
                                                                                                           servers worldwide
         b USC-ISI Marina del Rey, CA
         l ICANN Los Angeles, CA

      H. Fauconnier                                                                                              M2-Internet   4
TLD and Authoritative Servers
❒  Top-level domain (TLD) servers:
     responsible for com, org, net, edu, etc, and all
      top-level country domains uk, fr, ca, jp.
     Network Solutions maintains servers for com TLD
     Educause for edu TLD
❒  Authoritative DNS servers:
     organization’s DNS servers, providing
      authoritative hostname to IP mappings for
      organization’s servers (e.g., Web, mail).
     can be maintained by organization or service

H. Fauconnier                               M2-Internet   5
Local Name Server
❒  does not strictly belong to hierarchy
❒  each ISP (residential ISP, company,
    university) has one.
        also   called “default name server”
❒  when host makes DNS query, query is sent
    to its local DNS server
        acts   as proxy, forwards query into hierarchy

H. Fauconnier                                     M2-Internet   6
DNS name                                    root DNS server

resolution example
❒  Host at                       3
                                                      TLD DNS server
   wants IP address for                           4                              5

 iterated query:           local DNS server
 ❒  contacted server
    replies with name of                          7     6
                                   1    8
    server to contact
 ❒  “I don’t know this                            authoritative DNS server
    name, but ask this                     
    server”                requesting host


H. Fauconnier                                            M2-Internet      7
DNS name
resolution example                        root DNS server

recursive query:                      2                    3
❒  puts burden of name
   resolution on                           7       6
   contacted name                                                    TLD DNS server
❒  heavy load?
                         local DNS server
                                 5       4

                                 1    8

                                               authoritative DNS server
                         requesting host


H. Fauconnier                                                      M2-Internet   8
DNS: caching and updating records
 ❒  once (any) name server learns mapping, it                 caches
        cache entries timeout (disappear) after some
        TLD servers typically cached in local name
                •  Thus root name servers not often visited
 ❒  update/notify mechanisms under design by IETF
       RFC 2136

H. Fauconnier                                                 M2-Internet   9
DNS records
DNS: distributed db storing resource records (RR)
                 RR format: (name, value, type, ttl)

❒  Type=A                        ❒  Type=CNAME
          name is hostname             name is alias name for some
          value is IP address           “canonical” (the real) name
                                is really
❒  Type=NS                  
                                     value is canonical name
      name is domain (e.g.
      value is hostname of    ❒  Type=MX
       authoritative name server   value is name of mailserver
       for this domain                associated with name

H. Fauconnier                                            M2-Internet   10
DNS protocol, messages
DNS protocol : query and reply messages, both with
  same message format
msg header
❒  identification: 16 bit #
   for query, reply to query
   uses same #
❒  flags:
      query or reply
      recursion desired
      recursion available
      reply is authoritative

H. Fauconnier                              M2-Internet   11
DNS protocol, messages

     Name, type fields
          for a query

         RRs in response
                 to query

         records for
authoritative servers

   additional “helpful”
info that may be used

H. Fauconnier               M2-Internet   12
Inserting records into DNS
❒  example: new startup “Network Utopia”
❒  register name at DNS               registrar
    (e.g., Network Solutions)
          provide names, IP addresses of authoritative name server
           (primary and secondary)
          registrar inserts two RRs into com TLD server:

      (,, NS)
      (,, A)

❒  create authoritative server Type A record for; Type MX record for
❒  How do people get IP address of your Web site?

H. Fauconnier                                            M2-Internet   13
  Quelques compléments Java

H. Fauconnier            M2-Internet   14
  URL et URI
  html et swing
  URLconnections
     URLStreamHandler URLStreamHandlerFactory
     ContentHandler ContentHandlerFactory

H. Fauconnier                         M2-Internet   15
  la classe URL permet d'utiliser les URL
  les classes URLDecoder et URLEncoder
   permettent de traiter les formulaires HTML
  Une URI (Uniform ressource Identifier), est une
   sorte de généralisation des URL qui inclut de
   Uniform Resource Location (URL) mais aussi de
   Uniform Ressource Names (URN)
         exemples
           •  tel:1-800-9988-9938
           •  http://www.xml.comp/pub/stax.html#id =_hdbc
  (la classe URLConnection sera décrite plus loin)

H. Fauconnier                                               M2-Internet   16
  URI:
       URL      (location)

       ou      URN (name):
  Syntaxe:
       Général:
       Hiérarchique:
          Authorité: [user-info@]host[:port]

H. Fauconnier                                        M2-Internet   1-17
       protocol
       port
       authority
       chemin
       référence
       requête
       user

H. Fauconnier                            M2-Internet   18
Construire une URL

  public URL(String url) throws MalformedURLException
  public URL(String protocol, String hostname, String
   file) throws MalformedURLException
  public URL(String protocol, String host, int port,
   String file) throws MalformedURLException
  public URL(URL base, String relative) throws

H. Fauconnier                             M2-Internet   19
  tous les protocoles ne sont pas compris par
   la machine virtuelle…
  exception MalFormedURLException

H. Fauconnier                         M2-Internet   20
Exemple: Quels protocoles?
public class ProtocolTester {
  public static void main(String[] args) {
    testProtocol( "ldap://,c=US?
    testProtocol( "jar:!"

H. Fauconnier                                                       M2-Internet     21
Quels protocoles (suite)
 private static void testProtocol(String url) {

    try {
      URL u = new URL(url);
      System.out.println(u.getProtocol() +
               " is supported");
    catch (MalformedURLException ex) {
      String protocol = url.substring(0,url.indexOf(':'));
      System.out.println(protocol + " is not supported");

http is supported
https is supported
ftp is supported
mailto is supported
telnet is not supported
file is supported
gopher is supported
ldap is not supported
jar is supported
nfs is not supported
jdbc is not supported
rmi is not supported
doc is supported
netdoc is supported
systemresource is supported
verbatim is supported

H. Fauconnier                                                M2-Internet   22
Décomposer l'url
 public static void decomposeURL(String st) {
          URL u = new URL(st);
          System.out.println("URL :" + u);
          System.out.println("Protocole : " + u.getProtocol( ));
          System.out.println("UserInfo :" + u.getUserInfo( ));
        String host = u.getHost( );
        if (host != null) {
          int atSign = host.indexOf('@');
          if (atSign != -1) host = host.substring(atSign+1);
          System.out.println("hôte " + host);
        else {
          System.out.println("hôte null.");

H. Fauconnier                                      M2-Internet     23

          System.out.println("Port :" + u.getPort( ));
           System.out.println("Path : " + u.getPath( ));
           System.out.println("Ref : " + u.getRef( ));
           System.out.println("Query: " + u.getQuery( ));
        catch (MalformedURLException ex)
            {System.err.println(st + " URL malformée"); }

H. Fauconnier                                         M2-Internet   24

Protocole : http
UserInfo :null
Port :80
Path : /javafaq/book/index.html
Ref : toc
Query: isbn=56888

H. Fauconnier                              M2-Internet   25
Obtenir des données
  public InputStream openStream( ) throws
  public URLConnection openConnection( )
   throws IOException
  public URLConnection
   openConnection(Proxy proxy) throws
  public Object getContent( ) throws
  public Object getContent(Class[]
   classes) throws IOException

H. Fauconnier                      M2-Internet   26
Charger le contenu d'une URL
 public static void chargerFichier(String url) {
        try {
            URL u = new URL(url);
            InputStream in = u.openStream();
            in = new BufferedInputStream(in);
            Reader r = new InputStreamReader(in);
            int c;
            while ((c = != -1) {
                System.out.print((char) c);
        } catch (MalformedURLException ex) {
            System.err.println(url + " mauvaise URL");
        } catch (IOException e) {
            System.err.println(e + " problème de lecture url "
   +                          url);

H. Fauconnier                                      M2-Internet   27
Type du contenu
public static void typeObjetURL(String st) {
   try {
       URL u = new URL(st);
       try {
           Object o = u.getContent();
           System.out.println("L'objet est un " +
       } catch (IOException ex) {System.err.println(ex);}
   } catch (MalformedURLException ex) {
            System.err.println(st + " URL malformée");
L'objet est un

H. Fauconnier                                      M2-Internet   28
Et encore
  URLEncoder
  URLDecoder

H. Fauconnier   M2-Internet   29
URLEncode: Exemple
public class QueryString {
  private StringBuffer query = new StringBuffer();
  public QueryString(String name, String value) {
    encode(name, value);
  public synchronized void add(String name, String value) {
    encode(name, value);
  private synchronized void encode(String name, String value){
    try {
      query.append(URLEncoder.encode(name, "UTF-8"));
      query.append(URLEncoder.encode(value, "UTF-8"));
    catch (UnsupportedEncodingException ex) {
      throw new RuntimeException("??");

H. Fauconnier                                      M2-Internet   30
Exemple (suite)
    public String getQuery() {
       return query.toString();
    public String toString() {
       return getQuery();

H. Fauconnier                     M2-Internet   31
  Classe (abstraite) Authenticator
       PasswordAuthentication          représente un couple
        password + user
       Méthode getPasswordAuthentication() à
        redéfinir pour obtenir un
       Méthode setDefault(Authenticator) définit
        l'Authenticator pour le système
           •  C'est à lui que s'adressera le système à chaque fois
              que nécessaire

H. Fauconnier                                            M2-Internet   32
Pour apache:
  Fichier .htaccess: (il faut aussi un
   AllowOverride AuthConfig dans
           AuthType Basic
           AuthName "restreint"
           AuthUserFile /Users/hf/Sites/.psswd
           Require user hf

       .psswdest le fichier des mots de passe
       htpasswd –c /Users/hf/Sites/.passwd

H. Fauconnier                                    M2-Internet   33

H. Fauconnier   M2-Internet   34

To top