FAQ
I am trying convert clamav library to golang lang. Here doc about tcp
command clamav:

Scan a stream of data. The stream is sent to clamd in chunks, after
INSTREAM,
on the same socket on which the command was sent. This avoids the overhead
of establishing new TCP connections and problems with NAT. The format of the
chunk is: <length><data> where <length> is the size of the following data in
bytes expressed as a 4 byte unsigned integer in network byte order and
<data> is
the actual chunk. Streaming is terminated by sending a zero-length chunk.
Note:
do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will
reply with INSTREAM size limit exceeded and close the connection.

Here example on Java:

https://github.com/jenkinsci/clamav-plugin/blob/master/src/main/java/org/jenkinsci/plugins/clamav/scanner/ClamAvScanner.java#L147-L192

I try to create golang version, but it doesn't work (I am not sure how to
send <length><data> in valid way):

package main

import (


   stdlog "log"


   "os"
   "net"
   "bufio"
   "io"
   "strconv"
   "encoding/binary"


)

func main() {
   ip := net.ParseIP("127.0.0.1")
   addr := &net.TCPAddr{
     IP: ip,
     Port: 3310,
   }
   conn, err := net.DialTCP("tcp", nil, addr)
   if err != nil {
     return
   }
   _, err = conn.Write([]byte("zINSTREAM"))
   if err != nil {
     return
   }

   str := "$CEliacmaTrESTuScikgsn$FREE-TEST-SIGNATURE$EEEEE$"
   strBytes := []byte(strconv.Itoa(len(str))+str)

   err = binary.Write(conn, binary.BigEndian, &l1)
   _, err = conn.Write(strBytes)
   if err != nil {
     return
   }

   _, err = conn.Write([]byte(strconv.Itoa(0)))
   if err != nil {
     return
   }

   reader := bufio.NewReader(conn)
   // reading
   for {
     line, err := reader.ReadString('\n')
     if err == io.EOF {
       conn.Close()
       break
     }
     if err != nil {
       conn.Close()
       return
     }
   }

}


Thanks!

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Daniel Mal at Jun 16, 2013 at 7:08 am
    in my opinion:

      // header
    n, err := io.ReadFull(conn, header)
    size := binary.BigEndian.Uint16(header)

    // data
    data := make([]byte, size)
    n, err = io.ReadFull(conn, data)


    gonna work.

    在 2013年6月14日星期五UTC+8上午7时08分18秒,Alexey Vasilyev写道:
    I am trying convert clamav library to golang lang. Here doc about tcp
    command clamav:

    Scan a stream of data. The stream is sent to clamd in chunks, after
    INSTREAM,
    on the same socket on which the command was sent. This avoids the overhead
    of establishing new TCP connections and problems with NAT. The format of
    the
    chunk is: <length><data> where <length> is the size of the following data
    in
    bytes expressed as a 4 byte unsigned integer in network byte order and
    <data> is
    the actual chunk. Streaming is terminated by sending a zero-length chunk.
    Note:
    do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will
    reply with INSTREAM size limit exceeded and close the connection.

    Here example on Java:


    https://github.com/jenkinsci/clamav-plugin/blob/master/src/main/java/org/jenkinsci/plugins/clamav/scanner/ClamAvScanner.java#L147-L192

    I try to create golang version, but it doesn't work (I am not sure how to
    send <length><data> in valid way):

    package main

    import (


    stdlog "log"


    "os"
    "net"
    "bufio"
    "io"
    "strconv"
    "encoding/binary"


    )

    func main() {
    ip := net.ParseIP("127.0.0.1")
    addr := &net.TCPAddr{
    IP: ip,
    Port: 3310,
    }
    conn, err := net.DialTCP("tcp", nil, addr)
    if err != nil {
    return
    }
    _, err = conn.Write([]byte("zINSTREAM"))
    if err != nil {
    return
    }

    str := "$CEliacmaTrESTuScikgsn$FREE-TEST-SIGNATURE$EEEEE$"
    strBytes := []byte(strconv.Itoa(len(str))+str)

    err = binary.Write(conn, binary.BigEndian, &l1)
    _, err = conn.Write(strBytes)
    if err != nil {
    return
    }

    _, err = conn.Write([]byte(strconv.Itoa(0)))
    if err != nil {
    return
    }

    reader := bufio.NewReader(conn)
    // reading
    for {
    line, err := reader.ReadString('\n')
    if err == io.EOF {
    conn.Close()
    break
    }
    if err != nil {
    conn.Close()
    return
    }
    }

    }


    Thanks!
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 14, '13 at 12:15a
activeJun 16, '13 at 7:08a
posts2
users2
websitegolang.org

2 users in discussion

Alexey Vasilyev: 1 post Daniel Mal: 1 post

People

Translate

site design / logo © 2021 Grokbase