-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
222 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dns | ||
|
||
import "github.com/brewlin/net-protocol/protocol/header" | ||
|
||
//sendQuery udp query dns | ||
func (e *Endpoint) sendQuery () ( *[]header.DNSResource ,error ) { | ||
|
||
if err := e.c.Connect();err != nil { | ||
return nil,err | ||
} | ||
if err := e.c.Write(*e.req) ; err != nil { | ||
return nil,err | ||
} | ||
return e.parseResp() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dns | ||
|
||
import ( | ||
"github.com/brewlin/net-protocol/protocol/header" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
//parseResp | ||
//解析响应 | ||
func (e *Endpoint) parseResp() (*[]header.DNSResource,error){ | ||
rsp,err := e.c.Read() | ||
if err != nil { | ||
return nil,err | ||
} | ||
p := header.DNS(rsp) | ||
e.resp = &p | ||
e.answer = p.GetAnswer(e.Domain) | ||
return e.parseAnswer() | ||
} | ||
|
||
func (e *Endpoint) parseAnswer()(*[]header.DNSResource,error){ | ||
for i := 0; i < len(*e.answer) ; i++ { | ||
switch (*e.answer)[i].Type { | ||
case header.A: | ||
(*e.answer)[i].Address = e.parseAName((*e.answer)[i].RData) | ||
case header.CNAME: | ||
(*e.answer)[i].Address = e.parseCName((*e.answer)[i].RData) | ||
} | ||
} | ||
return e.answer,nil | ||
} | ||
func (e *Endpoint)parseAName(rd []byte) string { | ||
res := []string{} | ||
for _,v := range rd { | ||
res = append(res,strconv.Itoa(int(v))) | ||
} | ||
return strings.Join(res,".") | ||
} | ||
|
||
func (e *Endpoint)parseCName(rd []byte) (res string) { | ||
|
||
for{ | ||
l := int(rd[0]) | ||
if l >= len(rd){ | ||
res += ".com" | ||
return | ||
} | ||
rd = rd[1:] | ||
res += string(rd[0:l]) | ||
rd = rd[l:] | ||
if len(rd) == 0 { | ||
return | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters