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