~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/mibII/route_write.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Portions of this file are subject to the following copyright(s).  See
 
2
 * the Net-SNMP's COPYING file for more details and other copyrights
 
3
 * that may apply:
 
4
 */
 
5
/*
 
6
 * Portions of this file are copyrighted by:
 
7
 * Copyright ļæ½ 2003 Sun Microsystems, Inc. All rights reserved.
 
8
 * Use is subject to license terms specified in the COPYING file
 
9
 * distributed with the Net-SNMP package.
 
10
 */
 
11
#include <net-snmp/net-snmp-config.h>
 
12
 
 
13
#include <sys/types.h>
 
14
#if HAVE_SYS_PARAM_H
 
15
#include <sys/param.h>
 
16
#endif
 
17
#if HAVE_SYS_FILE_H
 
18
#include <sys/file.h>
 
19
#endif
 
20
#if HAVE_SYS_SOCKET_H
 
21
#include <sys/socket.h>
 
22
#endif
 
23
#if HAVE_SYS_SOCKIO_H
 
24
#include <sys/sockio.h>
 
25
#endif
 
26
#if HAVE_SYS_IOCTL_H
 
27
#include <sys/ioctl.h>
 
28
#endif
 
29
#if HAVE_SYS_MBUF_H
 
30
#include <sys/mbuf.h>
 
31
#endif
 
32
 
 
33
 
 
34
#if HAVE_SYS_STREAM_H
 
35
#include <sys/stream.h>
 
36
#endif
 
37
#if HAVE_NET_ROUTE_H
 
38
#include <net/route.h>
 
39
#endif
 
40
#if HAVE_NETINET_IN_H
 
41
#include <netinet/in.h>
 
42
#endif
 
43
#if HAVE_ARPA_INET_H
 
44
#include <arpa/inet.h>
 
45
#endif
 
46
#if HAVE_NETDB_H
 
47
#include <netdb.h>
 
48
#endif
 
49
 
 
50
#include <errno.h>
 
51
#if HAVE_UNISTD_H
 
52
#include <unistd.h>
 
53
#endif
 
54
#include <stdio.h>
 
55
#include <ctype.h>
 
56
#if HAVE_STRING_H
 
57
#include <string.h>
 
58
#endif
 
59
#if HAVE_STDLIB_H
 
60
#include <stdlib.h>
 
61
#endif
 
62
 
 
63
#if HAVE_WINSOCK_H
 
64
#include <winsock.h>
 
65
#endif
 
66
 
 
67
#if HAVE_DMALLOC_H
 
68
#include <dmalloc.h>
 
69
#endif
 
70
 
 
71
#include <net-snmp/net-snmp-includes.h>
 
72
#include <net-snmp/agent/net-snmp-agent-includes.h>
 
73
 
 
74
#include "ip.h"
 
75
#include "route_write.h"
 
76
 
 
77
#ifdef cygwin
 
78
#define WIN32
 
79
#include <windows.h>
 
80
#endif
 
81
 
 
82
#ifndef WIN32
 
83
 
 
84
#ifndef STRUCT_RTENTRY_HAS_RT_DST
 
85
#define rt_dst rt_nodes->rn_key
 
86
#endif
 
87
#ifndef STRUCT_RTENTRY_HAS_RT_HASH
 
88
#define rt_hash rt_pad1
 
89
#endif
 
90
 
 
91
#ifdef irix6
 
92
#define SIOCADDRT SIOCADDMULTI
 
93
#define SIOCDELRT SIOCDELMULTI
 
94
#endif
 
95
 
 
96
int
 
97
addRoute(u_long dstip, u_long gwip, u_long iff, u_short flags)
 
98
{
 
99
#ifndef dynix
 
100
    struct sockaddr_in dst;
 
101
    struct sockaddr_in gateway;
 
102
    int             s;
 
103
    RTENTRY         route;
 
104
 
 
105
    s = socket(AF_INET, SOCK_RAW, 0);
 
106
    if (s < 0) {
 
107
        snmp_log_perror("socket");
 
108
        return 0;
 
109
    }
 
110
 
 
111
 
 
112
    flags |= RTF_UP;
 
113
 
 
114
    dst.sin_family = AF_INET;
 
115
    dst.sin_addr.s_addr = htonl(dstip);
 
116
 
 
117
 
 
118
    gateway.sin_family = AF_INET;
 
119
    gateway.sin_addr.s_addr = htonl(gwip);
 
120
 
 
121
    memcpy(&route.rt_dst, &dst, sizeof(struct sockaddr_in));
 
122
    memcpy(&route.rt_gateway, &gateway, sizeof(struct sockaddr_in));
 
123
 
 
124
    route.rt_flags = flags;
 
125
#ifndef RTENTRY_4_4
 
126
    route.rt_hash = iff;
 
127
#endif
 
128
#ifdef irix6
 
129
    return 0;
 
130
#else
 
131
    return (ioctl(s, SIOCADDRT, (caddr_t) & route));
 
132
#endif
 
133
 
 
134
#else                           /* dynix */
 
135
    /*
 
136
     *  Throws up the following errors:
 
137
     *
 
138
     * "mibII/route_write.c", line 113: undefined struct/union member: rt_nodes
 
139
     * "mibII/route_write.c", line 113: undefined struct/union member: rn_key
 
140
     * "mibII/route_write.c", line 113: left operand of "->" must be pointer to struct/union
 
141
     * "mibII/route_write.c", line 118: undefined struct/union member: rt_pad1
 
142
     * "mibII/route_write.c", line 123: undefined symbol: SIOCADDRT
 
143
     * "mibII/route_write.c", line 155: undefined struct/union member: rt_nodes
 
144
     * "mibII/route_write.c", line 155: undefined struct/union member: rn_key
 
145
     * "mibII/route_write.c", line 155: left operand of "->" must be pointer to struct/union
 
146
     * "mibII/route_write.c", line 160: undefined struct/union member: rt_pad1
 
147
     * "mibII/route_write.c", line 166: undefined symbol: SIOCDELRT
 
148
     */
 
149
    return 0;
 
150
#endif
 
151
 
 
152
}
 
153
 
 
154
 
 
155
 
 
156
int
 
157
delRoute(u_long dstip, u_long gwip, u_long iff, u_short flags)
 
158
{
 
159
#ifndef dynix
 
160
 
 
161
    struct sockaddr_in dst;
 
162
    struct sockaddr_in gateway;
 
163
    int             s;
 
164
    RTENTRY         route;
 
165
 
 
166
    s = socket(AF_INET, SOCK_RAW, 0);
 
167
    if (s < 0) {
 
168
        snmp_log_perror("socket");
 
169
        return 0;
 
170
    }
 
171
 
 
172
 
 
173
    flags |= RTF_UP;
 
174
 
 
175
    dst.sin_family = AF_INET;
 
176
    dst.sin_addr.s_addr = htonl(dstip);
 
177
 
 
178
 
 
179
    gateway.sin_family = AF_INET;
 
180
    gateway.sin_addr.s_addr = htonl(gwip);
 
181
 
 
182
    memcpy(&route.rt_dst, &dst, sizeof(struct sockaddr_in));
 
183
    memcpy(&route.rt_gateway, &gateway, sizeof(struct sockaddr_in));
 
184
 
 
185
    route.rt_flags = flags;
 
186
#ifndef RTENTRY_4_4
 
187
    route.rt_hash = iff;
 
188
#endif
 
189
 
 
190
#ifdef irix6
 
191
    return 0;
 
192
#else
 
193
    return (ioctl(s, SIOCDELRT, (caddr_t) & route));
 
194
#endif
 
195
 
 
196
#else                           /* dynix */
 
197
    /*
 
198
     * See 'addRoute' for the list of errors.
 
199
     */
 
200
    return 0;
 
201
#endif
 
202
}
 
203
 
 
204
 
 
205
#ifndef STRUCT_RTENTRY_HAS_RT_DST
 
206
#undef rt_dst
 
207
#endif
 
208
 
 
209
 
 
210
#define  MAX_CACHE   8
 
211
 
 
212
struct rtent {
 
213
 
 
214
    u_long          in_use;
 
215
    u_long          old_dst;
 
216
    u_long          old_nextIR;
 
217
    u_long          old_ifix;
 
218
    u_long          old_flags;
 
219
 
 
220
    u_long          rt_dst;     /*  main entries    */
 
221
    u_long          rt_ifix;
 
222
    u_long          rt_metric1;
 
223
    u_long          rt_nextIR;
 
224
    u_long          rt_type;
 
225
    u_long          rt_proto;
 
226
 
 
227
 
 
228
    u_long          xx_dst;     /*  shadow entries  */
 
229
    u_long          xx_ifix;
 
230
    u_long          xx_metric1;
 
231
    u_long          xx_nextIR;
 
232
    u_long          xx_type;
 
233
    u_long          xx_proto;
 
234
};
 
235
 
 
236
struct rtent    rtcache[MAX_CACHE];
 
237
 
 
238
struct rtent   *
 
239
findCacheRTE(u_long dst)
 
240
{
 
241
    int             i;
 
242
 
 
243
    for (i = 0; i < MAX_CACHE; i++) {
 
244
 
 
245
        if (rtcache[i].in_use && (rtcache[i].rt_dst == dst)) {  /* valid & match? */
 
246
            return (&rtcache[i]);
 
247
        }
 
248
    }
 
249
    return 0;
 
250
}
 
251
 
 
252
struct rtent   *
 
253
newCacheRTE(void)
 
254
{
 
255
 
 
256
    int             i;
 
257
 
 
258
    for (i = 0; i < MAX_CACHE; i++) {
 
259
 
 
260
        if (!rtcache[i].in_use) {
 
261
            rtcache[i].in_use = 1;
 
262
            return (&rtcache[i]);
 
263
        }
 
264
    }
 
265
    return 0;
 
266
 
 
267
}
 
268
 
 
269
int
 
270
delCacheRTE(u_long dst)
 
271
{
 
272
    struct rtent   *rt;
 
273
 
 
274
    rt = findCacheRTE(dst);
 
275
    if (!rt) {
 
276
        return 0;
 
277
    }
 
278
 
 
279
    rt->in_use = 0;
 
280
    return 1;
 
281
}
 
282
 
 
283
 
 
284
struct rtent   *
 
285
cacheKernelRTE(u_long dst)
 
286
{
 
287
    return 0;                   /* for now */
 
288
    /*
 
289
     * ...... 
 
290
     */
 
291
}
 
292
 
 
293
/*
 
294
 * If statP is non-NULL, the referenced object is at that location.
 
295
 * If statP is NULL and ap is non-NULL, the instance exists, but not this variable.
 
296
 * If statP is NULL and ap is NULL, then neither this instance nor the variable exists.
 
297
 */
 
298
 
 
299
int
 
300
write_rte(int action,
 
301
          u_char * var_val,
 
302
          u_char var_val_type,
 
303
          size_t var_val_len, u_char * statP, oid * name, size_t length)
 
304
{
 
305
    struct rtent   *rp;
 
306
    int             var;
 
307
    long            val;
 
308
    u_long          dst;
 
309
    char            buf[8];
 
310
    u_short         flags;
 
311
    int             oldty;
 
312
 
 
313
    /*
 
314
     * object identifier is of form:
 
315
     * 1.3.6.1.2.1.4.21.1.X.A.B.C.D ,  where A.B.C.D is IP address.
 
316
     * IPADDR starts at offset 10.
 
317
     */
 
318
 
 
319
    if (length != 14) {
 
320
        snmp_log(LOG_ERR, "length error\n");
 
321
        return SNMP_ERR_NOCREATION;
 
322
    }
 
323
 
 
324
#ifdef solaris2         /* not implemented */
 
325
    return SNMP_ERR_NOTWRITABLE;
 
326
#endif
 
327
 
 
328
    var = name[9];
 
329
 
 
330
    dst = *((u_long *) & name[10]);
 
331
 
 
332
    rp = findCacheRTE(dst);
 
333
 
 
334
    if (!rp) {
 
335
        rp = cacheKernelRTE(dst);
 
336
    }
 
337
 
 
338
 
 
339
    if (action == RESERVE1 && !rp) {
 
340
 
 
341
        rp = newCacheRTE();
 
342
        if (!rp) {
 
343
            snmp_log(LOG_ERR, "newCacheRTE");
 
344
            return SNMP_ERR_RESOURCEUNAVAILABLE;
 
345
        }
 
346
        rp->rt_type = rp->xx_type = 2;
 
347
 
 
348
    } else if (action == COMMIT) {
 
349
 
 
350
 
 
351
    } else if (action == FREE) {
 
352
        if (rp->rt_type == 2) { /* was invalid before */
 
353
            delCacheRTE(dst);
 
354
        }
 
355
    }
 
356
 
 
357
 
 
358
 
 
359
 
 
360
    switch (var) {
 
361
 
 
362
    case IPROUTEDEST:
 
363
 
 
364
        if (action == RESERVE1) {
 
365
 
 
366
            if (var_val_type != ASN_OCTET_STR) {
 
367
                snmp_log(LOG_ERR, "not octet");
 
368
                return SNMP_ERR_WRONGTYPE;
 
369
            }
 
370
 
 
371
            memcpy(buf, var_val, (var_val_len > 8) ? 8 : var_val_len);
 
372
 
 
373
            if (var_val_type != ASN_OCTET_STR) {
 
374
                snmp_log(LOG_ERR, "not octet2");
 
375
                return SNMP_ERR_WRONGTYPE;
 
376
            }
 
377
 
 
378
            rp->xx_dst = *((u_long *) buf);
 
379
 
 
380
 
 
381
        } else if (action == COMMIT) {
 
382
            rp->rt_dst = rp->xx_dst;
 
383
        }
 
384
        break;
 
385
 
 
386
    case IPROUTEMETRIC1:
 
387
 
 
388
        if (action == RESERVE1) {
 
389
            if (var_val_type != ASN_INTEGER) {
 
390
                snmp_log(LOG_ERR, "not int1");
 
391
                return SNMP_ERR_WRONGTYPE;
 
392
            }
 
393
 
 
394
            val = *((long *) var_val);
 
395
 
 
396
            if (val < -1) {
 
397
                snmp_log(LOG_ERR, "not right1");
 
398
                return SNMP_ERR_WRONGVALUE;
 
399
            }
 
400
 
 
401
            rp->xx_metric1 = val;
 
402
 
 
403
        } else if (action == RESERVE2) {
 
404
 
 
405
            if ((rp->xx_metric1 == 1) && (rp->xx_type != 4)) {
 
406
                snmp_log(LOG_ERR, "reserve2 failed\n");
 
407
                return SNMP_ERR_WRONGVALUE;
 
408
            }
 
409
 
 
410
        } else if (action == COMMIT) {
 
411
            rp->rt_metric1 = rp->xx_metric1;
 
412
        }
 
413
        break;
 
414
 
 
415
    case IPROUTEIFINDEX:
 
416
 
 
417
        if (action == RESERVE1) {
 
418
            if (var_val_type != ASN_INTEGER) {
 
419
                snmp_log(LOG_ERR, "not right2");
 
420
                return SNMP_ERR_WRONGTYPE;
 
421
            }
 
422
 
 
423
            val = *((long *) var_val);
 
424
 
 
425
            if (val <= 0) {
 
426
                snmp_log(LOG_ERR, "not right3");
 
427
                return SNMP_ERR_WRONGVALUE;
 
428
            }
 
429
 
 
430
            rp->xx_ifix = val;
 
431
 
 
432
        } else if (action == COMMIT) {
 
433
            rp->rt_ifix = rp->xx_ifix;
 
434
        }
 
435
        break;
 
436
 
 
437
    case IPROUTENEXTHOP:
 
438
 
 
439
        if (action == RESERVE1) {
 
440
 
 
441
            if (var_val_type != ASN_OCTET_STR) {
 
442
                snmp_log(LOG_ERR, "not right4");
 
443
                return SNMP_ERR_WRONGTYPE;
 
444
            }
 
445
 
 
446
            memcpy(buf, var_val, (var_val_len > 8) ? 8 : var_val_len);
 
447
 
 
448
            if (var_val_type != ASN_OCTET_STR) {
 
449
                snmp_log(LOG_ERR, "not right5");
 
450
                return SNMP_ERR_WRONGTYPE;
 
451
            }
 
452
 
 
453
            rp->xx_nextIR = *((u_long *) buf);
 
454
 
 
455
        } else if (action == COMMIT) {
 
456
            rp->rt_nextIR = rp->xx_nextIR;
 
457
        }
 
458
 
 
459
 
 
460
    case IPROUTETYPE:
 
461
 
 
462
        /*
 
463
         *  flag meaning:
 
464
         *
 
465
         *  IPROUTEPROTO (rt_proto): none: (cant set == 3 (netmgmt)) 
 
466
         *
 
467
         *  IPROUTEMETRIC1:  1 iff gateway, 0 otherwise
 
468
         *  IPROUTETYPE:     4 iff gateway, 3 otherwise
 
469
         */
 
470
 
 
471
        if (action == RESERVE1) {
 
472
            if (var_val_type != ASN_INTEGER) {
 
473
                return SNMP_ERR_WRONGTYPE;
 
474
            }
 
475
 
 
476
            val = *((long *) var_val);
 
477
 
 
478
            if ((val < 2) || (val > 4)) {       /* only accept invalid, direct, indirect */
 
479
                snmp_log(LOG_ERR, "not right6");
 
480
                return SNMP_ERR_WRONGVALUE;
 
481
            }
 
482
 
 
483
            rp->xx_type = val;
 
484
 
 
485
        } else if (action == COMMIT) {
 
486
 
 
487
            oldty = rp->rt_type;
 
488
            rp->rt_type = rp->xx_type;
 
489
 
 
490
            if (rp->rt_type == 2) {     /* invalid, so delete from kernel */
 
491
 
 
492
                if (delRoute
 
493
                    (rp->rt_dst, rp->rt_nextIR, rp->rt_ifix,
 
494
                     rp->old_flags) < 0) {
 
495
                    snmp_log_perror("delRoute");
 
496
                }
 
497
 
 
498
            } else {
 
499
 
 
500
                /*
 
501
                 * it must be valid now, so flush to kernel 
 
502
                 */
 
503
 
 
504
                if (oldty != 2) {       /* was the old entry valid ?  */
 
505
                    if (delRoute
 
506
                        (rp->old_dst, rp->old_nextIR, rp->old_ifix,
 
507
                         rp->old_flags) < 0) {
 
508
                        snmp_log_perror("delRoute");
 
509
                    }
 
510
                }
 
511
 
 
512
                /*
 
513
                 * not invalid, so remove from cache 
 
514
                 */
 
515
 
 
516
                flags = (rp->rt_type == 4 ? RTF_GATEWAY : 0);
 
517
 
 
518
                if (addRoute(rp->rt_dst, rp->rt_nextIR, rp->rt_ifix, flags)
 
519
                    < 0) {
 
520
                    snmp_log_perror("addRoute");
 
521
                }
 
522
 
 
523
                delCacheRTE(rp->rt_type);
 
524
            }
 
525
        }
 
526
        break;
 
527
 
 
528
 
 
529
    case IPROUTEPROTO:
 
530
 
 
531
    default:
 
532
        DEBUGMSGTL(("snmpd", "unknown sub-id %d in write_rte\n", var));
 
533
        return SNMP_ERR_NOCREATION;
 
534
 
 
535
 
 
536
    }
 
537
 
 
538
    return SNMP_ERR_NOERROR;
 
539
}
 
540
 
 
541
#else                           /* WIN32 */
 
542
#include <iphlpapi.h>
 
543
 
 
544
extern PMIB_IPFORWARDROW route_row;
 
545
extern int      create_flag;
 
546
 
 
547
int
 
548
write_rte(int action,
 
549
          u_char * var_val,
 
550
          u_char var_val_type,
 
551
          size_t var_val_len, u_char * statP, oid * name, size_t length)
 
552
{
 
553
    int             var, retval = NO_ERROR;
 
554
    static PMIB_IPFORWARDROW oldroute_row = NULL;
 
555
    static int      mask_flag = 0, nexthop_flag = 0;
 
556
    static int      index_flag = 0, metric_flag = 0;
 
557
    static int      dest_flag = 0;
 
558
    DWORD           status = NO_ERROR;
 
559
    /*
 
560
     * object identifier is of form:
 
561
     * 1.3.6.1.2.1.4.21.1.X.A.B.C.D ,  where A.B.C.D is IP address.
 
562
     * IPADDR starts at offset 10.
 
563
     */
 
564
 
 
565
    if (length != 14) {
 
566
        snmp_log(LOG_ERR, "length error\n");
 
567
        return SNMP_ERR_NOCREATION;
 
568
    }
 
569
    /*
 
570
     * #define for ipRouteTable entries are 1 less than corresponding sub-id in MIB
 
571
     * * i.e. IPROUTEDEST defined as 0, but ipRouteDest registered as 1
 
572
     */
 
573
    var = name[9] - 1;
 
574
 
 
575
    switch (action) {
 
576
    case RESERVE1:
 
577
        switch (var) {
 
578
        case IPROUTEMETRIC1:
 
579
        case IPROUTEMETRIC2:
 
580
        case IPROUTEMETRIC3:
 
581
        case IPROUTEMETRIC4:
 
582
        case IPROUTEMETRIC5:
 
583
        case IPROUTETYPE:
 
584
        case IPROUTEAGE:
 
585
        case IPROUTEIFINDEX:
 
586
            if (var_val_type != ASN_INTEGER) {
 
587
                snmp_log(LOG_ERR, "not integer\n");
 
588
                return SNMP_ERR_WRONGTYPE;
 
589
            }
 
590
            if (var_val_len > sizeof(int)) {
 
591
                snmp_log(LOG_ERR, "bad length\n");
 
592
                return SNMP_ERR_WRONGLENGTH;
 
593
            }
 
594
            if (var == IPROUTETYPE) {
 
595
                if ((*((int *) var_val)) < 2 || (*((int *) var_val)) > 4) {
 
596
                    snmp_log(LOG_ERR, "invalid ipRouteType\n");
 
597
                    return SNMP_ERR_WRONGVALUE;
 
598
                }
 
599
            } else if ((var == IPROUTEIFINDEX) || (var == IPROUTEAGE)) {
 
600
                if ((*((int *) var_val)) < 0) {
 
601
                    snmp_log(LOG_ERR, "invalid ipRouteIfIndex\n");
 
602
                    return SNMP_ERR_WRONGVALUE;
 
603
                }
 
604
            } else {
 
605
                if ((*((int *) var_val)) < -1) {
 
606
                    snmp_log(LOG_ERR, "not right1");
 
607
                    return SNMP_ERR_WRONGVALUE;
 
608
                }
 
609
            }
 
610
            break;
 
611
        case IPROUTENEXTHOP:
 
612
        case IPROUTEMASK:
 
613
        case IPROUTEDEST:
 
614
            if (var_val_type != ASN_IPADDRESS) {
 
615
                snmp_log(LOG_ERR, "not right4");
 
616
                return SNMP_ERR_WRONGTYPE;
 
617
            }
 
618
            if (var_val_len != 4) {
 
619
                snmp_log(LOG_ERR, "incorrect ipAddress length");
 
620
                return SNMP_ERR_WRONGLENGTH;
 
621
            }
 
622
            break;
 
623
        default:
 
624
            DEBUGMSGTL(("snmpd", "unknown sub-id %d in write_rte\n",
 
625
                        var + 1));
 
626
            retval = SNMP_ERR_NOTWRITABLE;
 
627
        }
 
628
        break;
 
629
 
 
630
    case RESERVE2:
 
631
        /*
 
632
         * Save the old value, in case of UNDO 
 
633
         */
 
634
        if (oldroute_row == NULL) {
 
635
            oldroute_row =
 
636
                (PMIB_IPFORWARDROW) malloc(sizeof(MIB_IPFORWARDROW));
 
637
            *oldroute_row = *route_row;
 
638
        }
 
639
        break;
 
640
 
 
641
    case ACTION:               /* Perform the SET action (if reversible) */
 
642
        switch (var) {
 
643
        case IPROUTEMETRIC1:
 
644
            metric_flag = 1;
 
645
            route_row->dwForwardMetric1 = *((int *) var_val);
 
646
            break;
 
647
        case IPROUTEMETRIC2:
 
648
            route_row->dwForwardMetric2 = *((int *) var_val);
 
649
            break;
 
650
        case IPROUTEMETRIC3:
 
651
            route_row->dwForwardMetric3 = *((int *) var_val);
 
652
            break;
 
653
        case IPROUTEMETRIC4:
 
654
            route_row->dwForwardMetric4 = *((int *) var_val);
 
655
            break;
 
656
        case IPROUTEMETRIC5:
 
657
            route_row->dwForwardMetric5 = *((int *) var_val);
 
658
            break;
 
659
        case IPROUTETYPE:
 
660
            route_row->dwForwardType = *((int *) var_val);
 
661
            break;
 
662
        case IPROUTEAGE:
 
663
            /*
 
664
             * Irrespective of suppied value, this will be set with 0.
 
665
             * * As row will be updated and this field gives the number of 
 
666
             * * seconds since this route was last updated
 
667
             */
 
668
            route_row->dwForwardAge = *((int *) var_val);
 
669
            break;
 
670
        case IPROUTEIFINDEX:
 
671
            index_flag = 1;
 
672
            route_row->dwForwardIfIndex = *((int *) var_val);
 
673
            break;
 
674
 
 
675
        case IPROUTENEXTHOP:
 
676
            nexthop_flag = 1;
 
677
            route_row->dwForwardNextHop = *((DWORD *) var_val);
 
678
            break;
 
679
        case IPROUTEMASK:
 
680
            mask_flag = 1;
 
681
            route_row->dwForwardMask = *((DWORD *) var_val);
 
682
            break;
 
683
        case IPROUTEDEST:
 
684
            dest_flag = 1;
 
685
            route_row->dwForwardDest = *((DWORD *) var_val);
 
686
            break;
 
687
        default:
 
688
            DEBUGMSGTL(("snmpd", "unknown sub-id %d in write_rte\n",
 
689
                        var + 1));
 
690
            retval = SNMP_ERR_NOTWRITABLE;
 
691
        }
 
692
        return retval;
 
693
    case UNDO:
 
694
        /*
 
695
         * Reverse the SET action and free resources 
 
696
         */
 
697
        if (oldroute_row) {
 
698
            *route_row = *oldroute_row;
 
699
            free(oldroute_row);
 
700
            oldroute_row = NULL;
 
701
            free(route_row);
 
702
            route_row = NULL;
 
703
        }
 
704
        break;
 
705
 
 
706
    case COMMIT:
 
707
        /*
 
708
         * When this case entered 'route_row' will have user supplied values for asked entries. 
 
709
         * * Thats why it is enough if we call SetIpForwardEntry/CreateIpForwardEntry only once 
 
710
         * * SetIpForwardENtry is not done in ACTION phase, as that will reset ipRouteAge on success
 
711
         * * and if any varbind fails, then we can't UNDO the change for ipROuteAge. 
 
712
         */
 
713
        if (route_row) {
 
714
            if (!create_flag) {
 
715
 
 
716
                if (SetIpForwardEntry(route_row) != NO_ERROR) {
 
717
                    snmp_log(LOG_ERR,
 
718
                             "Can't set route table's row with specified value\n");
 
719
                    retval = SNMP_ERR_COMMITFAILED;
 
720
                } else {
 
721
                    /*
 
722
                     * SET on IpRouteNextHop, IpRouteMask & ipRouteDest creates new row. 
 
723
                     * *If Set succeeds, then delete the old row.
 
724
                     * * Don't know yet whether SET on ipRouteIfIndex creates new row.
 
725
                     * * If it creates then index_flag should be added to following if statement
 
726
                     */
 
727
 
 
728
                    if (dest_flag || nexthop_flag || mask_flag) {
 
729
                        oldroute_row->dwForwardType = 2;
 
730
                        if (SetIpForwardEntry(oldroute_row) != NO_ERROR) {
 
731
                            snmp_log(LOG_ERR,
 
732
                                     "Set on ipRouteTable created new row, but failed to delete the old row\n");
 
733
                            retval = SNMP_ERR_GENERR;
 
734
                        }
 
735
                    }
 
736
                }
 
737
            }
 
738
            /*
 
739
             * Only if create_flag, mask, nexthop, ifIndex and metric are specified, create new entry 
 
740
             */
 
741
            if (create_flag) {
 
742
                if (mask_flag && nexthop_flag && metric_flag && index_flag) {
 
743
                    if ((status =
 
744
                         CreateIpForwardEntry(route_row)) != NO_ERROR) {
 
745
                        snmp_log(LOG_ERR,
 
746
                                 "Inside COMMIT: CreateIpNetEntry failed, status %d\n",
 
747
                                 status);
 
748
                        retval = SNMP_ERR_COMMITFAILED;
 
749
                    }
 
750
                } else {
 
751
                    /*
 
752
                     * For new entry, mask, nexthop, ifIndex and metric must be supplied 
 
753
                     */
 
754
                    snmp_log(LOG_ERR,
 
755
                             "case COMMIT, can't create without index, mask, nextHop and metric\n");
 
756
                    retval = SNMP_ERR_WRONGVALUE;
 
757
                }
 
758
            }
 
759
        }
 
760
 
 
761
    case FREE:
 
762
        /*
 
763
         * Free any resources allocated 
 
764
         */
 
765
        free(oldroute_row);
 
766
        oldroute_row = NULL;
 
767
        free(route_row);
 
768
        route_row = NULL;
 
769
        mask_flag = nexthop_flag = metric_flag = index_flag = dest_flag =
 
770
            0;
 
771
        break;
 
772
    }
 
773
    return retval;
 
774
}
 
775
 
 
776
#endif                          /* WIN32 */