su_os_nw.c 7.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_os_nw
 *
 * @CFILE su_os_nw.c  
 * Implementation of OS-specific network events interface.
 *
 * @author Martti Mela <Martti.Mela@nokia.com>
 * @date Created: Fri Aug 11 07:30:04 2006 mela
 *
 */

35
#include "config.h"
36 37 38 39 40 41 42 43 44
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#define SU_MSG_ARG_T    struct su_network_changed_s

#include "sofia-sip/su.h"
#include "sofia-sip/su_alloc.h"
#include "sofia-sip/su_wait.h"
45
#include "sofia-sip/su_debug.h"
46
#include "sofia-sip/su_os_nw.h"
47
#include "sofia-sip/su_debug.h"
48

49 50 51
#if defined(__APPLE_CC__)
# define SU_NW_CHANGE_PTHREAD 1
#endif
52

53 54 55 56
#if defined (SU_NW_CHANGE_PTHREAD)
# define SU_HAVE_NW_CHANGE 1
# include <pthread.h>
#endif
57

58
#if defined(__APPLE_CC__)
59 60 61
#include <AvailabilityMacros.h>
#include <sys/cdefs.h>
#include <CoreFoundation/CoreFoundation.h>
62 63 64
#include <SystemConfiguration/SCDynamicStore.h>
#include <SystemConfiguration/SCDynamicStoreKey.h>
#include <SystemConfiguration/SCSchemaDefinitions.h>
65
#endif /* __APPLE_CC__ */
66 67 68 69 70 71 72 73 74

struct su_network_changed_s {
  su_root_t                  *su_root;
  su_home_t                  *su_home;

#if defined (__APPLE_CC__)
  SCDynamicStoreRef           su_storeRef[1];
  CFRunLoopSourceRef          su_sourceRef[1];
#endif
75
#if defined (SU_NW_CHANGE_PTHREAD)
76
  pthread_t                   su_os_thread;
77
#endif
78 79 80 81 82

  su_network_changed_f       *su_network_changed_cb;
  su_network_changed_magic_t *su_network_changed_magic;
};

83
#if defined(__APPLE_CC__)
84 85 86 87 88 89 90 91
static void su_nw_changed_msg_recv(su_root_magic_t *rm,
				   su_msg_r msg,
				   su_network_changed_t *snc)
{
  su_network_changed_magic_t *magic = snc->su_network_changed_magic;

  assert(magic);

92
  /* SU_DEBUG_5(("su_nw_changed_msg_recv: entering.\n")); */
93

94 95 96 97 98
  snc->su_network_changed_cb(magic, snc->su_root);

  return;
}

99

100 101 102 103 104 105 106 107
void nw_changed_cb(SCDynamicStoreRef store, 
		   CFArrayRef changedKeys, 
		   void *info)
{
  su_network_changed_t *snc = (su_network_changed_t *) info;
  su_network_changed_t *snc2;
  su_msg_r rmsg = SU_MSG_R_INIT;

108
  SU_DEBUG_7(("nw_changed_cb: entering.\n"));
109 110 111 112 113 114 115 116

  if (su_msg_create(rmsg,
		    su_root_task(snc->su_root),
		    su_root_task(snc->su_root),
		    su_nw_changed_msg_recv,
		    sizeof *snc) == SU_FAILURE) {

    return;
117
  }
118 119 120 121 122 123 124 125 126

  snc2 = su_msg_data(rmsg); assert(snc2);
  snc2->su_root = snc->su_root;
  snc2->su_home = snc->su_home;
  memcpy(snc2->su_storeRef, snc->su_storeRef, sizeof(SCDynamicStoreRef));
  memcpy(snc2->su_sourceRef, snc->su_sourceRef, sizeof(CFRunLoopSourceRef));
  snc2->su_os_thread = snc->su_os_thread;
  snc2->su_network_changed_cb = snc->su_network_changed_cb;
  snc2->su_network_changed_magic = snc->su_network_changed_magic;
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  if (su_msg_send(rmsg) == SU_FAILURE) {
    su_msg_destroy(rmsg);
    return;
  }

  return;
}

static OSStatus
CreateIPAddressListChangeCallbackSCF(SCDynamicStoreCallBack callback,
				     void *contextPtr,
				     SCDynamicStoreRef *storeRef,
				     CFRunLoopSourceRef *sourceRef)
    // Create a SCF dynamic store reference and a
    // corresponding CFRunLoop source.  If you add the
    // run loop source to your run loop then the supplied
    // callback function will be called when local IP
    // address list changes.
{
    OSStatus                err = 0;
    SCDynamicStoreContext   context = {0, NULL, NULL, NULL, NULL};
    SCDynamicStoreRef       ref;
    CFStringRef             pattern;
    CFArrayRef              patternList;
    CFRunLoopSourceRef      rls;

    assert(callback   != NULL);
    assert( storeRef  != NULL);
    assert(*storeRef  == NULL);
    assert( sourceRef != NULL);
    assert(*sourceRef == NULL);

    ref = NULL;
    pattern = NULL;
    patternList = NULL;
    rls = NULL;

    // Create a connection to the dynamic store, then create
    // a search pattern that finds all IPv4 entities.
    // The pattern is "State:/Network/Service/[^/]+/IPv4".

    context.info = contextPtr;
    ref = SCDynamicStoreCreate( NULL,
                                CFSTR("AddIPAddressListChangeCallbackSCF"),
                                callback,
                                &context);
    //err = MoreSCError(ref);
    if (err == noErr) {
        pattern = SCDynamicStoreKeyCreateNetworkServiceEntity(
                                NULL,
                                kSCDynamicStoreDomainState,
                                kSCCompAnyRegex,
                                kSCEntNetIPv4);
        //err = MoreSCError(pattern);
    }

    // Create a pattern list containing just one pattern,
    // then tell SCF that we want to watch changes in keys
    // that match that pattern list, then create our run loop
    // source.

    if (err == noErr) {
        patternList = CFArrayCreate(NULL,
                                    (const void **) &pattern, 1,
                                    &kCFTypeArrayCallBacks);
        //err = CFQError(patternList);
    }
    if (err == noErr) {
      //err = MoreSCErrorBoolean(
                SCDynamicStoreSetNotificationKeys(
                    ref,
                    NULL,
                    patternList);
		//      );
    }
    if (err == noErr) {
        rls = SCDynamicStoreCreateRunLoopSource(NULL, ref, 0);
        //err = MoreSCError(rls);
    }

    CFRunLoopAddSource(CFRunLoopGetCurrent(), rls,
		       kCFRunLoopDefaultMode);

    // Clean up.

    //CFQRelease(pattern);
    //CFQRelease(patternList);
    if (err != noErr) {
      //CFQRelease(ref);
        ref = NULL;
    }
    *storeRef = ref;
    *sourceRef = rls;

    assert( (err == noErr) == (*storeRef  != NULL) );
    assert( (err == noErr) == (*sourceRef != NULL) );

    return err;
}

static void *su_start_nw_os_thread(void *ptr)
{
  su_network_changed_t *snc = (su_network_changed_t *) ptr;

  assert(snc);

  CreateIPAddressListChangeCallbackSCF(nw_changed_cb,
				       (void *) snc,
				       snc->su_storeRef,
				       snc->su_sourceRef);

  CFRunLoopRun();

  return NULL;
}
243
#endif
244

245 246 247 248
/** Register a callback for the network change event.
 *
 * @since New in @VERSION_1_12_2.
 */
249 250 251 252 253 254 255 256 257
su_network_changed_t
*su_root_add_network_changed(su_home_t *home, su_root_t *root,
			     su_network_changed_f *network_changed_cb,
			     su_network_changed_magic_t *magic)
{
  su_network_changed_t *snc = NULL;

  assert(home && root && network_changed_cb && magic);

258
#if defined (SU_HAVE_NW_CHANGE)
259
  snc = su_zalloc(home, sizeof *snc);
260

261 262 263 264 265 266 267
  if (!snc)
    return NULL;

  snc->su_home = home;
  snc->su_root = root;
  snc->su_network_changed_cb = network_changed_cb;
  snc->su_network_changed_magic = magic;
268 269

# if defined (SU_NW_CHANGE_PTHREAD)
270 271 272 273 274
  if ((pthread_create(&(snc->su_os_thread), NULL,
		      su_start_nw_os_thread,
		      (void *) snc)) != 0) {
    return NULL;
  }
275
# endif
276
#endif
277
  return snc;
278 279
}

280 281 282 283
/** Remove a callback registered for the network change event.
 *
 * @since New in @VERSION_1_12_2.
 */
284
int su_root_remove_network_changed(su_network_changed_t *snc)
285
{
286 287
  return -1;
}