OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "errors" | 9 "errors" |
10 "fmt" | 10 "fmt" |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 for i := 0; i < csumcv; i += 2 { | 259 for i := 0; i < csumcv; i += 2 { |
260 s += uint32(b[i+1])<<8 | uint32(b[i]) | 260 s += uint32(b[i+1])<<8 | uint32(b[i]) |
261 } | 261 } |
262 if csumcv&1 == 0 { | 262 if csumcv&1 == 0 { |
263 s += uint32(b[csumcv]) | 263 s += uint32(b[csumcv]) |
264 } | 264 } |
265 s = s>>16 + s&0xffff | 265 s = s>>16 + s&0xffff |
266 s = s + s>>16 | 266 s = s + s>>16 |
267 // Place checksum back in header; using ^= avoids the | 267 // Place checksum back in header; using ^= avoids the |
268 // assumption the checksum bytes are zero. | 268 // assumption the checksum bytes are zero. |
269 » b[2] ^= byte(^s & 0xff) | 269 » b[2] ^= byte(^s) |
270 b[3] ^= byte(^s >> 8) | 270 b[3] ^= byte(^s >> 8) |
271 return b, nil | 271 return b, nil |
272 } | 272 } |
273 | 273 |
274 // parseICMPMessage parses b as an ICMP message. | 274 // parseICMPMessage parses b as an ICMP message. |
275 func parseICMPMessage(b []byte) (*icmpMessage, error) { | 275 func parseICMPMessage(b []byte) (*icmpMessage, error) { |
276 msglen := len(b) | 276 msglen := len(b) |
277 if msglen < 4 { | 277 if msglen < 4 { |
278 return nil, errors.New("message too short") | 278 return nil, errors.New("message too short") |
279 } | 279 } |
(...skipping 22 matching lines...) Expand all Loading... |
302 if p == nil { | 302 if p == nil { |
303 return 0 | 303 return 0 |
304 } | 304 } |
305 return 4 + len(p.Data) | 305 return 4 + len(p.Data) |
306 } | 306 } |
307 | 307 |
308 // Marshal returns the binary enconding of the ICMP echo request or | 308 // Marshal returns the binary enconding of the ICMP echo request or |
309 // reply message body p. | 309 // reply message body p. |
310 func (p *icmpEcho) Marshal() ([]byte, error) { | 310 func (p *icmpEcho) Marshal() ([]byte, error) { |
311 b := make([]byte, 4+len(p.Data)) | 311 b := make([]byte, 4+len(p.Data)) |
312 » b[0], b[1] = byte(p.ID>>8), byte(p.ID&0xff) | 312 » b[0], b[1] = byte(p.ID>>8), byte(p.ID) |
313 » b[2], b[3] = byte(p.Seq>>8), byte(p.Seq&0xff) | 313 » b[2], b[3] = byte(p.Seq>>8), byte(p.Seq) |
314 copy(b[4:], p.Data) | 314 copy(b[4:], p.Data) |
315 return b, nil | 315 return b, nil |
316 } | 316 } |
317 | 317 |
318 // parseICMPEcho parses b as an ICMP echo request or reply message | 318 // parseICMPEcho parses b as an ICMP echo request or reply message |
319 // body. | 319 // body. |
320 func parseICMPEcho(b []byte) (*icmpEcho, error) { | 320 func parseICMPEcho(b []byte) (*icmpEcho, error) { |
321 bodylen := len(b) | 321 bodylen := len(b) |
322 p := &icmpEcho{ID: int(b[0])<<8 | int(b[1]), Seq: int(b[2])<<8 | int(b[3
])} | 322 p := &icmpEcho{ID: int(b[0])<<8 | int(b[1]), Seq: int(b[2])<<8 | int(b[3
])} |
323 if bodylen > 4 { | 323 if bodylen > 4 { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 raddr := &IPAddr{IP: IPv4(127, 0, 0, 10).To4()} | 361 raddr := &IPAddr{IP: IPv4(127, 0, 0, 10).To4()} |
362 c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr) | 362 c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr) |
363 if err != nil { | 363 if err != nil { |
364 t.Fatalf("DialIP failed: %v", err) | 364 t.Fatalf("DialIP failed: %v", err) |
365 } | 365 } |
366 defer c.Close() | 366 defer c.Close() |
367 if !reflect.DeepEqual(raddr, c.RemoteAddr()) { | 367 if !reflect.DeepEqual(raddr, c.RemoteAddr()) { |
368 t.Fatalf("got %#v, expected %#v", c.RemoteAddr(), raddr) | 368 t.Fatalf("got %#v, expected %#v", c.RemoteAddr(), raddr) |
369 } | 369 } |
370 } | 370 } |
OLD | NEW |