github.com/mfpierre/corectl@v0.5.6/uuid2ip/uuid2ip.c (about)

     1  /*-
     2   * Copyright (c) 2011 NetApp, Inc.
     3   * Copyright (c) 2015 xhyve developers
     4   * All rights reserved.
     5   *
     6   * Redistribution and use in source and binary forms, with or without
     7   * modification, are permitted provided that the following conditions
     8   * are met:
     9   * 1. Redistributions of source code must retain the above copyright
    10   *    notice, this list of conditions and the following disclaimer.
    11   * 2. Redistributions in binary form must reproduce the above copyright
    12   *    notice, this list of conditions and the following disclaimer in the
    13   *    documentation and/or other materials provided with the distribution.
    14   *
    15   * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
    16   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18   * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
    19   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    20   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    21   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    22   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    23   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    24   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    25   * SUCH DAMAGE.
    26   *
    27   * $FreeBSD$
    28   */
    29  
    30  /*
    31   * https://github.com/mirage/ocaml-vmnet/blob/master/lib/vmnet_stubs.c
    32   *
    33   * Copyright (C) 2014 Anil Madhavapeddy <anil@recoil.org>
    34   *
    35   * Permission to use, copy, modify, and distribute this software for any
    36   * purpose with or without fee is hereby granted, provided that the above
    37   * copyright notice and this permission notice appear in all copies.
    38   *
    39   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    40   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    41   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    42   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    43   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    44   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    45   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    46   */
    47  
    48   /* originally at github.com/zchee/docker-machine-xhyve/blob/embed-xhyve/vmnet/
    49    * and slightly refactored to better fit the golang bindings
    50    * github.com/TheNewNormal/coreos-xhyve/uuid2ip
    51    */
    52  #include <stdio.h>
    53  
    54  #include <vmnet/vmnet.h>
    55  
    56  #include "uuid2ip.h"
    57  
    58  static char*
    59  vmnet_get_mac_address_from_uuid(char *guest_uuid_str) {
    60  /*
    61   * from vmn_create() in https://github.com/mist64/xhyve/blob/master/src/pci_virtio_vmnet.c
    62   */
    63    xpc_object_t interface_desc;
    64    uuid_t uuid;
    65    __block interface_ref iface;
    66    __block vmnet_return_t iface_status;
    67    __block char* mac = malloc(18);
    68    dispatch_semaphore_t iface_created, iface_stopped;
    69    dispatch_queue_t if_create_q, if_stop_q;
    70    uint32_t uuid_status;
    71  
    72    interface_desc = xpc_dictionary_create(NULL, NULL, 0);
    73    xpc_dictionary_set_uint64(interface_desc, vmnet_operation_mode_key, VMNET_SHARED_MODE);
    74  
    75    uuid_from_string(guest_uuid_str, &uuid, &uuid_status);
    76    if (uuid_status != uuid_s_ok) {
    77      fprintf(stderr, "Invalid UUID\n");
    78      return NULL;
    79    }
    80  
    81    xpc_dictionary_set_uuid(interface_desc, vmnet_interface_id_key, uuid);
    82    iface = NULL;
    83    iface_status = 0;
    84  
    85    if_create_q = dispatch_queue_create("org.xhyve.vmnet.create", DISPATCH_QUEUE_SERIAL);
    86  
    87    iface_created = dispatch_semaphore_create(0);
    88  
    89    iface = vmnet_start_interface(interface_desc, if_create_q,
    90      ^(vmnet_return_t status, xpc_object_t interface_param)
    91    {
    92      iface_status = status;
    93      if (status != VMNET_SUCCESS || !interface_param) {
    94        dispatch_semaphore_signal(iface_created);
    95        return;
    96      }
    97  
    98      //printf("%s\n", xpc_dictionary_get_string(interface_param, vmnet_mac_address_key));
    99      const char *macStr = xpc_dictionary_get_string(interface_param, vmnet_mac_address_key);
   100      strcpy(mac, macStr);
   101  
   102      dispatch_semaphore_signal(iface_created);
   103    });
   104  
   105    dispatch_semaphore_wait(iface_created, DISPATCH_TIME_FOREVER);
   106    dispatch_release(if_create_q);
   107  
   108    if (iface == NULL || iface_status != VMNET_SUCCESS) {
   109      // XXX supress noise in this cery specific case
   110      // XXX understand some day why some random UUIDs induce this...
   111      // fprintf(stderr, "virtio_net: Could not create vmnet interface, "
   112      //   "permission denied or no entitlement?\n");
   113      return NULL;
   114    }
   115  
   116    iface_status = 0;
   117  
   118    if_stop_q = dispatch_queue_create("org.xhyve.vmnet.stop", DISPATCH_QUEUE_SERIAL);
   119  
   120    iface_stopped = dispatch_semaphore_create(0);
   121  
   122    iface_status = vmnet_stop_interface(iface, if_stop_q,
   123      ^(vmnet_return_t status)
   124    {
   125      iface_status = status;
   126      dispatch_semaphore_signal(iface_stopped);
   127    });
   128  
   129    dispatch_semaphore_wait(iface_stopped, DISPATCH_TIME_FOREVER);
   130    dispatch_release(if_stop_q);
   131  
   132    if (iface_status != VMNET_SUCCESS) {
   133      fprintf(stderr, "virtio_net: Could not stop vmnet interface, "
   134        "permission denied or no entitlement?\n");
   135      return NULL;
   136    }
   137  
   138    return mac;
   139  }