Linux kernel--Network stack Implementation Analysis (ix)--UDP protocol for Transport layer (bottom)

Source: Internet
Author: User
Tags sin

This paper analyzes Linux Kernel 1.2.13 based on

Original works, reproduced please mark http://blog.csdn.net/yming0221/article/details/7549340

See column, Address http://blog.csdn.net/column/details/linux-kernel-net.html

Shuming

Note: In the title, "(UP)", "(bottom)" means that the analysis process is based on the delivery direction of the packet: "(top)" means that the analysis is parsed from the bottom up, and "(bottom)" indicates that the analysis is parsed from the top down.

In this paper, the application layer through the BSD socket layer to the INET socket layer function call relationship and data processing flow, the inet layer will invoke the specific transport layer protocol, or the UDP protocol as an example

Udp_write () function

[CPP]View Plaincopy
    1. static int udp_write (struct sock *sk, unsigned char *buff, int len, int noblock,
    2. Unsigned flags)
    3. {
    4. Return (Udp_sendto (SK, Buff, Len, Noblock, Flags, NULL, 0));
    5. }

Before analyzing the Udp_sendto () function, let's look at the sockaddr_in structure, which is the definition of the standard network interface address structure.

[CPP]View Plaincopy
  1. struct SOCKADDR_IN {
  2. short int sin_family; /* Address Family family */
  3. unsigned short int sin_port; /* Port number Port numbers */
  4. struct IN_ADDR sin_addr; /* Internet Address network addresses */
  5. /* Pad to size of ' struct sockaddr '. */
  6. unsigned char __pad[__sock_size__-sizeof (short int)-
  7. sizeof (unsigned short int)-sizeof (struct in_addr)];
  8. };
  9. #define Sin_zero __pad/* for BSD UNIX comp.-FVK */

Udp_sentdto () function

[CPP]View Plaincopy
  1. static int udp_sendto (struct sock *sk, unsigned char *from, int len, int noblock,
  2. Unsigned flags, struct sockaddr_in *usin, int addr_len)
  3. {
  4. struct sockaddr_in sin;
  5. int tmp;
  6. /*
  7. * Check the flags. WE Support No flags for UDP sending
  8. */
  9. if (Flags&~msg_dontroute)
  10. return (-einval);
  11. /*
  12. * Get and verify the address.
  13. */
  14. if (usin)//If usin is not empty
  15. {
  16. if (Addr_len < sizeof (sin))
  17. return (-einval);
  18. memcpy (&sin,usin,sizeof (sin));
  19. if (sin.sin_family && sin.sin_family! = af_inet)
  20. return (-einval);
  21. if (Sin.sin_port = = 0)
  22. return (-einval);
  23. }
  24. else//usin is empty
  25. {
  26. if (sk->state! = tcp_established)
  27. return (-einval);
  28. sin.sin_family = af_inet;//protocol Family
  29. Sin.sin_port = sk->dummy_th.dest;//Destination port
  30. SIN.SIN_ADDR.S_ADDR = sk->daddr;//Destination Address
  31. }
  32. /*
  33. * BSD socket semantics. You must set So_broadcast to permit
  34. * Broadcasting of data.
  35. */
  36. if (sin.sin_addr.s_addr==inaddr_any)//Destination address is a full 0 address, corresponding to the current host
  37. SIN.SIN_ADDR.S_ADDR=IP_MY_ADDR ();//Set the destination to the network address of the current host
  38. if (!sk->broadcast && ip_chk_addr (sin.sin_addr.s_addr) ==is_broadcast)
  39. Return-eacces; /* must turn broadcast on first */
  40. Sk->inuse = 1;
  41. /* Send the packet. */
  42. TMP = Udp_send (SK, &sin, from, Len, flags);//Call Udp_send () to actually send the data
  43. /* The datagram has been sent off. Release the socket. */
  44. Release_sock (SK);
  45. return (TMP);
  46. }

Udp_send () function

[CPP]View Plaincopy
  1. static int udp_send (struct sock *sk,//sock structure used for packets to be sent using the protocol pair
  2. The standard network interface address of the struct sockaddr_in *sin,//destination
  3. unsigned char *from,//the address of the data to be sent
  4. int len,//The length of the sent data
  5. int RT)
  6. {
  7. struct Sk_buff *skb;
  8. struct device *dev;
  9. struct UDPHDR *uh;
  10. unsigned char *buff;
  11. unsigned long saddr;
  12. int size, TMP;
  13. int ttl;
  14. /*
  15. * Allocate an sk_buff copy of the packet.
  16. */
  17. Size = Sk->prot->max_header + len;//Compute the longest header + data length for UDP
  18. SKB = SOCK_ALLOC_SEND_SKB (sk, size, 0, &tmp);//Allocate sk_buff structure space based on data to be sent and check for current socket status
  19. if (SKB = = NULL)
  20. return TMP;
  21. Skb->sk = NULL; /* To avoid changing sk->saddr */
  22. Skb->free = 1;
  23. Skb->localroute = sk->localroute| (Rt&msg_dontroute);
  24. /*
  25. * Now build the IP and MAC header.
  26. */
  27. Buff = skb->data;//Assigns a data pointer in SKB to the buff pointer
  28. SADDR = sk->saddr;//Local Address
  29. dev = NULL;
  30. TTL = sk->ip_ttl;//time to Live
  31. #ifdef Config_ip_multicast
  32. if (multicast (SIN->SIN_ADDR.S_ADDR))
  33. TTL = sk->ip_mc_ttl;
  34. #endif
  35. TMP = Sk->prot->build_header (SKB, saddr, SIN->SIN_ADDR.S_ADDR,
  36. &dev, IPPROTO_UDP, sk->opt, Skb->mem_len,sk->ip_tos,ttl);//Call Ip_build_header () to create an IP header and invoke Ip_send () Create a Mac Header
  37. skb->sk=sk; /* So memory is freed correctly */
  38. /*
  39. * Unable to put a header on the packet.
  40. */
  41. if (TMP < 0)
  42. {
  43. Sk->prot->wfree (SK, SKB->MEM_ADDR, Skb->mem_len);
  44. return (TMP);
  45. }
  46. Buff + = tmp;
  47. SADDR = skb->saddr; /*dev->pa_addr;*/
  48. Skb->len = tmp + sizeof (struct UDPHDR) + len; /* len + UDP + IP + MAC */
  49. Skb->dev = Dev;
  50. /*
  51. * Fill in the UDP header. Fill in the header of the UDP
  52. */
  53. Uh = (struct UDPHDR *) buff;
  54. Uh->len = htons (len + sizeof (struct UDPHDR));//Packet length
  55. Uh->source = sk->dummy_th.source;//Local Port
  56. Uh->dest = sin->sin_port;//Remote port
  57. Buff = (unsigned char *) (uh + 1);
  58. /*
  59. * Copy the user data.
  60. */
  61. Memcpy_fromfs (Buff, from, Len);//Copy User data
  62. /*
  63. * Set up the UDP checksum.
  64. */
  65. Udp_send_check (uh, saddr, SIN->SIN_ADDR.S_ADDR, Skb->len-tmp, SK);//Calculate the checksum of the UDP header
  66. /*
  67. * Send the datagram to the interface.
  68. */
  69. Udp_statistics. udpoutdatagrams++;
  70. Sk->prot->queue_xmit (SK, Dev, SKB, 1);//Call the IP layer function to send the data
  71. return (LEN);
  72. }

This sends the data to the Sk_buff structure that is populated after the IP data header and the Mac frame header are added to the UDP packet. Finally, the Send function of the IP layer is called to send the packet.

Linux kernel--Network stack Implementation Analysis (ix)--UDP protocol for Transport layer (bottom)

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.