This is a multi-part message in MIME format.
------=_NextPart_000_000E_01C85E74.051F3E10
Content-Type: text/plain;
format=flowed;
charset="iso-8859-1";
reply-type=original
Content-Transfer-Encoding: 7bit
Hi,
(.NET Framework 2.0 on Windows 2003 SP1)
I'm getting lots of 10004 WSAEINTR errors when I try to do some UDP
communication. From a packet capture I learned that my application is
responding with an ICMP Port Unreachable after the remote responds to my
query.
Example:
Me -> Remote: UDP request (valid request)
Remote -> Me: UDP response (valid response)
Me -> Remote: ICMP Port Unreachable (with a copy of the response
attached to the ICMP message)
Port numbers do match, so do my payload sequencenumbers.
My basicly code looks like this (removed irrelevant code):
socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, timeout);
bytesSend = socket.SendTo(buf, remoteEp);
bytesReceived = socket.ReceiveFrom(inData, inData.Length,
SocketFlags.None, ref sendingHost);
socket.Close();
I get the 10004 error once every 50 requests (could be more or lesss), all
other requests succeed without errors.
Can someone give me some more insight in why I am getting the WinSock 10004
errors?
I've attached the class which is causing the problems. The method in
question is called ExecRequest().
Regards,
Erik Tamminga
------=_NextPart_000_000E_01C85E74.051F3E10
Content-Type: text/plain;
format=flowed;
name="SnmpClient.cs";
reply-type=original
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="SnmpClient.cs"
public class SnmpClient
{
private const int SNMP_PORT =3D 161;
private string readCommunityName;
private string writeCommunityName;
private SnmpVersions version;
private IPAddress remoteHost;
private int timeout =3D 3500; // 3.5 seconds
private int requestId =3D 1;
private int retries =3D 3;
private int port =3D SNMP_PORT;
private bool isOpen =3D false; // Connection state, opens on =
first request or call to Open(), closes on destructor or call to Close()
private Socket socket =3D null;
private IPEndPoint remoteEp =3D null;
private int GetFirstRequestId()
{
Random r =3D new Random(Environment.TickCount);
return r.Next(9999);
}
public SnmpClient(SnmpAgentInfo agentInfo)
{
this.requestId =3D GetFirstRequestId();
this.version =3D agentInfo.Version;
this.remoteHost =3D agentInfo.AgentAddress;
this.readCommunityName =3D agentInfo.CommunityStringRead;
this.writeCommunityName =3D agentInfo.CommunityStringWrite;
}
public SnmpClient(IPAddress agentAddress)
{
this.requestId =3D GetFirstRequestId();
this.version =3D SnmpVersions.Version1;
this.remoteHost =3D new IPAddress(agentAddress);
this.readCommunityName =3D "public";
this.writeCommunityName =3D "public";
}
public SnmpClient(string agentAddress, string readCommunityName)
{
this.requestId =3D GetFirstRequestId();
this.version =3D SnmpVersions.Version1;
this.remoteHost =3D new IPAddress(agentAddress);
this.readCommunityName =3D readCommunityName;
this.writeCommunityName =3D readCommunityName;
}
public SnmpClient(string agentAddress, string readCommunityName, =
string writeCommunityName)
{
this.requestId =3D GetFirstRequestId();
this.version =3D SnmpVersions.Version1;
this.remoteHost =3D new IPAddress(agentAddress);
this.readCommunityName =3D readCommunityName;
this.writeCommunityName =3D writeCommunityName;
}
~SnmpClient()
{
if (isOpen)
Close();
}
public int Retries
{
get { return retries; }
set { retries =3D value; }
}
public int Port
{
get { return port; }
set { port =3D value; }
}
public SnmpVersions Version
{
get { return version; }
set { version =3D value; }
}
public IPAddress RemoteHost
{
get { return remoteHost; }
set { remoteHost =3D value; }
}
public string ReadCommunity
{
get { return readCommunityName; }
set { readCommunityName =3D value; }
}
public string WriteCommunity
{
get { return writeCommunityName; }
set { writeCommunityName =3D value; }
}
public int Timeout
{
get { return timeout; }
set
{
timeout =3D value;
if ((IsOpen) && (socket !=3D null))
socket.SetSocketOption(SocketOptionLevel.Socket, =
SocketOptionName.ReceiveTimeout, timeout);
}
}
public bool IsOpen
{
get { return isOpen; }
}
public void Open()
{
if (IsOpen)
throw new SnmpException(SnmpErrors.General, "Unable to =
start a new session because an other session is already active.");
bool a =3D false;
bool b =3D false;
bool c =3D false;
bool d =3D false;
bool e =3D false;
bool f =3D false;
try
{
a =3D true;
// Create a new Udp Socket
socket =3D new Socket(AddressFamily.InterNetwork, =
SocketType.Dgram, ProtocolType.Udp);
b =3D true;
//IPEndPoint localEp =3D new =
IPEndPoint(System.Net.IPAddress.Any, 0);
c =3D true;
//socket.Bind(localEp);
d =3D true;
// Set timeout parameter on socket
socket.SetSocketOption(SocketOptionLevel.Socket, =
SocketOptionName.ReceiveTimeout, timeout);
e =3D true;
remoteEp =3D new IPEndPoint(remoteHost.NetIPAddress, =
port);
f =3D true;
isOpen =3D true;
}
catch (SocketException ex)
{
socket =3D null;
remoteEp =3D null;
throw new SnmpException(SnmpErrors.General, =
string.Format("Unable to start a new session because of an unexpected =
error. NetIPAddress: {0}, ({1},{2},{3},{4},{5},{6}), ErrorCode {7}, =
Exception: {8}",
((remoteHost =3D=3D null) || =
(remoteHost.NetIPAddress =3D=3D null)) ? "(null)" : =
remoteHost.NetIPAddress.ToString(),
a, b, c, d, e, f,
ex.ErrorCode,
ex.ToString()), ex);
}
catch (Exception ex)
{
socket =3D null;
remoteEp =3D null;
throw new SnmpException(SnmpErrors.General, =
string.Format("Unable to start a new session because of an unexpected =
error. NetIPAddress: {0}, ({1},{2},{3},{4},{5},{6}), Exception: {7}",
((remoteHost =3D=3D null) || =
(remoteHost.NetIPAddress =3D=3D null)) ? "(null)" : =
remoteHost.NetIPAddress.ToString(),
a, b, c, d, e, f,
ex.ToString()), ex);
}
}
public void Close()
{
try
{
if (socket !=3D null)
socket.Close();
}
finally
{
socket =3D null;
remoteEp =3D null;
isOpen =3D false;
}
}
private SnmpMibCollection ExecFindRequest(IPAddress network, =
SnmpPduRequest request)
{
if (request.Command !=3D SnmpPduCommands.GetRequest)
throw new SnmpException(SnmpErrors.NoAccess, "Cannot =
execute a Find request with the Set command");
SnmpMibCollection results =3D new SnmpMibCollection();
// Create a new Udp Socket
Socket s =3D new Socket(AddressFamily.InterNetwork, =
SocketType.Dgram, ProtocolType.Udp);
try
{
IPEndPoint localEp =3D new =
IPEndPoint(System.Net.IPAddress.Any, 0);
s.Bind(localEp);
// Set timeout parameter on socket
s.SetSocketOption(SocketOptionLevel.Socket, =
SocketOptionName.ReceiveTimeout, timeout);
IPEndPoint remoteEp =3D new =
IPEndPoint(network.Broadcast.NetIPAddress, port);
// Determine community name
string community =3D readCommunityName;
if (request.Command =3D=3D SnmpPduCommands.SetRequest)
community =3D writeCommunityName;
// Encode message and sent to agent
SnmpMessage requestMsg =3D new SnmpMessage(version, =
community, ++requestId, request);
byte[] buf =3D requestMsg.Encode();
int attempts =3D 0;
int bytesReceived =3D 0;
bool receiveTimeout =3D false;
byte[] inData =3D null;
do
{
// Send data to agent
attempts++;
int bytesSend =3D s.SendTo(buf, remoteEp);
// Receive data from agent
inData =3D new byte[16 * 1024];
EndPoint sendingHost =3D remoteEp;
try
{
while (!receiveTimeout)
{
// Receive data from agent
bytesReceived =3D s.ReceiveFrom(inData, =
inData.Length, SocketFlags.None, ref sendingHost);
if (bytesReceived > 0)
{
// Handle received data
remoteHost =3D new =
IPAddress(remoteEp.Address);
SnmpMessage responseMsg =3D new =
SnmpMessage();
responseMsg.Decode(inData, =
bytesReceived);
if (responseMsg.Pdu is SnmpPduRequest)
{
SnmpPduRequest responsePdu =3D =
(SnmpPduRequest)responseMsg.Pdu;
// ET: Test to see if this works =
without the error-reset responsePdu.Fix();
if (responsePdu.ErrorStatus !=3D 0)
{
SnmpErrors err =3D =
(SnmpErrors)responsePdu.ErrorStatus;
throw new SnmpException(err, =
Enum.GetName(typeof(SnmpErrors), err));
}
// Add the response to the response =
list
if (responsePdu.VarBinds.Count > 0)
results.Add(new =
SnmpMib(responsePdu.VarBinds[0].Name, responsePdu.VarBinds[0].Value, =
remoteHost));
}
}
}
}
catch (SocketException se)
{
// ReceiveFrom: Error. Might be a timeout
if (se.ErrorCode =3D=3D 10060)
receiveTimeout =3D true;
// Other Error, bubble up
throw se;
}
}
while ((attempts <=3D retries) && !receiveTimeout); // =
Retry until retries reached
s.Close();
return results;
}
catch (SocketException se)
{
// ReceiveFrom: Error. Might be a timeout
if (se.ErrorCode =3D=3D 10060)
{
s.Close();
return null;
}
// Other Error, bubble up
throw se;
}
}
private SnmpPdu ExecRequest(SnmpPdu request)
{
// Do not allow snmp-requests to broadcast addresses (That's =
why the Find() method exists)
if (remoteHost.IsBroadcast)
throw new ApplicationException("Cannot Query a broadcast =
address. Try finding.");
if (!IsOpen)
Open();
try
{
// Determine community name
string community =3D readCommunityName;
if (request.Command =3D=3D SnmpPduCommands.SetRequest)
community =3D writeCommunityName;
// Encode message and sent to agent
SnmpMessage requestMsg =3D new SnmpMessage(version, =
community, ++requestId, request);
byte[] buf =3D requestMsg.Encode();
int attempts =3D 0;
bool success =3D false;
bool timeOut =3D false;
byte[] inData =3D new byte[16 * 1024];
int bytesSend;
int bytesReceived;
do
{
// Send data to agent
attempts++;
bool beforeSend =3D true;
bool beforeReceive =3D true;
bytesSend =3D 0;
bytesReceived =3D 0;
try
{
bytesSend =3D socket.SendTo(buf, remoteEp);
beforeSend =3D false;
EndPoint sendingHost =3D remoteEp;
// Receive data from agent
bytesReceived =3D socket.ReceiveFrom(inData, =
inData.Length, SocketFlags.None, ref sendingHost);
beforeReceive =3D false;
success =3D (bytesReceived > 0);
}
catch (SocketException se)
{
// ReceiveFrom: Error. Might be a timeout
if (se.ErrorCode =3D=3D 10060)
{
// Timeout, no response: Retry
if (attempts >=3D retries)
timeOut =3D true;
}
else if (se.ErrorCode =3D=3D 10054)
{
// An existing connection was forcibly =
closed by the remote host
// Remote host doesn't listen to this port, =
do not try again
attempts =3D retries + 1;
Close();
throw new SnmpException(SnmpErrors.NoAccess, =
string.Format("SNMP Request failed, remote port not available. {0} {1}", =
se.ErrorCode, se.Message), se);
}
else if (se.ErrorCode =3D=3D 10004) // WSAEINTR
{
// Why do we see these errors?
=
NmsTrace.WriteLineIf(NmsTraceSwitch.TraceInfo, string.Format("SNMP =
Request failed, Error 10004 (WSAEINTR) received. Before Send {0}, Before =
Receive {1}, Bytes Send {2}, Bytes Received {3}, Exception {4}", =
beforeSend, beforeReceive, bytesSend, bytesReceived, se.ToString()));
if (attempts >=3D retries)
timeOut =3D true;
else
{
=
NmsTrace.WriteLineIf(NmsTraceSwitch.TraceInfo, "Closing socket and =
opening a new socket");
Thread.Sleep(250);
Close();
Thread.Sleep(250);
Open();
}
}
else
{
// Do not try again
attempts =3D retries + 1;
Close();
// Other Error, bubble up
throw new SnmpException(SnmpErrors.General, =
string.Format("SNMP Request failed. Socket Exception {0} {1}", =
se.ErrorCode, se.Message), se);
}
}
}
while ((attempts < retries) && !success); // Retry until =
retries reached
if (success)
{
// Handle received data
remoteHost =3D new IPAddress(remoteEp.Address);
SnmpMessage responseMsg =3D new SnmpMessage();
responseMsg.Decode(inData, bytesReceived);
if (responseMsg.Pdu is SnmpPduRequest)
{
SnmpPduRequest responsePdu =3D =
(SnmpPduRequest)responseMsg.Pdu;
// responsePdu.Fix();
if (responsePdu.ErrorStatus !=3D 0)
{
SnmpErrors err =3D =
(SnmpErrors)responsePdu.ErrorStatus;
throw new SnmpException(err, =
Enum.GetName(typeof(SnmpErrors), err));
}
return responsePdu;
}
else
throw new SnmpException(SnmpErrors.General, =
string.Format("Unknown response message. Expected SnmpPduRequest. I =
tried {0} times. Host=3D{1}", attempts, =
remoteHost.ToString(IPAddressMaskFormat.NoMask)));
}
if (timeOut)
throw new SnmpException(SnmpErrors.General, =
string.Format("Unable to execute the request, timeout occured while =
waiting for the response. I tried {0} times. Host=3D{1}", attempts, =
remoteHost.ToString(IPAddressMaskFormat.NoMask)));
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
Close();
throw new SnmpException(SnmpErrors.General, "Unable to =
execute the request. Possible cause, timeout or host actively refused =
request.", ex);
}
throw new SnmpException(SnmpErrors.General, =
string.Format("Unknown state while executing the request. Host=3D{0}", =
remoteHost.ToString(IPAddressMaskFormat.NoMask)));
}
public SnmpMib Get(string oid)
{
try
{
SnmpPduRequest req =3D new =
SnmpPduRequest(SnmpPduCommands.GetRequest);
req.VarBinds.Add(new SnmpVarBind(new SnmpObjectId(oid), =
new SnmpNull()));
SnmpPdu responsePdu =3D ExecRequest(req);
if ((responsePdu !=3D null) && =
(responsePdu.VarBinds.Count > 0))
{
SnmpVarBind v =3D responsePdu.VarBinds[0];
return new SnmpMib(v.Name, v.Value, remoteHost);
}
return null;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.Get( {0}) {1}: Unable to get the requested =
oid. Message: {2}", oid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMibCollection Get(string[] oids)
{
try
{
SnmpMibCollection result =3D new SnmpMibCollection();
SnmpPduRequest req =3D new =
SnmpPduRequest(SnmpPduCommands.GetRequest);
foreach (string oid in oids)
req.VarBinds.Add(new SnmpVarBind(new =
SnmpObjectId(oid), new SnmpNull()));
SnmpPdu responsePdu =3D ExecRequest(req);
if ((responsePdu !=3D null) && =
(responsePdu.VarBinds.Count > 0))
{
foreach (SnmpVarBind v in responsePdu.VarBinds)
result.Add(new SnmpMib(v.Name, v.Value, =
remoteHost));
}
return result;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.Get( {0}) {1}: Unable to get the requested =
oid. Message: {2}", oids, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMib[] GetBulk(string[] oids, int nonRepeaters, int =
repeaters)
{
try
{
if (version =3D=3D SnmpVersions.Version1)
throw new SnmpException(SnmpErrors.General, =
string.Format("Bulk requests not possibile on v1 snmp agents.", =
remoteHost.ToString(IPAddressMaskFormat.NoMask)));
SnmpPduRequest req =3D new =
SnmpPduRequest(SnmpPduCommands.GetBulkRequest);
foreach (string oid in oids)
req.VarBinds.Add(new SnmpVarBind(new =
SnmpObjectId(oid), new SnmpNull()));
// Set nonrepeaters and repeaters
req.ErrorStatus =3D nonRepeaters;
req.ErrorIndex =3D repeaters;
SnmpPdu responsePdu =3D ExecRequest(req);
if ((responsePdu !=3D null) && =
(responsePdu.VarBinds.Count > 0))
{
ArrayList results =3D new ArrayList();
foreach (SnmpVarBind v in responsePdu.VarBinds)
results.Add(new SnmpMib(v.Name, v.Value, =
remoteHost));
return (SnmpMib[])results.ToArray(typeof(SnmpMib));
}
return null;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.GetBulk( {0}, {1}, {2}) {3}: Unable to get the =
requested oids. Message: {4}", oids, nonRepeaters, repeaters, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMib Set(string oid, ISnmpSyntax value, SMIDataTypes =
valueType)
{
try
{
if (writeCommunityName =3D=3D "")
throw new SnmpException(SnmpErrors.NotWritable, =
"Cannot set a value if the \"Write community name\" is unknown.");
SnmpPduRequest req =3D new =
SnmpPduRequest(SnmpPduCommands.SetRequest);
req.VarBinds.Add(new SnmpVarBind(new SnmpObjectId(oid), =
(ISnmpSyntax)value.Clone()));
SnmpPdu responsePdu =3D ExecRequest(req);
if ((responsePdu !=3D null) && =
(responsePdu.VarBinds.Count > 0))
{
SnmpVarBind v =3D responsePdu.VarBinds[0];
return new SnmpMib(v.Name, v.Value, remoteHost);
}
return null;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.Set( {0}, {1}) {2}: Unable to set the =
requested oid. Message: {3}", value, valueType, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMib GetNext(string oid)
{
try
{
SnmpPduRequest req =3D new =
SnmpPduRequest(SnmpPduCommands.GetNextRequest);
req.VarBinds.Add(new SnmpVarBind(new SnmpObjectId(oid), =
new SnmpNull()));
SnmpPdu responsePdu =3D ExecRequest(req);
if ((responsePdu !=3D null) && =
(responsePdu.VarBinds.Count > 0))
{
SnmpVarBind v =3D responsePdu.VarBinds[0];
return new SnmpMib(v.Name, v.Value, remoteHost);
}
return null;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.GetNext( {0}) {1}: Unable to get the requested =
oid. Message: {2}", oid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
private SnmpMibCollection GetTableData(string tableOid)
{
try
{
SnmpMibCollection result =3D new SnmpMibCollection();
if (Version =3D=3D SnmpVersions.Version1)
{
string tableOidPrefix =3D tableOid + ".";
SnmpMib mib =3D GetNext(tableOid);
while ((mib !=3D null) && !(mib.ObjectValue is =
SnmpEndOfMibView) && =
mib.ObjectId.StringValue.StartsWith(tableOidPrefix))
{
result.Add(mib);
mib =3D GetNext(mib.ObjectId.StringValue);
}
}
else
{
// ET: Some agents do not support GetBulk with =
invalid oids (and return the next available oids)
// So, we need to get the first valid Oid and have =
GetBulk do the rest!! (VPN Concentrator)
result.Add(GetNext(tableOid));
// result.AddRange( GetBulk( new string[] { =
mib.ObjectId.StringValue}, 0, 15));
if (result.Count > 0)
{
int count =3D result.Count;
int lastCount =3D 0;
SnmpMib lastMib =3D result[count - 1];
string lastOid =3D lastMib.ObjectId.StringValue;
bool revertToGetNext =3D false;
while (lastOid.StartsWith(tableOid) && =
!(lastMib.ObjectValue is Snmp.SnmpEndOfMibView) && (lastCount !=3D =
count))
{
lastCount =3D count;
if (!revertToGetNext)
{
SnmpMib[] fetchedMibs =3D GetBulk(new =
string[] { lastOid }, 0, 15);
if (fetchedMibs.Length =3D=3D 1)
revertToGetNext =3D true; // =
Apparently GetBulk only returns 1 oid, this doesn help us much
else
result.AddRange(fetchedMibs);
}
if (revertToGetNext)
{
SnmpMib mib =3D GetNext(lastOid);
if (mib.ObjectId.StringValue =3D=3D =
lastOid)
throw new =
NmsException("SnmpGetNext() found the current oid instead of the next =
oid.");
result.Add(mib);
}
count =3D result.Count;
lastMib =3D result[count - 1];
lastOid =3D lastMib.ObjectId.StringValue;
}
}
while ((result.Count > 0) &&
((result[result.Count - 1].ObjectValue is =
SnmpEndOfMibView) ||
!result[result.Count - =
1].ObjectId.StringValue.StartsWith(tableOid)))
result.RemoveAt(result.Count - 1);
}
return result;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.GetTableData( {0}) {1}: Unable to get the =
requested table. Message: {2}", tableOid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
private SnmpMibTable GetTable(string tableOid, SnmpMibCollection =
tableData)
{
try
{
SnmpMibTable result =3D new SnmpMibTable(tableOid);
string oid =3D tableOid;
string lastCol =3D "";
int x =3D 0;
bool inTable =3D true;
SnmpMib mib =3D null;
int index =3D 0;
do
{
//mib =3D GetNext( oid);
mib =3D null;
if (index < tableData.Count)
mib =3D tableData[index];
if ((mib !=3D null) && (mib.ObjectValue is =
SnmpEndOfMibView))
{
inTable =3D false;
}
else if ((mib !=3D null) && !(mib.ObjectValue is =
SnmpEndOfMibView))
{
oid =3D mib.ObjectId.StringValue;
inTable =3D oid.StartsWith(tableOid + ".");
if (inTable)
{
string row =3D oid.Substring(tableOid.Length =
+ 1);
int i =3D row.IndexOf(".");
string col =3D row.Substring(0, i);
row =3D row.Substring(i + 1);
if (col =3D=3D lastCol)
{
x++;
}
else
{
x =3D 0;
}
int y =3D int.Parse(col);
lastCol =3D col;
while (result.Rows.Count <=3D x)
result.Rows.Add(new =
SnmpMibTableRow(result.TableBaseOidLength));
SnmpMibTableRow r =3D =
(SnmpMibTableRow)result.Rows[x];
while (r.Columns.Count < (y - 1))
{
r.Columns.Add(SnmpMib.Empty);
}
r.Columns.Add(mib);
}
}
index++;
}
while (inTable && (mib !=3D null) && (index < =
tableData.Count));
return result;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.GetTable( {0}) {1}: Unable to get the =
requested table. Message: {2}", tableOid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMibTable GetTable(string tableOid, string entryId)
{
try
{
SnmpMibCollection tableData =3D GetTableData(tableOid);
tableOid =3D tableOid + "." + entryId;
return GetTable(tableOid, tableData);
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.GetTable( {0}) {1}: Unable to get the =
requested table. Message: {2}", tableOid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMibTable GetTable(string tableOid)
{
try
{
SnmpMibCollection tableData =3D GetTableData(tableOid);
return GetTable(tableOid, tableData);
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.GetTable( {0}) {1}: Unable to get the =
requested table. Message: {2}", tableOid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMibCollection Walk(string oid)
{
try
{
SnmpMibCollection result =3D GetTableData(oid);
return result;
}
catch (SnmpException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new SnmpException(SnmpErrors.General, =
string.Format("SnmpClient.Walk( {0}) {1}: Unable to get the requested =
table. Message: {2}", oid, =
remoteHost.ToString(IPAddressMaskFormat.NoMask), ex.Message), ex);
}
}
public SnmpMib GetTableElement(string tableOid, string rowOid, =
string columnOid)
{
string oid =3D tableOid + "." + columnOid + "." + rowOid;
return Get(oid);
}
public SnmpAgentInfo GetAgentInfo(SnmpCredentials[] =
possibleCredentials)
{
bool accessDenied =3D false;
for (int i =3D 0; (i < possibleCredentials.Length) && =
!accessDenied; i++)
{
SnmpCredentials credentials =3D possibleCredentials[i];
Version =3D credentials.Version;
try
{
ReadCommunity =3D credentials.ReadCommunity;
SnmpMib mib =3D Get(MIB2.sysName);
if ((mib !=3D null) && !(mib.ObjectValue is =
SnmpNoSuchObject))
return new SnmpAgentInfo(remoteHost, Version, =
ReadCommunity, mib.ObjectValue.StringValue);
}
catch (SnmpException snmpEx)
{
NmsTrace.WriteLineIf(NmsTraceSwitch.TraceVerbose, =
"Unable to snmp-connect to host {0} with community {1}, version {2}. =
Message=3D{3}", remoteHost.ToString(IPAddressMaskFormat.NoMask), =
credentials.ReadCommunity, credentials.Version, snmpEx.Message);
if (snmpEx.ErrorCode =3D=3D SnmpErrors.NoAccess)
accessDenied =3D true;
}
}
return null;
}
public SnmpAgentInfo GetAgentInfo(string[] communities)
{
foreach (string community in communities)
{
Version =3D SnmpVersions.Version2c;
try
{
ReadCommunity =3D community;
SnmpMib mib =3D Get(MIB2.sysName);
if ((mib !=3D null) && !(mib.ObjectValue is =
SnmpNoSuchObject))
return new SnmpAgentInfo(remoteHost, Version, =
ReadCommunity, mib.ObjectValue.StringValue);
}
catch (SnmpException snmpEx)
{
NmsTrace.WriteLineIf(NmsTraceSwitch.TraceVerbose, =
"Unable to snmp-connect to host {0} with community {1}, version {2}. =
Message=3D{3}", remoteHost.ToString(IPAddressMaskFormat.NoMask), =
community, Version, snmpEx.Message);
}
Version =3D SnmpVersions.Version1;
try
{
ReadCommunity =3D community;
SnmpMib mib =3D Get(MIB2.sysName);
if ((mib !=3D null) && !(mib.ObjectValue is =
SnmpNoSuchObject))
return new SnmpAgentInfo(remoteHost, Version, =
ReadCommunity, mib.ObjectValue.StringValue);
}
catch (SnmpException snmpEx)
{
NmsTrace.WriteLineIf(NmsTraceSwitch.TraceVerbose, =
"Unable to snmp-connect to host {0} with community {1}, version {2}. =
Message=3D{3}", remoteHost.ToString(IPAddressMaskFormat.NoMask), =
community, Version, snmpEx.Message);
}
}
return null;
}
}
------=_NextPart_000_000E_01C85E74.051F3E10--