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
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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
|
//+---------------------------------------------------------------------
//
// Microsoft Windows
// Copyright (C) Microsoft Corporation, 1993 - 1994.
//
// File: d:\nt\private\cairole\com\remote\channelb.cxx
//
// Contents: This module contains thunking classes that allow proxies
// and stubs to use a buffer interface on top of RPC for Cairo
//
// Classes: CRpcChannelBuffer
//
// Functions:
// ChannelThreadInitialize
// ChannelProcessInitialize
// ChannelRegisterProtseq
// ChannelThreadUninitialize
// ChannelProcessUninitialize
// CRpcChannelBuffer::AddRef
// CRpcChannelBuffer::AppInvoke
// CRpcChannelBuffer::CRpcChannelBuffer
// CRpcChannelBuffer::FreeBuffer
// CRpcChannelBuffer::GetBuffer
// CRpcChannelBuffer::QueryInterface
// CRpcChannelBuffer::Release
// CRpcChannelBuffer::SendReceive
// DebugCoSetRpcFault
// DllDebugObjectRPCHook
// ThreadInvoke
// ThreadSendReceive
//
// History: 22 Jun 93 AlexMi Created
// 31 Dec 93 ErikGav Chicago port
// 15 Mar 94 JohannP Added call category support.
// 09 Jun 94 BruceMa Get call category from RPC message
// 19 Jul 94 CraigWi Added support for ASYNC calls
// 01-Aug-94 BruceMa Memory sift fix
//
//----------------------------------------------------------------------
#include <ole2int.h>
#include <channelb.hxx>
#include <hash.hxx> // CUUIDHashTable
#include <riftbl.hxx> // gRIFTbl
#include <callctrl.hxx> // CAptRpcChnl, AptInvoke
#include <threads.hxx> // CRpcThreadCache
#include <service.hxx> // StopListen
#include <resolver.hxx> // CRpcResolver
extern "C"
{
#include "orpc_dbg.h"
}
#include <rpcdcep.h>
#include <rpcndr.h>
#include <obase.h>
#include <ipidtbl.hxx>
#include <security.hxx>
#include <chock.hxx>
// This is needed for the debug hooks. See orpc_dbg.h
#pragma code_seg(".orpc")
/***************************************************************************/
/* Defines. */
#define MAKE_WIN32( status ) \
MAKE_SCODE( SEVERITY_ERROR, FACILITY_WIN32, (status) )
// This should just return a status to runtime, but runtime does not
// support both comm and fault status yet.
#ifdef _CHICAGO_
#define RETURN_COMM_STATUS( status ) return (status)
#else
#define RETURN_COMM_STATUS( status ) RpcRaiseException( status )
#endif
// Flags for local rpc header.
// These are only valid on a request (in a ORPCTHIS header).
const int ORPCF_INPUT_SYNC = ORPCF_RESERVED1;
const int ORPCF_ASYNC = ORPCF_RESERVED2;
// These are only valid on a reply (in a ORPCTHAT header).
const int ORPCF_REJECTED = ORPCF_RESERVED1;
const int ORPCF_RETRY_LATER = ORPCF_RESERVED2;
// Default size of hash table.
const int INITIAL_NUM_BUCKETS = 20;
/***************************************************************************/
/* Typedefs. */
// This structure contains a copy of all the information needed to make a
// call. It is copied so it can be canceled without stray pointer references.
const DWORD CALLCACHE_SIZE = 8;
struct working_call : public CChannelCallInfo
{
working_call( CALLCATEGORY callcat,
RPCOLEMESSAGE *message,
DWORD flags,
REFIPID ipidServer,
DWORD destctx,
CRpcChannelBuffer *channel,
DWORD authn_level );
void *operator new ( size_t );
void operator delete( void * );
static void Cleanup ( void );
static void Initialize ( void );
RPCOLEMESSAGE message;
private:
static void *list[CALLCACHE_SIZE];
static DWORD next;
};
void *working_call::list[CALLCACHE_SIZE] =
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
DWORD working_call::next = 0;
/***************************************************************************/
/* Macros. */
// Compute the size needed for the implicit this pointer including the
// various optional headers.
inline DWORD SIZENEEDED_ORPCTHIS( BOOL local, DWORD debug_size )
{
if (debug_size == 0)
return sizeof(WireThisPart1) + ((local) ? sizeof(LocalThis) : 0);
else
return sizeof(WireThisPart2) + ((local) ? sizeof(LocalThis) : 0) +
debug_size;
}
inline DWORD SIZENEEDED_ORPCTHAT( DWORD debug_size )
{
if (debug_size == 0)
return sizeof(WireThatPart1);
else
return sizeof(WireThatPart2) + debug_size;
}
inline CALLCATEGORY GetCallCat( void *header )
{
WireThis *inb = (WireThis *) header;
if (inb->c.flags & ORPCF_ASYNC)
return CALLCAT_ASYNC;
else if (inb->c.flags & ORPCF_INPUT_SYNC)
return CALLCAT_INPUTSYNC;
else
return CALLCAT_SYNCHRONOUS;
}
/***************************************************************************/
/* Globals. */
// Should the debugger hooks be called?
BOOL DoDebuggerHooks = FALSE;
LPORPC_INIT_ARGS DebuggerArg = NULL;
// The extension identifier for debug data.
const uuid_t DEBUG_EXTENSION =
{ 0xf1f19680, 0x4d2a, 0x11ce, {0xa6, 0x6a, 0x00, 0x20, 0xaf, 0x6e, 0x72, 0xf4}};
#if DBG == 1
// strings that prefix the call
WCHAR *wszDebugORPCCallPrefixString[4] = { L"--> [BEG]", // Invoke
L" --> [end]",
L"<-- [BEG]", // SendReceive
L" <-- [end]" };
LONG ulDebugORPCCallNestingLevel[4] = {1, -1, 1, -1};
#endif
SHashChain OIDBuckets[23] = { {&OIDBuckets[0], &OIDBuckets[0]},
{&OIDBuckets[1], &OIDBuckets[1]},
{&OIDBuckets[2], &OIDBuckets[2]},
{&OIDBuckets[3], &OIDBuckets[3]},
{&OIDBuckets[4], &OIDBuckets[4]},
{&OIDBuckets[5], &OIDBuckets[5]},
{&OIDBuckets[6], &OIDBuckets[6]},
{&OIDBuckets[7], &OIDBuckets[7]},
{&OIDBuckets[8], &OIDBuckets[8]},
{&OIDBuckets[9], &OIDBuckets[9]},
{&OIDBuckets[10], &OIDBuckets[10]},
{&OIDBuckets[11], &OIDBuckets[11]},
{&OIDBuckets[12], &OIDBuckets[12]},
{&OIDBuckets[13], &OIDBuckets[13]},
{&OIDBuckets[14], &OIDBuckets[14]},
{&OIDBuckets[15], &OIDBuckets[15]},
{&OIDBuckets[16], &OIDBuckets[16]},
{&OIDBuckets[17], &OIDBuckets[17]},
{&OIDBuckets[18], &OIDBuckets[18]},
{&OIDBuckets[19], &OIDBuckets[19]},
{&OIDBuckets[20], &OIDBuckets[20]},
{&OIDBuckets[21], &OIDBuckets[21]},
{&OIDBuckets[22], &OIDBuckets[22]}
};
CUUIDHashTable gClientRegisteredOIDs;
// flag whether or not the channel has been initialized for current process
BOOL gfChannelProcessInitialized = 0;
BOOL gfMTAChannelInitialized = 0;
// count of multi-threaded apartment inits (see CoInitializeEx)
extern DWORD g_cMTAInits;
// Channel debug hook object.
CDebugChannelHook gDebugHook;
// Channel error hook object.
CErrorChannelHook gErrorHook;
#if DBG==1
//-------------------------------------------------------------------------
//
// Function: GetInterfaceName
//
// synopsis: Gets the human readable name of an Interface given it's IID.
//
// History: 12-Jun-95 Rickhi Created
//
//-------------------------------------------------------------------------
LONG GetInterfaceName(REFIID riid, WCHAR *pwszName)
{
// convert the iid to a string
CDbgGuidStr dbgsIID(riid);
// Read the registry entry for the interface to get the interface name
LONG ulcb=256;
WCHAR szKey[80];
szKey[0] = L'\0';
lstrcatW(szKey, L"Interface\\");
lstrcatW(szKey, dbgsIID._wszGuid);
LONG result = RegQueryValue(
HKEY_CLASSES_ROOT,
szKey,
pwszName,
&ulcb);
Win4Assert( result == 0 );
return result;
}
//---------------------------------------------------------------------------
//
// Function: DebugPrintORPCCall
//
// synopsis: Prints the interface name and method number to the debugger
// to allow simple ORPC call tracing.
//
// History: 12-Jun-95 Rickhi Created
//
//---------------------------------------------------------------------------
void DebugPrintORPCCall(DWORD dwFlag, REFIID riid, DWORD iMethod, DWORD Callcat)
{
if (CairoleInfoLevel & DEB_USER15)
{
Win4Assert (dwFlag < 4);
// adjust the nesting level for this thread.
COleTls tls;
tls->cORPCNestingLevel += ulDebugORPCCallNestingLevel[dwFlag];
// set the indentation string according to the nesting level
CHAR szNesting[100];
memset(szNesting, 0x20, 100);
if (tls->cORPCNestingLevel > 99) // watch for overflow
szNesting[99] = '\0';
else
szNesting[tls->cORPCNestingLevel] = '\0';
// construct the debug strings
WCHAR *pwszDirection = wszDebugORPCCallPrefixString[dwFlag];
WCHAR wszName[100];
GetInterfaceName(riid, wszName);
ComDebOut((DEB_USER15, "%s%ws [%x] %ws:: %x\n",
szNesting, pwszDirection, Callcat, wszName, iMethod));
}
}
#endif
/***************************************************************************/
void ByteSwapThis( DWORD drep, WireThis *inb )
{
if ((drep & NDR_LOCAL_DATA_REPRESENTATION) != NDR_LITTLE_ENDIAN)
{
// Extensions are swapped later. If we ever use the reserved field,
// swap it.
ByteSwapShort( inb->c.version.MajorVersion );
ByteSwapShort( inb->c.version.MinorVersion );
ByteSwapLong( inb->c.flags );
// ByteSwapLong( inb->c.reserved1 );
ByteSwapLong( inb->c.cid.Data1 );
ByteSwapShort( inb->c.cid.Data2 );
ByteSwapShort( inb->c.cid.Data3 );
}
}
/***************************************************************************/
void ByteSwapThat( DWORD drep, WireThat *outb )
{
if ((drep & NDR_LOCAL_DATA_REPRESENTATION) != NDR_LITTLE_ENDIAN)
{
// Extensions are swapped later.
ByteSwapLong( outb->c.flags );
}
}
//+-------------------------------------------------------------------
//
// Function: ChannelProcessInitialize, public
//
// Synopsis: Initializes the channel subsystem per process data.
//
// History: 23-Nov-93 AlexMit Created
//
//--------------------------------------------------------------------
STDAPI ChannelProcessInitialize()
{
TRACECALL(TRACE_RPC, "ChannelProcessInitialize");
ComDebOut((DEB_COMPOBJ, "ChannelProcessInitialize [IN]\n"));
Win4Assert( (sizeof(WireThisPart1) & 7) == 0 );
Win4Assert( (sizeof(WireThisPart2) & 7) == 0 );
Win4Assert( (sizeof(LocalThis) & 7) == 0 );
Win4Assert( (sizeof(WireThatPart1) & 7) == 0 );
Win4Assert( (sizeof(WireThatPart2) & 7) == 0 );
// we want to take the gComLock since that prevents other Rpc
// threads from accessing anything we are about to create, in
// particular, the event cache and working_call cache are accessed
// by Rpc worker threads of cancelled calls.
ASSERT_LOCK_RELEASED
LOCK
HRESULT hr = S_OK;
if (!gfChannelProcessInitialized)
{
// Initialize the interface hash tables, the OID hash table, and
// the MID hash table. We dont need to cleanup these on errors.
gMIDTbl.Initialize();
gOXIDTbl.Initialize();
gRIFTbl.Initialize();
gIPIDTbl.Initialize();
gSRFTbl.Initialize();
gClientRegisteredOIDs.Initialize(OIDBuckets);
// Register the debug channel hook.
hr = CoRegisterChannelHook( DEBUG_EXTENSION, &gDebugHook );
// Register the error channel hook.
if(SUCCEEDED(hr))
{
hr = CoRegisterChannelHook( ERROR_EXTENSION, &gErrorHook );
}
// Initialize security.
if (SUCCEEDED(hr))
{
hr = InitializeSecurity();
}
// always set to TRUE if we initialized ANYTHING, regardless of
// whether there were any errors. That way, ChannelProcessUninit
// will cleanup anything we have initialized.
gfChannelProcessInitialized = TRUE;
}
UNLOCK
ASSERT_LOCK_RELEASED
if (FAILED(hr))
{
// cleanup anything we have created thus far.
ChannelProcessUninitialize();
}
ComDebOut((DEB_COMPOBJ, "ChannelProcessInitialize [OUT] hr:%x\n", hr));
return hr;
}
//+-------------------------------------------------------------------
//
// Function: CleanupRegOIDs, public
//
// Synopsis: called to delete each node of the registered OID list.
//
//+-------------------------------------------------------------------
void CleanupRegOIDs(SHashChain *pNode)
{
delete pNode;
}
//+-------------------------------------------------------------------
//
// Function: ChannelProcessUninitialize, public
//
// Synopsis: Uninitializes the channel subsystem global data.
//
// History: 23-Nov-93 Rickhi Created
//
// Notes: This is called at process uninitialize, not thread
// uninitialize.
//
//--------------------------------------------------------------------
STDAPI_(void) ChannelProcessUninitialize(void)
{
TRACECALL(TRACE_RPC, "ChannelProcessUninitialize");
ComDebOut((DEB_COMPOBJ, "ChannelProcessUninitialize [IN]\n"));
if (gfChannelProcessInitialized)
{
// Stop accepting calls from the object resolver and flag that service
// is no longer initialized. This can result in calls being
// dispatched. Do not hold the lock around this call.
UnregisterDcomInterfaces();
}
gResolver.ReleaseSCMProxy();
// we want to take the gComLock since that prevents other Rpc
// threads from accessing anything we are about to cleanup, in
// particular, the event cache and working_call are accessed by
// Rpc worker threaded for cancelled calls.
ASSERT_LOCK_RELEASED
LOCK
if (gfChannelProcessInitialized)
{
// Release the interface tables. This causes RPC to stop dispatching
// DCOM calls. This can result in calls being dispatched.
// UnRegisterServerInterface releases and reaquires the lock each
// time it is called.
gRIFTbl.Cleanup();
if (gpLocalMIDEntry)
{
// release the local MIDEntry
DecMIDRefCnt(gpLocalMIDEntry);
gpLocalMIDEntry = NULL;
}
// release the MTA apartment's OXIDEntry if there is one. Do this
// after the RIFTble cleanup so we are not processing any calls
// while it happens.
gOXIDTbl.ReleaseLocalMTAEntry();
if (gpsaCurrentProcess)
{
// delete the string bindings for the current process
PrivMemFree(gpsaCurrentProcess);
gpsaCurrentProcess = NULL;
}
// cleanup the IPID, OXID, and MID tables
gOXIDTbl.FreeExpiredEntries(GetTickCount()+1);
gIPIDTbl.Cleanup();
gOXIDTbl.Cleanup();
gMIDTbl.Cleanup();
gSRFTbl.Cleanup();
// Cleanup the OID registration table.
gClientRegisteredOIDs.Cleanup(CleanupRegOIDs);
// Cleanup the call cache.
working_call::Cleanup();
// Release all cached threads.
gRpcThreadCache.ClearFreeList();
// cleanup the event cache
gEventCache.Cleanup();
// Cleanup the channel hooks.
CleanupChannelHooks();
}
// Always cleanup the RPC OXID resolver since security may initialize it.
gResolver.Cleanup();
// Cleanup security.
UninitializeSecurity();
// mark the channel as no longer intialized for this process
gfChannelProcessInitialized = FALSE;
gfMTAChannelInitialized = FALSE;
UNLOCK
ASSERT_LOCK_RELEASED
// release the static unmarshaler
if (gpStdMarshal)
{
((CStdIdentity *)gpStdMarshal)->UnlockAndRelease();
gpStdMarshal = NULL;
}
ComDebOut((DEB_COMPOBJ, "ChannelProcessUninitialize [OUT]\n"));
return;
}
//+-------------------------------------------------------------------
//
// Function: STAChannelInitialize, public
//
// Synopsis: Initializes the channel subsystem per thread data
// for single-threaded apartments.
//
// History: 23-Nov-93 Rickhi Created
//
// Notes: This is called at thread initialize, not process
// initialize. Cleanup is done in ChannelThreadUninitialize.
//
//--------------------------------------------------------------------
STDAPI STAChannelInitialize(void)
{
ComDebOut((DEB_COMPOBJ, "STAChannelInitialize [IN]\n"));
Win4Assert(IsSTAThread());
HRESULT hr = S_OK;
if (!gfChannelProcessInitialized)
{
// process initialization has not been done, do that now.
if (FAILED(hr = ChannelProcessInitialize()))
return hr;
}
// create the callctrl before calling ThreadStart, since the latter
// tries to register with the call controller. We might already have
// a callctrl if some DDE stuff has already run.
COleTls tls;
if (tls->pCallCtrl == NULL)
{
// assume OOM and try to create callctrl. ctor sets tls.
hr = E_OUTOFMEMORY;
CAptCallCtrl *pCallCtrl = new CAptCallCtrl();
}
if (tls->pCallCtrl)
{
// mark the channel as initialized now to prevent re-entracy in
// GetLocalEntry.
tls->dwFlags |= OLETLS_CHANNELTHREADINITIALZED;
// Precreate the thread window. The window is normally only used
// for requests (and thus created during marshalling). But in WOW
// it is used for responses (and thus created during initialization).
// We do it for normal cases here too in order to avoid recursion
// when marshaling the first interface.
ASSERT_LOCK_RELEASED
LOCK
OXIDEntry *pOxid;
hr = gOXIDTbl.GetLocalEntry( &pOxid );
UNLOCK
ASSERT_LOCK_RELEASED
if (SUCCEEDED(hr))
{
hr = ThreadStart();
}
// Clear the channel initialized flag if initialization fails.
// Everything gets cleaned up in uninitialize regardless of the
// channel flag.
if (FAILED(hr))
tls->dwFlags &= ~OLETLS_CHANNELTHREADINITIALZED;
}
ComDebOut((DEB_COMPOBJ, "STAChannelInitialize [OUT] hr:%x\n", hr));
return hr;
}
//+-------------------------------------------------------------------
//
// Function: MTAChannelInitialize, public
//
// Synopsis: Initializes the channel subsystem per thread data
// for multi-threaded apartments.
//
// History: 19-Mar-96 Rickhi Created
//
// Notes: This is called at thread initialize, not process
// initialize. Cleanup is done in ChannelThreadUninitialize.
//
//--------------------------------------------------------------------
STDAPI MTAChannelInitialize(void)
{
ComDebOut((DEB_COMPOBJ, "MTAChannelInitialize [IN]\n"));
Win4Assert(IsMTAThread());
HRESULT hr = S_OK;
if (!gfChannelProcessInitialized)
{
// process initialization has not been done, do that now.
if (FAILED(hr = ChannelProcessInitialize()))
return hr;
}
ASSERT_LOCK_RELEASED
LOCK
if (!gfMTAChannelInitialized)
{
// Create the OXID entry for this apartment. Do it now to avoid
// any races with two threads creating it simultaneously.
OXIDEntry *pOxid;
hr = gOXIDTbl.GetLocalEntry( &pOxid );
if (SUCCEEDED(hr))
{
pOxid->dwFlags &= ~ OXIDF_STOPPED;
gfMTAChannelInitialized = TRUE;
}
}
UNLOCK
ASSERT_LOCK_RELEASED
ComDebOut((DEB_COMPOBJ, "MTAChannelInitialize [OUT] hr:%x\n", hr));
return hr;
}
//+-------------------------------------------------------------------
//
// Function: ChannelThreadUninitialize, private
//
// Synopsis: Uninitializes the channel subsystem per thread data.
//
// History: 23-Nov-93 Rickhi Created
//
// Notes: This is called at thread uninitialize, not process
// uninitialize.
//
//--------------------------------------------------------------------
STDAPI_(void) ChannelThreadUninitialize(void)
{
TRACECALL(TRACE_RPC, "ChannelThreadUninitialize");
ComDebOut((DEB_COMPOBJ, "ChannelThreadUninitialize [IN]\n"));
COleTls tls;
if (tls->dwFlags & OLETLS_APARTMENTTHREADED)
{
// Cleanup the window for this thread.
ThreadCleanup();
// Free the apartment call control.
delete tls->pCallCtrl;
tls->pCallCtrl = NULL;
// Free any registered MessageFilter that has not been picked
// up by the call ctrl.
if (tls->pMsgFilter)
{
tls->pMsgFilter->Release();
tls->pMsgFilter = NULL;
}
// release the OXIDEntry for this thread.
ASSERT_LOCK_RELEASED
LOCK
gOXIDTbl.ReleaseLocalSTAEntry();
UNLOCK
ASSERT_LOCK_RELEASED
// mark the thread as no longer intialized for the channel
tls->dwFlags &= ~OLETLS_CHANNELTHREADINITIALZED;
}
else
{
// the MTA channel is no longer initialized.
gfMTAChannelInitialized = FALSE;
}
ComDebOut((DEB_COMPOBJ, "ChannelThreadUninitialize [OUT]\n"));
}
// count of multi-threaded inits
//+-------------------------------------------------------------------
//
// Function: InitChannelIfNecessary, private
//
// Synopsis: Checks if the ORPC channel has been initialized for
// the current apartment and initializes if not. This is
// required by the delayed initialization logic.
//
// History: 26-Oct-95 Rickhi Created
//
//--------------------------------------------------------------------
INTERNAL InitChannelIfNecessary()
{
HRESULT hr;
COleTls tls(hr);
if (FAILED(hr))
return hr;
if (!(tls->dwFlags & OLETLS_APARTMENTTHREADED))
{
if (!gfMTAChannelInitialized)
{
if (g_cMTAInits > 0)
{
// initialize the MTAChannel
return MTAChannelInitialize();
}
// CoInitializeEx(MULTITHREADED) has not been called
return CO_E_NOTINITIALIZED;
}
}
else if (!(tls->dwFlags & OLETLS_CHANNELTHREADINITIALZED))
{
if (tls->cComInits > 0 &&
!(tls->dwFlags & OLETLS_THREADUNINITIALIZING))
return STAChannelInitialize();
// CoInitializeEx(APARTMENTTHREADED) has not been called,
// or the thread is Uninitializing
return CO_E_NOTINITIALIZED;
}
return S_OK;
}
/***************************************************************************/
/*
Make a copy of a message for an asyncronous call. Also make a fake
reply for the call.
*/
CChannelCallInfo *MakeAsyncCopy( CChannelCallInfo *original )
{
void *pBuffer = NULL;
WireThat *outb;
BOOL success;
ASSERT_LOCK_HELD
working_call *copy = new working_call( original->category,
original->pmessage,
original->iFlags,
original->ipid,
original->iDestCtx,
original->pChannel,
original->lAuthnLevel );
if (copy != NULL)
{
original->hResult = S_OK;
if (original->pmessage->rpcFlags & RPCFLG_LOCAL_CALL)
{
// no need to duplicate the buffer, just use it as is.
copy->message.Buffer = original->pmessage->Buffer;
}
else
{
pBuffer = PrivMemAlloc8(original->pmessage->cbBuffer);
Win4Assert(((ULONG)pBuffer & 0x7) == 0 && "Buffer not aligned properly");
if (pBuffer != NULL)
{
copy->message.Buffer = pBuffer;
memcpy(pBuffer, original->pmessage->Buffer,
original->pmessage->cbBuffer);
}
else
{
original->hResult = RPC_E_OUT_OF_RESOURCES;
}
}
if (SUCCEEDED(original->hResult))
{
// pretend local so we don't touch rpc for more buffers, etc.
copy->message.rpcFlags |= RPCFLG_LOCAL_CALL;
// Create a fake reply containing a result even though the
// client will never see it.
original->pmessage->cbBuffer = SIZENEEDED_ORPCTHAT(0) + 4;
if (original->pmessage->rpcFlags & RPCFLG_LOCAL_CALL)
{
original->pmessage->Buffer = PrivMemAlloc8(original->pmessage->cbBuffer);
success = original->pmessage->Buffer != NULL;
}
else
{
success = I_RpcGetBuffer((RPC_MESSAGE *) original->pmessage) == RPC_S_OK;
}
// simulate success in method call
if (success)
{
outb = (WireThat *) original->pmessage->Buffer;
outb->c.flags = ORPCF_NULL;
outb->c.unique = 0;
*(SCODE *)((WireThatPart1 *)outb + 1) = S_OK;
return copy;
}
}
PrivMemFree(pBuffer);
delete copy;
}
return NULL;
}
/***************************************************************************/
STDMETHODIMP_(ULONG) CRpcChannelBuffer::AddRef( THIS )
{
// can't call AssertValid(FALSE) since it is used in asserts
InterlockedIncrement( (long *) &ref_count );
return ref_count;
}
/***************************************************************************/
HRESULT CRpcChannelBuffer::AppInvoke( CChannelCallInfo *call,
IRpcStubBuffer *stub,
void *pv,
void *orig_stub_buffer,
LocalThis *localb )
{
ASSERT_LOCK_RELEASED
RPC_MESSAGE *message = (RPC_MESSAGE *) call->pmessage;
void *orig_buffer = message->Buffer;
WireThat *outb = NULL;
HRESULT result;
// Save a pointer to the inbound header.
call->pHeader = message->Buffer;
// Adjust the buffer.
message->BufferLength -= (char *) orig_stub_buffer - (char *) message->Buffer;
message->Buffer = orig_stub_buffer;
message->ProcNum &= ~RPC_FLAGS_VALID_BIT;
// if the incoming call is from a non-NDR client, then set a bit in
// the message flags field so the stub can figure out how to dispatch
// the call. This allows a 32bit server to simultaneously service a
// 32bit client using NDR and a 16bit client using non-NDR, in particular,
// to support OLE Automation.
if (localb != NULL && localb->flags & LOCALF_NONNDR)
message->RpcFlags |= RPCFLG_NON_NDR;
if (IsMTAThread())
{
// do multi-threaded apartment invoke
result = MTAInvoke((RPCOLEMESSAGE *)message, GetCallCat( call->pHeader ),
stub, this, &call->server_fault);
}
else
{
// do single-threaded apartment invoke
result = STAInvoke((RPCOLEMESSAGE *)message, GetCallCat( call->pHeader ),
stub, this, pv, &call->server_fault);
}
// For local calls, just free the in buffer. For non-local calls,
// the RPC runtime does this for us.
if (message->RpcFlags & RPCFLG_LOCAL_CALL)
PrivMemFree( orig_buffer );
// If an exception occurred before a new buffer was allocated,
// set the Buffer field to point to the original buffer.
if (message->Buffer == orig_stub_buffer)
{
// The buffer pointer in the message must be correct so RPC can free it.
if (message->RpcFlags & RPCFLG_LOCAL_CALL)
message->Buffer = NULL;
else
message->Buffer = orig_buffer;
}
else if (message->Buffer != NULL)
{
// An out buffer exists, get the pointer to the channel header.
Win4Assert( call->pHeader != orig_buffer );
message->BufferLength += (char *) message->Buffer - (char *) call->pHeader;
message->Buffer = call->pHeader;
outb = (WireThat *) message->Buffer;
}
// If successful, adjust the buffer.
if (result == S_OK)
{
if (call->iDestCtx == MSHCTX_DIFFERENTMACHINE)
outb->c.flags = 0;
else
outb->c.flags = ORPCF_LOCAL;
// For asynchronous calls, MSWMSG will delete the out buffer. If MSWMSG
// is not the transport, delete it here. Non-Mswmsg async calls have
// been converted to local calls.
if (message->RpcFlags & RPCFLG_LOCAL_CALL &&
call->category == CALLCAT_ASYNC)
{
PrivMemFree( message->Buffer );
message->Buffer = NULL;
}
}
else if (result == RPC_E_CALL_REJECTED)
{
// Call was rejected. If the caller is on another machine, just fail the
// call.
if (call->iDestCtx != MSHCTX_DIFFERENTMACHINE && outb != NULL)
{
// Otherwise return S_OK so the buffer gets back, but set the flag
// to indicate it was rejected.
outb->c.flags = ORPCF_LOCAL | ORPCF_REJECTED;
result = S_OK;
}
}
else if (result == RPC_E_SERVERCALL_RETRYLATER)
{
// Call was rejected. If the caller is on another machine, just fail the
// call.
if (call->iDestCtx != MSHCTX_DIFFERENTMACHINE && outb != NULL)
{
// Otherwise return S_OK so the buffer gets back, but set the flag
// to indicate it was rejected with retry later.
outb->c.flags = ORPCF_LOCAL | ORPCF_RETRY_LATER;
result = S_OK;
}
}
else if (message->RpcFlags & RPCFLG_LOCAL_CALL)
{
// call failed and the call is local, free the out buffer. For
// non-local calls the RPC runtime does this for us.
PrivMemFree( message->Buffer );
message->Buffer = NULL;
}
ASSERT_LOCK_RELEASED
return result;
}
//+---------------------------------------------------------------------------
//
// Function: AppInvokeExceptionFilter
//
// Synopsis: Determine if the application as thrown an exception we want
// to report. If it has, then print out enough information for
// the 'user' to debug the problem
//
// Arguments: [lpep] -- Exception context records
//
// History: 6-20-95 kevinro Created
//
// Notes:
//
// At the moment, I was unable to get this to work for Win95, so I have
// commented out the code.
//
//----------------------------------------------------------------------------
#ifdef _CHICAGO_
//
// Win95 doesn't appear to support this functionality by default.
//
inline LONG
AppInvokeExceptionFilter(
LPEXCEPTION_POINTERS lpep
)
{
return(EXCEPTION_EXECUTE_HANDLER);
}
#else
#include <imagehlp.h>
#define SYM_HANDLE GetCurrentProcess()
#if defined(_M_IX86)
#define MACHINE_TYPE IMAGE_FILE_MACHINE_I386
#elif defined(_M_MRX000)
#define MACHINE_TYPE IMAGE_FILE_MACHINE_R4000
#elif defined(_M_ALPHA)
#define MACHINE_TYPE IMAGE_FILE_MACHINE_ALPHA
#elif defined(_M_PPC)
#define MACHINE_TYPE IMAGE_FILE_MACHINE_POWERPC
#else
#error( "unknown target machine" );
#endif
LONG
AppInvokeExceptionFilter(
LPEXCEPTION_POINTERS lpep
)
{
#if DBG == 1
BOOL rVal;
STACKFRAME StackFrame;
CONTEXT Context;
SymSetOptions( SYMOPT_UNDNAME );
SymInitialize( SYM_HANDLE, NULL, TRUE );
ZeroMemory( &StackFrame, sizeof(StackFrame) );
Context = *lpep->ContextRecord;
#if defined(_M_IX86)
StackFrame.AddrPC.Offset = Context.Eip;
StackFrame.AddrPC.Mode = AddrModeFlat;
StackFrame.AddrFrame.Offset = Context.Ebp;
StackFrame.AddrFrame.Mode = AddrModeFlat;
StackFrame.AddrStack.Offset = Context.Esp;
StackFrame.AddrStack.Mode = AddrModeFlat;
#endif
ComDebOut((DEB_FORCE,"An Exception occurred while calling into app\n"));
ComDebOut((DEB_FORCE,
"Exception address = 0x%x Exception number 0x%x\n",
lpep->ExceptionRecord->ExceptionAddress,
lpep->ExceptionRecord->ExceptionCode ));
ComDebOut((DEB_FORCE,"The following stack trace is where the exception occured\n"));
ComDebOut((DEB_FORCE,"Frame RetAddr mod!symbol\n"));
do
{
rVal = StackWalk(MACHINE_TYPE,SYM_HANDLE,0,&StackFrame,&Context,ReadProcessMemory,
SymFunctionTableAccess,SymGetModuleBase,NULL);
if (rVal)
{
DWORD dump[200];
ULONG Displacement;
PIMAGEHLP_SYMBOL sym = (PIMAGEHLP_SYMBOL) &dump;
IMAGEHLP_MODULE ModuleInfo;
LPSTR pModuleName = "???";
BOOL fSuccess;
sym->SizeOfStruct = sizeof(dump);
fSuccess = SymGetSymFromAddr(SYM_HANDLE,StackFrame.AddrPC.Offset,
&Displacement,sym);
//
// If there is module name information available, then grab it.
//
if(SymGetModuleInfo(SYM_HANDLE,StackFrame.AddrPC.Offset,&ModuleInfo))
{
pModuleName = ModuleInfo.ModuleName;
}
if (fSuccess)
{
ComDebOut((DEB_FORCE,
"%08x %08x %s!%s + %x\n",
StackFrame.AddrFrame.Offset,
StackFrame.AddrReturn.Offset,
pModuleName,
sym->Name,
Displacement));
}
else
{
ComDebOut((DEB_FORCE,
"%08x %08x %s!%08x\n",
StackFrame.AddrFrame.Offset,
StackFrame.AddrReturn.Offset,
pModuleName,
StackFrame.AddrPC.Offset));
}
}
} while( rVal );
SymCleanup( SYM_HANDLE );
#endif
return EXCEPTION_EXECUTE_HANDLER;
}
#endif // _CHICAGO_
/***************************************************************************/
#if DBG == 1
DWORD AppInvoke_break = 0;
DWORD AppInvoke_count = 0;
#endif
HRESULT StubInvoke(RPCOLEMESSAGE *pMsg, IRpcStubBuffer *pStub,
IRpcChannelBuffer *pChnl, DWORD *pdwFault)
{
ComDebOut((DEB_CHANNEL, "StubInvoke pMsg:%x pStub:%x pChnl:%x pdwFault:%x\n",
pMsg, pStub, pChnl, pdwFault));
ASSERT_LOCK_RELEASED
HRESULT hr;
#if DBG==1
DWORD dwMethod = pMsg->iMethod;
IID iidBeingCalled = ((RPC_SERVER_INTERFACE *) pMsg->reserved2[1])->InterfaceId.SyntaxGUID;
#endif
_try
{
TRACECALL(TRACE_RPC, "StubInvoke");
#if DBG == 1
//
// On a debug build, we are able to break on a call by serial number.
// This isn't really 100% thread safe, but is still extremely useful
// when debugging a problem.
//
DWORD dwBreakCount = ++AppInvoke_count;
ComDebOut((DEB_CHANNEL, "AppInvoke(0x%x) calling method 0x%x iid %I\n",
dwBreakCount,dwMethod, &iidBeingCalled));
if(AppInvoke_break == dwBreakCount)
{
DebugBreak();
}
#endif
#ifdef WX86OLE
if (! gcwx86.IsN2XProxy(pStub))
{
IUnknown *pActual;
hr = pStub->DebugServerQueryInterface((void **)&pActual);
if (SUCCEEDED(hr))
{
if (gcwx86.IsN2XProxy(pActual))
{
// If we are going to invoke a native stub that is
// connected to an object on the x86 side then
// set a flag in the Wx86 thread environment to
// let the thunk layer know that the call is a
// stub invoked call and allow any in or out
// custom interface pointers to be thunked as
// IUnknown rather than failing the interface thunking
gcwx86.SetStubInvokeFlag((BOOL)1);
}
pStub->DebugServerRelease(pActual);
}
}
#endif
hr = pStub->Invoke(pMsg, pChnl);
}
_except(AppInvokeExceptionFilter( GetExceptionInformation()))
{
hr = RPC_E_SERVERFAULT;
*pdwFault = GetExceptionCode();
#if DBG == 1
//
// OLE catches exceptions when the server generates them. This is so we can
// cleanup properly, and allow the client to continue.
//
if (*pdwFault == STATUS_ACCESS_VIOLATION ||
*pdwFault == STATUS_POSSIBLE_DEADLOCK ||
*pdwFault == STATUS_INSTRUCTION_MISALIGNMENT ||
*pdwFault == STATUS_DATATYPE_MISALIGNMENT )
{
WCHAR iidName[256];
iidName[0] = 0;
char achProgname[256];
achProgname[0] = 0;
GetModuleFileNameA(NULL,achProgname,sizeof(achProgname));
GetInterfaceName(iidBeingCalled,iidName);
ComDebOut((DEB_FORCE,
"OLE has caught a fault 0x%08x on behalf of the server %s\n",
*pdwFault,
achProgname));
ComDebOut((DEB_FORCE,
"The fault occured when OLE called the interface %I (%ws) method 0x%x\n",
&iidBeingCalled,iidName,dwMethod));
Win4Assert(!"The server application has faulted processing an inbound RPC request. Check the kernel debugger for useful output. OLE can continue but you probably want to stop and debug the application.");
}
#endif
}
ASSERT_LOCK_RELEASED
ComDebOut((DEB_CHANNEL, "StubInvoke hr:%x dwFault:%x\n", hr, *pdwFault));
return hr;
}
/***************************************************************************/
#if DBG==1
LONG ComInvokeExceptionFilter( DWORD lCode,
LPEXCEPTION_POINTERS lpep )
{
ComDebOut((DEB_ERROR, "Exception 0x%x in ComInvoke at address 0x%x\n",
lCode, lpep->ExceptionRecord->ExceptionAddress));
DebugBreak();
return EXCEPTION_EXECUTE_HANDLER;
}
#endif
/***************************************************************************/
HRESULT ComInvoke( CChannelCallInfo *call )
{
TRACECALL(TRACE_RPC, "ComInvoke");
ASSERT_LOCK_RELEASED
RPC_MESSAGE *message = (RPC_MESSAGE *) call->pmessage;
LocalThis *localb;
void *saved_buffer;
RPC_STATUS status;
HRESULT result;
IPIDEntry *ipid_entry = NULL;
CRpcChannelBuffer *server_channel = NULL;
DWORD TIDCallerSaved;
BOOL fLocalSaved;
UUID saved_threadid;
IUnknown *save_context;
DWORD saved_authn_level;
char *stub_data;
WireThis *inb = (WireThis *) message->Buffer;
OXIDEntry *oxid;
HANDLE hWakeup = NULL;
ComDebOut((DEB_CHANNEL, "ComInvoke callinfo:%x header:%x\n",
call, message->Buffer));
COleTls tls(result);
if (FAILED(result))
return result;
// Catch exceptions that might keep the lock.
#if DBG == 1
_try
{
#endif
// Find the IPID entry. Fail if the IPID or the OXID are not ready.
LOCK
ipid_entry = gIPIDTbl.LookupIPID( call->ipid );
Win4Assert( ipid_entry == NULL || ipid_entry->pOXIDEntry != NULL );
if (ipid_entry == NULL || (ipid_entry->dwFlags & IPIDF_DISCONNECTED) ||
(ipid_entry->pOXIDEntry->dwFlags & OXIDF_STOPPED) ||
ipid_entry->pChnl == NULL)
result = RPC_E_DISCONNECTED;
else if (ipid_entry->pStub == NULL)
result = E_NOINTERFACE;
// Keep the server object and our associated objects alive during the call.
if (SUCCEEDED(result))
{
oxid = ipid_entry->pOXIDEntry;
server_channel = ipid_entry->pChnl;
Win4Assert( server_channel != NULL && server_channel->pStdId != NULL );
server_channel->pStdId->LockServer();
InterlockedIncrement( &oxid->cCalls );
}
UNLOCK
ASSERT_LOCK_RELEASED
if (FAILED(result))
{
return result;
}
// Create a new security call context;
CServerSecurity security( call );
save_context = tls->pCallContext;
tls->pCallContext = &security;
// save the original threadid & copy in the new one.
if (!(tls->dwFlags & OLETLS_UUIDINITIALIZED))
{
UuidCreate(&tls->LogicalThreadId);
tls->dwFlags |= OLETLS_UUIDINITIALIZED;
}
saved_threadid = tls->LogicalThreadId;
tls->LogicalThreadId = inb->c.cid;
ComDebOut((DEB_CALLCONT, "ComInvoke: LogicalThreads Old:%I New:%I\n",
&saved_threadid, &tls->LogicalThreadId));
// Save the call info in TLS.
call->pNext = (CChannelCallInfo *) tls->pCallInfo;
tls->pCallInfo = call;
saved_authn_level = tls->dwAuthnLevel;
tls->dwAuthnLevel = call->lAuthnLevel;
// Call the channel hooks. Set up as much TLS data as possible before
// calling the hooks so they can access it.
result = ServerNotify(
((RPC_SERVER_INTERFACE *) message->RpcInterfaceInformation)->InterfaceId.SyntaxGUID,
(WireThis *) message->Buffer,
message->BufferLength,
(void **) &stub_data,
message->DataRepresentation );
// Find the local header.
if (inb->c.flags & ORPCF_LOCAL)
{
localb = (LocalThis *) stub_data;
stub_data += sizeof(LocalThis);
}
else
localb = NULL;
// Set the caller TID. This is needed by some interop code in order
// to do focus management via tying queues together. We first save the
// current one so we can restore later to deal with nested calls
// correctly.
TIDCallerSaved = tls->dwTIDCaller;
fLocalSaved = tls->dwFlags & OLETLS_LOCALTID;
tls->dwTIDCaller = localb != NULL ? localb->client_thread : 0;
if (call->iFlags & CF_PROCESS_LOCAL)
tls->dwFlags |= OLETLS_LOCALTID; // turn the local bit on
else
tls->dwFlags &= ~OLETLS_LOCALTID; // turn the local bit off
// Continue dispatching the call.
if (result == S_OK)
{
result = server_channel->AppInvoke(
call,
(IRpcStubBuffer *) ipid_entry->pStub,
ipid_entry->pv,
stub_data,
localb );
}
// Restore the original thread id, call info, dest context and thread id.
tls->LogicalThreadId = saved_threadid;
tls->pCallInfo = call->pNext;
tls->dwTIDCaller = TIDCallerSaved;
tls->dwAuthnLevel = saved_authn_level;
if (fLocalSaved)
tls->dwFlags |= OLETLS_LOCALTID;
else
tls->dwFlags &= ~OLETLS_LOCALTID;
// Restore the security context;
tls->pCallContext = save_context;
security.EndCall();
// Decrement the call count. If the MTA is waiting to uninitialize
// and this is the last call, wake up the uninitializing thread, but
// do this *after* calling UnLockServer so the other thread does not
// blow away the server.
if (InterlockedDecrement( &oxid->cCalls ) == 0 &&
(oxid->dwFlags & (OXIDF_MTASERVER | OXIDF_STOPPED)) == (OXIDF_MTASERVER | OXIDF_STOPPED))
hWakeup = oxid->hComplete;
// Release our hold on the object and channel.
server_channel->pStdId->UnLockServer();
if (hWakeup)
SetEvent(hWakeup);
// Catch exceptions that might keep the lock.
#if DBG == 1
}
_except( ComInvokeExceptionFilter(GetExceptionCode(),
GetExceptionInformation()) )
{
}
#endif
ASSERT_LOCK_RELEASED
return result;
}
/***************************************************************************/
CRpcChannelBuffer *CRpcChannelBuffer::Copy(OXIDEntry *pOXIDEntry,
REFIPID ripid, REFIID riid)
{
Win4Assert( !(state & server_cs) );
CRpcChannelBuffer *chan;
if (IsMTAThread())
{
// make client side multi-threaded apartment version of channel
chan = new CMTARpcChnl(pStdId, pOXIDEntry, state);
}
else
{
// make client side single-threaded apartment version of channel
chan = new CAptRpcChnl(pStdId, pOXIDEntry, state);
}
if (chan != NULL)
{
chan->state = proxy_cs | (state & ~client_cs);
chan->lAuthnLevel = lAuthnLevel;
}
return chan;
}
/***************************************************************************/
HRESULT CRpcChannelBuffer::InitClientSideHandle()
{
Win4Assert((state & proxy_cs));
ASSERT_LOCK_HELD
if (state & initialized_cs)
return S_OK;
// Lookup the interface info. This cant fail.
pInterfaceInfo = gRIFTbl.GetClientInterfaceInfo(pIPIDEntry->iid);
RPC_STATUS status;
#ifndef _CHICAGO_
if (state & process_local_cs)
{
handle = NULL;
status = RPC_S_OK;
}
else
#endif
{
status = RpcBindingCopy(pOXIDEntry->hServerSTA, &handle);
if (status == RPC_S_OK)
// If this is a single threaded apartment, give LRPC the blocking
// hook.
if (state & mswmsg_cs)
status = I_RpcBindingSetAsync(handle, OleModalLoopBlockFn);
#ifndef CHICAGO
// If the server is a single threaded apartment, tell LRPC to
// use MSWMSG to dispatch.
else if (pOXIDEntry->dwTid != 0)
status = I_RpcBindingSetAsync(handle, NULL);
#endif
if (status == RPC_S_OK)
status = RpcBindingSetObject(handle, (GUID *)&pIPIDEntry->ipid);
}
if (status == RPC_S_OK)
{
state |= initialized_cs;
return S_OK;
}
return MAKE_WIN32(status);
}
/***************************************************************************/
CRpcChannelBuffer::CRpcChannelBuffer(CStdIdentity *standard_identity,
OXIDEntry *pOXID,
DWORD eState )
{
ComDebOut((DEB_MARSHAL, "CRpcChannelBuffer %s Created this:%x pOXID:%x\n",
(eState & client_cs) ? "CLIENT" : "SERVER", this, pOXID));
// Fill in the easy fields first.
ref_count = 1;
pStdId = standard_identity;
handle = NULL;
pOXIDEntry = pOXID;
pIPIDEntry = NULL;
pInterfaceInfo = NULL;
hToken = NULL;
lAuthnLevel = gAuthnLevel;
state = eState;
state |= pOXID->dwPid == GetCurrentProcessId() ? process_local_cs : 0;
SetImpLevel( gImpLevel );
if ((pOXID->dwFlags & OXIDF_MSWMSG) && IsSTAThread())
{
// use MSWMSG protocol with the blocking hook
state |= mswmsg_cs;
}
if (state & (client_cs | proxy_cs))
{
// Determine the destination context.
if (pOXID->dwFlags & OXIDF_MACHINE_LOCAL)
if (!IsWOWThread() && (state & process_local_cs))
iDestCtx = MSHCTX_INPROC;
else
iDestCtx = MSHCTX_LOCAL;
else
iDestCtx = MSHCTX_DIFFERENTMACHINE;
}
else
{
// On the server side, the destination context isn't known
// untill a call arrives.
iDestCtx = 0;
}
}
/***************************************************************************/
CRpcChannelBuffer::~CRpcChannelBuffer()
{
ComDebOut((DEB_MARSHAL, "CRpcChannelBuffer %s Deleted this:%x\n",
(state & server_cs) ? "SERVER" : "CLIENT", this));
if (handle != NULL)
RpcBindingFree( &handle );
if (hToken != NULL)
CloseHandle( hToken );
}
/***************************************************************************/
STDMETHODIMP CRpcChannelBuffer::FreeBuffer( RPCOLEMESSAGE *pMessage )
{
TRACECALL(TRACE_RPC, "CRpcChannelBuffer::FreeBuffer");
ASSERT_LOCK_RELEASED
AssertValid(FALSE, TRUE);
if (pMessage->Buffer == NULL)
return S_OK;
// Pop the call stack.
COleTls tls;
Win4Assert( tls->pCallInfo != NULL );
working_call *pCall = (working_call *) tls->pCallInfo;
tls->pCallInfo = pCall->pNext;
tls->dwAuthnLevel = pCall->lSavedAuthnLevel;
pMessage->Buffer = pCall->pHeader;;
DeallocateBuffer(pCall->pmessage);
// Resume any outstanding impersonation.
ResumeImpersonate( tls->pCallContext, pCall->iFlags & CF_WAS_IMPERSONATING );
// Release the AddRef we did earlier. Note that we cant do this until
// after DeallocateBuffer since it may release a binding handle that
// I_RpcFreeBuffer needs.
if (pCall->Locked())
pStdId->UnLockClient();
pMessage->Buffer = NULL;
delete pCall;
ASSERT_LOCK_RELEASED
return S_OK;
}
//-------------------------------------------------------------------------
//
// Member: CRpcChannelBuffer::GetBuffer
//
// Synopsis: Calls ClientGetBuffer or ServerGetBuffer
//
//-------------------------------------------------------------------------
STDMETHODIMP CRpcChannelBuffer::GetBuffer( RPCOLEMESSAGE *pMessage,
REFIID riid )
{
gOXIDTbl.ValidateOXID();
if (state & proxy_cs)
return ClientGetBuffer( pMessage, riid );
else
return ServerGetBuffer( pMessage, riid );
}
//-------------------------------------------------------------------------
//
// Member: CRpcChannelBuffer::ClientGetBuffer
//
// Synopsis: Gets a buffer and sets up client side stuff
//
//-------------------------------------------------------------------------
HRESULT CRpcChannelBuffer::ClientGetBuffer( RPCOLEMESSAGE *pMessage,
REFIID riid )
{
TRACECALL(TRACE_RPC, "CRpcChannelBuffer::ClientGetBuffer");
ASSERT_LOCK_RELEASED
RPC_STATUS status;
CALLCATEGORY callcat = CALLCAT_SYNCHRONOUS;
ULONG debug_size;
ULONG num_extent;
WireThis *inb;
LocalThis *localb;
IID *logical_thread;
working_call *call;
DWORD flags;
BOOL resume;
DWORD orig_size = pMessage->cbBuffer;
COleTls tls;
Win4Assert(state & proxy_cs);
AssertValid(FALSE, TRUE);
// Don't allow remote calls if DCOM is disabled.
if (gDisableDCOM && iDestCtx == MSHCTX_DIFFERENTMACHINE)
return RPC_E_REMOTE_DISABLED;
// Fetch the call category from the RPC message structure
if (pMessage->rpcFlags & RPCFLG_ASYNCHRONOUS)
{
// only allow async for these two interfaces for now
if (riid != IID_IAdviseSink && riid != IID_IAdviseSink2)
return E_UNEXPECTED;
callcat = CALLCAT_ASYNC;
}
else
{
logical_thread = TLSGetLogicalThread();
if (logical_thread == NULL)
{
return RPC_E_OUT_OF_RESOURCES;
}
if (pMessage->rpcFlags & RPCFLG_INPUT_SYNCHRONOUS)
{
callcat = CALLCAT_INPUTSYNC;
}
}
// Set the buffer complete flag for local calls.
pMessage->rpcFlags |= RPC_BUFFER_COMPLETE;
// Note - RPC requires that the 16th bit of the proc num be set because
// we use the rpcFlags field of the RPC_MESSAGE struct.
pMessage->iMethod |= RPC_FLAGS_VALID_BIT;
// if service object of destination is in same process, definitely local
// calls; async calls are also forced to be local.
if (state & process_local_cs)
{
pMessage->rpcFlags |= RPCFLG_LOCAL_CALL;
flags = CF_PROCESS_LOCAL;
}
else
flags = 0;
// Find out if we need hook data.
debug_size = ClientGetSize( riid, &num_extent );
LOCK
// Complete the channel initialization if needed.
status = InitClientSideHandle();
if (status != RPC_S_OK)
{
UNLOCK;
ASSERT_LOCK_RELEASED;
return status;
}
// Fill in the binding handle. Adjust the size. Clear the transfer
// syntax. Set the interface identifier.
if ((pMessage->rpcFlags & RPCFLG_LOCAL_CALL) == 0)
pMessage->reserved1 = handle;
pMessage->cbBuffer += SIZENEEDED_ORPCTHIS( pOXIDEntry->dwFlags & OXIDF_MACHINE_LOCAL,
debug_size );
pMessage->reserved2[0] = 0;
pMessage->reserved2[1] = pInterfaceInfo;
Win4Assert( pMessage->reserved2[1] != NULL );
// Allocate a call record.
call = new working_call( callcat, pMessage, flags, pIPIDEntry->ipid,
iDestCtx, this, lAuthnLevel );
pMessage->cbBuffer = orig_size;
UNLOCK
ASSERT_LOCK_RELEASED
if (call == NULL)
return E_OUTOFMEMORY;
// Suspend any outstanding impersonation and ignore failures.
SuspendImpersonate( tls->pCallContext, &resume );
// Get a buffer.
if (call->pmessage->rpcFlags & RPCFLG_LOCAL_CALL)
{
// NDR_DREP_ASCII | NDR_DREP_LITTLE_ENDIAN | NDR_DREP_IEEE
call->pmessage->dataRepresentation = 0x00 | 0x10 | 0x0000;
call->pmessage->Buffer = PrivMemAlloc8( call->pmessage->cbBuffer );
if (call->pmessage->Buffer == NULL)
status = RPC_S_OUT_OF_MEMORY;
else
status = RPC_S_OK;
}
else
{
TRACECALL(TRACE_RPC, "I_RpcGetBuffer");
status = I_RpcGetBuffer( (RPC_MESSAGE *) call->pmessage );
}
if (status != RPC_S_OK)
{
// Resume any outstanding impersonation.
ResumeImpersonate( tls->pCallContext, resume );
// Cleanup.
pMessage->cbBuffer = 0;
tls->fault = MAKE_WIN32( status );
delete call;
return MAKE_WIN32( status );
}
// Save the impersonation flag.
if (resume)
call->iFlags |= CF_WAS_IMPERSONATING;
// Chain the call info in TLS.
call->pNext = (CChannelCallInfo *)tls->pCallInfo;
tls->pCallInfo = call;
call->pHeader = call->message.Buffer;
// Adjust the authentication level in TLS.
call->lSavedAuthnLevel = tls->dwAuthnLevel;
tls->dwAuthnLevel = lAuthnLevel;
// Fill in the COM header.
pMessage->Buffer = call->message.Buffer;
inb = (WireThis *) pMessage->Buffer;
inb->c.version.MajorVersion = COM_MAJOR_VERSION;
inb->c.version.MinorVersion = COM_MINOR_VERSION;
inb->c.reserved1 = 0;
// Generate a new logical thread for async calls.
if (callcat == CALLCAT_ASYNC)
UuidCreate( &inb->c.cid );
// Find the logical thread id.
else
inb->c.cid = *logical_thread;
if (pOXIDEntry->dwFlags & OXIDF_MACHINE_LOCAL)
inb->c.flags = ORPCF_LOCAL;
else
inb->c.flags = ORPCF_NULL;
// Fill in any hook data and adjust the buffer pointer.
if (debug_size != 0)
{
pMessage->Buffer = FillBuffer( riid, &inb->d.ea, debug_size, num_extent,
TRUE );
inb->c.unique = 0x77646853; // Any non-zero value.
}
else
{
pMessage->Buffer = (void *) &inb->d.ea;
inb->c.unique = FALSE;
}
// Fill in the local header.
if (pOXIDEntry->dwFlags & OXIDF_MACHINE_LOCAL)
{
localb = (LocalThis *) pMessage->Buffer;
localb->client_thread = GetCurrentApartmentId();
localb->flags = 0;
pMessage->Buffer = localb + 1;
if (callcat == CALLCAT_ASYNC)
inb->c.flags |= ORPCF_ASYNC;
else if (callcat == CALLCAT_INPUTSYNC)
inb->c.flags |= ORPCF_INPUT_SYNC;
// if the caller is using a non-NDR proxy, set a bit in the local
// header flags so that server side stub knows which way to unmarshal
// the parameters. This lets a 32bit server simultaneously service calls
// from 16bit non-NDR clients and 32bit NDR clients, in particular, to
// support OLE Automation.
if (pIPIDEntry->dwFlags & (IPIDF_NONNDRPROXY | IPIDF_NONNDRSTUB))
localb->flags |= LOCALF_NONNDR;
}
ComDebOut((DEB_CALLCONT, "ClientGetBuffer: LogicalThreadId:%I\n",
&(tls->LogicalThreadId)));
ASSERT_LOCK_RELEASED
return S_OK;
}
//-------------------------------------------------------------------------
//
// Member: CRpcChannelBuffer::ServerGetBuffer
//
// Synopsis: Gets a buffer and sets up server side stuff
//
//-------------------------------------------------------------------------
HRESULT CRpcChannelBuffer::ServerGetBuffer( RPCOLEMESSAGE *pMessage,
REFIID riid )
{
TRACECALL(TRACE_RPC, "CRpcChannelBuffer::ServerGetBuffer");
ASSERT_LOCK_RELEASED
RPC_STATUS status;
ULONG debug_size;
ULONG num_extent;
HRESULT result = S_OK;
WireThis *inb;
WireThat *outb;
CChannelCallInfo *call;
void *stub_data;
DWORD orig_size = pMessage->cbBuffer;
Win4Assert( state & server_cs );
AssertValid(FALSE, TRUE);
// Get the call info from TLS.
COleTls tls;
call = (CChannelCallInfo *) tls->pCallInfo;
Win4Assert( call != NULL );
// Find out if we need debug data.
pMessage->Buffer = call->pHeader;
debug_size = ServerGetSize( riid, &num_extent );
// Adjust the buffer size.
pMessage->cbBuffer += SIZENEEDED_ORPCTHAT( debug_size );
// Get a buffer.
if (pMessage->rpcFlags & RPCFLG_LOCAL_CALL)
{
// NDR_DREP_ASCII | NDR_DREP_LITTLE_ENDIAN | NDR_DREP_IEEE
pMessage->dataRepresentation = 0x00 | 0x10 | 0x0000;
pMessage->Buffer = PrivMemAlloc8( pMessage->cbBuffer );
if (pMessage->Buffer == NULL)
status = RPC_S_OUT_OF_MEMORY;
else
status = RPC_S_OK;
}
else
{
TRACECALL(TRACE_RPC, "I_RpcGetBuffer");
status = I_RpcGetBuffer( (RPC_MESSAGE *) pMessage );
Win4Assert( call->pHeader != pMessage->Buffer || status != RPC_S_OK );
}
if (status != RPC_S_OK)
{
pMessage->cbBuffer = 0;
pMessage->Buffer = NULL;
tls->fault = MAKE_WIN32( status );
return MAKE_WIN32( status );
}
// Fill in the outbound COM header.
call->pHeader = pMessage->Buffer;
outb = (WireThat *) pMessage->Buffer;
outb->c.flags = ORPCF_NULL;
pMessage->cbBuffer = orig_size;
if (debug_size != 0)
{
stub_data = FillBuffer( riid, &outb->d.ea, debug_size, num_extent, FALSE );
outb->c.unique = 0x77646853; // Any non-zero value.
pMessage->Buffer = stub_data;
}
else
{
outb->c.unique = 0;
pMessage->Buffer = &outb->d.ea;
}
ComDebOut((DEB_CALLCONT, "ServerGetBuffer: LogicalThreadId:%I\n",
&(tls->LogicalThreadId)));
ASSERT_LOCK_RELEASED
return S_OK;
}
/***************************************************************************/
STDMETHODIMP CRpcChannelBuffer::GetDestCtx( DWORD FAR* lpdwDestCtx,
LPVOID FAR* lplpvDestCtx )
{
TRACECALL(TRACE_RPC, "CRpcChannelBuffer::GetDestCtx");
AssertValid(FALSE, FALSE);
// On the client side, get the destination context from the channel.
if (state & (client_cs | proxy_cs))
{
*lpdwDestCtx = iDestCtx;
}
// On the server side, get the destination context from TLS.
else
{
COleTls tls;
Win4Assert( tls->pCallInfo != NULL );
*lpdwDestCtx = ((CChannelCallInfo *) tls->pCallInfo)->iDestCtx;
}
if (lplpvDestCtx != NULL)
*lplpvDestCtx = NULL;
return S_OK;
}
/***************************************************************************/
STDMETHODIMP CRpcChannelBuffer::IsConnected( THIS )
{
// must be on right thread because it is only called by proxies and stubs.
AssertValid(FALSE, TRUE);
// Server channels never know if they are connected. The only time the
// client side knows it is disconnected is after the standard identity
// has disconnected the proxy from the channel. In that case it doesn't
// matter.
return S_OK;
}
/***************************************************************************/
STDMETHODIMP CRpcChannelBuffer::QueryInterface( THIS_ REFIID riid, LPVOID FAR* ppvObj)
{
AssertValid(FALSE, FALSE);
// IMarshal is queried more frequently than any other interface, so
// check for that first.
if (IsEqualIID(riid, IID_IMarshal))
{
*ppvObj = (IMarshal *) this;
}
else if (IsEqualIID(riid, IID_IUnknown) ||
IsEqualIID(riid, IID_IRpcChannelBuffer))
{
*ppvObj = (IRpcChannelBuffer *) this;
}
else if (IsEqualIID(riid, IID_INonNDRStub) &&
(state & proxy_cs) && pIPIDEntry &&
(pIPIDEntry->dwFlags & IPIDF_NONNDRSTUB))
{
// this interface is used to tell proxies whether the server side speaks
// NDR or not. Returns S_OK if NOT NDR.
*ppvObj = (IUnknown *) this;
}
else
{
*ppvObj = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
/***************************************************************************/
STDMETHODIMP_(ULONG) CRpcChannelBuffer::Release( THIS )
{
// can't call AssertValid(FALSE) since it is used in asserts
ULONG lRef = ref_count - 1;
if (InterlockedDecrement( (long*) &ref_count ) == 0)
{
delete this;
return 0;
}
else
{
return lRef;
}
}
/***************************************************************************/
STDMETHODIMP CRpcChannelBuffer::SendReceive( THIS_ RPCOLEMESSAGE *pMessage,
ULONG *status )
{
return CRpcChannelBuffer::SendReceive2(pMessage, status);
}
/***************************************************************************/
STDMETHODIMP CRpcChannelBuffer::SendReceive2( THIS_ RPCOLEMESSAGE *pMessage,
ULONG *status )
{
TRACECALL(TRACE_RPC, "CRpcChannelBuffer::SendReceive");
ComDebOut((DEB_CHANNEL, "CRpcChannelBuffer::SendReceive pChnl:%x pMsg:%x\n",
this, pMessage));
AssertValid(FALSE, TRUE);
Win4Assert( state & proxy_cs );
gOXIDTbl.ValidateOXID();
ASSERT_LOCK_RELEASED
HRESULT result;
working_call *call;
working_call *next_call;
IID iid;
WireThis *inb;
WireThat *outb;
DWORD saved_authn_level;
BOOL resume;
char *stub_data;
// Get the information about the call stored in TLS
COleTls tls;
call = (working_call *) tls->pCallInfo;
Win4Assert( call != NULL );
next_call = (working_call *) call->pNext;
saved_authn_level = call->lSavedAuthnLevel;
resume = call->iFlags & CF_WAS_IMPERSONATING;
// Set up the header pointers.
inb = (WireThis *) call->pHeader;
iid =
((RPC_CLIENT_INTERFACE *) ((RPC_MESSAGE *) call->pmessage)->RpcInterfaceInformation)->InterfaceId.SyntaxGUID;
// we must ensure that we dont go away during this call. we will Release
// ourselves in the FreeBuffer call, or in the error handling at the
// end of this function.
pStdId->LockClient();
#if DBG==1
DWORD CallCat = GetCallCat( inb );
DebugPrintORPCCall(ORPC_SENDRECEIVE_BEGIN, iid, call->message.iMethod, CallCat);
RpcSpy((CALLOUT_BEGIN, inb, iid, call->message.iMethod, 0));
#endif
// Send the request.
if ((state & mswmsg_cs) || (IsMTAThread() && !call->Local()))
{
// For MSWMSG or non-local MTA, call ThreadSendReceive directly.
result = ThreadSendReceive( call );
}
else
{
if (call->Local())
call->message.reserved2[3] = NULL;
if (IsMTAThread())
{
LOCK
result = GetToSTA( pOXIDEntry, call);
UNLOCK
}
else
{
result = SwitchSTA( pOXIDEntry, (CChannelCallInfo **) &call );
}
}
#if DBG==1
DebugPrintORPCCall(ORPC_SENDRECEIVE_END, iid, pMessage->iMethod, CallCat);
RpcSpy((CALLOUT_END, inb, iid, pMessage->iMethod, result));
#endif
// We can't look at the call structure if the call was canceled.
if (result != RPC_E_CALL_CANCELED)
{
// Get the reply header if there is a reply buffer.
if ((state & mswmsg_cs) && (pMessage->rpcFlags & RPCFLG_ASYNCHRONOUS))
outb = NULL;
else
outb = (WireThat *) call->message.Buffer;
// Local calls reuse pNext on the server side.
call->pNext = next_call;
// Save the real buffer pointer for FreeBuffer.
call->pHeader = call->message.Buffer;
}
else
outb = NULL;
// Figure out when to retry.
// FreeThreaded - treat retry as a failure.
// Apartment - return the buffer and let call control decide.
if (result == S_OK)
{
// No buffer was returned for async calls on MSWMSG.
if (outb == NULL)
*status = S_OK;
else if (IsMTAThread())
{
if (outb->c.flags & ORPCF_REJECTED)
result = RPC_E_CALL_REJECTED;
else if (outb->c.flags & ORPCF_RETRY_LATER)
result = RPC_E_SERVERCALL_RETRYLATER;
else
*status = S_OK;
}
else if (outb->c.flags & ORPCF_REJECTED)
*status = (ULONG) RPC_E_CALL_REJECTED;
else if (outb->c.flags & ORPCF_RETRY_LATER)
*status = (ULONG) RPC_E_SERVERCALL_RETRYLATER;
else
*status = S_OK;
}
// Check the packet extensions.
if (result != RPC_E_CALL_CANCELED)
{
stub_data = (char *) call->message.Buffer;
result = ClientNotify( iid, outb, call->message.cbBuffer,
(void **) &stub_data,
call->message.dataRepresentation,
result );
}
else
result = ClientNotify( iid, outb, 0, (void **) &stub_data, 0, result );
// Call succeeded.
if (result == S_OK && outb != NULL)
{
// The locked flag lets FreeBuffer know that it has to call
// RH->UnlockClient.
call->iFlags |= CF_LOCKED;
pMessage->Buffer = stub_data;
pMessage->cbBuffer = call->message.cbBuffer -
(stub_data - (char *) call->message.Buffer);
pMessage->dataRepresentation = call->message.dataRepresentation;
result = *status;
// Copy a portion of the message structure that RPC updated on SendReceive.
// This is needed to free the buffer. Note that we still have to free
// the buffer in certain failure cases (reject).
pMessage->reserved2[2] = call->message.reserved2[2];
}
else
{
// Resume any outstanding impersonation.
ResumeImpersonate( tls->pCallContext, resume );
// Clean up the call.
pStdId->UnLockClient();
tls->pCallInfo = next_call;
tls->dwAuthnLevel = saved_authn_level;
delete call;
// Make sure FreeBuffer doesn't try to free the in buffer.
pMessage->Buffer = NULL;
// If the result is server fault, get the exception code from the CChannelCallInfo.
if (result == RPC_E_SERVERFAULT)
{
*status = call->server_fault;
}
// Everything else is a comm fault.
else if (result != S_OK)
{
*status = result;
result = RPC_E_FAULT;
}
tls->fault = *status;
// Since result is almost always mapped to RPC_E_FAULT, display the
// real error here to assist debugging.
if (*status != S_OK)
ComDebOut((DEB_CHANNEL, "ORPC call failed. status = %x\n", *status));
}
ASSERT_LOCK_RELEASED
gOXIDTbl.ValidateOXID();
ComDebOut((DEB_CHANNEL, "CRpcChannelBuffer::SendReceive hr:%x\n", result));
return result;
}
/***************************************************************************/
HANDLE CRpcChannelBuffer::SwapSecurityToken( HANDLE hNew )
{
HANDLE hOld = hToken;
hToken = hNew;
return hOld;
}
#if DBG == 1
//+-------------------------------------------------------------------
//
// Member: CRpcChannelBuffer::AssertValid
//
// Synopsis: Validates that the state of the object is consistent.
//
// History: 25-Jan-94 CraigWi Created.
//
// DCOMWORK - Put in some asserts.
//
//--------------------------------------------------------------------
void CRpcChannelBuffer::AssertValid(BOOL fKnownDisconnected,
BOOL fMustBeOnCOMThread)
{
Win4Assert(state & (proxy_cs | client_cs | server_cs ));
if (state & (client_cs | proxy_cs))
{
;
}
else if (state & server_cs)
{
Win4Assert( !(state & freethreaded_cs) );
if (fMustBeOnCOMThread && IsSTAThread())
Win4Assert(IsMTAThread() || pOXIDEntry->dwTid == GetCurrentThreadId());
// ref count can be 0 in various stages of connection and disconnection
Win4Assert(ref_count < 0x7fff && "Channel ref count unreasonably high");
// the pStdId pointer can not be NULL
// Win4Assert(IsValidInterface(pStdId));
}
}
#endif // DBG == 1
/***************************************************************************/
STDAPI_(ULONG) DebugCoGetRpcFault()
{
HRESULT hr;
COleTls tls(hr);
if (SUCCEEDED(hr))
return tls->fault;
return 0;
}
/***************************************************************************/
STDAPI_(void) DebugCoSetRpcFault( ULONG fault )
{
HRESULT hr;
COleTls tls(hr);
if (SUCCEEDED(hr))
tls->fault = fault;
}
/***************************************************************************/
extern "C"
BOOL _stdcall DllDebugObjectRPCHook( BOOL trace, LPORPC_INIT_ARGS pass_through )
{
if (!IsWOWThread())
{
DoDebuggerHooks = trace;
DebuggerArg = pass_through;
return TRUE;
}
else
return FALSE;
}
/***************************************************************************/
BOOL LocalCall()
{
CChannelCallInfo *call;
// Get the call info from TLS.
COleTls tls;
call = (CChannelCallInfo *) tls->pCallInfo;
Win4Assert( call != NULL );
return call->iFlags & CF_PROCESS_LOCAL;
}
/***************************************************************************/
LONG ThreadInvokeExceptionFilter( DWORD lCode,
LPEXCEPTION_POINTERS lpep )
{
ComDebOut((DEB_ERROR, "Exception 0x%x in ThreadInvoke at address 0x%x\n",
lCode, lpep->ExceptionRecord->ExceptionAddress));
DebugBreak();
return EXCEPTION_EXECUTE_HANDLER;
}
/***************************************************************************/
/* This routine returns both comm status and server faults to the runtime
by raising exceptions. If FreeThreading is true, ComInvoke will throw
exceptions to indicate server faults. These will not be caught and will
propogate directly to the runtime. If FreeThreading is false, ComInvoke
will return the result and fault in the CChannelCallInfo record.
NOTE:
This function switches to the 32 bit stack under WIN95.
An exception has to be caught while switched to the 32 bit stack.
The exceptions has to be pass as a value and rethrown again on the
16 bit stack (see SSInvoke in stkswtch.cxx)
*/
#ifdef _CHICAGO_
DWORD
#else
void
#endif
SSAPI(ThreadInvoke)(RPC_MESSAGE *message )
{
HRESULT result = S_OK;
TRACECALL(TRACE_RPC, "ThreadInvoke");
ComDebOut((DEB_CHANNEL,"ThreadInvoke pMsg:%x\n", message));
gOXIDTbl.ValidateOXID();
ASSERT_LOCK_RELEASED
BOOL success;
WireThis *inb = (WireThis *) message->Buffer;
IPID ipid;
RPC_STATUS status;
OXIDEntry *pOxid;
unsigned int transport_type;
DWORD authn_level;
// Byte swap the header.
ByteSwapThis( message->DataRepresentation, inb );
// Validate several things:
// The packet size is larger then the first header size.
// No extra flags are set.
// The procedure number is greater then 2 (not QI, AddRef, Release).
if (sizeof(WireThisPart1) > message->BufferLength ||
(inb->c.flags & ~(ORPCF_LOCAL | ORPCF_RESERVED1 |
ORPCF_RESERVED2 | ORPCF_RESERVED3 | ORPCF_RESERVED4)) != 0 ||
message->ProcNum < 3)
RETURN_COMM_STATUS( RPC_E_INVALID_HEADER );
// Validate the version.
if (inb->c.version.MajorVersion != COM_MAJOR_VERSION ||
inb->c.version.MinorVersion > COM_MINOR_VERSION)
RETURN_COMM_STATUS( RPC_E_VERSION_MISMATCH );
// Get the transport the call arrived on.
status = I_RpcServerInqTransportType( &transport_type );
if (status != RPC_S_OK)
RETURN_COMM_STATUS( RPC_E_SYS_CALL_FAILED );
// Don't accept the local header on remote calls.
if (inb->c.flags & ORPCF_LOCAL)
{
if (transport_type != TRANSPORT_TYPE_LPC &&
transport_type != TRANSPORT_TYPE_WMSG)
RETURN_COMM_STATUS( RPC_E_INVALID_HEADER );
// For local calls the authentication level will always be encrypt.
authn_level = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
}
// Don't accept remote calls if DCOM is diabled.
else if (gDisableDCOM &&
(transport_type == TRANSPORT_TYPE_CN || transport_type == TRANSPORT_TYPE_DG))
RETURN_COMM_STATUS( RPC_E_CALL_REJECTED );
// Lookup the authentication level.
else
{
result = RpcBindingInqAuthClient( message->Handle, NULL,
NULL, &authn_level, NULL, NULL );
if (result == RPC_S_BINDING_HAS_NO_AUTH)
authn_level = RPC_C_AUTHN_LEVEL_NONE;
else if (result != RPC_S_OK)
{
Win4Assert( result == RPC_S_OUT_OF_RESOURCES );
RETURN_COMM_STATUS( MAKE_WIN32( result ) );
}
// Verify the authentication level.
if (gAuthnLevel > RPC_C_AUTHN_LEVEL_NONE ||
gImpLevel > 0)
{
if (authn_level < gAuthnLevel)
RETURN_COMM_STATUS( RPC_E_ACCESS_DENIED );
}
}
#if DBG==1
_try
{
#endif
// Find the ipid entry from the ipid.
status = RpcBindingInqObject( message->Handle, &ipid );
if (status == RPC_S_OK)
{
// The CChannelCallInfo is created in a nested scope so that it
// is destroyed before the calls to throw an exception at the
// end of this function.
CChannelCallInfo call(
GetCallCat( inb ),
(RPCOLEMESSAGE *) message,
0,
ipid,
(inb->c.flags & ORPCF_LOCAL) ? MSHCTX_LOCAL : MSHCTX_DIFFERENTMACHINE,
NULL,
authn_level );
// Find the OXIDEntry of the server apartment.
ASSERT_LOCK_RELEASED
LOCK
IPIDEntry *ipid_entry = gIPIDTbl.LookupIPID( ipid );
if (ipid_entry == NULL || (ipid_entry->dwFlags & IPIDF_DISCONNECTED)
|| ipid_entry->pChnl == NULL )
{
UNLOCK
ASSERT_LOCK_RELEASED
result = RPC_E_DISCONNECTED;
}
else
{
pOxid = ipid_entry->pOXIDEntry;
// NCALRPC always gets the thread right (except on Chicago).
// For MTAs, any thread will do.
if (transport_type == TRANSPORT_TYPE_WMSG ||
#ifndef _CHICAGO_
transport_type == TRANSPORT_TYPE_LPC ||
#endif
(pOxid->dwFlags & OXIDF_MTASERVER))
{
UNLOCK
ASSERT_LOCK_RELEASED
result = ComInvoke( &call );
}
else
{
// Pass the message to the app thread.
IncOXIDRefCnt( pOxid );
result = GetToSTA( pOxid, &call );
DecOXIDRefCnt( pOxid );
UNLOCK
ASSERT_LOCK_RELEASED
}
}
}
else
{
result = MAKE_WIN32( status );
}
#if DBG==1
}
_except( ThreadInvokeExceptionFilter(GetExceptionCode(),
GetExceptionInformation()) )
{
}
#endif
// For comm and server faults, generate an exception. Otherwise the buffer
// is set up correctly.
gOXIDTbl.ValidateOXID();
if (result == RPC_E_SERVERFAULT)
{
ASSERT_LOCK_RELEASED
RETURN_COMM_STATUS( RPC_E_SERVERFAULT );
}
else if (result != S_OK)
{
ASSERT_LOCK_RELEASED
RETURN_COMM_STATUS( result );
}
#ifdef _CHICAGO_
return 0;
#endif //_CHICAGO_
}
/***************************************************************************/
HRESULT ThreadSendReceive( CChannelCallInfo *call )
{
TRACECALL(TRACE_RPC, "ThreadSendReceive");
ComDebOut((DEB_CHANNEL, "ThreadSendReceive pCall:%x\n", call));
ASSERT_LOCK_RELEASED
HRESULT result;
RPCOLEMESSAGE *message = call->pmessage;
WireThat *outb;
// Call the runtime. In the future, detect server faults and
// change the value of result to RPC_E_SERVERFAULT.
if (call->pChannel->state & mswmsg_cs)
{
CAptCallCtrl *pACC = GetAptCallCtrl();
CCliModalLoop *pCML = (pACC) ? pACC->GetTopCML() : NULL;
OXIDEntry *pOxidClient;
HWND hwnd = NULL;
if (IsWOWThread())
{
LOCK
result = gOXIDTbl.GetLocalEntry( &pOxidClient );
UNLOCK
Win4Assert( result == S_OK );
hwnd = (HWND) pOxidClient->hServerSTA;
}
TRACECALL(TRACE_RPC, "I_RpcAsyncSendReceive");
result = I_RpcAsyncSendReceive( (RPC_MESSAGE *) message, pCML, hwnd );
// If the call was canceled, the rest of the code path assumes that
// the call was deleted (by SwitchComThread). So delete it.
if (result == RPC_S_CALL_CANCELLED)
{
// Convert the win32 error to a hresult.
result = RPC_E_CALL_CANCELED;
delete call;
}
}
else
{
TRACECALL(TRACE_RPC, "I_RpcSendReceive");
result = I_RpcSendReceive( (RPC_MESSAGE *) message );
}
// If the result is small, it is probably a Win32 code.
if (result != 0)
{
message->Buffer = NULL;
if ((ULONG) result > 0xfffffff7 || (ULONG) result < 0x2000)
result = MAKE_WIN32( result );
}
else
{
// No buffer is returned for asynchronous calls on MSWMSG.
if ((call->pChannel->state & mswmsg_cs) == 0 ||
(message->rpcFlags & RPCFLG_ASYNCHRONOUS) == 0)
{
// Byte swap the reply header. Fail the call if the buffer is too
// small.
outb = (WireThat *) message->Buffer;
if (message->cbBuffer >= sizeof(WireThatPart1))
ByteSwapThat( message->dataRepresentation, outb);
else
result = RPC_E_INVALID_HEADER;
}
}
ComDebOut((DEB_CHANNEL, "ThreadSendReceive pCall:%x hr:%x\n", call, result));
return result;
}
/***************************************************************************/
/* static */
void working_call::Cleanup()
{
ASSERT_LOCK_HELD
DWORD i;
// Release everything.
if (next <= CALLCACHE_SIZE)
{
for (i = 0; i < next; i++)
if (list[i] != NULL)
{
PrivMemFree( list[i] );
list[i] = NULL;
}
next = 0;
}
}
/***************************************************************************/
/* static */
void working_call::Initialize()
{
ASSERT_LOCK_HELD
next = 0;
}
//---------------------------------------------------------------------------
//
// Method: working_call:: operator delete
//
// Synopsis: Cache or actually free a working call.
//
// Notes: gComLock need not be held before calling this function.
//
//---------------------------------------------------------------------------
void working_call::operator delete( void *call )
{
// Add the structure to the list if the list is not full and
// if the process is still initialized (since latent threads may try
// to return stuff).
LOCK
if (next < CALLCACHE_SIZE && gfChannelProcessInitialized)
{
list[next] = call;
next += 1;
}
// Otherwise just free it.
else
{
PrivMemFree( call );
}
UNLOCK
}
//---------------------------------------------------------------------------
//
// Method: working_call:: operator new
//
// Synopsis: Keep a cache of working_calls. Since the destructor is
// virtual, the correct delete will be called if any base
// class is deleted.
//
// Notes: gComLock must be held before calling this function.
//
//---------------------------------------------------------------------------
void *working_call::operator new( size_t size )
{
ASSERT_LOCK_HELD
void *call;
// Get the last entry from the cache.
Win4Assert( size == sizeof( working_call ) );
if (next > 0 && next < CALLCACHE_SIZE+1)
{
next -= 1;
call = list[next];
list[next] = NULL;
}
// If there are none, allocate a new one.
else
call = PrivMemAlloc(size);
return call;
}
/**********************************************************************/
working_call::working_call( CALLCATEGORY callcat,
RPCOLEMESSAGE *original_msg,
DWORD flags,
REFIPID ipidServer,
DWORD destctx,
CRpcChannelBuffer *channel,
DWORD authn_level ) :
CChannelCallInfo( callcat, &message, flags, ipidServer, destctx, channel,
authn_level )
{
message = *original_msg;
}
|