fix buffer size for control messages

This commit is contained in:
Christian Ulrich 2020-08-23 12:45:05 +02:00
parent 6f75ffb35c
commit 80c2c50d26
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
2 changed files with 5 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import asyncdispatch, asyncnet, os, strformat, strutils, tables
from nativeSockets import Domain, SockType, Protocol
from net import IpAddress, Port, isIpAddress, `$`
from posix import CMSG_SPACE
import asyncutils
import ../../message
import random
@ -77,7 +78,8 @@ proc handleServerMessages(conn: ServerConnection) {.async.} =
proc handlePunchdMessages(conn: PunchdConnection) {.async.} =
while true:
let fd = conn.sock.getFd.AsyncFD
let resp = await fd.asyncRecvMsg(size = 400, cmsgSize = sizeof(AsyncFD))
let cmsgSize = CMSG_SPACE(sizeof(AsyncFD).csize_t)
let resp = await fd.asyncRecvMsg(400, cmsgSize)
let line = resp.data.strip(leading = false, trailing = true, chars = {'\n'})
echo "received punchd message: ", line
let args = line.parseArgs(3, 1)

View File

@ -34,7 +34,7 @@ proc getFd*(cmsg: ControlMessage): AsyncFD =
result = cast[ptr AsyncFD](cmsg.data.cstring)[]
proc asyncRecvMsg*(fd: AsyncFD, size: int = BufferSize,
cmsgSize: int = BufferSize):
cmsgSize: csize_t = BufferSize.csize_t):
Future[tuple[data: string, cmsgs: seq[ControlMessage]]] =
var retFuture = newFuture[tuple[data: string,
cmsgs: seq[ControlMessage]]]("asyncRecvMsg")
@ -55,7 +55,7 @@ proc asyncRecvMsg*(fd: AsyncFD, size: int = BufferSize,
var msg = Tmsghdr(msg_iov: addr iovec,
msg_iovlen: 1,
msg_control: addr cmsgBuffer[0],
msg_controllen: cmsgSize.SockLen)
msg_controllen: cmsgBuffer.len.SockLen)
let res = recvmsg(sock.SocketHandle, addr msg, 0)
if res < 0: