Toon posts:

[Win32/Qos] Probleem Traffic API - TcAddFlow doet raar

Pagina: 1
Acties:
  • 182 views sinds 30-01-2008
  • Reageer

Verwijderd

Topicstarter
Hoi! Ik probeer hier een traffic shaper te bouwen en wil daarvoor Microsoft's Traffic API gebruiken.
Ik loop nu echter tegen een aantal probleempjes aan. Helaas is er op het net nergens voorbeeldcode te vinden, en moet ik het doen met de documentatie van MSDN.

- Hoe weet ik hoeveel interface descriptors TcEnumerateInterfaces retouneerd?

Ik gebruik nu:

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HANDLE ch, ifh, fh;                 // tc client handle, interface handle, tc flow handle
TCI_CLIENT_FUNC_LIST cfl;
BYTE ibuf[1024];                    // Interface buffer
PTC_IFC_DESCRIPTOR ifcd;
CHAR iname[256];                   // Interface name
ULONG bufsz = sizeof(ibuf);

TcEnumerateInterfaces(ch, &bufsz, (PTC_IFC_DESCRIPTOR) ibuf)

// Selecteer de eerste Qos-enabled interface
ifcd = (PTC_IFC_DESCRIPTOR) ibuf;
// En zo de tweede...
ifcd = (PTC_IFC_DESCRIPTOR) ((char *) ifcd + ifcd->Length);
// en zo verder...
ifcd = (PTC_IFC_DESCRIPTOR) ((char *) ifcd + ifcd->Length);


Nu weet ik natuurlijk hoeveel interfaces ikzelf heb, maar als ik de user wil laten kiezen weet ik niet hoe ik dit aan moet pakken.

Verder krijg ik als ik uiteindelijk TcAddFlow aanroep ERROR_SIGNAL_PENDING terug, maar wordt MainClAddFlowComplete nooit aangeroepen.

Heeft iemand iets dergelijks ooit werkend gekregen? Alle hulp is meer dan welkom!

Cheers,

The_Fruit

En tot slot nog even mijn hele code:
C:
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
#include <windows.h>
#include <tcerror.h>
#include <ntddndis.h>
#include <qos.h>
#include <traffic.h>

 HWND hwndMain;
 LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

 // Callback routines for traffic control
 void CALLBACK
 MainClNotifyHandler(HANDLE ClRegCtx, HANDLE ClIfcCtx, ULONG Event, HANDLE SubCode, ULONG BufSize, PVOID Buffer)
 {
    MessageBox(NULL, "callback!", "ClNotifyHandler:", MB_OK);
 }

 void CALLBACK MainClAddFlowComplete(HANDLE ClFlowCtx, ULONG Status)
 {
    if (Status != NO_ERROR)
        MessageBox(NULL, "Some error occured!", "ClAddFlowComplete:", MB_OK);
    else
        MessageBox(NULL, "Flow installed!", "ClAddFlowComplete:", MB_OK);
 }

 // Windows entry point
 int WINAPI
 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
 {
    MSG msg; // MSG structure to store messages
    WNDCLASSEX wcx; // WINDOW class information
    HANDLE ch, ifh, fh; // tc client handle, interface handle, tc flow handle
    TCI_CLIENT_FUNC_LIST cfl;
    BYTE ibuf[1024]; // Interface buffer
    PTC_IFC_DESCRIPTOR ifcd;
    CHAR iname[256]; // Interface name
    ULONG bufsz = sizeof(ibuf);

    struct _myflow{
        FLOWSPEC SendingFlowspec;
        FLOWSPEC ReceivingFlowspec;
        ULONG TcObjectsLength;
        QOS_SHAPING_RATE TcShapingRate;
        QOS_OBJECT_HDR TcObjectHdr;
    } myflow;

    // Initialize window context
    wcx.cbSize = sizeof(WNDCLASSEX); // Window size. Must always be sizeof(WNDCLASSEX)
    wcx.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ; // Class styles
    wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure
    wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure
    wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure
    wcx.hInstance = hInstance; // Instance of the application
    wcx.hIcon = NULL; // Class Icon
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Class Cursor
    wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW); // Background brush
    wcx.lpszMenuName = NULL; // Menu resource
    wcx.lpszClassName = "foo"; // Name of this class
    wcx.hIconSm = NULL; // Small icon for this class

    // Register this window class with MS-Windows
    if (!RegisterClassEx(&wcx))
        return 0;

    // Create the window
    hwndMain = CreateWindowEx(0, //Extended window style
                "foo", // Window class name
                "foo", // Window title
                WS_OVERLAPPEDWINDOW, // Window style
                CW_USEDEFAULT,CW_USEDEFAULT, // (x,y) pos of the window
                CW_USEDEFAULT,CW_USEDEFAULT, // Width and height of the window
                HWND_DESKTOP, // HWND of the parent window (can be null also)
                NULL, // Handle to menu
                hInstance, // Handle to application instance
                NULL); // Pointer to window creation data

    // Check if window creation was successful
    if (!hwndMain)
        return 0;

    // Make the window visible
    ShowWindow(hwndMain,SW_SHOW);

    // Initialize callback routines for traffic control
    cfl.ClNotifyHandler = (TCI_NOTIFY_HANDLER) MainClNotifyHandler;
    cfl.ClAddFlowCompleteHandler = (TCI_ADD_FLOW_COMPLETE_HANDLER) MainClAddFlowComplete;

    // Register our client with traffic control
    if (TcRegisterClient(CURRENT_TCI_VERSION, NULL, &cfl, &ch) != NO_ERROR) {
        MessageBox(NULL, "Can't register with traffic control!", "TcRegisterClient:", MB_OK);
        return(0);
    }

    // Get the list of Qos-enabled interfaces
    switch (TcEnumerateInterfaces(ch, &bufsz, (PTC_IFC_DESCRIPTOR) ibuf))
    {
        case ERROR_INVALID_HANDLE:
            MessageBox(NULL, "Invalid client handle!", "TcEnumerateInterfaces:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_PARAMETER:
            MessageBox(NULL, "Invalid parameter!", "TcEnumerateInterfaces:", MB_OK);
            return(0);
            break;
        case ERROR_INSUFFICIENT_BUFFER:
            MessageBox(NULL, "Insufficient buffersize!", "TcEnumerateInterfaces:", MB_OK);
            return(0);
            break;
        case ERROR_NOT_ENOUGH_MEMORY:
            MessageBox(NULL, "Out of memory!", "TcEnumerateInterfaces:", MB_OK);
            return(0);
            break;
    }

    ifcd = (PTC_IFC_DESCRIPTOR) ibuf;
//    ifcd = (PTC_IFC_DESCRIPTOR) ((char *) ifcd + ifcd->Length);
//    ifcd = (PTC_IFC_DESCRIPTOR) ((char *) ifcd + ifcd->Length);

    wsprintf( iname, "%S, next descriptor: %d", ifcd->pInterfaceName, ifcd->Length );
    MessageBox(NULL, iname, "Configuring interface:", MB_OK);

    // Open the first Qos-enabled interface
    switch (TcOpenInterface(iname, ch, NULL, &ifh))
    {
        case ERROR_INVALID_PARAMETER:
            MessageBox(NULL, "Invalid parameter!", "TcOpenInterface:", MB_OK);
            return(0);
            break;
        case ERROR_NOT_ENOUGH_MEMORY:
            MessageBox(NULL, "Out of memory!", "TcOpenInterface:", MB_OK);
            return(0);
            break;
        case ERROR_NOT_FOUND:
            MessageBox(NULL, "Can't find interface!", "TcOpenInterface:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_HANDLE:
            MessageBox(NULL, "Invalid handle!", "TcOpenInterface:", MB_OK);
            return(0);
            break;
    }

    // Initialize flow specifications
    myflow.SendingFlowspec.TokenRate = 1024;
    myflow.SendingFlowspec.TokenBucketSize = 1500;
    myflow.SendingFlowspec.PeakBandwidth = 2048;
    myflow.SendingFlowspec.Latency = 0;
    myflow.SendingFlowspec.ServiceType = SERVICETYPE_GUARANTEED;
    myflow.SendingFlowspec.DelayVariation = 0;
    myflow.SendingFlowspec.MaxSduSize = 1500;
    myflow.SendingFlowspec.MinimumPolicedSize = 128;
    myflow.ReceivingFlowspec.TokenRate = 1024;
    myflow.ReceivingFlowspec.TokenBucketSize = 1500;
    myflow.ReceivingFlowspec.PeakBandwidth = 2048;
    myflow.ReceivingFlowspec.Latency = 0;
    myflow.ReceivingFlowspec.ServiceType = SERVICETYPE_GUARANTEED;
    myflow.ReceivingFlowspec.DelayVariation = 0;
    myflow.ReceivingFlowspec.MaxSduSize = 1500;
    myflow.ReceivingFlowspec.MinimumPolicedSize = 128;

    // Initialize Qos objects
    myflow.TcObjectsLength = sizeof(QOS_SHAPING_RATE) + sizeof(QOS_OBJECT_HDR);
    myflow.TcShapingRate.ObjectHdr.ObjectType = QOS_OBJECT_SHAPING_RATE;
    myflow.TcShapingRate.ObjectHdr.ObjectLength = sizeof(QOS_SHAPING_RATE);
    myflow.TcShapingRate.ShapingRate = TC_NONCONF_SHAPE;
    myflow.TcObjectHdr.ObjectType = QOS_OBJECT_END_OF_LIST;
    myflow.TcObjectHdr.ObjectLength = sizeof(QOS_OBJECT_HDR);

    // Finally, register flow with traffic control
    switch (TcAddFlow(ifh,  NULL, 0, (PTC_GEN_FLOW) &myflow, &fh))
    {
        case NO_ERROR:
            MessageBox(NULL, "No error occured!", "TcAddFlow:", MB_OK);
            break;
        case ERROR_SIGNAL_PENDING:
            MessageBox(NULL, "Signal pending...", "TcAddFlow:", MB_OK);
            break;
        case ERROR_INVALID_HANDLE:
            MessageBox(NULL, "Invalid interface handle!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_NOT_ENOUGH_MEMORY:
            MessageBox(NULL, "Out of memory!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_PARAMETER:
            MessageBox(NULL, "Invalid parameter!", "TcAddFlow:", MB_OK);
            return(0);
            break;
       case ERROR_INVALID_SERVICE_TYPE:
            MessageBox(NULL, "Invalid service type!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_TOKEN_RATE:
            MessageBox(NULL, "Invalid token rate!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_PEAK_RATE:
            MessageBox(NULL, "Invalid peak rate!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_SD_MODE:
            MessageBox(NULL, "Invalid shape discard mode!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_QOS_PRIORITY:
            MessageBox(NULL, "Invalid Qos priority!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_TRAFFIC_CLASS:
            MessageBox(NULL, "Invalid traffic class!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_NO_SYSTEM_RESOURCES:
            MessageBox(NULL, "Insufficient system resources!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_TC_OBJECT_LENGTH_INVALID:
            MessageBox(NULL, "Bad length specified for TC objects!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_DIFFSERV_FLOW:
            MessageBox(NULL, "Invalid parameter to diffserv!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_DS_MAPPING_EXISTS:
            MessageBox(NULL, "Some diffserv error!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_SHAPE_RATE:
            MessageBox(NULL, "Invalid shape rate!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_INVALID_DS_CLASS:
            MessageBox(NULL, "Invalid QOS_DS_CLASS!", "TcAddFlow:", MB_OK);
            return(0);
            break;
        case ERROR_NETWORK_UNREACHABLE:
            MessageBox(NULL, "Networkcable unplugged!", "TcAddFlow:", MB_OK);
            return(0);
            break;
    }

    // Process messages coming to this window
    while (GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // return value to the system
    return msg.wParam;
 }

 LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
 {

    switch (msg)
    {
        case WM_DESTROY:
            // User closed the window
            PostQuitMessage(0);
            break;
        default:
            // Call the default window handler
            return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;
 }