* ecore: add ecore_con_lookup for dns request retrieval.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 22 Jul 2010 11:32:55 +0000 (11:32 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 22 Jul 2010 11:32:55 +0000 (11:32 +0000)
NOTE: ecore_con_info_get is now private has it can't be used outside of Ecore_Con.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/ecore@50425 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/lib/ecore_con/Ecore_Con.h
src/lib/ecore_con/ecore_con.c
src/lib/ecore_con/ecore_con_private.h

index 7061f9f..8886cf6 100644 (file)
@@ -75,9 +75,12 @@ extern "C" {
    typedef struct _Ecore_Con_Server  Ecore_Con_Server; /**< A connection handle */
    typedef struct _Ecore_Con_Client  Ecore_Con_Client; /**< A connection handle */
    typedef struct _Ecore_Con_Url     Ecore_Con_Url;
-   typedef struct _Ecore_Con_Info    Ecore_Con_Info;
 
-   typedef void (*Ecore_Con_Info_Cb)(void *data, Ecore_Con_Info *infos);
+   typedef void (*Ecore_Con_Dns_Cb)(const char *canonname,
+                                   const char *ip,
+                                   struct sockaddr *addr,
+                                   int addrlen,
+                                   void *data);
 
    typedef enum _Ecore_Con_Type
      {
@@ -89,7 +92,7 @@ extern "C" {
        ECORE_CON_REMOTE_UDP       = 5,
        ECORE_CON_REMOTE_BROADCAST = 6,
        ECORE_CON_REMOTE_NODELAY   = 7,
-          
+
        ECORE_CON_USE_SSL2 = (1 << 4),
        ECORE_CON_USE_SSL3 = (1 << 5),
        ECORE_CON_USE_TLS  = (1 << 6)
@@ -227,7 +230,7 @@ extern "C" {
    EAPI int               ecore_con_url_send(Ecore_Con_Url *url_con, const void *data, size_t length, const char *content_type);
    EAPI void              ecore_con_url_time(Ecore_Con_Url *url_con, Ecore_Con_Url_Time condition, time_t tm);
 
-   EAPI int              ecore_con_info_get(Ecore_Con_Server *svr, Ecore_Con_Info_Cb done_cb, void *data, struct addrinfo *hints);
+   EAPI Eina_Bool         ecore_con_lookup(const char *name, Ecore_Con_Dns_Cb done_cb, const void *data);
 
    EAPI int              ecore_con_url_ftp_upload(Ecore_Con_Url *url_con, const char *filename, const char *user, const char *pass, const char *upload_dir);
    EAPI void             ecore_con_url_verbose_set(Ecore_Con_Url *url_con, int verbose);
index 265e0f1..682b331 100644 (file)
@@ -71,6 +71,8 @@ static void _ecore_con_event_server_add_free(void *data, void *ev);
 static void _ecore_con_event_server_del_free(void *data, void *ev);
 static void _ecore_con_event_server_data_free(void *data, void *ev);
 
+static void _ecore_con_lookup_done(void *data, Ecore_Con_Info *infos);
+
 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
@@ -742,6 +744,68 @@ ecore_con_client_flush(Ecore_Con_Client *cl)
    _ecore_con_client_flush(cl);
 }
 
+/**
+ * Do an asynchronous DNS lookup.
+ *
+ * @params name IP address or server name to translate.
+ * @params done_cb Callback to notify when done.
+ * @params data User data to be given to done_cb.
+ * @return EINA_TRUE if the request is going on, EINA_FALSE if it failed.
+ */
+EAPI Eina_Bool
+ecore_con_lookup(const char *name, Ecore_Con_Dns_Cb done_cb, const void *data)
+{
+   Ecore_Con_Server *svr;
+   Ecore_Con_Lookup *lk;
+   struct addrinfo hints;
+
+   if (!name || !done_cb)
+     return EINA_FALSE;
+
+   svr = calloc(1, sizeof(Ecore_Con_Server));
+   if (!svr) return EINA_FALSE;
+
+   lk = malloc(sizeof (Ecore_Con_Lookup));
+   if (!lk)
+     {
+       free(svr);
+       return EINA_FALSE;
+     }
+
+   lk->done_cb = done_cb;
+   lk->data = data;
+
+   svr->name = strdup(name);
+   if (!svr->name) goto on_error;
+
+   svr->type = ECORE_CON_REMOTE_TCP;
+   svr->port = 1025;
+   svr->data = lk;
+   svr->created = 1;
+   svr->reject_excess_clients = 0;
+   svr->client_limit = -1;
+   svr->clients = NULL;
+   svr->ppid = getpid();
+
+   memset(&hints, 0, sizeof(struct addrinfo));
+   hints.ai_family = AF_INET6;
+   hints.ai_socktype = SOCK_STREAM;
+   hints.ai_flags = AI_CANONNAME;
+   hints.ai_protocol = IPPROTO_TCP;
+   hints.ai_canonname = NULL;
+   hints.ai_next = NULL;
+   hints.ai_addr = NULL;
+
+   if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr, &hints))
+     return EINA_TRUE;
+
+   free(svr->name);
+ on_error:
+   free(lk);
+   free(svr);
+   return EINA_FALSE;
+}
+
 static void
 _ecore_con_server_free(Ecore_Con_Server *svr)
 {
@@ -1711,3 +1775,23 @@ _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
      _ecore_con_server_free(e->server);
    free(e);
 }
+
+static void
+_ecore_con_lookup_done(void *data, Ecore_Con_Info *infos)
+{
+   Ecore_Con_Server *svr;
+   Ecore_Con_Lookup *lk;
+
+   svr = data;
+   lk = svr->data;
+
+   if (infos)
+     lk->done_cb(infos->info.ai_canonname, infos->ip, infos->info.ai_addr, infos->info.ai_addrlen, (void*) lk->data);
+   else
+     lk->done_cb(NULL, NULL, NULL, 0, (void*) lk->data);
+
+   free(svr->name);
+   free(lk);
+   free(svr);
+}
+
index b07fe2d..b5efa9e 100644 (file)
@@ -54,6 +54,11 @@ extern int  _ecore_con_log_dom ;
 #endif
 #define CRIT(...) EINA_LOG_DOM_CRIT(_ecore_con_log_dom, __VA_ARGS__)
 
+typedef struct _Ecore_Con_Lookup Ecore_Con_Lookup;
+typedef struct _Ecore_Con_Info Ecore_Con_Info;
+
+typedef void (*Ecore_Con_Info_Cb)(void *data, Ecore_Con_Info *infos);
+
 typedef enum _Ecore_Con_State
   {
     ECORE_CON_CONNECTED,
@@ -163,6 +168,12 @@ struct _Ecore_Con_Info
    char                   service[NI_MAXSERV];
 };
 
+struct _Ecore_Con_Lookup
+{
+   Ecore_Con_Dns_Cb done_cb;
+   const void *data;
+};
+
 /* from ecore_local.c */
 int ecore_con_local_init(void);
 int ecore_con_local_shutdown(void);
@@ -191,5 +202,11 @@ Ecore_Con_State     ecore_con_ssl_client_try(Ecore_Con_Client *svr);
 int                 ecore_con_ssl_client_read(Ecore_Con_Client *svr, unsigned char *buf, int size);
 int                 ecore_con_ssl_client_write(Ecore_Con_Client *svr, unsigned char *buf, int size);
 
+int                ecore_con_info_get(Ecore_Con_Server *svr,
+                                      Ecore_Con_Info_Cb done_cb,
+                                      void *data,
+                                      struct addrinfo *hints);
+
+
 
 #endif