loudmouth/lm-blocking-resolver.c
author Frank Zschockelt <lm@freakysoft.de>
Sat, 11 May 2019 22:25:49 +0200
changeset 738 264fece7ff0d
parent 725 05fa3e01e5b1
permissions -rw-r--r--
Fix getaddrinfo() handling in blocking resolver If getaddrinfo() fails, the first call setting the result will already free the resolver. Trying to access it afterwards will lead to a warning to the console from glib. getaddrinfo() shouldn't return NULL for the result list if it returns successful.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
516
4dd3aa6b83e5 Change the tab width to 4 steps in the emacs headers
Mikael Hallendal <micke@imendio.com>
parents: 515
diff changeset
     1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     2
/*
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     3
 * Copyright (C) 2008 Imendio AB
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     4
 *
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public License as
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     7
 * published by the Free Software Foundation; either version 2 of the
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     8
 * License, or (at your option) any later version.
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
     9
 *
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    13
 * Lesser General Public License for more details.
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    14
 *
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
690
7ccf2113ec5f Update the postal address of the FSF
Frank Zschockelt <lm@freakysoft.de>
parents: 677
diff changeset
    16
 * License along with this program; if not, see <https://www.gnu.org/licenses>
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    17
 */
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    18
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    19
#include <config.h>
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    20
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    21
#include <string.h>
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    22
#include <sys/types.h>
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    23
#include <netdb.h>
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    24
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    25
/* Needed on Mac OS X */
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    26
#if HAVE_ARPA_NAMESER_COMPAT_H
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    27
#include <arpa/nameser_compat.h>
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    28
#endif
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    29
677
a6a41aec40f2 include netinet/in.h in resolver
Christoph Moench-Tegeder <cmt@burggraben.net>
parents: 624
diff changeset
    30
#include <netinet/in.h>
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    31
#include <arpa/nameser.h>
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    32
#include <resolv.h>
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    33
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    34
#include "lm-marshal.h"
462
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
    35
#include "lm-misc.h"
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
    36
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    37
#include "lm-blocking-resolver.h"
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    38
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    39
#define SRV_LEN 8192
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    40
725
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
    41
#define GET_PRIV(obj) (lm_blocking_resolver_get_instance_private (LM_BLOCKING_RESOLVER(obj)))
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    42
725
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
    43
typedef struct LmBlockingResolverPrivate LmBlockingResolverPrivate;
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
    44
struct LmBlockingResolverPrivate {
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    45
    GSource *idle_source;
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    46
};
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    47
712
ba47719252ad lm-asyncns-resolver: Fix resolver uninitialization
Mykhailo Danylenko <risbea@gmail.com>
parents: 690
diff changeset
    48
static void     blocking_resolver_dispose     (GObject       *object);
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    49
static void     blocking_resolver_lookup      (LmResolver    *resolver);
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    50
static void     blocking_resolver_cancel      (LmResolver    *resolver);
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    51
725
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
    52
G_DEFINE_TYPE_WITH_PRIVATE (LmBlockingResolver, lm_blocking_resolver, LM_TYPE_RESOLVER)
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    53
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    54
static void
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    55
lm_blocking_resolver_class_init (LmBlockingResolverClass *class)
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    56
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    57
    GObjectClass    *object_class   = G_OBJECT_CLASS (class);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    58
    LmResolverClass *resolver_class = LM_RESOLVER_CLASS (class);
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    59
712
ba47719252ad lm-asyncns-resolver: Fix resolver uninitialization
Mykhailo Danylenko <risbea@gmail.com>
parents: 690
diff changeset
    60
    object_class->dispose = blocking_resolver_dispose;
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    61
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    62
    resolver_class->lookup = blocking_resolver_lookup;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    63
    resolver_class->cancel = blocking_resolver_cancel;
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    64
}
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    65
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    66
static void
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    67
lm_blocking_resolver_init (LmBlockingResolver *blocking_resolver)
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    68
{
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    69
}
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    70
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    71
static void
712
ba47719252ad lm-asyncns-resolver: Fix resolver uninitialization
Mykhailo Danylenko <risbea@gmail.com>
parents: 690
diff changeset
    72
blocking_resolver_dispose (GObject *object)
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    73
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    74
    /* Ensure we don't have an idle around */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    75
    blocking_resolver_cancel (LM_RESOLVER (object));
460
4d978afca525 More work on the blocking resolver
Mikael Hallendal <micke@imendio.com>
parents: 459
diff changeset
    76
712
ba47719252ad lm-asyncns-resolver: Fix resolver uninitialization
Mykhailo Danylenko <risbea@gmail.com>
parents: 690
diff changeset
    77
    (G_OBJECT_CLASS (lm_blocking_resolver_parent_class)->dispose) (object);
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    78
}
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
    79
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
    80
static gboolean
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
    81
blocking_resolver_lookup_host (LmBlockingResolver *resolver)
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
    82
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    83
    gchar           *host;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    84
    struct addrinfo  req;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    85
    struct addrinfo *ans;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    86
    int              err;
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
    87
    gboolean         retval = TRUE;
462
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
    88
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    89
    g_object_get (resolver, "host", &host, NULL);
462
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
    90
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    91
    /* Lookup */
462
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
    92
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    93
    memset (&req, 0, sizeof(req));
587
7c793095138e Fixed Retry Behavior, Fixed Crash on IPv6 Addresses, Log and Exit On No Host
Jayson Vantuyl <jvantuyl@engineyard.com>
parents: 586
diff changeset
    94
    req.ai_family   = PF_UNSPEC;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    95
    req.ai_socktype = SOCK_STREAM;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    96
    req.ai_protocol = IPPROTO_TCP;
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    97
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
    98
    err = getaddrinfo (host, NULL, &req, &ans);
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
    99
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   100
    if (err != 0) {
547
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   101
        _lm_resolver_set_result (LM_RESOLVER (resolver), LM_RESOLVER_RESULT_FAILED,
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   102
                                 NULL);
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   103
        retval = FALSE;
738
264fece7ff0d Fix getaddrinfo() handling in blocking resolver
Frank Zschockelt <lm@freakysoft.de>
parents: 725
diff changeset
   104
    } else {
585
bccdb423a8e9 Fixed Resolver Memory Management Issues (by creating less severe issues)
Jayson Vantuyl <jvantuyl@engineyard.com>
parents: 584
diff changeset
   105
        _lm_resolver_set_result (LM_RESOLVER (resolver), LM_RESOLVER_RESULT_OK,
bccdb423a8e9 Fixed Resolver Memory Management Issues (by creating less severe issues)
Jayson Vantuyl <jvantuyl@engineyard.com>
parents: 584
diff changeset
   106
                                 ans);
bccdb423a8e9 Fixed Resolver Memory Management Issues (by creating less severe issues)
Jayson Vantuyl <jvantuyl@engineyard.com>
parents: 584
diff changeset
   107
    }
477
09fe238533b9 Put a ref on the resolver while calling the callback.
Mikael Hallendal <micke@imendio.com>
parents: 473
diff changeset
   108
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   109
    g_free (host);
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   110
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   111
    return retval;
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   112
}
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   113
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   114
static gboolean
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   115
blocking_resolver_lookup_service (LmBlockingResolver *resolver)
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   116
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   117
    gchar *domain;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   118
    gchar *service;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   119
    gchar *protocol;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   120
    gchar *srv;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   121
    gchar *new_server = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   122
    guint  new_port = 0;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   123
    gboolean  result;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   124
    unsigned char    srv_ans[SRV_LEN];
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   125
    int              len;
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   126
    gboolean         retval = TRUE;
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   127
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   128
    g_object_get (resolver,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   129
                  "domain", &domain,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   130
                  "service", &service,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   131
                  "protocol", &protocol,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   132
                  NULL);
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   133
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   134
    srv = _lm_resolver_create_srv_string (domain, service, protocol);
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   135
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   136
    res_init ();
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
   137
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   138
    len = res_query (srv, C_IN, T_SRV, srv_ans, SRV_LEN);
462
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
   139
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   140
    result = _lm_resolver_parse_srv_response (srv_ans, len,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   141
                                              &new_server, &new_port);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   142
    if (result == FALSE) {
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   143
        retval = FALSE;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   144
    }
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
   145
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   146
    g_object_set (resolver,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   147
                  "host", new_server,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   148
                  "port", new_port,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   149
                  NULL);
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
   150
547
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   151
    g_object_ref (resolver);
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   152
    _lm_resolver_set_result (LM_RESOLVER (resolver), LM_RESOLVER_RESULT_OK, NULL);
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   153
    g_object_unref (resolver);
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   154
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   155
    /* Lookup the new server and the new port */
547
692c7753f64e Improved the resolvers a bit.
Mikael Hallendal <micke@imendio.com>
parents: 518
diff changeset
   156
   /* blocking_resolver_lookup_host (resolver); */
463
158aebfa7b7c Moved MIN_PORT and MAX_PORT to lm-internals.h and renamed them.
Mikael Hallendal <micke@imendio.com>
parents: 462
diff changeset
   157
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   158
    g_free (new_server);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   159
    g_free (srv);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   160
    g_free (domain);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   161
    g_free (service);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   162
    g_free (protocol);
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   163
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   164
    return retval;
461
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   165
}
f7c5f4b5dadd Added protocol for srv lookups
Mikael Hallendal <micke@imendio.com>
parents: 460
diff changeset
   166
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   167
static gboolean
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   168
blocking_resolver_idle_lookup (LmBlockingResolver *resolver)
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   169
{
725
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
   170
    LmBlockingResolverPrivate *priv = GET_PRIV (resolver);
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   171
    gint                    type;
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   172
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   173
    /* Start the DNS querying */
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   174
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   175
    /* Decide if we are going to lookup a srv or host */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   176
    g_object_get (resolver, "type", &type, NULL);
460
4d978afca525 More work on the blocking resolver
Mikael Hallendal <micke@imendio.com>
parents: 459
diff changeset
   177
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   178
    switch (type) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   179
    case LM_RESOLVER_HOST:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   180
        blocking_resolver_lookup_host (resolver);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   181
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   182
    case LM_RESOLVER_SRV:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   183
        blocking_resolver_lookup_service (resolver);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   184
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   185
    };
460
4d978afca525 More work on the blocking resolver
Mikael Hallendal <micke@imendio.com>
parents: 459
diff changeset
   186
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   187
    /* End of DNS querying */
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   188
    priv->idle_source = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   189
    return FALSE;
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   190
}
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   191
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   192
static void
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   193
blocking_resolver_lookup (LmResolver *resolver)
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   194
{
725
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
   195
    LmBlockingResolverPrivate *priv;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   196
    GMainContext           *context;
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   197
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   198
    g_return_if_fail (LM_IS_BLOCKING_RESOLVER (resolver));
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   199
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   200
    priv = GET_PRIV (resolver);
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   201
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   202
    g_object_get (resolver, "context", &context, NULL);
462
11f85004aa32 Added GMainContext to LmResolver and use lm_misc_add_idle to add an idle in it.
Mikael Hallendal <micke@imendio.com>
parents: 461
diff changeset
   203
581
757d716a8f8a Applied ksmith's DNS patch.
Samuel Tesla <stesla@engineyard.com>
parents: 578
diff changeset
   204
    priv->idle_source = lm_misc_add_idle (context,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   205
                                          (GSourceFunc) blocking_resolver_idle_lookup,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   206
                                          resolver);
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   207
}
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   208
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   209
static void
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   210
blocking_resolver_cancel (LmResolver *resolver)
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   211
{
725
05fa3e01e5b1 Move away from g_type_class_add_private()
Michał Kępień <github@kempniu.pl>
parents: 712
diff changeset
   212
    LmBlockingResolverPrivate *priv;
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   213
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   214
    g_return_if_fail (LM_IS_BLOCKING_RESOLVER (resolver));
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   215
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   216
    priv = GET_PRIV (resolver);
459
e6e0a95d108a Implemented the idle "spawn" in LmBlockingResolver.
Mikael Hallendal <micke@imendio.com>
parents: 458
diff changeset
   217
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   218
    if (priv->idle_source) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   219
        g_source_destroy (priv->idle_source);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   220
        priv->idle_source = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 516
diff changeset
   221
    }
458
0cd193ceddaa Added LmBlockingResolver which will handle the case when we do not have libasyncns.
Mikael Hallendal <micke@imendio.com>
parents:
diff changeset
   222
}