c1e10df6d7611d3fd19bb77c240edb66e7c764c4
[ghc-hetmet.git] / ghc / lib / misc / cbits / recvFrom.c
1 #if 0
2 %
3 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 %
5 \subsection[recvFrom.lc]{recvFrom run-time support}
6
7 \begin{code}
8 #endif
9
10 #define NON_POSIX_SOURCE
11 #include "rtsdefs.h"
12 #include "ghcSockets.h"
13
14 StgInt
15 recvFrom__(fd, buf, nbytes, from)
16 StgInt fd;
17 StgAddr buf;
18 StgInt nbytes;
19 StgAddr from;
20 {
21   StgInt count;
22   int sz;
23   int flags = 0;
24
25   sz = sizeof(struct sockaddr_in);
26
27   while ( (count = recvfrom((int)fd, (void*)buf, (int)nbytes, flags, (struct sockaddr*)from, &sz)) < 0) {
28       if (errno != EINTR) {
29           cvtErrno();
30           switch (ghc_errno) {
31           case GHC_EBADF:
32               ghc_errtype = ERR_INVALIDARGUMENT;
33               ghc_errstr  = "Not a valid read descriptor";
34               break;
35           case GHC_EBADMSG:
36               ghc_errtype = ERR_SYSTEMERROR;
37               ghc_errstr  = "Message waiting to be read is not a data message";
38               break;
39           case GHC_EFAULT:
40               ghc_errtype = ERR_INVALIDARGUMENT;
41               ghc_errstr  = "Data buffer not in readable part of user address space";
42               break;
43           case GHC_EINVAL:
44               ghc_errtype = ERR_INVALIDARGUMENT;
45               ghc_errstr  = "Seek pointer associated with descriptor negative";
46               break;
47           case GHC_EIO:
48               ghc_errtype = ERR_SYSTEMERROR;
49               ghc_errstr  = "I/O error occurred while reading";
50               break;
51           case GHC_EISDIR:
52               ghc_errtype = ERR_INAPPROPRIATETYPE;
53               ghc_errstr  = "Descriptor refers to a directory";
54               break;
55           case GHC_EAGAIN:
56           case GHC_EWOULDBLOCK:
57               ghc_errtype = ERR_OTHERERROR;
58               ghc_errstr  = "No data could be read immediately";
59               break;
60           default:
61               stdErrno();
62               break;
63           }
64           return -1;
65       }
66     }
67     return count;
68 }