forked from cellularmitosis/ADC-reference-library-2009-july
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting11.html
More file actions
executable file
·1919 lines (1534 loc) · 73.1 KB
/
Copy pathlisting11.html
File metadata and controls
executable file
·1919 lines (1534 loc) · 73.1 KB
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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>MoreSCF - /MoreSCF/MoreSCFPortScanner.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Networking/index.html">Networking</a> > <a href="../../samplecode/Networking/idxDarwin-date.html">Darwin</a> > <A HREF="javascript:location.replace('index.html');">MoreSCF</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">MoreSCF</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/MoreSCF/MoreSCFPortScanner.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/MoreCFQ/MoreCFQ.c</option>
<option value="listing2.html">/MoreCFQ/MoreCFQ.h</option>
<option value="listing3.html">/MoreSCF/MoreSCF.c</option>
<option value="listing4.html">/MoreSCF/MoreSCF.h</option>
<option value="listing5.html">/MoreSCF/MoreSCFCCLScanner.c</option>
<option value="listing6.html">/MoreSCF/MoreSCFCCLScanner.h</option>
<option value="listing7.html">/MoreSCF/MoreSCFDigest.c</option>
<option value="listing8.html">/MoreSCF/MoreSCFDigest.h</option>
<option value="listing9.html">/MoreSCF/MoreSCFHelpers.c</option>
<option value="listing10.html">/MoreSCF/MoreSCFHelpers.h</option>
<option value="listing11.html">/MoreSCF/MoreSCFPortScanner.c</option>
<option value="listing12.html">/MoreSCF/MoreSCFPortScanner.h</option>
<option value="listing13.html">/MoreSCF/MoreSCFTest/MoreSCFTest.c</option>
<option value="listing14.html">/MoreSCF/Read Me About MoreSCF.txt</option>
<option value="listing15.html">/Read Me.txt</option></select>
</p>
</form>
<p><strong><a href="MoreSCF.zip">Download Sample</a></strong> (“MoreSCF.zip”, 126.7K)<BR>
<strong><a href="MoreSCF.dmg">Download Sample</a></strong> (“MoreSCF.dmg”, 191.8K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: MoreSCFPortScanner.c
Contains: Code to generate list of network ports on Mac OS X.
Written by: DTS, based on code by Network preferences engineering
Copyright: Copyright (c) 2007 by Apple Inc., All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
$Log: MoreSCFPortScanner.c,v $
Revision 1.20 2006/11/24 15:01:12
Make it possible to explicitly test the old port scanning code.
Revision 1.19 2006/11/24 12:43:52
Added a new implementation that calls the real API. Added rudimentary Bluetooth and FireWire support for the old implementation. Don't show hidden serial ports. Other minor changes.
Revision 1.18 2006/11/23 15:04:13
Correct an uninitialised variable bug in previous check in.
Revision 1.17 2006/11/23 14:58:10
When getting the "name" property, handle it being a CFString as well as a CFData.
Revision 1.16 2006/03/27 14:42:37
Eliminate high-bit set characters.
Revision 1.15 2006/03/24 15:44:42
Updated copyright.
Revision 1.14 2006/03/24 12:38:50
Eliminate "pascal" keyword.
Revision 1.13 2006/03/24 11:30:16
Eliminated "MoreSetup.h" to make it easier for folks to copy MIB source into their projects.
Revision 1.12 2006/03/23 15:19:31
Correct a compiler warning by adopting IO_OBJECT_NULL.
Revision 1.11 2003/03/11 23:20:45
Fixed a crashing bug in CopyUserVisibleSlotName caused by not retaining the constant strings that we return.
Revision 1.10 2003/02/26 21:32:24
Only translate slot names to numbers on 10.2 and later.
Revision 1.9 2003/02/26 20:58:37
<rdar://problem/3183087> Added support for V.92 modem hold. <rdar://problem/3182842> We now translate slot names into user friendly numbers. <rdar://problem/3182889> Handle USB "Product Name" more like the Network control panel.
Revision 1.8 2003/02/26 12:35:09
C++ compatibility.
Revision 1.7 2003/01/20 07:54:16
Correctly handle the case where the user visible name returned by the port name callback is NULL. This was always allowed in the documentation, but it didn't work.
Revision 1.6 2002/11/25 16:49:03
Handle weird devices that have no "name" property.
Revision 1.5 2002/11/09 00:02:11
Include our prototype early to flush out any missing dependencies. Convert nil to NULL. Convert MoreAssertQ to assert.
Revision 1.4 2002/10/25 16:41:15
Messed up handling of BSD name in SubstituteKeywordsInUserVisibleName [3084354].
Revision 1.3 2002/08/14 13:45:06
Fix problem with IrDA ports on tray load iMacs showing up even though they're not supported on Mac OS X.
Revision 1.2 2002/01/22 06:17:20
Total rewrite. Kept Robert's ideas but complete rewrote the implementation and also changed the API to work better in an SCF environment.
Revision 1.1 2002/01/16 22:52:36
First checked in.
*/
/////////////////////////////////////////////////////////////////
// Our prototypes
#include "MoreSCFPortScanner.h"
// System Interfaces
#include <net/if_types.h>
#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/network/IONetworkController.h>
#include <IOKit/network/IONetworkInterface.h>
#include <IOKit/network/IOEthernetInterface.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/ndrvsupport/IOMacOSTypes.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
#include <SystemConfiguration/SystemConfiguration.h>
// MIB prototypes
#include "MoreCFQ.h"
#include "MoreSCF.h"
/////////////////////////////////////////////////////////////////
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && ! MORE_SCF_NO_DEPRECATION_WARNINGS
#warning MoreSCF is deprecated if you are building for 10.4 or later.
#endif
/////////////////////////////////////////////////////////////////
#pragma mark ***** Old Implementation
// Ports are sorted by a primary sort order (defined by the enumeration
// below) and then sorted by name within their groups. This enumeration
// closely mirrors the behaviour of the Network preferences panel.
enum {
kInternalModemSortOrder ,
kUSBModemSortOrder ,
kModemSortOrder ,
kBluetoothSortOrder ,
kIrDASerialSortOrder ,
kSerialSortOrder ,
kBuiltInEthernetSortOrder ,
kBuiltInFireWireSortOrder ,
kEthernetPCISortOrder ,
kFireWireSortOrder ,
kAirPortSortOrder ,
kWirelessSortOrder ,
kEthernetSortOrder ,
kDefaultSortOrder = 100
};
#define kSortOrderKey CFSTR("SortOrder")
// We use this key to store the sort order inside the port's
// dictionary, which means we don't have to track it by other
// means.
static long SortOrder(CFDictionaryRef portDict)
// Extract the kSortOrderKey from the dictionary as a number.
{
long result;
CFNumberRef numRef;
assert(portDict != NULL);
result = kDefaultSortOrder;
numRef = (CFNumberRef) CFDictionaryGetValue(portDict, kSortOrderKey);
if (numRef != NULL) {
assert( CFGetTypeID(numRef) == CFNumberGetTypeID() );
(void) CFNumberGetValue(numRef, kCFNumberLongType, &result);
}
return result;
}
static CFComparisonResult PortSorter(const void *lhs, const void *rhs, void *context)
// Compares two port dictionaries and orders them appropriately.
{
#pragma unused(context)
CFDictionaryRef lhsDict;
CFDictionaryRef rhsDict;
int lhsOrder;
int rhsOrder;
assert(lhs != NULL);
assert(rhs != NULL);
lhsDict = (CFDictionaryRef) lhs;
assert( CFGetTypeID(lhsDict) == CFDictionaryGetTypeID() );
rhsDict = (CFDictionaryRef) rhs;
assert( CFGetTypeID(rhsDict) == CFDictionaryGetTypeID() );
lhsOrder = SortOrder(lhsDict);
rhsOrder = SortOrder(rhsDict);
if (lhsOrder > rhsOrder)
return kCFCompareGreaterThan;
if (lhsOrder < rhsOrder)
return kCFCompareLessThan;
// objects are the same type, so order by name
return CFStringCompare(
(CFStringRef) CFDictionaryGetValue(lhsDict, kSCPropUserDefinedName),
(CFStringRef) CFDictionaryGetValue(rhsDict, kSCPropUserDefinedName), kCFCompareLocalized);
}
static CFStringRef CreateStringFromProperty(CFTypeRef prop)
// Some IOKit strings are encoded in CFDatas. Some are encoded as CFStrings.
// Some have trailing null characters. Handle all of these edge cases.
{
CFStringRef result;
assert(prop != NULL);
result = NULL;
if ( CFGetTypeID(prop) == CFDataGetTypeID() ) {
result = CFStringCreateWithBytes(NULL, CFDataGetBytePtr(prop), CFDataGetLength(prop), kCFStringEncodingMacRoman, false);
} else if ( CFGetTypeID(prop) == CFStringGetTypeID() ) {
(void) CFRetain(prop);
result = (CFStringRef) prop;
}
if (result != NULL) {
CFIndex len;
CFStringRef tmp;
len = CFStringGetLength(result);
while ( (len > 0) && (CFStringGetCharacterAtIndex(result, len - 1) == 0) ) {
len -= 1;
}
tmp = CFStringCreateWithSubstring(NULL, result, CFRangeMake(0, len));
CFRelease(result); // Do the swap even if tmp is NULL, that way the
result = tmp; // function as a whole returns NULL.
}
return result;
}
static CFStringRef CreateMACFromData(CFDataRef data)
{
CFIndex dataCount;
CFIndex dataIndex;
CFMutableStringRef result;
assert(data != NULL);
result = NULL;
dataCount = CFDataGetLength(data);
assert( (dataCount == 6) || (dataCount == 8) ); // Ethernet == 6, FireWire == 8, anything else we should investigate
if (dataCount > 0) {
result = CFStringCreateMutable(NULL, 0);
assert(result != NULL);
}
if (result != NULL) {
for (dataIndex = 0; dataIndex < dataCount; dataIndex++) {
CFStringAppendFormat(result, NULL, CFSTR("%2.2x:"), CFDataGetBytePtr(data)[dataIndex]);
}
CFStringTrim(result, CFSTR(":")); // get rid of last colon
} else {
assert(false);
}
return result;
}
static Boolean CFEqualString( CFStringRef s1, CFStringRef s2 )
// Compares two CFStringRefs and return true if they're equal.
// Handles NULL, which CFEqual does not.
{
if (( s1 == NULL ) || ( s2 == NULL ))
return false;
if ( s1 == s2 )
return true;
return ( CFStringCompare( s1, s2, 0 ) == kCFCompareEqualTo );
}
static MoreSCFPortNameCallback gPortNameCallback = NULL;
static kern_return_t SubstituteKeywordsInUserVisibleName(io_object_t interface,
CFMutableDictionaryRef interfaceInfo,
CFStringRef nameTemplate,
CFStringRef *userVisibleName)
// This routine creates a new string and returns it in *userVisibleName.
// The string is based on nameTemplate, with any of the keywords (defined
// in our header file) replace with the appropriate strings.
//
// I could probably write better, more general, code to do this, but
// this is the way the code turned out (after several design iterations)
// and it's not too atrocious.
//
// Note that this routine assumes that whoever set up nameTemplate
// to included a keyword also included the appropriate string in the
// interfaceInfo dictionary. This might not be true if the client's
// callback routine returns a wacky localisation for the user-visible
// name. I haven't seen fit to fix this because it's an obscure
// case and the client's callback could fix it by adding the appropriate
// keys to the interfaceInfo dictionary (which it is passed).
//
// I originally tried to using CFStringCreateWithFormat to create
// the output string (hence the keyword syntax) but I discovered
// that the CF routine requires you to always substitute all keywords
// (which is obvious when you consider how varargs works, but it
// threw me for a loop). So I abandoned CFStringCreateWithFormat, but
// I couldn't think of any good reason to change the keyword syntax.
{
#pragma unused(interface)
kern_return_t err;
CFMutableStringRef result;
CFRange foundRange;
assert( interface != IO_OBJECT_NULL);
assert( interfaceInfo != NULL);
assert( nameTemplate != NULL);
assert( userVisibleName != NULL);
assert(*userVisibleName == NULL);
err = 0;
result = CFStringCreateMutableCopy(NULL, 0, nameTemplate);
if (result == NULL) {
err = -1;
}
// BSD Name
if ( (err == 0) && CFStringFindWithOptions(result, CFSTR("%1$@"), CFRangeMake(0, CFStringGetLength(result)), kNilOptions, &foundRange) ) {
assert(CFDictionaryGetValue(interfaceInfo, CFSTR(kIOBSDNameKey)) != NULL);
CFStringReplace(result, foundRange, (CFStringRef) CFDictionaryGetValue(interfaceInfo, CFSTR(kIOBSDNameKey)));
}
// PCI Slot Name
if ( (err == 0) && CFStringFindWithOptions(result, CFSTR("%2$@"), CFRangeMake(0, CFStringGetLength(result)), kNilOptions, &foundRange) ) {
assert(CFDictionaryGetValue(interfaceInfo, CFSTR("AAPL,slot-name")) != NULL);
CFStringReplace(result, foundRange, (CFStringRef) CFDictionaryGetValue(interfaceInfo, CFSTR("AAPL,slot-name")));
}
// PCI Function Number
if ( (err == 0) && CFStringFindWithOptions(result, CFSTR("%3$@"), CFRangeMake(0, CFStringGetLength(result)), kNilOptions, &foundRange) ) {
assert(CFDictionaryGetValue(interfaceInfo, CFSTR("IOChildIndex")) != NULL);
CFStringReplace(result, foundRange, (CFStringRef) CFDictionaryGetValue(interfaceInfo, CFSTR("IOChildIndex")));
}
// IOTTYBaseName
if ( (err == 0) && CFStringFindWithOptions(result, CFSTR("%4$@"), CFRangeMake(0, CFStringGetLength(result)), kNilOptions, &foundRange) ) {
assert(CFDictionaryGetValue(interfaceInfo, CFSTR(kIOTTYBaseNameKey)) != NULL);
CFStringReplace(result, foundRange, (CFStringRef) CFDictionaryGetValue(interfaceInfo, CFSTR(kIOTTYBaseNameKey)));
}
// Clean up.
if (err == 0) {
*userVisibleName = result;
} else {
CFQRelease(result);
}
assert( (err == 0) == (*userVisibleName != NULL) );
return err;
}
static CFStringRef CopyUserVisibleSlotName(CFStringRef slotName)
// Translate the PCI card slot name into a user friendly number.
//
// Machine Slot Name (in numeric order, starting at 1)
// --------- ---------
// Beige G3 A1, B1, C1
// B&W G3, G4 (PCI Graphics) J12, J11, J10, J9
// G4 (AGP Graphics) A, B, C, D
// G4 (Digital Audio)... 1, 2, 3, 4, 5
// ... and later ditto
{
CFStringRef result;
CFStringRef slotPrefix;
CFStringRef trimmedSlotName;
assert(slotName != NULL);
result = NULL; // not really necessary, but used for post-condition debug assert
slotPrefix = NULL;
trimmedSlotName = NULL;
slotPrefix = CFSTR("SLOT-");
if ( CFStringHasPrefix(slotName, slotPrefix) ) {
trimmedSlotName = CFStringCreateWithSubstring(NULL,
slotName,
CFRangeMake(CFStringGetLength(slotPrefix),
CFStringGetLength(slotName) - CFStringGetLength(slotPrefix))
);
}
if (trimmedSlotName == NULL) {
trimmedSlotName = (CFStringRef) CFRetain(slotName);
}
// The Network preferences panel on 10.2 and later will translate
// slot names into slot numbers. We replicate it's behaviour here.
if (MoreSCGetSystemVersion() >= 0x01020) {
// Beige G3
if ( CFEqual(trimmedSlotName, CFSTR("A1")) ) {
result = CFSTR("1");
} else if ( CFEqual(trimmedSlotName, CFSTR("B1")) ) {
result = CFSTR("2");
} else if ( CFEqual(trimmedSlotName, CFSTR("C1")) ) {
result = CFSTR("3");
// Blue and White G3, G4 (PCI Graphics)
} else if ( CFEqual(trimmedSlotName, CFSTR("J12")) ) {
result = CFSTR("1");
} else if ( CFEqual(trimmedSlotName, CFSTR("J11")) ) {
result = CFSTR("2");
} else if ( CFEqual(trimmedSlotName, CFSTR("J10")) ) {
result = CFSTR("3");
} else if ( CFEqual(trimmedSlotName, CFSTR("J9")) ) {
result = CFSTR("4");
// G4 (AGP Graphics)
} else if ( CFEqual(trimmedSlotName, CFSTR("A")) ) {
result = CFSTR("1");
} else if ( CFEqual(trimmedSlotName, CFSTR("B")) ) {
result = CFSTR("2");
} else if ( CFEqual(trimmedSlotName, CFSTR("C")) ) {
result = CFSTR("3");
} else if ( CFEqual(trimmedSlotName, CFSTR("D")) ) {
result = CFSTR("4");
// all later models
} else {
result = trimmedSlotName;
}
} else {
result = trimmedSlotName;
}
CFRetain(result);
CFQRelease(trimmedSlotName);
assert(result != NULL);
return result;
}
static kern_return_t CopyUserVisibleNameForEthernetLikePort(io_object_t interface,
CFMutableDictionaryRef interfaceInfo,
CFStringRef *userVisibleName)
// Given an Ethernet-like device (interface) and a dictionary of
// information about the device (interfaceInfo), guess at the
// user-visible name for the device.
//
// I'm somewhat unhappy with this code (both its length and its style)
// but there's no point fixing it now that Apple provides a proper
// API for this (and thus this code only runs on old systems).
{
kern_return_t err;
kern_return_t junk;
io_object_t controller;
io_object_t bus;
CFStringRef bsdName;
CFMutableDictionaryRef controllerDict;
assert(interface != IO_OBJECT_NULL);
assert(interfaceInfo != NULL);
assert( userVisibleName != NULL);
assert(*userVisibleName == NULL);
// Look up the parent service, and the parent of that.
controller = IO_OBJECT_NULL;
bus = IO_OBJECT_NULL;
bsdName = NULL;
controllerDict = NULL;
err = IORegistryEntryGetParentEntry(interface, kIOServicePlane, &controller);
if (err == 0) {
err = IORegistryEntryGetParentEntry(controller, kIOServicePlane, &bus);
}
// Establish dictionary for the controller and bsdName for the device.
if (err == 0) {
err = IORegistryEntryCreateCFProperties(controller, &controllerDict, NULL, kNilOptions );
}
if (err == 0) {
bsdName = (CFStringRef) IORegistryEntryCreateCFProperty(interface, CFSTR(kIOBSDNameKey), NULL, kNilOptions);
if (bsdName == NULL) {
err = -1;
}
}
// Now handle all of the wacky special cases.
if (err == 0) {
CFStringRef tmpStr;
// Set userVisibleName to a reference to the port's user-visible name,
// handling all of the special cases.
if ( CFEqualString(bsdName, CFSTR("en0")) ) {
// built-in ethernet
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kBuiltInEthernetSortOrder);
assert(junk == 0);
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelEthernetBuiltIn );
} else if ( CFEqualString(bsdName, CFSTR("fw0")) ) {
// built-in FireWire
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kBuiltInFireWireSortOrder);
assert(junk == 0);
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelFireWireBuiltIn );
} else if ( IOObjectConformsTo(interface, "IO80211Interface")
|| ( CFDictionaryGetValueIfPresent(controllerDict, CFSTR(kIOClassKey), (const void **) &tmpStr)
&& CFStringHasPrefix(tmpStr, CFSTR("AirPort"))
)
) {
// airport
CFDictionarySetValue(interfaceInfo, kSCPropNetInterfaceHardware, kSCEntNetAirPort);
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kAirPortSortOrder);
assert(junk == 0);
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelAirPort );
} else {
CFStringRef nameProperty;
CFMutableDictionaryRef busDict;
// Some other type of Ethernet-like device.
busDict = NULL;
nameProperty = NULL;
err = IORegistryEntryCreateCFProperties(bus, &busDict, NULL, kNilOptions);
if (err == 0) {
CFTypeRef nameProp;
nameProp = (CFTypeRef) CFDictionaryGetValue(busDict, CFSTR("name"));
if (nameProp == NULL) {
nameProperty = NULL;
} else {
nameProperty = CreateStringFromProperty(nameProp);
if (nameProperty == NULL) {
err = -1;
}
}
}
if (err == 0) {
if ( (nameProperty != NULL) && (CFEqualString(nameProperty, CFSTR("radio"))) ) {
// non-airport wireless (assuming someone writes a driver)
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kWirelessSortOrder);
assert(junk == 0);
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelWireless );
} else if ( CFDictionaryGetValueIfPresent(controllerDict, CFSTR(kIOProviderClassKey), (const void **) &tmpStr)
&& CFEqualString(tmpStr, CFSTR("IOPCIDevice"))) {
CFTypeRef slotProp;
CFStringRef slotName;
CFNumberRef portNum;
SInt32 junkNum;
// It's on a PCI bus.
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kEthernetPCISortOrder);
assert(junk == 0);
// Get "AAPL,slot-name" property from I/O Registry and copy it into
// our interfaceInfo dictionary. But first, mutate the slot name
// into a user-visible name version.
slotName = NULL;
slotProp = (CFTypeRef) IORegistryEntryCreateCFProperty(bus, CFSTR("AAPL,slot-name"), NULL, kNilOptions);
if (slotProp != NULL) {
slotName = CreateStringFromProperty(slotProp);
}
if (slotName != NULL) {
CFStringRef tmp;
tmp = CopyUserVisibleSlotName(slotName);
if (tmp != NULL) {
CFQRelease(slotName);
slotName = tmp;
}
}
if (slotName == NULL) {
CFDictionarySetValue(interfaceInfo, CFSTR("AAPL,slot-name"), CFSTR("unknown"));
} else {
CFDictionarySetValue(interfaceInfo, CFSTR("AAPL,slot-name"), slotName);
}
CFQRelease(slotName);
// Now get the IOChildIndex property to decide whether it's a single
// or multi-port Ethernet card.
portNum = (CFNumberRef) CFDictionaryGetValue(busDict, CFSTR("IOChildIndex"));
if ( (portNum == NULL) || ! CFNumberGetValue(portNum, kCFNumberSInt32Type, &junkNum) ) {
// single-port pci enet card
// Either there is no IOChildIndex property, or it doesn't contain a
// valid number.
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelEthernetPCI );
} else {
CFNumberRef portNumRef;
SInt32 portNum;
CFStringRef portNumString;
// multi-port pci enet card
// Get "IOChildIndex" property from I/O Registry and copy it into
// our interfaceInfo dictionary.
portNumString = NULL;
portNumRef = (CFNumberRef) IORegistryEntryCreateCFProperty(bus, CFSTR("IOChildIndex"), NULL, kNilOptions);
if ( (portNumRef != NULL) && CFNumberGetValue(portNumRef, kCFNumberSInt32Type, &portNum) ) {
portNumString = CFStringCreateWithFormat(NULL, NULL, CFSTR("%ld"), portNum + 1);
}
if (portNumString == NULL) {
CFDictionarySetValue(interfaceInfo, CFSTR("IOChildIndex"), CFSTR("unknown"));
} else {
CFDictionarySetValue(interfaceInfo, CFSTR("IOChildIndex"), portNumString);
}
CFQRelease(portNumString);
CFQRelease(portNumRef);
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelEthernetPCIMultiport );
}
} else {
CFStringRef bsdName;
// unknown enet device - use a generic name and include its BSD name,
// since we don't know what else to do
bsdName = (CFStringRef) IORegistryEntryCreateCFProperty(interface, CFSTR(kIOBSDNameKey), NULL, kNilOptions);
if (bsdName == NULL) {
CFDictionarySetValue(interfaceInfo, CFSTR(kIOBSDNameKey), CFSTR("unknown"));
} else {
CFDictionarySetValue(interfaceInfo, CFSTR(kIOBSDNameKey), bsdName);
}
CFQRelease(bsdName);
*userVisibleName = (CFStringRef) CFQRetain( kMoreSCPortLabelEthernet );
}
}
CFQRelease(nameProperty);
CFQRelease(busDict);
}
}
// Clean up.
junk = IOObjectRelease(bus);
assert(junk == 0);
junk = IOObjectRelease(controller);
assert(junk == 0);
CFQRelease(controllerDict);
CFQRelease(bsdName);
assert( (err == 0) == (*userVisibleName != NULL) );
return err;
}
static kern_return_t CopyUserVisibleNameForModemOrSerialPort(io_object_t interface,
CFMutableDictionaryRef interfaceInfo,
CFStringRef *userVisibleName)
// Given a serial device (interface) and a dictionary of
// information about the device (interfaceInfo), guess at the
// user-visible name for the device.
//
// I'm somewhat unhappy with this code (both its length and its style)
// but there's no point fixing it now that Apple provides a proper
// API for this (and thus this code only runs on old systems).
{
kern_return_t err;
kern_return_t junk;
CFStringRef serialType;
Boolean isModem;
CFStringRef baseName;
CFStringRef productName;
assert(interface != IO_OBJECT_NULL);
assert(interfaceInfo != NULL);
assert( userVisibleName != NULL);
assert(*userVisibleName == NULL);
isModem = false; // quieten warning
serialType = NULL;
baseName = NULL;
productName = NULL;
// Determine whether the device is a modem.
err = 0;
serialType = (CFStringRef) IORegistryEntryCreateCFProperty(interface, CFSTR(kIOSerialBSDTypeKey), NULL, kNilOptions);
if (serialType == NULL) {
err = -1;
}
if (err == 0) {
isModem = CFEqual(serialType, CFSTR(kIOSerialBSDModemType));
if (isModem) {
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kModemSortOrder);
assert(junk == 0);
}
}
// Get the "IOTTYBaseName" property and derive the user-visible name from that.
if (err == 0) {
baseName = (CFStringRef) IORegistryEntryCreateCFProperty(interface, CFSTR(kIOTTYBaseNameKey), NULL, kNilOptions);
if (baseName == NULL) {
err = -1;
}
}
if (err == noErr) {
// See whether the device has a "Product Name" property anywhere in its
// parent chain.
productName = (CFStringRef) IORegistryEntrySearchCFProperty(interface, kIOServicePlane,
CFSTR(kIOPropertyProductNameKey),
NULL,
kIORegistryIterateRecursively | kIORegistryIterateParents);
}
if (err == 0) {
if ( isModem && CFEqualString(baseName, CFSTR("modem")) ) {
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kInternalModemSortOrder);
assert(junk == 0);
*userVisibleName = kMoreSCPortLabelModemInternal;
CFRetain(*userVisibleName);
} else if ( CFEqualString(baseName, CFSTR("modem"))) {
*userVisibleName = kMoreSCPortLabelModemPort;
CFRetain(*userVisibleName);
} else if ( CFEqualString(baseName, CFSTR("printer"))) {
*userVisibleName = kMoreSCPortLabelPrinterPort;
CFRetain(*userVisibleName);
} else if ( CFEqualString(baseName, CFSTR("modem-printer"))) {
*userVisibleName = kMoreSCPortLabelModemPrinterPort;
CFRetain(*userVisibleName);
} else if ( CFEqualString(baseName, CFSTR("IrDA-IrCOMM"))) {
*userVisibleName = kMoreSCPortLabelModemIrDA;
CFRetain(*userVisibleName);
} else if ( CFStringHasPrefix(baseName, CFSTR("Bluetooth-"))) {
*userVisibleName = kMoreSCPortLabelModemBluetooth;
CFRetain(*userVisibleName);
} else if ( CFEqualString(baseName, CFSTR("usbmodem")) || (productName != NULL) ) {
// It may not be a modem (it may just be a USB serial device), so
// we only override the default sort order (serial) if the we
// know it's a modem.
if (isModem) {
junk = CFQDictionarySetNumber(interfaceInfo, kSortOrderKey, kUSBModemSortOrder);
assert(junk == 0);
}
if (productName != NULL) {
// Serial or modem USB device with "Product Name" key.
*userVisibleName = productName;
} else {
// productName is NULL so baseName must be "usbmodem".
*userVisibleName = kMoreSCPortLabelModemUSB;
}
CFRetain(*userVisibleName);
} else {
CFStringRef baseNameProperty;
CFMutableStringRef ttyBase;
ttyBase = NULL;
baseNameProperty = (CFStringRef) IORegistryEntryCreateCFProperty(interface, CFSTR(kIOTTYBaseNameKey), NULL, kNilOptions);
if (baseNameProperty != NULL) {
ttyBase = CFStringCreateMutableCopy(NULL, 0, baseNameProperty);
}
if (ttyBase == NULL) {
CFDictionarySetValue(interfaceInfo, CFSTR(kIOTTYBaseNameKey), CFSTR("unknown"));
} else {
CFStringLowercase(ttyBase, NULL);
CFDictionarySetValue(interfaceInfo, CFSTR(kIOTTYBaseNameKey), ttyBase);
}
CFQRelease(ttyBase);
CFQRelease(baseNameProperty);
// This string is pretty unlikely, but it does match what the
// Network preferences panel creates in Mac OS X 10.1.x for
// unknown devices, such as a Keyspan serial adapter.
*userVisibleName = kMoreSCPortLabelSerial;
CFRetain(*userVisibleName);
}
}
CFQRelease(serialType);
CFQRelease(baseName);
CFQRelease(productName);
assert( (err == 0) == (*userVisibleName != NULL) );
return err;
}
static kern_return_t CopyUserVisiblePortName(io_object_t interface,
CFMutableDictionaryRef interfaceInfo,
CFStringRef *userVisibleName)
// Given an IOKit device (interface) and a dictionary of
// information about the device (interfaceInfo), guess at the
// user-visible name for the device. The basic algorithm is
// as follows:
//
// 1. Decide on a proposed user-visible name, using heuristics
// that are based on the device type. While doing this add
// keyword substition values to interfaceInfo.
//
// 2. Call the client's callback to see whether it wants to
// modify our decision.
//
// 3. Finally, replace certain keywords in the string with
// values from the interfaceInfo dictionary.
//
// This design allows the client callback to see a limited number
// of strings, and hence makes it easy for the client to localise
// those strings. It also means we only have one set of I/O Registry
// parsing code (in step 1), rather than messing with IOKit once in
// step 1 to decide on the device type and again in step 3 when
// doing the keyword replacement.
{
kern_return_t err;
CFStringRef proposedName;
CFStringRef baseName;
assert(interface != IO_OBJECT_NULL);
assert(interfaceInfo != NULL);
assert( userVisibleName != NULL);
assert(*userVisibleName == NULL);
proposedName = NULL;
baseName = NULL;
// First we synthesise our candidate user-visible name.
if ( IOObjectConformsTo(interface, kIONetworkInterfaceClass) ) {
err = CopyUserVisibleNameForEthernetLikePort(interface, interfaceInfo, &proposedName);
} else if ( IOObjectConformsTo(interface, kIOSerialBSDServiceValue) ) {
err = CopyUserVisibleNameForModemOrSerialPort(interface, interfaceInfo, &proposedName);
} else {
err = 0;
proposedName = (CFStringRef) IORegistryEntryCreateCFProperty(interface, CFSTR(kIOBSDNameKey), NULL, kNilOptions);
if (proposedName == NULL) {
err = -1;
}
}
// Then we ask our client to munge it as it likes.