[snmp] TrapPduv1.getIpAddress() returns negative values

Jordi Vea i Barbany vea at inicia.es
Tue Nov 7 00:19:47 GMT 2006


You are right. The stack wasn't the problem. The thing is that you can't 
cast byte to int so easily.

To represent it as a String I was doing:

    byte[] addr = trap.getIpAddress();
    String agentAddr = addr[0] + "." + addr[1] + "." + addr[2] + "." + 
addr[3];

Doing the following solves the problem:

    byte[] addr = trap.getIpAddress();
    String agentAddr = ( addr[0] & 0xFF ) + "." + ( addr[1] & 0xFF ) + 
"." + ( addr[2] & 0xFF ) + "." + ( addr[3] & 0xFF ) ;

Thanks a lot.

Jordi Vea


Josh Bers wrote:

>This may be an issues with Java and not with the stack. Since Java assumes
>all byte variables represent signed integers if you print them as such they
>will appear as negative values, which would explain why you see
>-64.-88.10.60. This is in fact the correct IP address (192.168.10.60). So it
>sounds like your problem may just be one of representing the ip addresses as
>strings. What function are you using to print them out?
>
>Josh Bers
>Mobile Networking Systems
>BBN Technologies
>jbers at bbn.com
>ph. 617.873.4262
>fax. 617.873.4523
>www.bbn.com
>
>
>  
>
>>-----Original Message-----
>>From: snmp-bounces at snmp.westhawk.co.uk 
>>[mailto:snmp-bounces at snmp.westhawk.co.uk] On Behalf Of Birgit 
>>Arkesteijn
>>Sent: Monday, November 06, 2006 8:08 AM
>>To: List for discussion of the Westhawk SNMP stack
>>Subject: Re: [snmp] TrapPduv1.getIpAddress() returns negative values
>>
>>
>>Hi Jordi,
>>
>>I've never seen this behaviour before.
>>
>>I just run the snmpv1 examples ReceiveTrap (on one machine) & 
>>SendTrap (on the other machine) against each other, using jdk 
>>1.4 on Linux, successfully.
>>
>>run 1:
>>trap id 1, v SNMPv1 from host 192.67.4.53, sent from port 33504
>>
>>run 2:
>>raw pdu v SNMPv1 from host 192.67.4.53, sent from port 33504
>>
>>
>>Which platform do you run the stack?
>>Which jdk do you use?
>>
>>Which IP address are you talking about?
>>- the TrapPduv1.getIpAddress() 
>>- TrapEvent.getPdu().getContext().getHost()
>>- something else
>>What do you use to print the IP address?
>>
>>Cheers, Birgit
>>
>>
>>On Sun, Nov 05, 2006 at 07:14:28PM +0100, Jordi Vea wrote:
>>    
>>
>>>Hello,
>>>
>>>I'm quite new using the stack (v 5.1), but I have detected a strange
>>>behaviour with the TrapPduv1.
>>>I'm receiving traps from a SNMP agent at the IP 192.168.10.60, but 
>>>the stack gets the IP as -64.-88.10.60. ( -64=192-256, -88=168-256 )
>>>I sniffed the SNMP PDU using Ethereal, and the Agent 
>>>      
>>>
>>Address was ok, 
>>    
>>
>>>so I think this might be a bug from the stack. It has an easy work 
>>>around, but has anyone detected this behaviour before?
>>>
>>>Thanks,
>>>Jordi Vea
>>>      
>>>
>>-- 
>>-- Birgit Arkesteijn, birgit at westhawk.co.uk,
>>-- Westhawk Ltd, Albion Wharf, 19 Albion Street, Manchester M1 5LN, UK
>>-- tel.: +44 (0)161 237 0660
>>-- <URL: http://www.westhawk.co.uk> 
>>_______________________________________________
>>snmp mailing list
>>snmp at snmp.westhawk.co.uk 
>>http://snmp.westhawk.co.uk/mailman/listinfo/snmp
>>
>>    
>>
>
>
>
>_______________________________________________
>snmp mailing list
>snmp at snmp.westhawk.co.uk
>http://snmp.westhawk.co.uk/mailman/listinfo/snmp
>
>  
>



More information about the snmp mailing list