Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I convert an integer value representing a specific TCP or UDP port into the correct 2-byte value in C?

Posted on 2007-07-24
5
Medium Priority
?
220 Views
Last Modified: 2010-04-01
How do I convert an integer value representing a specific TCP or UDP port into the correct 2-byte value in C?
0
Comment
Question by:jchristn123
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 19563222
Just use :

        htons(port)
0
 
LVL 1

Author Comment

by:jchristn123
ID: 19563237
Hi infinity08,

I tried this expecting that it would put the data into a 2-byte string:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>

int main(int nArg, char* pszArgs[])
{
  int portnum;
  portnum=14392;
  char portnumhtons[3];
  portnumhtons=htons(portnum);
  printf("portnumhtons=%s\n",portnumhtons);
  return 0;
}

but of course, the result was:

htons.c: In function 'main':
htons.c:16: error: incompatible types in assignment

and when I try with portnumhtons as an int, i.e.:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>

int main(int nArg, char* pszArgs[])
{
  int portnum;
  portnum=14392;
  int portnumhtons;
  portnumhtons=htons(portnum);
  printf("portnumhtons=%d\n",portnumhtons);
  return 0;
}

I get:

portnumhtons=14392

Which doesn't convert to a 2-byte value.  I'm looking for the 2-byte representation of the integer port number in char array format if possible.

Thanks for all of your help!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19563283
>> I tried this expecting that it would put the data into a 2-byte string:

No, it returns a 16 bit value that represents the port number in network byte order.

If you want, you can access the individual bytes like this :

        int portnum = 14392;
        unsigned short port_nbo = htons(portnum);
        printf("first byte = %02x\nsecond byte = %02x\n", ((unsigned char*) &port_nbo)[0], ((unsigned char*) &port_nbo)[1]);

But I don't see why ...

If you just want to print the port number, then just do :

        int portnum = 14392;
        printf("port = %d\n", portnum);

Or if you have it in network byte order :

        int portnum = 14392;
        unsigned short port_nbo = htons(portnum);
        printf("port = %d\n", (int) ntohs(port_nbo));
0
 
LVL 1

Author Comment

by:jchristn123
ID: 19563311
Hi infinity08,

Perhaps some clarification on what I'm wanting to do will help.  I need to embed a return port number into the datagram being sent - not the same as the source port that is specified in the datagram header.  The port number needs to be 2 bytes, and the recipient will receive the datagram, parse through the data portion, and extract the two bytes that identify the return port number to which it should reply, which again is not the same port number used as the source port in the original datagram.

Thus, if I want to embed 14392 as the return port number (even though the source port number in the datagram header is different) as a 2-byte character, I need to convert it first to the 2-byte character.

Does that help?

Thanks again
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 19563384
Then you can just do :

        int portnum = 14392;
        unsigned short port_nbo = htons(portnum);

to get the port in network byte order, and then do something like :

        unsigned char datagram[1024]; // <--- your datagram (might be defined differently)
        unsigned char * dg_ptr = datagram; // <--- points to the current location inside the datagram
        // fill up the datagram with everything before the port number

        // dg_ptr now points to the position in the datagram where the port number has to come, so we place it there :
        memcpy(dg_ptr, &port_nbo, 2);
        dg_ptr += 2;

        // fill up the datagram with everything after the port number

to copy it into the datagram.


Or any other way you like to do the same thing.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question