📄 sockets.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/sockets.c,v 1.5 2003/01/15 14:04:56 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1991-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: sockets.c,v $ * Revision 1.5 2003/01/15 14:04:56 josh * directory structure shifting * * Revision 1.4 2002/05/23 19:42:42 josh * updates to support the entity MIB * * Revision 1.3 2002/03/11 19:17:11 josh * snmptalk's destination address is now stored and passed around * as an ipaddr_t. This required changes to a lot of internal function * calls that used to expect an inaddr_t to be passed around. Also, * snmptalk is now capable of using IPv6 sockets * * Revision 1.2 2001/11/08 16:47:31 tneale * Updated for newset file layout * * Revision 1.1.1.1 2001/11/05 17:49:13 tneale * Tornado shuffle * * Revision 7.17 2001/01/19 22:24:51 paul * Update copyright. * * Revision 7.16 2000/07/10 15:55:16 mrf * Add configuration for Cygnus NT environment with associated changes. * * Revision 7.15 2000/03/17 00:14:36 meister * Update copyright message * * Revision 7.14 1998/11/25 03:38:21 sra * "timeout" => "snmp_timeout" to fix OpEN symbol conflict. * * Revision 7.13 1998/07/02 06:55:42 sra * Make Snark restartable under pSOS, and other minor cleanups. * * Revision 7.12 1998/06/23 20:56:46 sar * Moved parse.h to snark/h/parse.h * * Revision 7.11 1998/06/07 03:40:56 sar * Added some dummy routines to keep linkers happy * * Revision 7.10 1998/02/25 04:58:07 sra * Update copyrights. * * Revision 7.9 1997/10/22 03:03:33 sar * Updated makefile.cfg to use AGENTX_MASTER & AGENTX_SUB * * Added agentx_init() to snmptalk & agentx and envoy_init to sockets * * Revision 7.8 1997/03/20 06:53:42 sra * DFARS-safe copyright text. Zap! * * Revision 7.7 1997/03/19 23:45:52 sra * Turn off echoing on the cty, since fgets() already does that. * * Revision 7.6 1997/03/17 22:09:28 mrf * conditionally compile unix-only include file and function * * Revision 7.5 1997/02/19 08:10:29 sra * More fun merging snmptalk into snark, general snark cleanup. * * Revision 7.4 1997/01/08 23:26:53 sar * Updated include files to use envoy/h as appropriate after * move from envoy/utils to snark/snmptalk * * Revision 7.3 1996/10/30 22:06:39 sar * Removed the bsd_release stuff * * Revision 7.2 1996/07/02 19:58:55 mrf * added select call so that receives can time out. * * Revision 7.0 1996/03/18 20:19:20 sar * Updated rev to 7.0 and copyright to 96 * * Revision 6.1 1995/10/20 22:39:59 sar * removed the party.h include * removed the no_pp stuff * * Revision 6.0 1995/05/31 21:50:26 sra * Release 6.0. * * Revision 5.2 1995/03/21 00:50:48 sar * Added a packet_dump option to snmptalk, this allows the user to trun * packet dumping on & off while snmptalk is running. * * Revision 5.1 1994/07/24 15:41:03 dab * Added ability to set UDP port used by manager and the set command can * now specify enumerated integer values by symbolic name. * * Revision 5.0 1994/05/16 15:57:40 sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.2 1993/11/12 19:38:42 sar * Added a comment in the SV_INTERRUPT section to describe a warning from * sun compilers that should be ignored. * * Revision 4.1 1993/07/20 16:21:49 dab * Fixed problem of not timing out on newer versions of SunOS. * * Revision 4.0 1993/06/24 17:14:56 sar * Updated rev to 4.0 and copyright to 93 * * Revision 3.9 1993/05/29 00:19:50 sar * modifed the get_sysutpime stub to match the extern def. * * Revision 3.8 1993/05/28 17:59:00 sar * added a get_sysuptime stub to allow simple compilation with * ojbect resources from the party.c file. This is only needed * because we want to use the same libraries for both an agent * and a manager. * * Revision 3.7 1993/03/02 18:22:28 dab * Don't print out packet dump unless SNMPTALK_DEBUG is set. * * Revision 3.6 1993/02/24 23:39:11 sar * Updated use of (party, context, create)_calls and setting of the appropriate * fields to have the creates generate basically the defval information. * Also finished moving the creates/installs from sockets.c to snmpint.c * * Revision 3.5 1993/02/24 01:06:40 sar * Updated to use the new OIDC_T/length versions of the SNMP_(Party, Context)_* * calls. Then commented out the install stuff as that is in snmpint.c * * Revision 3.4 1993/02/22 18:56:21 sar * Modified to use new acl and context routines. * * Revision 3.3 1993/02/17 21:25:34 sar * Create a bunch of partes for testing. These should be removed before * shipping and replaced with some file config system. * Currently have a print packet routine in which will be removed before * shipping. * * Revision 3.1 92/06/16 16:05:50 dab * Added batch mode * * Revision 3.0 92/04/03 17:51:47 dab * Release 3.0 * * Revision 1.6 91/11/04 16:15:15 dab * checked in with -k by dab at 92.04.03.17.48.09. * * Revision 1.6 91/11/04 16:15:15 dab * Improved for use with higher warning levels on the compiler. * * Revision 1.5 91/10/30 09:23:47 dab * Changed md_hostname_to_addr to be async. * * Revision 1.4 91/10/01 13:58:20 dab * Added some checking on the reply SNMP packet to make sure it's * the response we're looking for. * * Revision 1.3 91/09/30 14:10:25 dab * Handle setup returning int. * * Revision 1.2 91/09/17 11:55:07 dab * Got working in asynch mode. * *//* [clearcase]modification history-------------------01b,19apr05,job update copyright notices01a,28mar05,job add IPv6 support for Windows build*//* * Dirty-trick snark glue code for snmptalk over BSD sockets or Winsock. * This should be replaced Real Soon Now with something that allows * full access to snark's command line editing, etcetera, but I've * got a deadline, and this code, while gross, this isn't actually any * worse than what snmptalk already did in these environments. * * WARNING: The tasks_run() and other snark functions in this * file are NOT normal. Do not use these as examples of how * to write the system dependent parts of a snark port. * * In the long run, this should split out into two separate versions * of snmptalk-over-sockets: * * 1) snmptalk over "normal" (unix) BSD sockets, which should use the bsdif * mechanism, the snark/lib/snarkbsd.c select() loop, Envoy timers, and * an asynchronous implementation of snmp_send_rec() adapted from the * one in snark/snmptalk/attache.c, and * * 2) snmptalk over winsock, which should be a real windows event-driven * implementation, catching asynch WSAxxx events, keyboard events, etc. * Not really as hard as it sounds, since the whole snark architecture * is asynchronous already anyway. */#include <snmptalk.h>#include <wrn/wm/common/install.h>#include <stdio.h>#include <signal.h>#include <errno.h>#include <sys/types.h>#if INSTALL_on_winsock#if INSTALL_SNMP_DEMO_AGENT_IPV6#include <winsock2.h>#include <ws2tcpip.h>#include "tpipv6.h"#else#include <winsock.h>#endif /* INSTALL_SNMP_DEMO_AGENT_IPV6 */#else#include <sys/socket.h>#include <netinet/in.h>#include <ctype.h>#include <netdb.h>#include <arpa/inet.h>#endif /* INSTALL_on_winsock */#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/objectid.h>#include "md.h"#include "snmpint.h"#include <stdlib.h>#if INSTALL_on_unix#include <unistd.h>#endif#include <sys/types.h>#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/snmpconf.h>#ifdef SPRINTF#undef SPRINTF#define SPRINTF(x) ((size_t)sprintf (x))#endif#if (INSTALL_on_winsock && INSTALL_SNMP_DEMO_AGENT_IPV6)static const char * inet_ntop6 ( const u_char *src, char *dst, size_t size );#endif/* * These are defined here rather than in snark/lib/main.c so that this * code can be used without that module if the customer so desires. */int snark_exit_flag, snark_exit_value;#define TBSIZE 2048static unsigned char rcvbuff[TBSIZE];#if INSTALL_SNMP_DEMO_AGENT_IPV6#define NS_IN6ADDRSZ 16#define NS_INT16SZ 2#define NS_INADDRSZ 4#if INSTALL_on_winsockconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;#endif/* int * inet_pton4(src, dst) * like inet_aton() but without all the hexadecimal and shorthand. * return: * 1 if `src' is a valid dotted quad, else 0. * notice: * does not touch `dst' unless it's returning 1. * author: * Paul Vixie, 1996. */static intinet_pton4(src, dst) const char *src; u_char *dst;{ static const char digits[] = "0123456789"; int saw_digit, octets, ch; u_char tmp[NS_INADDRSZ], *tp; saw_digit = 0; octets = 0; *(tp = tmp) = 0; while ((ch = *src++) != '\0') { const char *pch; if ((pch = strchr(digits, ch)) != NULL) { u_int new = *tp * 10 + (pch - digits); if (new > 255) return (0); *tp = new; if (! saw_digit) { if (++octets > 4) return (0); saw_digit = 1; } } else if (ch == '.' && saw_digit) { if (octets == 4) return (0); *++tp = 0; saw_digit = 0; } else return (0); } if (octets < 4) return (0); memcpy(dst, tmp, NS_INADDRSZ); return (1);}/* int * inet_pton6(src, dst) * convert presentation level address to network order binary form. * return: * 1 if `src' is a valid [RFC1884 2.2] address, else 0. * notice: * (1) does not touch `dst' unless it's returning 1. * (2) :: in a full address is silently ignored. * credit: * inspired by Mark Andrews. * author: * Paul Vixie, 1996. */static intinet_pton6(src, dst) const char *src; u_char *dst;{ static const char xdigits_l[] = "0123456789abcdef", xdigits_u[] = "0123456789ABCDEF"; u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; const char *xdigits, *curtok; int ch, saw_xdigit; u_int val; memset((tp = tmp), '\0', NS_IN6ADDRSZ); endp = tp + NS_IN6ADDRSZ; colonp = NULL; /* Leading :: requires some special handling. */ if (*src == ':') if (*++src != ':') return (0); curtok = src; saw_xdigit = 0; val = 0; while ((ch = *src++) != '\0') { const char *pch; if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) pch = strchr((xdigits = xdigits_u), ch); if (pch != NULL) { val <<= 4; val |= (pch - xdigits); if (val > 0xffff) return (0); saw_xdigit = 1; continue; } if (ch == ':') { curtok = src; if (!saw_xdigit) { if (colonp) return (0); colonp = tp; continue; } if (tp + NS_INT16SZ > endp) return (0); *tp++ = (u_char) (val >> 8) & 0xff; *tp++ = (u_char) val & 0xff; saw_xdigit = 0; val = 0; continue; } if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && inet_pton4(curtok, tp) > 0) { tp += NS_INADDRSZ; saw_xdigit = 0; break; /* '\0' was seen by inet_pton4(). */ } return (0); } if (saw_xdigit) { if (tp + NS_INT16SZ > endp) return (0); *tp++ = (u_char) (val >> 8) & 0xff; *tp++ = (u_char) val & 0xff; } if (colonp != NULL) { /* * Since some memmove()'s erroneously fail to handle * overlapping regions, we'll do the shift by hand. */ const int n = tp - colonp; int i; for (i = 1; i <= n; i++) { endp[- i] = colonp[n - i]; colonp[n - i] = 0; } tp = endp; } if (tp != endp) return (0); memcpy(dst, tmp, NS_IN6ADDRSZ); return (1);}void md_hostname_to_v6_addr(struct sty *sty, char *name, void (*answer)(struct sty *, char *, ipaddr_t *)){#if INSTALL_on_winsock /* Need a routine that returns IP6 addr given name */#else struct hostent *host; int errorcode;#endif ipaddr_t v6_dest_addr; int i; int isName = 0; v6_dest_addr.type = IPV6; for (i = 0; name[i] == '\0'; i++) if (!isxdigit(name[i]) && (name[i] != ':')) { isName = 1; break; } /* If we found nothing but digits and colons, assume it's an address. */ if ((isName == 0) || (name[i] == '\0')) { inet_pton6(name, &v6_dest_addr.addr); if (memcmp(&(v6_dest_addr.addr), &in6addr_any, sizeof(in6addr_any)) == 0) (*answer)(sty, name, 0); else (*answer)(sty, name, &v6_dest_addr); } else (*answer)(sty, name, 0);#if INSTALL_on_winsock /* Need a routine that returns IP6 addr given name */#else else { if ((host = getipnodebyname(name, AF_INET6, AI_DEFAULT, &errorcode)) != 0) { memcpy(&(v6_dest_addr.addr), host->h_addr_list[0], sizeof(struct in6_addr)); if (memcmp(&(v6_dest_addr.addr), &in6addr_any, sizeof(struct in6_addr)) == 0) (*answer)(sty, name, 0); else (*answer)(sty, name, &v6_dest_addr); freehostent(host); } }#endif}#endifvoid md_hostname_to_addr(struct sty *sty, char *name, void (*answer)(struct sty *, char *, ipaddr_t *)){ struct hostent *host; ipaddr_t v4_dest_addr; bits32_t dest_ipv4; int i; v4_dest_addr.type = IPV4; for (i = 0; name[i] == '\0'; i++) if (!isdigit(name[i]) && (name[i] != '.')) break; /* If we found nothing but digits and dots, assume it's an address. */ if (name[i] == '\0') { dest_ipv4 = inet_addr(name); if (dest_ipv4 == 0) (*answer)(sty, name, 0); memcpy(&(v4_dest_addr.addr), &dest_ipv4, sizeof(dest_ipv4)); (*answer)(sty, name, &v4_dest_addr); } else { if ((host = gethostbyname(name)) != 0) { memcpy(&(v4_dest_addr.addr), host->h_addr, sizeof(bits32_t)); (*answer)(sty, name, &v4_dest_addr); } else (*answer)(sty, name, 0); }}/* * Code to implement snmp_send_rec(). */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -