[project @ 1996-06-27 16:13:29 by partain]
[ghc-hetmet.git] / ghc / runtime / io / getSockName.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1996
3 %
4 \subsection[getSockName.lc]{Return name of process assoc with socket}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_SYS_SOCKET_H
16 #include <sys/socket.h>
17 #endif
18
19
20 StgInt
21 getSockName(int sockfd, struct sockaddr *peer, int *namelen)
22 {
23     StgInt name;
24     
25     while ((name = getsockname(sockfd, peer, namelen)) < 0) {
26       if (errno != EINTR) {
27           cvtErrno();
28           switch (ghc_errno) {
29           default:
30               stdErrno();
31               break;
32           case GHC_EBADF:
33               ghc_errtype = ERR_INVALIDARGUMENT;
34               ghc_errstr  = "Not a valid write descriptor";
35               break;
36           case GHC_EFAULT:
37               ghc_errtype = ERR_INVALIDARGUMENT;
38               ghc_errstr  = "Data not in writeable part of user address space";
39               break;
40           case GHC_ENOBUFS:
41               ghc_errtype = ERR_RESOURCEEXHAUSTED;
42               ghc_errstr  = "Insuffcient resources";
43               break;
44           case GHC_ENOTSOCK:
45               ghc_errtype = ERR_INVALIDARGUMENT;
46               ghc_errstr  = "Descriptor is not a socket";
47               break;
48           }
49           return -1;
50       }
51     }
52     return name;
53 }
54
55 \end{code}