[PATCH] Modify UDP interface
Modify UDP interface
signed-off-by: Changxian Tan <ChangXian.Tan@gmail.com>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
From 33b29926157180d2a8aa3aaeca6bc61485d12da2 Mon Sep 17 00:00:00 2001
From: Changxian Tan <Changxian.Tan@gmail.com>
Date: Wed, 30 Sep 2009 10:45:32 +0800
Subject: [PATCH] modify udp interface
---
device/net/socket.c | 34 ++++++++++++----------------------
device/net/tftp.c | 17 +++++++++--------
include/net/net.h | 8 ++++----
3 files changed, 25 insertions(+), 34 deletions(-)
diff --git a/device/net/socket.c b/device/net/socket.c
index 1bcaeed..135bb24 100644
--- a/device/net/socket.c
+++ b/device/net/socket.c
@@ -87,25 +87,10 @@ struct sockaddr *gethostaddr(const char *sip)
return sockAddr;
}
-int connect(int fd, const struct sockaddr *addr, socklen_t len)
-{
- struct Socket *pSock;
-
- pSock = GetSock(fd);
-
- if (NULL == pSock)
- {
- // printf
- return -EINVAL;
- }
-
- memcpy(&pSock->addr, addr, len);
-
- return 0;
-}
//
-long send(int fd, const void *buf, ULONG n)
+long sendto(int fd, const void *buf, size_t len, int flags,
+ const struct sockaddr *destAddr, socklen_t addrlen)
{
struct Socket *pSock;
struct SocketBuffer *pSockBuff;
@@ -118,19 +103,22 @@ long send(int fd, const void *buf, ULONG n)
return -EINVAL;
}
- pSockBuff = GkSockBuffAlloc(ETH_HDR_LEN + IP_HDR_LEN + UDP_HDR_LEN, n);
+ memcpy(&pSock->addr, destAddr, addrlen);
+
+ pSockBuff = GkSockBuffAlloc(ETH_HDR_LEN + IP_HDR_LEN + UDP_HDR_LEN, len);
pSockBuff->pSock = pSock;
- memcpy(pSockBuff->pSkbData, buf, n);
+ memcpy(pSockBuff->pSkbData, buf, len);
GUdpSendPacket(pSockBuff);
- return n;
+ return len;
}
//
-long recv(int fd, void *buf, ULONG n)
+long recvfrom(int fd, void *buf, size_t len, int flags,
+ struct sockaddr *srcAddr, socklen_t *addrlen)
{
struct Socket *pSock;
struct SocketBuffer *pSockBuff;
@@ -146,9 +134,11 @@ long recv(int fd, void *buf, ULONG n)
pSockBuff = GUdpRecvPacket(pSock);
// fixme !
- nPktLen = pSockBuff->wSkbSize <= n ? pSockBuff->wSkbSize : n;
+ nPktLen = pSockBuff->wSkbSize <= len ? pSockBuff->wSkbSize : len;
memcpy(buf, pSockBuff->pSkbData, nPktLen);
+ *addrlen = sizeof(pSockBuff->pSock->addr);
+ memcpy(srcAddr, &(pSockBuff->pSock->addr), *addrlen);
GkSockBuffFree(pSockBuff);
diff --git a/device/net/tftp.c b/device/net/tftp.c
index 1f30c67..3509d70 100644
--- a/device/net/tftp.c
+++ b/device/net/tftp.c
@@ -41,14 +41,14 @@ static int TftpMakeRRQ(BYTE *pbBuff, const char *pchFileName)
}
-static void TftpSendAck(const int fd, const UINT16 nBlkNum)
+static void TftpSendAck(const int fd, const UINT16 nBlkNum, struct sockaddr *skaddr)
{
struct TftpPacket tftpPkt;
tftpPkt.wOpCode = TFTP_ACK;
tftpPkt.wBlkNum = CPU_TO_BE16(nBlkNum);
- send(fd, &tftpPkt, TFTP_HDR_LEN);
+ sendto(fd, &tftpPkt, TFTP_HDR_LEN, 0, skaddr, sizeof(struct sockaddr));
}
@@ -109,11 +109,9 @@ int GuNetTftpGetFile(struct TftpOpt *opt)
skaddr->wSrcPort = CPU_TO_BE16(1234); // fixme: NetPortAlloc
skaddr->wDesPort = CPU_TO_BE16(STD_PORT_TFTP);
- ret = connect(sockfd, skaddr, sizeof(struct sockaddr));
-
nPktLen = TftpMakeRRQ((BYTE *)pTftpPkt, opt->szFileName);
- send(sockfd, pTftpPkt, nPktLen);
+ sendto(sockfd, pTftpPkt, nPktLen, 0, skaddr, sizeof(struct sockaddr));
pbBufPtr = opt->pLoadAddr;
nLoadLen = 0;
@@ -121,7 +119,10 @@ int GuNetTftpGetFile(struct TftpOpt *opt)
do
{
- nPktLen = recv(sockfd, pTftpPkt, TFTP_BUF_LEN);
+ struct sockaddr srcAddr;
+ socklen_t srcAddrlen;
+
+ nPktLen = recvfrom(sockfd, pTftpPkt, TFTP_BUF_LEN, 0, &srcAddr, &srcAddrlen);
nPktLen -= TFTP_HDR_LEN;
@@ -133,7 +134,7 @@ int GuNetTftpGetFile(struct TftpOpt *opt)
case TFTP_DAT:
if (BE16_TO_CPU(pTftpPkt->wBlkNum) == wCurBlkNum)
{
- TftpSendAck(sockfd, wCurBlkNum);
+ TftpSendAck(sockfd, wCurBlkNum, skaddr);
wCurBlkNum++;
nLoadLen += nPktLen;
@@ -173,7 +174,7 @@ int GuNetTftpGetFile(struct TftpOpt *opt)
printf("\t%s(): LOST Packet = 0x%x (0x%x).\r",
__FUNC__, wCurBlkNum, BE16_TO_CPU(pTftpPkt->wBlkNum));
#endif
- TftpSendAck(sockfd, wCurBlkNum - 1);
+ TftpSendAck(sockfd, wCurBlkNum - 1, skaddr);
}
break;
diff --git a/include/net/net.h b/include/net/net.h
index 1ec12c1..ed0e4f4 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -204,11 +204,11 @@ int socket(int domain, int type, int protocol);
struct sockaddr *getaddr(UINT32 ip);
-int connect(int fd, const struct sockaddr *addr, socklen_t len);
+long sendto(int fd, const void *buf, size_t len, int flags,
+ const struct sockaddr *destAddr, socklen_t addrlen);
-long send(int fd, const void *buf, ULONG n);
-
-long recv(int fd, void *buf, ULONG n);
+long recvfrom(int fd, void *buf, size_t len, int flags,
+ struct sockaddr *srcAddr, socklen_t *addrlen);
int close(int fd);
--
1.6.0.4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论