Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update package to work with Go 1

  • Loading branch information...
commit fa90b9f91d5462690fdd9a8a259eb187806182cd 1 parent 1b8838c
Jesper Louis Andersen authored July 03, 2012

Showing 2 changed files with 27 additions and 27 deletions. Show diff stats Hide diff stats

  1. 8  Makefile
  2. 46  ident.go
8  Makefile
... ...
@@ -1,8 +0,0 @@
1  
-include $(GOROOT)/src/Make.inc
2  
-
3  
-TARG=ident
4  
-GOFILES=\
5  
-	ident.go\
6  
-
7  
-include $(GOROOT)/src/Make.pkg
8  
-
46  ident.go
@@ -3,15 +3,16 @@ package ident
3 3
 import (
4 4
 	"bufio"
5 5
 	"bytes"
  6
+	"errors"
6 7
 	"fmt"
7 8
 	"net"
8  
-	"os"
9 9
 	"strconv"
10 10
 	"strings"
  11
+	"time"
11 12
 )
12 13
 
13 14
 type ProtocolError struct {
14  
-	os.ErrorString
  15
+	Err error
15 16
 }
16 17
 
17 18
 type Validity interface {
@@ -37,8 +38,8 @@ const (
37 38
 )
38 39
 
39 40
 var (
40  
-	ErrLineTooLong = &ProtocolError{"response line too long"}
41  
-	ErrNoCR        = &ProtocolError{"no CR character found before LF"}
  41
+	ErrLineTooLong = errors.New("response line too long")
  42
+	ErrNoCR        = errors.New("no CR character found before LF")
42 43
 )
43 44
 
44 45
 type badStringError struct {
@@ -46,7 +47,7 @@ type badStringError struct {
46 47
 	str  string
47 48
 }
48 49
 
49  
-func (e *badStringError) String() string {
  50
+func (e *badStringError) Error() string {
50 51
 	return fmt.Sprintf("%s %q", e.what, e.str)
51 52
 }
52 53
 
@@ -59,7 +60,7 @@ func idString(sPort int, cPort int) (r []byte) {
59 60
 // Give up if the line exceeds maxLineLength.
60 61
 // The returned bytes are a pointer into storage in
61 62
 // the bufio, so they are only valid until the next bufio read.
62  
-func readLineBytes(b *bufio.Reader) (p []byte, err os.Error) {
  63
+func readLineBytes(b *bufio.Reader) (p []byte, err error) {
63 64
 	if p, err = b.ReadSlice('\n'); err != nil {
64 65
 		return nil, err
65 66
 	}
@@ -81,7 +82,7 @@ func readLineBytes(b *bufio.Reader) (p []byte, err os.Error) {
81 82
 }
82 83
 
83 84
 // readLineBytes, but convert the bytes into a string.
84  
-func readLine(b *bufio.Reader) (s string, err os.Error) {
  85
+func readLine(b *bufio.Reader) (s string, err error) {
85 86
 	p, e := readLineBytes(b)
86 87
 	if e != nil {
87 88
 		return "", e
@@ -98,7 +99,7 @@ var (
98 99
 // one to five 'digit' characters. No kind of checking is made to make sure it is a TCP
99 100
 // port number. '99999' is a valid response as a result. We will check that the response
100 101
 // matches the query in another place.
101  
-func parsePort(l []byte) (int, os.Error) {
  102
+func parsePort(l []byte) (int, error) {
102 103
 	for _, c := range l {
103 104
 		if c != ' ' && c != '\t' && (c < '0' || c > '9') {
104 105
 			return 0, &badStringError{"Port value not all digits", string(l)}
@@ -140,7 +141,7 @@ func allTokenChars(ai []byte) bool {
140 141
 }
141 142
 
142 143
 // If the message is an ERROR message, parse the Additional info block of the Error
143  
-func parseErrorAddInfo(r *Response, ai []byte) (*Response, os.Error) {
  144
+func parseErrorAddInfo(r *Response, ai []byte) (*Response, error) {
144 145
 	s := strings.TrimSpace(string(ai))
145 146
 	switch s {
146 147
 	case "INVALID-PORT", "NO-USER", "HIDDEN-USER", "UNKNOWN-ERROR":
@@ -183,13 +184,13 @@ func validUserId(userId []byte) bool {
183 184
 }
184 185
 
185 186
 // If the response type is USERID, parse the additional info block
186  
-func parseUserIdAddInfo(r *Response, ai []byte) (*Response, os.Error) {
187  
-	ais := bytes.Split(ai, colon, -1)
  187
+func parseUserIdAddInfo(r *Response, ai []byte) (*Response, error) {
  188
+	ais := bytes.Split(ai, colon)
188 189
 	if len(ais) < 2 {
189 190
 		return nil, &badStringError{"Could not split USERID response", string(ai)}
190 191
 	}
191 192
 
192  
-	osc := bytes.Split(ais[0], comma, -1)
  193
+	osc := bytes.Split(ais[0], comma)
193 194
 	if len(osc) == 2 {
194 195
 		cs := strings.TrimSpace(string(osc[1]))
195 196
 		switch cs {
@@ -233,7 +234,7 @@ func parseUserIdAddInfo(r *Response, ai []byte) (*Response, os.Error) {
233 234
 }
234 235
 
235 236
 // Parse the type and addInfo sections into an IdentResponse.
236  
-func parseType(l []byte, ai []byte, r *Response) (*Response, os.Error) {
  237
+func parseType(l []byte, ai []byte, r *Response) (*Response, error) {
237 238
 	s := strings.TrimSpace(string(l))
238 239
 	switch s {
239 240
 	case "USERID":
@@ -249,10 +250,10 @@ func parseType(l []byte, ai []byte, r *Response) (*Response, os.Error) {
249 250
 
250 251
 // Response parser. Given a byte slice, it parses the slice for the RFC1413 protocol and then
251 252
 // fills the result into the IdentResponse object returned.
252  
-func parseResponse(l []byte) (r *Response, e os.Error) {
  253
+func parseResponse(l []byte) (r *Response, e error) {
253 254
 	r = new(Response)
254 255
 	// Parse Server Port
255  
-	bs := bytes.Split(l, comma, 2)
  256
+	bs := bytes.SplitN(l, comma, 2)
256 257
 	if len(bs) < 2 {
257 258
 		goto Malformed
258 259
 	}
@@ -262,7 +263,7 @@ func parseResponse(l []byte) (r *Response, e os.Error) {
262 263
 	}
263 264
 
264 265
 	// Parse Client port
265  
-	bs = bytes.Split(bs[1], colon, 2)
  266
+	bs = bytes.SplitN(bs[1], colon, 2)
266 267
 	if len(bs) < 2 {
267 268
 		goto Malformed
268 269
 	}
@@ -272,7 +273,7 @@ func parseResponse(l []byte) (r *Response, e os.Error) {
272 273
 	}
273 274
 
274 275
 	// Parse response type
275  
-	bs = bytes.Split(bs[1], colon, 2)
  276
+	bs = bytes.SplitN(bs[1], colon, 2)
276 277
 	if len(bs) < 2 {
277 278
 		goto Malformed
278 279
 	}
@@ -295,20 +296,27 @@ Malformed:
295 296
 //
296 297
 // The function returns the source port reflected on the server, the destination port reflected
297 298
 // on the server and an IdentResponse struct containing the ident information.
298  
-func Query(hostname string, sPort int, cPort int) (*Response, os.Error) {
  299
+func Query(hostname string, sPort int, cPort int) (*Response, error) {
  300
+
  301
+	duration, err0 := time.ParseDuration("30s")
  302
+	if err0 != nil {
  303
+		return nil, err0
  304
+	}
  305
+
299 306
 	conn, err1 := net.Dial("tcp", hostname+":"+string(identPort))
300 307
 	if err1 != nil {
301 308
 		return nil, err1
302 309
 	}
303 310
 	defer conn.Close()
304  
-	conn.SetTimeout(30000 * 1000000)
305 311
 
306 312
 	idS := idString(sPort, cPort)
  313
+	conn.SetDeadline(time.Now().Add(duration))
307 314
 	if _, e := conn.Write(idS); e != nil {
308 315
 		return nil, e
309 316
 	}
310 317
 
311 318
 	r := bufio.NewReader(conn)
  319
+	conn.SetDeadline(time.Now().Add(duration))
312 320
 	response, err2 := readLineBytes(r)
313 321
 	if err2 != nil {
314 322
 		return nil, err2

0 notes on commit fa90b9f

Please sign in to comment.
Something went wrong with that request. Please try again.