summaryrefslogtreecommitdiff
path: root/src/platform/nm-platform-utils.c
blob: 9ef0707d584a9d8f2ed50ee82c83e215aab86384 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
319
320
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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
454
455
456
457
458
459
460
461
462
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
488
489
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-platform.c - Handle runtime kernel networking configuration
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2015 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-platform-utils.h"

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <linux/mii.h>
#include <linux/version.h>
#include <linux/rtnetlink.h>

#include "nm-utils.h"
#include "nm-setting-wired.h"

#include "nm-core-utils.h"

/******************************************************************
 * ethtool
 ******************************************************************/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
#define ethtool_cmd_speed(pedata) ((pedata)->speed)

#define ethtool_cmd_speed_set(pedata, speed) \
	G_STMT_START { (pedata)->speed = (guint16) (speed); } G_STMT_END
#endif


static gboolean
ethtool_get (const char *name, gpointer edata)
{
	struct ifreq ifr;
	int fd;

	if (!name || !*name)
		return FALSE;

	if (!nmp_utils_device_exists (name))
		return FALSE;

	/* nmp_utils_device_exists() already errors out if @name is invalid. */
	nm_assert (strlen (name) < IFNAMSIZ);

	memset (&ifr, 0, sizeof (ifr));
	nm_utils_ifname_cpy (ifr.ifr_name, name);
	ifr.ifr_data = edata;

	fd = socket (PF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		nm_log_err (LOGD_PLATFORM, "ethtool: Could not open socket.");
		return FALSE;
	}

	if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) {
		nm_log_dbg (LOGD_PLATFORM, "ethtool: Request failed: %s", strerror (errno));
		close (fd);
		return FALSE;
	}

	close (fd);
	return TRUE;
}

static int
ethtool_get_stringset_index (const char *ifname, int stringset_id, const char *string)
{
	gs_free struct ethtool_sset_info *info = NULL;
	gs_free struct ethtool_gstrings *strings = NULL;
	guint32 len, i;

	info = g_malloc0 (sizeof (*info) + sizeof (guint32));
	info->cmd = ETHTOOL_GSSET_INFO;
	info->reserved = 0;
	info->sset_mask = 1ULL << stringset_id;

	if (!ethtool_get (ifname, info))
		return -1;
	if (!info->sset_mask)
		return -1;

	len = info->data[0];

	strings = g_malloc0 (sizeof (*strings) + len * ETH_GSTRING_LEN);
	strings->cmd = ETHTOOL_GSTRINGS;
	strings->string_set = stringset_id;
	strings->len = len;
	if (!ethtool_get (ifname, strings))
		return -1;

	for (i = 0; i < len; i++) {
		if (!strcmp ((char *) &strings->data[i * ETH_GSTRING_LEN], string))
			return i;
	}

	return -1;
}

gboolean
nmp_utils_ethtool_get_driver_info (const char *ifname,
                                   char **out_driver_name,
                                   char **out_driver_version,
                                   char **out_fw_version)
{
	struct ethtool_drvinfo drvinfo = { 0 };

	if (!ifname)
		return FALSE;

	drvinfo.cmd = ETHTOOL_GDRVINFO;
	if (!ethtool_get (ifname, &drvinfo))
		return FALSE;

	if (out_driver_name)
		*out_driver_name = g_strdup (drvinfo.driver);
	if (out_driver_version)
		*out_driver_version = g_strdup (drvinfo.version);
	if (out_fw_version)
		*out_fw_version = g_strdup (drvinfo.fw_version);

	return TRUE;
}

gboolean
nmp_utils_ethtool_get_permanent_address (const char *ifname,
                                         guint8 *buf,
                                         size_t *length)
{
	struct {
		struct ethtool_perm_addr e;
		guint8 _extra_data[NM_UTILS_HWADDR_LEN_MAX + 1];
	} edata;
	guint i;

	if (!ifname)
		return FALSE;

	memset (&edata, 0, sizeof (edata));
	edata.e.cmd = ETHTOOL_GPERMADDR;
	edata.e.size = NM_UTILS_HWADDR_LEN_MAX;

	if (!ethtool_get (ifname, &edata.e))
		return FALSE;

	if (edata.e.size > NM_UTILS_HWADDR_LEN_MAX)
		return FALSE;
	if (edata.e.size < 1)
		return FALSE;

	if (NM_IN_SET (edata.e.data[0], 0, 0xFF)) {
		/* Some drivers might return a permanent address of all zeros.
		 * Reject that (rh#1264024)
		 *
		 * Some drivers return a permanent address of all ones. Reject that too */
		for (i = 1; i < edata.e.size; i++) {
			if (edata.e.data[0] != edata.e.data[i])
				goto not_all_0or1;
		}
		return FALSE;
	}
not_all_0or1:

	memcpy (buf, edata.e.data, edata.e.size);
	*length = edata.e.size;
	return TRUE;
}

gboolean
nmp_utils_ethtool_supports_carrier_detect (const char *ifname)
{
	struct ethtool_cmd edata = { .cmd = ETHTOOL_GLINK };

	/* We ignore the result. If the ETHTOOL_GLINK call succeeded, then we
	 * assume the device supports carrier-detect, otherwise we assume it
	 * doesn't.
	 */
	return ethtool_get (ifname, &edata);
}

gboolean
nmp_utils_ethtool_supports_vlans (const char *ifname)
{
	gs_free struct ethtool_gfeatures *features = NULL;
	int idx, block, bit, size;

	if (!ifname)
		return FALSE;

	idx = ethtool_get_stringset_index (ifname, ETH_SS_FEATURES, "vlan-challenged");
	if (idx == -1) {
		nm_log_dbg (LOGD_PLATFORM, "ethtool: vlan-challenged ethtool feature does not exist for %s?", ifname);
		return FALSE;
	}

	block = idx /  32;
	bit = idx % 32;
	size = block + 1;

	features = g_malloc0 (sizeof (*features) + size * sizeof (struct ethtool_get_features_block));
	features->cmd = ETHTOOL_GFEATURES;
	features->size = size;

	if (!ethtool_get (ifname, features))
		return FALSE;

	return !(features->features[block].active & (1 << bit));
}

int
nmp_utils_ethtool_get_peer_ifindex (const char *ifname)
{
	gs_free struct ethtool_stats *stats = NULL;
	int peer_ifindex_stat;

	if (!ifname)
		return 0;

	peer_ifindex_stat = ethtool_get_stringset_index (ifname, ETH_SS_STATS, "peer_ifindex");
	if (peer_ifindex_stat == -1) {
		nm_log_dbg (LOGD_PLATFORM, "ethtool: peer_ifindex stat for %s does not exist?", ifname);
		return FALSE;
	}

	stats = g_malloc0 (sizeof (*stats) + (peer_ifindex_stat + 1) * sizeof (guint64));
	stats->cmd = ETHTOOL_GSTATS;
	stats->n_stats = peer_ifindex_stat + 1;
	if (!ethtool_get (ifname, stats))
		return 0;

	return stats->data[peer_ifindex_stat];
}

gboolean
nmp_utils_ethtool_get_wake_on_lan (const char *ifname)
{
	struct ethtool_wolinfo wol;

	if (!ifname)
		return FALSE;

	memset (&wol, 0, sizeof (wol));
	wol.cmd = ETHTOOL_GWOL;
	if (!ethtool_get (ifname, &wol))
		return FALSE;

	return wol.wolopts != 0;
}

gboolean
nmp_utils_ethtool_get_link_settings (const char *ifname,
                                     gboolean *out_autoneg,
                                     guint32 *out_speed,
                                     NMPlatformLinkDuplexType *out_duplex)
{
	struct ethtool_cmd edata = {
		.cmd = ETHTOOL_GSET,
	};

	if (!ethtool_get (ifname, &edata))
		return FALSE;

	if (out_autoneg)
		*out_autoneg = (edata.autoneg == AUTONEG_ENABLE);

	if (out_speed) {
		guint32 speed;

		speed = ethtool_cmd_speed (&edata);
		if (speed == G_MAXUINT16 || speed == G_MAXUINT32)
			speed = 0;

		*out_speed = speed;
	}

	if (out_duplex) {
		switch (edata.duplex) {
		case DUPLEX_HALF:
			*out_duplex = NM_PLATFORM_LINK_DUPLEX_HALF;
			break;
		case DUPLEX_FULL:
			*out_duplex = NM_PLATFORM_LINK_DUPLEX_FULL;
			break;
		default: /* DUPLEX_UNKNOWN */
			*out_duplex = NM_PLATFORM_LINK_DUPLEX_UNKNOWN;
			break;
		}
	}

	return TRUE;
}

gboolean
nmp_utils_ethtool_set_link_settings (const char *ifname, gboolean autoneg, guint32 speed, NMPlatformLinkDuplexType duplex)
{
	struct ethtool_cmd edata = {
		.cmd = ETHTOOL_GSET,
	};

	/* retrieve first current settings */
	if (!ethtool_get (ifname, &edata))
		return FALSE;

	/* then change the needed ones */
	edata.cmd = ETHTOOL_SSET;
	if (autoneg) {
		edata.autoneg = AUTONEG_ENABLE;
		edata.advertising = edata.supported;
	} else {
		edata.autoneg = AUTONEG_DISABLE;

		if (speed)
			ethtool_cmd_speed_set (&edata, speed);

		switch (duplex) {
		case NM_PLATFORM_LINK_DUPLEX_HALF:
			edata.duplex = DUPLEX_HALF;
			break;
		case NM_PLATFORM_LINK_DUPLEX_FULL:
			edata.duplex = DUPLEX_FULL;
			break;
		case NM_PLATFORM_LINK_DUPLEX_UNKNOWN:
			break;
		default:
			g_return_val_if_reached (FALSE);
		}
	}

	return ethtool_get (ifname, &edata);
}

gboolean
nmp_utils_ethtool_set_wake_on_lan (const char *ifname,
                                   NMSettingWiredWakeOnLan wol,
                                   const char *wol_password)
{
	struct ethtool_wolinfo wol_info = { };

	if (wol == NM_SETTING_WIRED_WAKE_ON_LAN_IGNORE)
		return TRUE;

	nm_log_dbg (LOGD_PLATFORM, "setting Wake-on-LAN options 0x%x, password '%s'",
	            (unsigned int) wol, wol_password);

	wol_info.cmd = ETHTOOL_SWOL;
	wol_info.wolopts = 0;

	if (NM_FLAGS_HAS (wol, NM_SETTING_WIRED_WAKE_ON_LAN_PHY))
		wol_info.wolopts |= WAKE_PHY;
	if (NM_FLAGS_HAS (wol, NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST))
		wol_info.wolopts |= WAKE_UCAST;
	if (NM_FLAGS_HAS (wol, NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST))
		wol_info.wolopts |= WAKE_MCAST;
	if (NM_FLAGS_HAS (wol, NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST))
		wol_info.wolopts |= WAKE_BCAST;
	if (NM_FLAGS_HAS (wol, NM_SETTING_WIRED_WAKE_ON_LAN_ARP))
		wol_info.wolopts |= WAKE_ARP;
	if (NM_FLAGS_HAS (wol, NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC))
		wol_info.wolopts |= WAKE_MAGIC;

	if (wol_password) {
		if (!nm_utils_hwaddr_aton (wol_password, wol_info.sopass, ETH_ALEN)) {
			nm_log_dbg (LOGD_PLATFORM, "couldn't parse Wake-on-LAN password '%s'", wol_password);
			return FALSE;
		}
		wol_info.wolopts |= WAKE_MAGICSECURE;
	}

	return ethtool_get (ifname, &wol_info);
}

/******************************************************************
 * mii
 ******************************************************************/

gboolean
nmp_utils_mii_supports_carrier_detect (const char *ifname)
{
	int fd, errsv;
	struct ifreq ifr;
	struct mii_ioctl_data *mii;
	gboolean supports_mii = FALSE;

	if (!ifname)
		return FALSE;

	if (!nmp_utils_device_exists (ifname))
		return FALSE;

	fd = socket (PF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		nm_log_err (LOGD_PLATFORM, "mii: couldn't open control socket (%s)", ifname);
		return FALSE;
	}

	memset (&ifr, 0, sizeof (struct ifreq));
	nm_utils_ifname_cpy (ifr.ifr_name, ifname);

	errno = 0;
	if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0) {
		errsv = errno;
		nm_log_dbg (LOGD_PLATFORM, "mii: SIOCGMIIPHY failed: %s (%d) (%s)", strerror (errsv), errsv, ifname);
		goto out;
	}

	/* If we can read the BMSR register, we assume that the card supports MII link detection */
	mii = (struct mii_ioctl_data *) &ifr.ifr_ifru;
	mii->reg_num = MII_BMSR;

	if (ioctl (fd, SIOCGMIIREG, &ifr) == 0) {
		nm_log_dbg (LOGD_PLATFORM, "mii: SIOCGMIIREG result 0x%X (%s)", mii->val_out, ifname);
		supports_mii = TRUE;
	} else {
		errsv = errno;
		nm_log_dbg (LOGD_PLATFORM, "mii: SIOCGMIIREG failed: %s (%d) (%s)", strerror (errsv), errsv, ifname);
	}

out:
	close (fd);
	nm_log_dbg (LOGD_PLATFORM, "mii: MII %s supported (%s)", supports_mii ? "is" : "not", ifname);
	return supports_mii;
}

/******************************************************************
 * udev
 ******************************************************************/

const char *
nmp_utils_udev_get_driver (GUdevDevice *device)
{
	GUdevDevice *parent = NULL, *grandparent = NULL;
	const char *driver, *subsys;

	driver = g_udev_device_get_driver (device);
	if (driver)
		goto out;

	/* Try the parent */
	parent = g_udev_device_get_parent (device);
	if (parent) {
		driver = g_udev_device_get_driver (parent);
		if (!driver) {
			/* Try the grandparent if it's an ibmebus device or if the
			 * subsys is NULL which usually indicates some sort of
			 * platform device like a 'gadget' net interface.
			 */
			subsys = g_udev_device_get_subsystem (parent);
			if (   (g_strcmp0 (subsys, "ibmebus") == 0)
			    || (subsys == NULL)) {
				grandparent = g_udev_device_get_parent (parent);
				if (grandparent)
					driver = g_udev_device_get_driver (grandparent);
			}
		}
	}
	g_clear_object (&parent);
	g_clear_object (&grandparent);

out:
	/* Intern the string so we don't have to worry about memory
	 * management in NMPlatformLink. */
	return g_intern_string (driver);
}

/******************************************************************************
 * utils
 *****************************************************************************/

gboolean
nmp_utils_device_exists (const char *name)
{
#define SYS_CLASS_NET "/sys/class/net/"
	char sysdir[NM_STRLEN (SYS_CLASS_NET) + IFNAMSIZ];

	if (   !name
	    || strlen (name) >= IFNAMSIZ
	    || !nm_utils_is_valid_path_component (name))
		g_return_val_if_reached (FALSE);

	memcpy (sysdir, SYS_CLASS_NET, NM_STRLEN (SYS_CLASS_NET));
	nm_utils_ifname_cpy (&sysdir[NM_STRLEN (SYS_CLASS_NET)], name);
	return g_file_test (sysdir, G_FILE_TEST_EXISTS);
}

NMIPConfigSource
nmp_utils_ip_config_source_from_rtprot (guint8 rtprot)
{
	return ((int) rtprot) + 1;
}

NMIPConfigSource
nmp_utils_ip_config_source_round_trip_rtprot (NMIPConfigSource source)
{
	/* when adding a route to kernel for a give @source, the resulting route
	 * will be put into the cache with a source of NM_IP_CONFIG_SOURCE_RTPROT_*.
	 * This function returns that. */
	return nmp_utils_ip_config_source_from_rtprot (nmp_utils_ip_config_source_coerce_to_rtprot (source));
}

guint8
nmp_utils_ip_config_source_coerce_to_rtprot (NMIPConfigSource source)
{
	/* when adding a route to kernel, we coerce the @source field
	 * to rtm_protocol. This is not lossless as we map different
	 * source values to the same RTPROT uint8 value. */
	if (source <= NM_IP_CONFIG_SOURCE_UNKNOWN)
		return RTPROT_UNSPEC;

	if (source <= _NM_IP_CONFIG_SOURCE_RTPROT_LAST)
		return source - 1;

	switch (source) {
	case NM_IP_CONFIG_SOURCE_KERNEL:
		return RTPROT_KERNEL;
	case NM_IP_CONFIG_SOURCE_DHCP:
		return RTPROT_DHCP;
	case NM_IP_CONFIG_SOURCE_NDISC:
		return RTPROT_RA;

	default:
		return RTPROT_STATIC;
	}
}

NMIPConfigSource
nmp_utils_ip_config_source_coerce_from_rtprot (NMIPConfigSource source)
{
	/* When we receive a route from kernel and put it into the platform cache,
	 * we preserve the protocol field by converting it to a NMIPConfigSource
	 * via nmp_utils_ip_config_source_from_rtprot().
	 *
	 * However, that is not the inverse of nmp_utils_ip_config_source_coerce_to_rtprot().
	 * Instead, to go back to the original value, you need another step:
	 *   nmp_utils_ip_config_source_coerce_from_rtprot (nmp_utils_ip_config_source_from_rtprot (rtprot)).
	 *
	 * This might partly restore the original source value, but of course that
	 * is not really possible because nmp_utils_ip_config_source_coerce_to_rtprot()
	 * is not injective.
	 * */
	switch (source) {
	case NM_IP_CONFIG_SOURCE_RTPROT_UNSPEC:
		return NM_IP_CONFIG_SOURCE_UNKNOWN;

	case NM_IP_CONFIG_SOURCE_RTPROT_KERNEL:
	case NM_IP_CONFIG_SOURCE_RTPROT_REDIRECT:
		return NM_IP_CONFIG_SOURCE_KERNEL;

	case NM_IP_CONFIG_SOURCE_RTPROT_RA:
		return NM_IP_CONFIG_SOURCE_NDISC;

	case NM_IP_CONFIG_SOURCE_RTPROT_DHCP:
		return NM_IP_CONFIG_SOURCE_DHCP;

	default:
		return NM_IP_CONFIG_SOURCE_USER;
	}
}

const char *
nmp_utils_ip_config_source_to_string (NMIPConfigSource source, char *buf, gsize len)
{
	const char *s = NULL;
	nm_utils_to_string_buffer_init (&buf, &len); \

	if (!len)
		return buf;

	switch (source) {
	case NM_IP_CONFIG_SOURCE_UNKNOWN:         s = "unknown"; break;

	case NM_IP_CONFIG_SOURCE_RTPROT_UNSPEC:   s = "rt-unspec"; break;
	case NM_IP_CONFIG_SOURCE_RTPROT_REDIRECT: s = "rt-redirect"; break;
	case NM_IP_CONFIG_SOURCE_RTPROT_KERNEL:   s = "rt-kernel"; break;
	case NM_IP_CONFIG_SOURCE_RTPROT_BOOT:     s = "rt-boot"; break;
	case NM_IP_CONFIG_SOURCE_RTPROT_STATIC:   s = "rt-static"; break;
	case NM_IP_CONFIG_SOURCE_RTPROT_DHCP:     s = "rt-dhcp"; break;
	case NM_IP_CONFIG_SOURCE_RTPROT_RA:       s = "rt-ra"; break;

	case NM_IP_CONFIG_SOURCE_KERNEL:          s = "kernel"; break;
	case NM_IP_CONFIG_SOURCE_SHARED:          s = "shared"; break;
	case NM_IP_CONFIG_SOURCE_IP4LL:           s = "ipv4ll"; break;
	case NM_IP_CONFIG_SOURCE_PPP:             s = "ppp"; break;
	case NM_IP_CONFIG_SOURCE_WWAN:            s = "wwan"; break;
	case NM_IP_CONFIG_SOURCE_VPN:             s = "vpn"; break;
	case NM_IP_CONFIG_SOURCE_DHCP:            s = "dhcp"; break;
	case NM_IP_CONFIG_SOURCE_NDISC:           s = "ndisc"; break;
	case NM_IP_CONFIG_SOURCE_USER:            s = "user"; break;
	default:
		break;
	}

	if (source >= 1 && source <= 0x100) {
		if (s)
			g_snprintf (buf, len, "%s", s);
		else
			g_snprintf (buf, len, "rt-%d", ((int) source) - 1);
	} else {
		if (s)
			g_strlcpy (buf, s, len);
		else
			g_snprintf (buf, len, "(%d)", source);
	}
	return buf;
}