lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] Usage example for DNS client (resolv.c)


From: Pettinato, Jim
Subject: [lwip-users] Usage example for DNS client (resolv.c)
Date: Tue, 19 Jun 2007 08:54:56 -0400

The following is one of the places where I am using the DNS
functionality - I'm posting it as an example for others who wish to
integrate it. 

The resolv_init() function must be called with the IP address of your
DNS server before any lookups are performed. (You don't want to call
this more than once unless your DNS server IP changes, except if you
also want to delete any cached entries in the local table).

Remember you must add a call to resolv_tmr() so it runs approximately
once a second in your timer routine - that is what actually triggers any
pending queries to be processed.

Hope you find it useful,

- Jim

------------------------------------------------------------------------
----------------
// sample DNS callback function - callback for SMTP server name lookup
void smtp_serverFound(char *name, struct ip_addr *ipaddr)
{ 
        if ((ipaddr) && (ipaddr->addr))
        {
                smtp.serverIP.addr = ipaddr->addr;
                smtp.state = SMTP_NAME_RESOLVED;
                if (smtp_connect() == ERR_OK)
                        return;
                smtp.lastError = SMTP_CONNECT_FAILED;
        }
        else
                smtp.lastError = SMTP_UNKNOWN_HOST;
        smtp.state = SMTP_IDLE;
}


// sample usage of gethostbyname()
SMTP_RESULT smtp_send_mail(char *to, char *from, char *subj, char *msg)
{
      RESOLV_RESULT result;

      if (SMTP_IDLE == smtp.state)
      {
                <SMTP specific code removed for brevity - cache mail
message>

            // set server to configured server name/IP address
            result = gethostbyname(smtp.server, &smtp.serverIP,
smtp_serverFound);
            if (RESOLV_COMPLETE == result)      // already know SMTP
server
            {
                    if (smtp_connect() == ERR_OK)     // state will be
advanced in
                            return SMTP_SUCCESS;      //
smtp_connected() callback
                    else
                            return SMTP_CONNECT_FAILED;
            }
            else if (RESOLV_QUERY_QUEUED == result)
            {
                    smtp.state = SMTP_DNS_QUERY_PENDING;   // needed a
DNS query
                    return SMTP_QUEUED_PENDING_DNS_LOOKUP; // go to
waiting state
            }
            else
                    return SMTP_UNKNOWN_HOST;
        }
        return SMTP_BUSY; // calling smtp_send_mail in all other states
is invalid
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]