dbus.c 39.9 KB
Newer Older
1
/*
2
 * DBus devices support
3
 *
4
 * Copyright 2006, 2011 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
27
#include <stdio.h>
28
#ifdef SONAME_LIBDBUS_1
29
# include <dbus/dbus.h>
30 31
#endif
#ifdef SONAME_LIBHAL
32 33
# include <hal/libhal.h>
#endif
34

35
#include "mountmgr.h"
36
#include "winnls.h"
37
#include "excpt.h"
38 39 40 41 42 43 44 45
#define USE_WS_PREFIX
#include "winsock2.h"
#include "ws2ipdef.h"
#include "nldef.h"
#include "netioapi.h"
#include "inaddr.h"
#include "ip2string.h"
#include "dhcpcsdk.h"
46 47 48 49

#include "wine/exception.h"
#include "wine/debug.h"

50
WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
51

52
#ifdef SONAME_LIBDBUS_1
53

54
#define DBUS_FUNCS               \
55
    DO_FUNC(dbus_bus_add_match); \
56
    DO_FUNC(dbus_bus_get); \
57
    DO_FUNC(dbus_bus_get_private); \
58 59
    DO_FUNC(dbus_bus_remove_match); \
    DO_FUNC(dbus_connection_add_filter); \
60
    DO_FUNC(dbus_connection_read_write_dispatch); \
61 62
    DO_FUNC(dbus_connection_remove_filter); \
    DO_FUNC(dbus_connection_send_with_reply_and_block); \
63
    DO_FUNC(dbus_error_free); \
64 65 66 67 68 69 70 71 72 73 74 75
    DO_FUNC(dbus_error_init); \
    DO_FUNC(dbus_error_is_set); \
    DO_FUNC(dbus_free_string_array); \
    DO_FUNC(dbus_message_get_args); \
    DO_FUNC(dbus_message_get_interface); \
    DO_FUNC(dbus_message_get_member); \
    DO_FUNC(dbus_message_get_path); \
    DO_FUNC(dbus_message_get_type); \
    DO_FUNC(dbus_message_is_signal); \
    DO_FUNC(dbus_message_iter_append_basic); \
    DO_FUNC(dbus_message_iter_get_arg_type); \
    DO_FUNC(dbus_message_iter_get_basic); \
76
    DO_FUNC(dbus_message_iter_get_fixed_array); \
77 78 79 80 81
    DO_FUNC(dbus_message_iter_init); \
    DO_FUNC(dbus_message_iter_init_append); \
    DO_FUNC(dbus_message_iter_next); \
    DO_FUNC(dbus_message_iter_recurse); \
    DO_FUNC(dbus_message_new_method_call); \
82
    DO_FUNC(dbus_message_unref)
83 84 85 86 87 88 89 90 91

#define DO_FUNC(f) static typeof(f) * p_##f
DBUS_FUNCS;
#undef DO_FUNC

static int udisks_timeout = -1;
static DBusConnection *connection;

#ifdef SONAME_LIBHAL
92 93 94 95 96 97 98 99 100 101 102

#define HAL_FUNCS \
    DO_FUNC(libhal_ctx_free); \
    DO_FUNC(libhal_ctx_init); \
    DO_FUNC(libhal_ctx_new); \
    DO_FUNC(libhal_ctx_set_dbus_connection); \
    DO_FUNC(libhal_ctx_set_device_added); \
    DO_FUNC(libhal_ctx_set_device_property_modified); \
    DO_FUNC(libhal_ctx_set_device_removed); \
    DO_FUNC(libhal_ctx_shutdown); \
    DO_FUNC(libhal_device_get_property_bool); \
103
    DO_FUNC(libhal_device_get_property_int); \
104 105 106 107 108 109 110 111 112 113 114
    DO_FUNC(libhal_device_get_property_string); \
    DO_FUNC(libhal_device_add_property_watch); \
    DO_FUNC(libhal_device_remove_property_watch); \
    DO_FUNC(libhal_free_string); \
    DO_FUNC(libhal_free_string_array); \
    DO_FUNC(libhal_get_all_devices)

#define DO_FUNC(f) static typeof(f) * p_##f
HAL_FUNCS;
#undef DO_FUNC

115
static BOOL load_hal_functions(void)
116
{
117
    void *hal_handle;
118

119 120 121
    /* Load libhal with RTLD_GLOBAL so that the dbus symbols are available.
     * We can't load libdbus directly since libhal may have been built against a
     * different version but with the same soname. Binary compatibility is for wimps. */
122

123
    if (!(hal_handle = dlopen( SONAME_LIBHAL, RTLD_NOW | RTLD_GLOBAL )))
124 125
        goto failed;

126
#define DO_FUNC(f) if (!(p_##f = dlsym( RTLD_DEFAULT, #f ))) goto failed
127 128 129
    DBUS_FUNCS;
#undef DO_FUNC

130
#define DO_FUNC(f) if (!(p_##f = dlsym( hal_handle, #f ))) goto failed
131 132 133
    HAL_FUNCS;
#undef DO_FUNC

134
    udisks_timeout = 3000;  /* don't try for too long if we can fall back to HAL */
135 136 137
    return TRUE;

failed:
138
    WARN( "failed to load HAL support: %s\n", dlerror() );
139 140 141
    return FALSE;
}

142 143
#endif /* SONAME_LIBHAL */

144
static LONG WINAPI assert_fault(EXCEPTION_POINTERS *eptr)
145
{
146 147
    if (eptr->ExceptionRecord->ExceptionCode == EXCEPTION_WINE_ASSERTION)
        return EXCEPTION_EXECUTE_HANDLER;
148 149 150
    return EXCEPTION_CONTINUE_SEARCH;
}

151 152 153 154 155
static inline int starts_with( const char *str, const char *prefix )
{
    return !strncmp( str, prefix, strlen(prefix) );
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static GUID *parse_uuid( GUID *guid, const char *str )
{
    /* standard uuid format */
    if (strlen(str) == 36)
    {
        UNICODE_STRING strW;
        WCHAR buffer[39];

        if (MultiByteToWideChar( CP_UNIXCP, 0, str, 36, buffer + 1, 36 ))
        {
            buffer[0] = '{';
            buffer[37] = '}';
            buffer[38] = 0;
            RtlInitUnicodeString( &strW, buffer );
            if (!RtlGUIDFromString( &strW, guid )) return guid;
        }
    }

    /* check for xxxx-xxxx format (FAT serial number) */
    if (strlen(str) == 9 && str[4] == '-')
    {
        memset( guid, 0, sizeof(*guid) );
        if (sscanf( str, "%hx-%hx", &guid->Data2, &guid->Data3 ) == 2) return guid;
    }
    return NULL;
}

183 184 185 186
static BOOL load_dbus_functions(void)
{
    void *handle;

187
    if (!(handle = dlopen( SONAME_LIBDBUS_1, RTLD_NOW )))
188 189
        goto failed;

190
#define DO_FUNC(f) if (!(p_##f = dlsym( handle, #f ))) goto failed
191 192 193 194 195
    DBUS_FUNCS;
#undef DO_FUNC
    return TRUE;

failed:
196
    WARN( "failed to load DBUS support: %s\n", dlerror() );
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    return FALSE;
}

static const char *udisks_next_dict_entry( DBusMessageIter *iter, DBusMessageIter *variant )
{
    DBusMessageIter sub;
    const char *name;

    if (p_dbus_message_iter_get_arg_type( iter ) != DBUS_TYPE_DICT_ENTRY) return NULL;
    p_dbus_message_iter_recurse( iter, &sub );
    p_dbus_message_iter_next( iter );
    p_dbus_message_iter_get_basic( &sub, &name );
    p_dbus_message_iter_next( &sub );
    p_dbus_message_iter_recurse( &sub, variant );
    return name;
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
static enum device_type udisks_parse_media_compatibility( DBusMessageIter *iter )
{
    DBusMessageIter media;
    enum device_type drive_type = DEVICE_UNKNOWN;

    p_dbus_message_iter_recurse( iter, &media );
    while (p_dbus_message_iter_get_arg_type( &media ) == DBUS_TYPE_STRING)
    {
        const char *media_type;
        p_dbus_message_iter_get_basic( &media, &media_type );
        if (starts_with( media_type, "optical_dvd" ))
            drive_type = DEVICE_DVD;
        if (starts_with( media_type, "floppy" ))
            drive_type = DEVICE_FLOPPY;
        else if (starts_with( media_type, "optical_" ) && drive_type == DEVICE_UNKNOWN)
            drive_type = DEVICE_CDROM;
        p_dbus_message_iter_next( &media );
    }
    return drive_type;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
/* UDisks callback for new device */
static void udisks_new_device( const char *udi )
{
    static const char *dev_name = "org.freedesktop.UDisks.Device";
    DBusMessage *request, *reply;
    DBusMessageIter iter, variant;
    DBusError error;
    const char *device = NULL;
    const char *mount_point = NULL;
    const char *type = NULL;
    GUID guid, *guid_ptr = NULL;
    int removable = FALSE;
    enum device_type drive_type = DEVICE_UNKNOWN;

    request = p_dbus_message_new_method_call( "org.freedesktop.UDisks", udi,
                                              "org.freedesktop.DBus.Properties", "GetAll" );
    if (!request) return;

    p_dbus_message_iter_init_append( request, &iter );
    p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &dev_name );

    p_dbus_error_init( &error );
    reply = p_dbus_connection_send_with_reply_and_block( connection, request, -1, &error );
    p_dbus_message_unref( request );
    if (!reply)
    {
261
        WARN( "failed: %s\n", error.message );
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        p_dbus_error_free( &error );
        return;
    }
    p_dbus_error_free( &error );

    p_dbus_message_iter_init( reply, &iter );
    if (p_dbus_message_iter_get_arg_type( &iter ) == DBUS_TYPE_ARRAY)
    {
        const char *name;

        p_dbus_message_iter_recurse( &iter, &iter );
        while ((name = udisks_next_dict_entry( &iter, &variant )))
        {
            if (!strcmp( name, "DeviceFile" ))
                p_dbus_message_iter_get_basic( &variant, &device );
            else if (!strcmp( name, "DeviceIsRemovable" ))
                p_dbus_message_iter_get_basic( &variant, &removable );
            else if (!strcmp( name, "IdType" ))
                p_dbus_message_iter_get_basic( &variant, &type );
            else if (!strcmp( name, "DriveMediaCompatibility" ))
282
                drive_type = udisks_parse_media_compatibility( &variant );
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
            else if (!strcmp( name, "DeviceMountPaths" ))
            {
                DBusMessageIter paths;
                p_dbus_message_iter_recurse( &variant, &paths );
                if (p_dbus_message_iter_get_arg_type( &paths ) == DBUS_TYPE_STRING)
                    p_dbus_message_iter_get_basic( &paths, &mount_point );
            }
            else if (!strcmp( name, "IdUuid" ))
            {
                char *uuid_str;
                p_dbus_message_iter_get_basic( &variant, &uuid_str );
                guid_ptr = parse_uuid( &guid, uuid_str );
            }
        }
    }

    TRACE( "udi %s device %s mount point %s uuid %s type %s removable %u\n",
           debugstr_a(udi), debugstr_a(device), debugstr_a(mount_point),
           debugstr_guid(guid_ptr), debugstr_a(type), removable );

    if (type)
    {
        if (!strcmp( type, "iso9660" ))
        {
            removable = TRUE;
            drive_type = DEVICE_CDROM;
        }
        else if (!strcmp( type, "udf" ))
        {
            removable = TRUE;
            drive_type = DEVICE_DVD;
        }
    }

    if (device)
    {
319
        if (removable) add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr, NULL );
320
        else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr, NULL );
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    }

    p_dbus_message_unref( reply );
}

/* UDisks callback for removed device */
static void udisks_removed_device( const char *udi )
{
    TRACE( "removed %s\n", wine_dbgstr_a(udi) );

    if (!remove_dos_device( -1, udi )) remove_volume( udi );
}

/* UDisks callback for changed device */
static void udisks_changed_device( const char *udi )
{
    udisks_new_device( udi );
}

static BOOL udisks_enumerate_devices(void)
{
    DBusMessage *request, *reply;
    DBusError error;
    char **paths;
    int i, count;

    request = p_dbus_message_new_method_call( "org.freedesktop.UDisks", "/org/freedesktop/UDisks",
                                              "org.freedesktop.UDisks", "EnumerateDevices" );
    if (!request) return FALSE;

    p_dbus_error_init( &error );
    reply = p_dbus_connection_send_with_reply_and_block( connection, request, udisks_timeout, &error );
    p_dbus_message_unref( request );
    if (!reply)
    {
        WARN( "failed: %s\n", error.message );
        p_dbus_error_free( &error );
        return FALSE;
    }
    p_dbus_error_free( &error );

    if (p_dbus_message_get_args( reply, &error, DBUS_TYPE_ARRAY,
                                 DBUS_TYPE_OBJECT_PATH, &paths, &count, DBUS_TYPE_INVALID ))
    {
        for (i = 0; i < count; i++) udisks_new_device( paths[i] );
        p_dbus_free_string_array( paths );
    }
    else WARN( "unexpected args in EnumerateDevices reply\n" );

    p_dbus_message_unref( reply );
    return TRUE;
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387
/* to make things easier, UDisks2 stores strings as array of bytes instead of strings... */
static const char *udisks2_string_from_array( DBusMessageIter *iter )
{
    DBusMessageIter string;
    const char *array;
    int size;

    p_dbus_message_iter_recurse( iter, &string );
    p_dbus_message_iter_get_fixed_array( &string, &array, &size );
    return array;
}

/* find the drive entry in the dictionary and get its parameters */
static void udisks2_get_drive_info( const char *drive_name, DBusMessageIter *dict,
388
                                    enum device_type *drive_type, int *removable, const char **serial )
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
{
    DBusMessageIter iter, drive, variant;
    const char *name;

    p_dbus_message_iter_recurse( dict, &iter );
    while ((name = udisks_next_dict_entry( &iter, &drive )))
    {
        if (strcmp( name, drive_name )) continue;
        while ((name = udisks_next_dict_entry( &drive, &iter )))
        {
            if (strcmp( name, "org.freedesktop.UDisks2.Drive" )) continue;
            while ((name = udisks_next_dict_entry( &iter, &variant )))
            {
                if (!strcmp( name, "Removable" ))
                    p_dbus_message_iter_get_basic( &variant, removable );
                else if (!strcmp( name, "MediaCompatibility" ))
                    *drive_type = udisks_parse_media_compatibility( &variant );
406 407
                else if (!strcmp( name, "Id" ))
                    p_dbus_message_iter_get_basic( &variant, serial );
408 409 410 411 412 413 414 415 416 417 418 419
            }
        }
    }
}

static void udisks2_add_device( const char *udi, DBusMessageIter *dict, DBusMessageIter *block )
{
    DBusMessageIter iter, variant, paths, string;
    const char *device = NULL;
    const char *mount_point = NULL;
    const char *type = NULL;
    const char *drive = NULL;
420
    const char *id = NULL;
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    GUID guid, *guid_ptr = NULL;
    const char *iface, *name;
    int removable = FALSE;
    enum device_type drive_type = DEVICE_UNKNOWN;

    while ((iface = udisks_next_dict_entry( block, &iter )))
    {
        if (!strcmp( iface, "org.freedesktop.UDisks2.Filesystem" ))
        {
            while ((name = udisks_next_dict_entry( &iter, &variant )))
            {
                if (!strcmp( name, "MountPoints" ))
                {
                    p_dbus_message_iter_recurse( &variant, &paths );
                    if (p_dbus_message_iter_get_arg_type( &paths ) == DBUS_TYPE_ARRAY)
                    {
                        p_dbus_message_iter_recurse( &variant, &string );
                        mount_point = udisks2_string_from_array( &string );
                    }
                }
            }
        }
        if (!strcmp( iface, "org.freedesktop.UDisks2.Block" ))
        {
            while ((name = udisks_next_dict_entry( &iter, &variant )))
            {
                if (!strcmp( name, "Device" ))
                    device = udisks2_string_from_array( &variant );
                else if (!strcmp( name, "IdType" ))
                    p_dbus_message_iter_get_basic( &variant, &type );
                else if (!strcmp( name, "Drive" ))
                {
                    p_dbus_message_iter_get_basic( &variant, &drive );
454
                    udisks2_get_drive_info( drive, dict, &drive_type, &removable, &id );
455 456 457
                }
                else if (!strcmp( name, "IdUUID" ))
                {
458 459 460 461 462
                    const char *uuid_str;
                    if (p_dbus_message_iter_get_arg_type( &variant ) == DBUS_TYPE_ARRAY)
                        uuid_str = udisks2_string_from_array( &variant );
                    else
                        p_dbus_message_iter_get_basic( &variant, &uuid_str );
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                    guid_ptr = parse_uuid( &guid, uuid_str );
                }
            }
        }
    }

    TRACE( "udi %s device %s mount point %s uuid %s type %s removable %u\n",
           debugstr_a(udi), debugstr_a(device), debugstr_a(mount_point),
           debugstr_guid(guid_ptr), debugstr_a(type), removable );

    if (type)
    {
        if (!strcmp( type, "iso9660" ))
        {
            removable = TRUE;
            drive_type = DEVICE_CDROM;
        }
        else if (!strcmp( type, "udf" ))
        {
            removable = TRUE;
            drive_type = DEVICE_DVD;
        }
    }
    if (device)
    {
488
        if (removable) add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr, NULL );
489
        else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr, id );
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    }
}

/* UDisks2 is almost, but not quite, entirely unlike UDisks.
 * It would have been easy to make it backwards compatible, but where would be the fun in that?
 */
static BOOL udisks2_add_devices( const char *changed )
{
    DBusMessage *request, *reply;
    DBusMessageIter dict, iter, block;
    DBusError error;
    const char *udi;

    request = p_dbus_message_new_method_call( "org.freedesktop.UDisks2", "/org/freedesktop/UDisks2",
                                              "org.freedesktop.DBus.ObjectManager", "GetManagedObjects" );
    if (!request) return FALSE;

    p_dbus_error_init( &error );
    reply = p_dbus_connection_send_with_reply_and_block( connection, request, udisks_timeout, &error );
    p_dbus_message_unref( request );
    if (!reply)
    {
        WARN( "failed: %s\n", error.message );
        p_dbus_error_free( &error );
        return FALSE;
    }
    p_dbus_error_free( &error );

    p_dbus_message_iter_init( reply, &dict );
    if (p_dbus_message_iter_get_arg_type( &dict ) == DBUS_TYPE_ARRAY)
    {
        p_dbus_message_iter_recurse( &dict, &iter );
        while ((udi = udisks_next_dict_entry( &iter, &block )))
        {
            if (!starts_with( udi, "/org/freedesktop/UDisks2/block_devices/" )) continue;
            if (changed && strcmp( changed, udi )) continue;
            udisks2_add_device( udi, &dict, &block );
        }
    }
    else WARN( "unexpected args in GetManagedObjects reply\n" );

    p_dbus_message_unref( reply );
    return TRUE;
}

535 536 537 538 539 540 541
static DBusHandlerResult udisks_filter( DBusConnection *ctx, DBusMessage *msg, void *user_data )
{
    char *path;
    DBusError error;

    p_dbus_error_init( &error );

542
    /* udisks signals */
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    if (p_dbus_message_is_signal( msg, "org.freedesktop.UDisks", "DeviceAdded" ) &&
        p_dbus_message_get_args( msg, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID ))
    {
        udisks_new_device( path );
    }
    else if (p_dbus_message_is_signal( msg, "org.freedesktop.UDisks", "DeviceRemoved" ) &&
             p_dbus_message_get_args( msg, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID ))
    {
        udisks_removed_device( path );
    }
    else if (p_dbus_message_is_signal( msg, "org.freedesktop.UDisks", "DeviceChanged" ) &&
             p_dbus_message_get_args( msg, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID ))
    {
        udisks_changed_device( path );
    }
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    /* udisks2 signals */
    else if (p_dbus_message_is_signal( msg, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded" ) &&
             p_dbus_message_get_args( msg, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID ))
    {
        TRACE( "added %s\n", wine_dbgstr_a(path) );
        udisks2_add_devices( path );
    }
    else if (p_dbus_message_is_signal( msg, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved" ) &&
             p_dbus_message_get_args( msg, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID ))
    {
        udisks_removed_device( path );
    }
    else if (p_dbus_message_is_signal( msg, "org.freedesktop.DBus.Properties", "PropertiesChanged" ))
    {
        const char *udi = p_dbus_message_get_path( msg );
        TRACE( "changed %s\n", wine_dbgstr_a(udi) );
        udisks2_add_devices( udi );
    }
576 577 578 579 580 581 582 583 584 585
    else TRACE( "ignoring message type=%d path=%s interface=%s method=%s\n",
                p_dbus_message_get_type( msg ), p_dbus_message_get_path( msg ),
                p_dbus_message_get_interface( msg ), p_dbus_message_get_member( msg ) );

    p_dbus_error_free( &error );
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

#ifdef SONAME_LIBHAL

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
static BOOL hal_get_ide_parameters( LibHalContext *ctx, const char *udi, SCSI_ADDRESS *scsi_addr, UCHAR *devtype, char *ident, size_t ident_size )
{
    DBusError error;
    char *parent = NULL;
    char *type = NULL;
    char *model = NULL;
    int host, chan;
    BOOL ret = FALSE;

    p_dbus_error_init( &error );

    if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
        goto done;
    if ((host = p_libhal_device_get_property_int( ctx, parent, "ide.host", &error )) < 0)
        goto done;
    if ((chan = p_libhal_device_get_property_int( ctx, parent, "ide.channel", &error )) < 0)
        goto done;

    ret = TRUE;

    if (devtype)
    {
        if (!(type = p_libhal_device_get_property_string( ctx, udi, "storage.drive_type", &error )))
            *devtype = SCSI_UNKNOWN_PERIPHERAL;
        else if (!strcmp( type, "disk" ) || !strcmp( type, "floppy" ))
            *devtype = SCSI_DISK_PERIPHERAL;
        else if (!strcmp( type, "tape" ))
            *devtype = SCSI_TAPE_PERIPHERAL;
        else if (!strcmp( type, "cdrom" ))
            *devtype = SCSI_CDROM_PERIPHERAL;
        else if (!strcmp( type, "raid" ))
            *devtype = SCSI_ARRAY_PERIPHERAL;
        else
            *devtype = SCSI_UNKNOWN_PERIPHERAL;
    }

    if (ident)
    {
        if (!(model = p_libhal_device_get_property_string( ctx, udi, "storage.model", &error )))
            p_dbus_error_free( &error );  /* ignore error */
        else
            lstrcpynA( ident, model, ident_size );
    }

    scsi_addr->PortNumber = host;
    scsi_addr->PathId = 0;
    scsi_addr->TargetId = chan;
    scsi_addr->Lun = 0;

done:
    if (model) p_libhal_free_string( model );
    if (type) p_libhal_free_string( type );
    if (parent) p_libhal_free_string( parent );
    p_dbus_error_free( &error );
    return ret;
}

static BOOL hal_get_scsi_parameters( LibHalContext *ctx, const char *udi, SCSI_ADDRESS *scsi_addr, UCHAR *devtype, char *ident, size_t ident_size )
{
    DBusError error;
    char *type = NULL;
    char *vendor = NULL;
    char *model = NULL;
    int host, bus, target, lun;
    BOOL ret = FALSE;

    p_dbus_error_init( &error );

    if ((host = p_libhal_device_get_property_int( ctx, udi, "scsi.host", &error )) < 0)
        goto done;
    if ((bus = p_libhal_device_get_property_int( ctx, udi, "scsi.bus", &error )) < 0)
        goto done;
    if ((target = p_libhal_device_get_property_int( ctx, udi, "scsi.target", &error )) < 0)
        goto done;
    if ((lun = p_libhal_device_get_property_int( ctx, udi, "scsi.lun", &error )) < 0)
        goto done;

    ret = TRUE;
    scsi_addr->PortNumber = host;
    scsi_addr->PathId = bus;
    scsi_addr->TargetId = target;
    scsi_addr->Lun = lun;

    if (ident)
    {
        if (!(vendor = p_libhal_device_get_property_string( ctx, udi, "scsi.vendor", &error )))
            p_dbus_error_free( &error );  /* ignore error */
        if (!(model = p_libhal_device_get_property_string( ctx, udi, "scsi.model", &error )))
            p_dbus_error_free( &error );  /* ignore error */
        snprintf( ident, ident_size, "%-8s%-16s", vendor ? vendor : "WINE", model ? model : "SCSI" );
    }

    if (devtype)
    {
        if (!(type = p_libhal_device_get_property_string( ctx, udi, "scsi.type", &error )))
        {
            *devtype = SCSI_UNKNOWN_PERIPHERAL;
            goto done;
        }
        if (!strcmp( type, "disk" ))
            *devtype = SCSI_DISK_PERIPHERAL;
        else if (!strcmp( type, "tape" ))
            *devtype = SCSI_TAPE_PERIPHERAL;
        else if (!strcmp( type, "printer" ))
            *devtype = SCSI_PRINTER_PERIPHERAL;
        else if (!strcmp( type, "processor" ))
            *devtype = SCSI_PROCESSOR_PERIPHERAL;
        else if (!strcmp( type, "cdrom" ))
            *devtype = SCSI_CDROM_PERIPHERAL;
        else if (!strcmp( type, "scanner" ))
            *devtype = SCSI_SCANNER_PERIPHERAL;
        else if (!strcmp( type, "medium_changer" ))
            *devtype = SCSI_MEDIUM_CHANGER_PERIPHERAL;
        else if (!strcmp( type, "comm" ))
            *devtype = SCSI_COMMS_PERIPHERAL;
        else if (!strcmp( type, "raid" ))
            *devtype = SCSI_ARRAY_PERIPHERAL;
        else
            *devtype = SCSI_UNKNOWN_PERIPHERAL;
    }

done:
    if (type) p_libhal_free_string( type );
    if (vendor) p_libhal_free_string( vendor );
    if (model) p_libhal_free_string( model );
    p_dbus_error_free( &error );
    return ret;
}

static void hal_new_ide_device( LibHalContext *ctx, const char *udi )
{
    SCSI_ADDRESS scsi_addr;
    UCHAR devtype;
    char ident[40];

    if (!hal_get_ide_parameters( ctx, udi, &scsi_addr, &devtype, ident, sizeof(ident) )) return;
    create_scsi_entry( &scsi_addr, 255, devtype == SCSI_CDROM_PERIPHERAL ? "atapi" : "WINE SCSI", devtype, ident, NULL );
}

static void hal_set_device_name( LibHalContext *ctx, const char *udi, const UNICODE_STRING *devname )
{
    DBusError error;
    SCSI_ADDRESS scsi_addr;
    char *parent = NULL;

    p_dbus_error_init( &error );

    if (!hal_get_ide_parameters( ctx, udi, &scsi_addr, NULL, NULL, 0 ))
    {
        if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
            goto done;
        if (!hal_get_scsi_parameters( ctx, parent, &scsi_addr, NULL, NULL, 0 ))
            goto done;
    }
    set_scsi_device_name( &scsi_addr, devname );

done:
    if (parent) p_libhal_free_string( parent );
    p_dbus_error_free( &error );
}

static void hal_new_scsi_device( LibHalContext *ctx, const char *udi )
{
    SCSI_ADDRESS scsi_addr;
    UCHAR devtype;
    char ident[40];

    if (!hal_get_scsi_parameters( ctx, udi, &scsi_addr, &devtype, ident, sizeof(ident) )) return;
    /* FIXME: get real controller Id for SCSI */
    create_scsi_entry( &scsi_addr, 255, "WINE SCSI", devtype, ident, NULL );
}

758
/* HAL callback for new device */
759
static void hal_new_device( LibHalContext *ctx, const char *udi )
760 761
{
    DBusError error;
762 763 764 765
    char *parent = NULL;
    char *mount_point = NULL;
    char *device = NULL;
    char *type = NULL;
766 767
    char *uuid_str = NULL;
    GUID guid, *guid_ptr = NULL;
768
    enum device_type drive_type;
769 770 771

    p_dbus_error_init( &error );

772 773 774
    hal_new_scsi_device( ctx, udi );
    hal_new_ide_device( ctx, udi );

775 776 777 778 779 780 781 782 783
    if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
        goto done;

    if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
        goto done;

    if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
        goto done;

784 785 786 787 788
    if (!(uuid_str = p_libhal_device_get_property_string( ctx, udi, "volume.uuid", &error )))
        p_dbus_error_free( &error );  /* ignore error */
    else
        guid_ptr = parse_uuid( &guid, uuid_str );

789 790 791
    if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
        p_dbus_error_free( &error );  /* ignore error */

792 793 794
    if (type && !strcmp( type, "cdrom" )) drive_type = DEVICE_CDROM;
    else if (type && !strcmp( type, "floppy" )) drive_type = DEVICE_FLOPPY;
    else drive_type = DEVICE_UNKNOWN;
795

796 797
    if (p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
    {
798 799 800 801
        UNICODE_STRING devname;

        add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr, &devname );
        hal_set_device_name( ctx, parent, &devname );
802 803 804
        /* add property watch for mount point */
        p_libhal_device_add_property_watch( ctx, udi, &error );
    }
805
    else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr, NULL );
806 807

done:
808 809 810
    if (type) p_libhal_free_string( type );
    if (parent) p_libhal_free_string( parent );
    if (device) p_libhal_free_string( device );
811
    if (uuid_str) p_libhal_free_string( uuid_str );
812
    if (mount_point) p_libhal_free_string( mount_point );
813 814 815 816
    p_dbus_error_free( &error );
}

/* HAL callback for removed device */
817
static void hal_removed_device( LibHalContext *ctx, const char *udi )
818 819 820
{
    DBusError error;

821
    TRACE( "removed %s\n", wine_dbgstr_a(udi) );
822

823
    if (!remove_dos_device( -1, udi ))
824 825 826 827 828
    {
        p_dbus_error_init( &error );
        p_libhal_device_remove_property_watch( ctx, udi, &error );
        p_dbus_error_free( &error );
    }
829
    else remove_volume( udi );
830 831 832
}

/* HAL callback for property changes */
833 834
static void hal_property_modified (LibHalContext *ctx, const char *udi,
                                   const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
835
{
836
    TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
837
           is_added ? "added" : is_removed ? "removed" : "modified" );
838

839
    if (!strcmp( key, "volume.mount_point" )) hal_new_device( ctx, udi );
840 841
}

842
static BOOL hal_enumerate_devices(void)
843 844
{
    LibHalContext *ctx;
845
    DBusError error;
846 847 848
    int i, num;
    char **list;

849 850
    if (!p_libhal_ctx_new) return FALSE;
    if (!(ctx = p_libhal_ctx_new())) return FALSE;
851

852
    p_libhal_ctx_set_dbus_connection( ctx, connection );
853 854 855
    p_libhal_ctx_set_device_added( ctx, hal_new_device );
    p_libhal_ctx_set_device_removed( ctx, hal_removed_device );
    p_libhal_ctx_set_device_property_modified( ctx, hal_property_modified );
856

857
    p_dbus_error_init( &error );
858 859
    if (!p_libhal_ctx_init( ctx, &error ))
    {
860
        WARN( "HAL context init failed: %s\n", error.message );
861
        p_dbus_error_free( &error );
862
        return FALSE;
863 864 865 866 867 868
    }

    /* retrieve all existing devices */
    if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
    else
    {
869
        for (i = 0; i < num; i++) hal_new_device( ctx, list[i] );
870 871
        p_libhal_free_string_array( list );
    }
872 873 874 875 876 877 878 879 880 881
    return TRUE;
}

#endif /* SONAME_LIBHAL */

static DWORD WINAPI dbus_thread( void *arg )
{
    static const char udisks_match[] = "type='signal',"
                                       "interface='org.freedesktop.UDisks',"
                                       "sender='org.freedesktop.UDisks'";
882 883 884 885 886 887
    static const char udisks2_match_interfaces[] = "type='signal',"
                                                   "interface='org.freedesktop.DBus.ObjectManager',"
                                                   "path='/org/freedesktop/UDisks2'";
    static const char udisks2_match_properties[] = "type='signal',"
                                                   "interface='org.freedesktop.DBus.Properties'";

888 889 890 891 892 893 894 895 896 897 898

    DBusError error;

    p_dbus_error_init( &error );
    if (!(connection = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
    {
        WARN( "failed to get system dbus connection: %s\n", error.message );
        p_dbus_error_free( &error );
        return 1;
    }

899
    /* first try UDisks2 */
900

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    p_dbus_connection_add_filter( connection, udisks_filter, NULL, NULL );
    p_dbus_bus_add_match( connection, udisks2_match_interfaces, &error );
    p_dbus_bus_add_match( connection, udisks2_match_properties, &error );
    if (udisks2_add_devices( NULL )) goto found;
    p_dbus_bus_remove_match( connection, udisks2_match_interfaces, &error );
    p_dbus_bus_remove_match( connection, udisks2_match_properties, &error );

    /* then try UDisks */

    p_dbus_bus_add_match( connection, udisks_match, &error );
    if (udisks_enumerate_devices()) goto found;
    p_dbus_bus_remove_match( connection, udisks_match, &error );
    p_dbus_connection_remove_filter( connection, udisks_filter, NULL );

    /* then finally HAL */
916 917

#ifdef SONAME_LIBHAL
918 919 920 921
    if (!hal_enumerate_devices())
    {
        p_dbus_error_free( &error );
        return 1;
922
    }
923
#endif
924

925
found:
926 927
    __TRY
    {
928
        while (p_dbus_connection_read_write_dispatch( connection, -1 )) /* nothing */ ;
929 930 931
    }
    __EXCEPT( assert_fault )
    {
932
        WARN( "dbus assertion failure, disabling support\n" );
933 934 935 936 937 938 939
        return 1;
    }
    __ENDTRY;

    return 0;
}

940
void initialize_dbus(void)
941 942 943
{
    HANDLE handle;

944 945 946 947
#ifdef SONAME_LIBHAL
    if (!load_hal_functions())
#endif
        if (!load_dbus_functions()) return;
948
    if (!(handle = CreateThread( NULL, 0, dbus_thread, NULL, 0, NULL ))) return;
949 950 951
    CloseHandle( handle );
}

952 953
#if !defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) || !defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
/* The udisks dispatch loop will block all threads using the same connection, so we'll
   use a private connection. Multiple threads can make methods calls at the same time
   on the same connection, according to the documentation.
 */
static DBusConnection *dhcp_connection;
static DBusConnection *get_dhcp_connection(void)
{
    if (!dhcp_connection)
    {
        DBusError error;
        p_dbus_error_init( &error );
        if (!(dhcp_connection = p_dbus_bus_get_private( DBUS_BUS_SYSTEM, &error )))
        {
            WARN( "failed to get system dbus connection: %s\n", error.message );
            p_dbus_error_free( &error );
        }
    }
    return dhcp_connection;
}

static DBusMessage *device_by_iface_request( const char *iface )
{
    DBusMessage *request, *reply;
    DBusMessageIter iter;
    DBusError error;

    request = p_dbus_message_new_method_call( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
                                              "org.freedesktop.NetworkManager", "GetDeviceByIpIface" );
    if (!request) return NULL;

    p_dbus_message_iter_init_append( request, &iter );
    p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &iface );

    p_dbus_error_init( &error );
    reply = p_dbus_connection_send_with_reply_and_block( get_dhcp_connection(), request, -1, &error );
    p_dbus_message_unref( request );
    if (!reply)
    {
        WARN( "failed: %s\n", error.message );
        p_dbus_error_free( &error );
        return NULL;
    }

    p_dbus_error_free( &error );
    return reply;
}

#define IF_NAMESIZE 16
static BOOL map_adapter_name( const WCHAR *name, char *unix_name, DWORD len )
{
    WCHAR unix_nameW[IF_NAMESIZE];
    UNICODE_STRING str;
    GUID guid;

    RtlInitUnicodeString( &str, name );
    if (!RtlGUIDFromString( &str, &guid ))
    {
        NET_LUID luid;
        if (ConvertInterfaceGuidToLuid( &guid, &luid ) ||
            ConvertInterfaceLuidToNameW( &luid, unix_nameW, ARRAY_SIZE(unix_nameW) )) return FALSE;

        name = unix_nameW;
    }
    return WideCharToMultiByte( CP_UNIXCP, 0, name, -1, unix_name, len, NULL, NULL ) != 0;
}

static DBusMessage *dhcp4_config_request( const WCHAR *adapter )
{
    static const char *device = "org.freedesktop.NetworkManager.Device";
    static const char *dhcp4_config = "Dhcp4Config";
    char iface[IF_NAMESIZE];
    DBusMessage *request, *reply;
    DBusMessageIter iter;
    DBusError error;
    const char *path = NULL;

    if (!map_adapter_name( adapter, iface, sizeof(iface) )) return NULL;
    if (!(reply = device_by_iface_request( iface ))) return NULL;

    p_dbus_message_iter_init( reply, &iter );
    if (p_dbus_message_iter_get_arg_type( &iter ) == DBUS_TYPE_OBJECT_PATH) p_dbus_message_iter_get_basic( &iter, &path );
    p_dbus_message_unref( reply );
    if (!path) return NULL;

    request = p_dbus_message_new_method_call( "org.freedesktop.NetworkManager", path,
                                              "org.freedesktop.DBus.Properties", "Get" );
    if (!request) return NULL;

    p_dbus_message_iter_init_append( request, &iter );
    p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &device );
    p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &dhcp4_config );

    p_dbus_error_init( &error );
    reply = p_dbus_connection_send_with_reply_and_block( get_dhcp_connection(), request, -1, &error );
    p_dbus_message_unref( request );
    if (!reply)
    {
        WARN( "failed: %s\n", error.message );
        p_dbus_error_free( &error );
        return NULL;
    }

    p_dbus_error_free( &error );
    return reply;
}

static DBusMessage *dhcp4_config_options_request( const WCHAR *adapter )
{
    static const char *dhcp4_config = "org.freedesktop.NetworkManager.DHCP4Config";
    static const char *options = "Options";
    DBusMessage *request, *reply;
    DBusMessageIter iter, sub;
    DBusError error;
    const char *path = NULL;

    if (!(reply = dhcp4_config_request( adapter ))) return NULL;

    p_dbus_message_iter_init( reply, &iter );
    if (p_dbus_message_iter_get_arg_type( &iter ) == DBUS_TYPE_VARIANT)
    {
        p_dbus_message_iter_recurse( &iter, &sub );
        p_dbus_message_iter_get_basic( &sub, &path );
    }
    if (!path)
    {
        p_dbus_message_unref( reply );
        return NULL;
    }

    request = p_dbus_message_new_method_call( "org.freedesktop.NetworkManager", path,
                                              "org.freedesktop.DBus.Properties", "Get" );
    p_dbus_message_unref( reply );
    if (!request) return NULL;

    p_dbus_message_iter_init_append( request, &iter );
    p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &dhcp4_config );
    p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &options );

    p_dbus_error_init( &error );
    reply = p_dbus_connection_send_with_reply_and_block( get_dhcp_connection(), request, -1, &error );
    p_dbus_message_unref( request );
    if (!reply)
    {
        p_dbus_error_free( &error );
        return NULL;
    }

    p_dbus_error_free( &error );
    return reply;
}

static const char *dhcp4_config_option_next_dict_entry( DBusMessageIter *iter, DBusMessageIter *variant )
{
    DBusMessageIter sub;
    const char *name;

    if (p_dbus_message_iter_get_arg_type( iter ) != DBUS_TYPE_DICT_ENTRY) return NULL;
    p_dbus_message_iter_recurse( iter, &sub );
    p_dbus_message_iter_next( iter );
    p_dbus_message_iter_get_basic( &sub, &name );
    p_dbus_message_iter_next( &sub );
    p_dbus_message_iter_recurse( &sub, variant );
    return name;
}

static DBusMessage *dhcp4_config_option_request( const WCHAR *adapter, const char *option, const char **value )
{
    DBusMessage *reply;
    DBusMessageIter iter, variant;
    const char *name;

    if (!(reply = dhcp4_config_options_request( adapter ))) return NULL;

    *value = NULL;
    p_dbus_message_iter_init( reply, &iter );
    if (p_dbus_message_iter_get_arg_type( &iter ) == DBUS_TYPE_VARIANT)
    {
        p_dbus_message_iter_recurse( &iter, &iter );
        if (p_dbus_message_iter_get_arg_type( &iter ) == DBUS_TYPE_ARRAY)
        {
            p_dbus_message_iter_recurse( &iter, &iter );
            while ((name = dhcp4_config_option_next_dict_entry( &iter, &variant )))
            {
                if (!strcmp( name, option ))
                {
                    p_dbus_message_iter_get_basic( &variant, value );
                    break;
                }
            }
        }
    }

    return reply;
}

static const char *map_option( ULONG option )
{
    switch (option)
    {
    case OPTION_SUBNET_MASK:         return "subnet_mask";
    case OPTION_ROUTER_ADDRESS:      return "next_server";
    case OPTION_HOST_NAME:           return "host_name";
    case OPTION_DOMAIN_NAME:         return "domain_name";
    case OPTION_BROADCAST_ADDRESS:   return "broadcast_address";
    case OPTION_MSFT_IE_PROXY:       return "wpad";
    default:
        FIXME( "unhandled option %u\n", option );
        return "";
    }
}

ULONG get_dhcp_request_param( const WCHAR *adapter, struct mountmgr_dhcp_request_param *param, char *buf, ULONG offset,
                              ULONG size )
{
    DBusMessage *reply;
    const char *value;
    ULONG ret = 0;

    param->offset = param->size = 0;

    if (!(reply = dhcp4_config_option_request( adapter, map_option(param->id), &value ))) return 0;

    switch (param->id)
    {
    case OPTION_SUBNET_MASK:
    case OPTION_ROUTER_ADDRESS:
    case OPTION_BROADCAST_ADDRESS:
    {
        IN_ADDR *ptr = (IN_ADDR *)(buf + offset);
        if (value && size >= sizeof(IN_ADDR) && !RtlIpv4StringToAddressA( value, TRUE, NULL, ptr ))
        {
            param->offset = offset;
            param->size   = sizeof(*ptr);
            TRACE( "returning %08x\n", *(DWORD *)ptr );
        }
        ret = sizeof(*ptr);
        break;
    }
    case OPTION_HOST_NAME:
    case OPTION_DOMAIN_NAME:
    case OPTION_MSFT_IE_PROXY:
    {
        char *ptr = buf + offset;
        int len = value ? strlen( value ) : 0;
        if (len && size >= len)
        {
            memcpy( ptr, value, len );
            param->offset = offset;
            param->size   = len;
            TRACE( "returning %s\n", debugstr_an(ptr, len) );
        }
        ret = len;
        break;
    }
    default:
        FIXME( "option %u not supported\n", param->id );
        break;
    }

    p_dbus_message_unref( reply );
    return ret;
}
1216
#endif
1217

1218
#else  /* SONAME_LIBDBUS_1 */
1219

1220
void initialize_dbus(void)
1221
{
1222
    TRACE( "Skipping, DBUS support not compiled in\n" );
1223 1224
}

1225
#endif  /* SONAME_LIBDBUS_1 */