[project @ 1996-06-27 16:13:29 by partain]
[ghc-hetmet.git] / ghc / runtime / io / bindSocket.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[bindSocket.lc]{Assign name to unnamed 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 StgInt
20 bindSocket(I_ sockfd, A_ myaddr, I_ addrlen, I_ isUnixDomain)
21 {
22     int rc;
23     
24     while ((rc = bind((int)sockfd, (struct sockaddr *)myaddr, (int)addrlen)) < 0) {
25       if (errno != EINTR) {
26           cvtErrno();
27           switch (ghc_errno) {
28           default:
29               stdErrno();
30               break;
31           case GHC_EACCES:
32               ghc_errtype = ERR_PERMISSIONDENIED;
33               if (isUnixDomain != 0)
34                  ghc_errstr = "For a component of path prefix of path name";
35               else
36                  ghc_errstr  = "Requested address protected, cannot bind socket";
37               break;
38           case GHC_EISCONN:
39           case GHC_EADDRINUSE:
40               ghc_errtype = ERR_RESOURCEBUSY;
41               ghc_errstr  = "Address already in use";
42               break;
43           case GHC_EADDRNOTAVAIL:
44               ghc_errtype = ERR_PERMISSIONDENIED;
45               ghc_errstr  = "Address not available from local machine";
46               break;
47           case GHC_EBADF:
48               ghc_errtype = ERR_INVALIDARGUMENT;
49               ghc_errstr  = "Not a valid socket file descriptor";
50               break;
51           case GHC_EFAULT:
52               ghc_errtype = ERR_INVALIDARGUMENT;
53               ghc_errstr  = "Address not in valid part of user address space";
54               break;
55           case GHC_EINVAL:
56               ghc_errtype = ERR_SYSTEMERROR;
57               ghc_errstr  = "Specified size of structure not equal valid address for family";
58               break;
59           case GHC_ENOTSOCK:
60               ghc_errtype = ERR_INAPPROPRIATETYPE;
61               ghc_errstr  = "Descriptor for file, not a socket";
62               break;
63           case GHC_EIO:
64               ghc_errtype = ERR_SYSTEMERROR;
65               ghc_errstr  = "Could not make directory entry or alloc inode";
66               break;
67           case GHC_EISDIR:
68               ghc_errtype = ERR_INVALIDARGUMENT;
69               ghc_errstr  = "A null path name was given";
70               break;
71           case GHC_ELOOP:
72               ghc_errtype = ERR_SYSTEMERROR;
73               ghc_errstr  = "Too many symbolic links encountered";
74               break;
75           case GHC_ENAMETOOLONG:
76               ghc_errtype = ERR_INVALIDARGUMENT;
77               ghc_errstr  = "Max length of path name exceeded";
78               break;
79           case GHC_ENOENT:
80               ghc_errtype = ERR_INVALIDARGUMENT;
81               ghc_errstr  = "Component in path prefix does not exist";
82               break;
83           case GHC_ENOTDIR:
84               ghc_errtype = ERR_INVALIDARGUMENT;
85               ghc_errstr  = "Component in path prefix is not a directory";
86               break;
87           case GHC_EROFS:
88               ghc_errtype = ERR_INVALIDARGUMENT;
89               ghc_errstr  = "The inode would reside on read only file system";
90               break;
91           }
92           return -1;
93       }
94     }
95     return 0;
96 }
97
98 \end{code}