Re: How to convert a port number (_be16) to a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



El Wed, Mar 18, 2009 at 02:59:28AM +0800 Cliffe ha dit:

> I have the destination port from a struct socket and I need to convert  
> it to a string.
>
>        struct inet_sock *inet = inet_sk(sock->sk);
>        unsigned int sport = inet->sport;
>
> I have tried using snprintf with %d, and %lu but I am not getting the  
> values I expect. For example port 80 is displayed as 20480.

the problem is that the port in inet->sport is stored in big endian
format, while your system seems to be little
endian (80d == 0x50, 20480d == 0x5000).

be16_to_cpu(inet->sport) should do the trick

-- 
Matthias Kaehlcke
Embedded Linux Engineer
Barcelona

     Nationalism is an infantile disease. It is the measles of mankind
                         (Albert Einstein)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux