How Linux socket traffic gets the local source port number

Source: Internet
Author: User
Tags connect socket

There is a lot of information about TCP IP network traffic, and TCP IP is communicating end-to-end through IP packet mode. A typical TCP packet is as follows
You can see that the packet contains the source port number and the destination port number, when the client socket initiates a connection to the server, the system will randomly assign a source port number to the socket, and we can obtain the original port information of the socket successfully connected via getsocketname. Function prototypes [CPP]View PlainCopy
    1. #include <sys/socket.h>
    2. int getsockname (int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Parameters:
SOCKFD Socket Connection Handle
Addr network address pointer, used to store the local port socket address information,
Addrlen addr size of the space
Returns the result, if the call succeeds, returns 0, and stores the local network address information inside the addr, fails to return 1, and responds with an error message via errno. Source_port.cpp [CPP]View PlainCopy
  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <netinet/ip.h>
  8. #include <netdb.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <arpa/inet.h>
  12. void Safe_close (int &sock);
  13. int main (int argc, char *argv[]) {
  14. int sockfd = 0, n = 0;
  15. Socklen_t len = 0;
  16. char host[512] = {0};
  17. char buf[1024] = {0};
  18. struct hostent *server;
  19. struct sockaddr_in serv_addr, loc_addr;
  20. if (argc < 2) {
  21. printf ("Please input host name\n");
  22. Exit (-1);
  23. }
  24. strncpy (host, argv[1], sizeof (host));
  25. Server = gethostbyname (host); //Determine if the domain name entered is correct
  26. if (NULL = = Server) {
  27. printf ("Find host:%s failed.\n", host);
  28. Exit (-1);
  29. }
  30. if ( -1 = = (SOCKFD = socket (af_inet, sock_stream, 0)) {//create socket
  31. memset (buf, 0, sizeof (BUF));
  32. snprintf (buf, sizeof (BUF), "new socket failed.  errno:%d, Error:%s ", errno, Strerror (errno));
  33. Perror (BUF);
  34. Exit (-1);
  35. }
  36. memset (&serv_addr, 0, sizeof (SERV_ADDR));
  37. serv_addr.sin_family = af_inet;
  38. Serv_addr.sin_port = htons (80); //HTTP standard port number
  39. memcpy (&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
  40. if ( -1 = = Inet_pton (af_inet, Host, &serv_addr.sin_addr)) {
  41. memset (buf, 0, sizeof (BUF));
  42. snprintf (buf, sizeof (BUF), "Inet_pton failed."  errno:%d, Error:%s ", errno, Strerror (errno));
  43. Perror (BUF);
  44. Exit (-1);
  45. }
  46. if ( -1 = = Connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (SERV_ADDR))) {//Connect socket
  47. memset (buf, 0, sizeof (BUF));
  48. snprintf (buf, sizeof (BUF), "Connect socket failed.  errno:%d, Error:%s ", errno, Strerror (errno));
  49. Perror (BUF);
  50. Exit (-1);
  51. }
  52. printf ("Connect to%s success.\n", host);
  53. Len = sizeof (sizeof (LOC_ADDR));
  54. memset (&loc_addr, 0, Len);
  55. if ( -1 = = GetSockName (sockfd, (struct sockaddr *) &loc_addr, &len)){//Get local address information for socket bindings /c4>
  56. memset (buf, 0, sizeof (BUF));
  57. snprintf (buf, sizeof (BUF), "Get socket name failed.  errno:%d, Error:%s ", errno, Strerror (errno));
  58. Perror (BUF);
  59. Safe_close (SOCKFD);
  60. Exit (-1);
  61. }
  62. if (loc_addr.sin_family = = af_inet) {//print information
  63. printf ("Local port:%u\n", Ntohs (Loc_addr.sin_port));
  64. }
  65. Safe_close (SOCKFD);
  66. return 0;
  67. }
  68. void Safe_close (int &sock) {
  69. if ( -1! = sock) {
  70. Shutdown (sock, Shut_rdwr);
  71. sock =-1;
  72. }
  73. }
This program first initiates a socket connection to a normal HTTP server (BAIDU,QQ,163,CSDN), and when the socket is connected, it obtains the local address of the connection binding via Getsocketname, and obtains the source port number through that address.
Terminal 1: Compiling and running
$ g++ Source_port.cpp
$./a.out www.baidu.com
Connect to www.baidu.com success.
Local port:39702
Terminal 2: Verification via tcpdump capture package
$ sudo tcpdump host Www.baidu.com-v
Tcpdump:listening on eth0, Link-type EN10MB (Ethernet), capture size 65535 bytes
18:38:32.381448 IP (Tos 0x0, TTL-up, id 35033, offset 0, flags [DF], Proto TCP (6), length 60)
icentos.39702 > 220.181.111.188.http:flags [S], cksum 0x8cd2 (Incorrect-0x596a), Seq 2381397554, Win 29200, Opti ONS [MSS 1460,sackok,ts val 3513497323 ECR 0,nop,wscale 7], length 0
18:38:32.425904 IP (Tos 0x0, TTL, id 35033, offset 0, flags [DF], Proto TCP (6), length 60)
220.181.111.188.http > Icentos.39702:flags [S.], cksum 0xc315 (correct), seq 3561856904, Ack 2381397555, win 8192, opt ions [MSS 1424,sackok,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,wscale 5], length 0
18:38:32.425930 IP (Tos 0x0, TTL-up, id 35034, offset 0, flags [DF], Proto TCP (6), length 40)

Comparing terminal One and terminal two indicates that the source port address obtained is correct.

How Linux socket traffic gets the local source port number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.