📄 route_write.c
字号:
#include <config.h>#include <sys/types.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if HAVE_SYS_FILE_H#include <sys/file.h>#endif#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_SYS_SOCKIO_H#include <sys/sockio.h>#endif#if HAVE_SYS_IOCTL_H#include <sys/ioctl.h>#endif#if HAVE_SYS_MBUF_H#include <sys/mbuf.h>#endif#if HAVE_SYS_STREAM_H#include <sys/stream.h>#endif#if HAVE_NET_ROUTE_H#include <net/route.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#if HAVE_NETDB_H#include <netdb.h>#endif#include <errno.h>#if HAVE_UNISTD_H#include <unistd.h>#endif#include <stdio.h>#include <ctype.h>#if HAVE_STRING_H#include <string.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "asn1.h"#include "snmp.h"#include "snmp_api.h"#include "snmp_impl.h"#include "snmp_vars.h"#include "snmp_debug.h"#include "ip.h"#include "route_write.h"#include "snmp_logging.h"#ifdef cygwin#define WIN32#include <windows.h>#endif#ifndef WIN32#ifndef STRUCT_RTENTRY_HAS_RT_DST#define rt_dst rt_nodes->rn_key#endif#ifndef STRUCT_RTENTRY_HAS_RT_HASH#define rt_hash rt_pad1#endif#ifdef irix6#define SIOCADDRT SIOCADDMULTI#define SIOCDELRT SIOCDELMULTI#endif/* * didn't wanna take the time to fix these errors: * * "mibII/route_write.c", line 113: undefined struct/union member: rt_nodes * "mibII/route_write.c", line 113: undefined struct/union member: rn_key * "mibII/route_write.c", line 113: left operand of "->" must be pointer to struct/union * "mibII/route_write.c", line 118: undefined struct/union member: rt_pad1 * "mibII/route_write.c", line 123: undefined symbol: SIOCADDRT * "mibII/route_write.c", line 155: undefined struct/union member: rt_nodes * "mibII/route_write.c", line 155: undefined struct/union member: rn_key * "mibII/route_write.c", line 155: left operand of "->" must be pointer to struct/union * "mibII/route_write.c", line 160: undefined struct/union member: rt_pad1 * "mibII/route_write.c", line 166: undefined symbol: SIOCDELRT * */#ifdef dynixint addRoute(u_long dstip, u_long gwip, u_long iff, u_short flags){ return 0;}#elseint addRoute(u_long dstip, u_long gwip, u_long iff, u_short flags){ struct sockaddr_in dst; struct sockaddr_in gateway; int s; RTENTRY route; s = socket(AF_INET, SOCK_RAW, 0); if (s<0) { snmp_log_perror("socket"); return 0; } flags |= RTF_UP; dst.sin_family = AF_INET; dst.sin_addr.s_addr = htonl(dstip); gateway.sin_family = AF_INET; gateway.sin_addr.s_addr = htonl(gwip); memcpy(&route.rt_dst, &dst, sizeof(struct sockaddr_in)); memcpy(&route.rt_gateway, &gateway, sizeof(struct sockaddr_in)); route.rt_flags = flags;#ifndef RTENTRY_4_4 route.rt_hash = iff;#endif#ifdef irix6 return 0;#else return (ioctl(s, SIOCADDRT , (caddr_t)&route));#endif}#endif/* * didn't wanna take the time to fix these errors: * * "mibII/route_write.c", line 113: undefined struct/union member: rt_nodes * "mibII/route_write.c", line 113: undefined struct/union member: rn_key * "mibII/route_write.c", line 113: left operand of "->" must be pointer to struct/union * "mibII/route_write.c", line 118: undefined struct/union member: rt_pad1 * "mibII/route_write.c", line 123: undefined symbol: SIOCADDRT * "mibII/route_write.c", line 155: undefined struct/union member: rt_nodes * "mibII/route_write.c", line 155: undefined struct/union member: rn_key * "mibII/route_write.c", line 155: left operand of "->" must be pointer to struct/union * "mibII/route_write.c", line 160: undefined struct/union member: rt_pad1 * "mibII/route_write.c", line 166: undefined symbol: SIOCDELRT * */#ifdef dynixint delRoute(u_long dstip, u_long gwip, u_long iff, u_short flags){ return 0;}#elseint delRoute(u_long dstip, u_long gwip, u_long iff, u_short flags){ struct sockaddr_in dst; struct sockaddr_in gateway; int s; RTENTRY route; s = socket(AF_INET, SOCK_RAW, 0); if (s<0) { snmp_log_perror("socket"); return 0; } flags |= RTF_UP; dst.sin_family = AF_INET; dst.sin_addr.s_addr = htonl(dstip); gateway.sin_family = AF_INET; gateway.sin_addr.s_addr = htonl(gwip); memcpy(&route.rt_dst, &dst, sizeof(struct sockaddr_in)); memcpy(&route.rt_gateway, &gateway, sizeof(struct sockaddr_in)); route.rt_flags = flags;#ifndef RTENTRY_4_4 route.rt_hash = iff;#endif#ifdef irix6 return 0;#else return (ioctl(s, SIOCDELRT , (caddr_t)&route));#endif}#endif#ifndef STRUCT_RTENTRY_HAS_RT_DST#undef rt_dst#endif#define MAX_CACHE 8struct rtent { u_long in_use; u_long old_dst; u_long old_nextIR; u_long old_ifix; u_long old_flags; u_long rt_dst; /* main entries */ u_long rt_ifix; u_long rt_metric1; u_long rt_nextIR; u_long rt_type; u_long rt_proto; u_long xx_dst; /* shadow entries */ u_long xx_ifix; u_long xx_metric1; u_long xx_nextIR; u_long xx_type; u_long xx_proto;};struct rtent rtcache[MAX_CACHE];struct rtent *findCacheRTE(u_long dst){ int i; for (i = 0; i < MAX_CACHE; i++) { if (rtcache[i].in_use && (rtcache[i].rt_dst == dst)) { /* valid & match? */ return (&rtcache[i]); } } return 0;}struct rtent *newCacheRTE(void){ int i; for (i = 0; i < MAX_CACHE; i++) { if (!rtcache[i].in_use) { rtcache[i].in_use = 1; return (&rtcache[i]); } } return 0;}int delCacheRTE(u_long dst){ struct rtent *rt; rt = findCacheRTE(dst); if (!rt) { return 0; } rt->in_use = 0; return 1;}struct rtent *cacheKernelRTE(u_long dst){ return 0; /* for now */ /* ...... */}/* * If statP is non-NULL, the referenced object is at that location. * If statP is NULL and ap is non-NULL, the instance exists, but not this variable. * If statP is NULL and ap is NULL, then neither this instance nor the variable exists. */intwrite_rte( int action, u_char *var_val, u_char var_val_type, size_t var_val_len, u_char *statP, oid *name, size_t length){ struct rtent *rp; int var; long val; u_long dst; char buf[8]; u_short flags; int oldty; /* * object identifier is of form: * 1.3.6.1.2.1.4.21.1.X.A.B.C.D , where A.B.C.D is IP address. * IPADDR starts at offset 10. */ if (length != 14) { snmp_log(LOG_ERR, "length error\n"); return SNMP_ERR_NOCREATION; } var = name[9]; dst = *((u_long *) & name[10] ); rp = findCacheRTE(dst); if (!rp) { rp = cacheKernelRTE(dst); } if (action == RESERVE1 && !rp) { rp = newCacheRTE(); if (!rp) { snmp_log(LOG_ERR, "newCacheRTE"); return SNMP_ERR_RESOURCEUNAVAILABLE; } rp->rt_type = rp->xx_type = 2; } else if (action == COMMIT){ } else if (action == FREE) { if (rp->rt_type == 2) { /* was invalid before */ delCacheRTE(dst); } } switch(var){ case IPROUTEDEST: if (action == RESERVE1){ if (var_val_type != ASN_OCTET_STR) { snmp_log(LOG_ERR, "not octet"); return SNMP_ERR_WRONGTYPE; } memcpy(buf, var_val, (var_val_len > 8) ? 8 : var_val_len);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -