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