TCP packets are constructed under Go language Linux (NET package and CGO are not used)

Source: Internet
Author: User
Tags htons
This is a creation in Article, where the information may have evolved or changed.

Golang can be networked by using the relevant socket functions in the Syscall package (unlike the network programming functions in Windows and Linux syscall)

Process, you can use the raw socket in our familiar way, because recently engaged in some protocol learning, so deliberately tried, and ordinary linux+c

Socket programming is basically no different.


The code is as follows:

Package Mainimport ("bytes" "Encoding/binary".    "FMT" "StrConv" "Strings" "Syscall" "unsafe") type Tcpheader struct {srcport uint16 dstport uint16 SeqNum uint32 acknum uint32 Offset uint8 Flag uint8 Window uint16 Checksum uint16 Ur Gentptr uint16}type psdheader struct {srcaddr uint32 dstaddr uint32 Zero uint8 ProtoType uint8 Tc Plength uint16}func inet_addr (ipaddr string) UInt32 {var (segments []string = strings.        Split (IPAddr, ".") IP [4]uint64 ret UInt64) for I: = 0; I < 4; i++ {Ip[i], _ = StrConv. Parseuint (Segments[i], ten, max)} ret = ip[3]<<24 + ip[2]<<16 + ip[1]<<8 + ip[0] return UInt32 (r ET)}func htons (port uint16) uint16 {var (high uint16 = Port >> 8 ret uint16 = port<<8 + hi)  GH) return ret}func CheckSum (data []byte) uint16 {var (sum uint32length int = len (data) indexint) for length > 1 {sum + = UInt32 (Data[index]) <<8 + uint32 (data[index+1]) index + 2length-= 2}if length > 0 {        Sum + = UInt32 (Data[index])}sum + = (sum >>) return uint16 (^sum)}func main () {var (msg string Psdheader psdheader tcpheader Tcpheader) Printf ("Input The Content:") Scanf ("%s", &msg)/* Fill TC P Pseudo-Header */Psdheader. srcaddr = inet_addr ("127.0.0.1") Psdheader. dstaddr = inet_addr ("127.0.0.1") Psdheader. Zero = 0 Psdheader. ProtoType = Syscall. Ipproto_tcp Psdheader. Tcplength = uint16 (unsafe. Sizeof (tcpheader{})) + UInt16 (len (msg))/* Fills the TCP header */Tcpheader. Srcport = htons (Tcpheader). dstport = htons (8080) tcpheader. SeqNum = 0 Tcpheader. Acknum = 0 Tcpheader. Offset = uint8 (uint16 (unsafe). Sizeof (tcpheader{}))/4) << 4 tcpheader. Flag = 2//syn tcpheader. Window = 60000 Tcpheader. Checksum = 0/*buffer is used to write two headers to obtain the checksum */var (buffer bytes. Buffer) binary. Write (&buffer, Binary. Bigendian, Psdheader) binary. Write (&buffer, Binary. Bigendian, Tcpheader) Tcpheader. Checksum = Checksum (buffer. Bytes ()///* Next empty buffer, fill in the actual part to be sent */buffer. Reset () binary. Write (&buffer, Binary. Bigendian, Tcpheader) binary. Write (&buffer, Binary. Bigendian, MSG)/* The following operations are raw socket operations, everyone can understand */var (sockfd int addr Syscall. SOCKADDRINET4 err Error) if sockfd, err = Syscall. Socket (Syscall.af_inet, Syscall. Sock_raw, Syscall. IPPROTO_TCP); Err! = Nil {Println ("Socket () Error:", err.) Error ()) return} defer syscall. Shutdown (SOCKFD, Syscall. SHUT_RDWR) addr. Addr[0], Addr. Addr[1], Addr. ADDR[2], Addr. ADDR[3] = 127, 0, 0, 1 Addr. Port = 8080 If err = syscall. Sendto (SOCKFD, buffer. Bytes (), 0, &addr); Err! = Nil {Println ("Sendto () Error:", err. Error ()) return} Println ("Send success!")}


If reproduced please indicate the source of the article: http://blog.csdn.net/gophers/article/details/20393601

Related Article

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.