[project @ 1996-06-27 16:13:29 by partain]
[ghc-hetmet.git] / ghc / runtime / io / connectSocket.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[connectSocket.lc]{Assign name to client 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 connectSocket(I_ sockfd, A_ servaddr, I_ addrlen, I_ isUnixDomain)
21 {
22     int rc;
23     
24     while ((rc = connect((int)sockfd, (struct sockaddr *)servaddr, (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_EAFNOSUPPORT:
48               ghc_errtype = ERR_INVALIDARGUMENT;
49               ghc_errstr  = "Address cannot be used with socket";
50               break;
51           case GHC_EINPROGRESS:
52           case GHC_EALREADY:
53               ghc_errtype = ERR_RESOURCEBUSY;
54               ghc_errstr  = "Non-blocking socket, previous connection attempt not completed";
55               break;
56           case GHC_EBADF:
57               ghc_errtype = ERR_INVALIDARGUMENT;
58               ghc_errstr  = "Not a valid socket file descriptor";
59               break;
60           case GHC_ECONNREFUSED:
61               ghc_errtype = ERR_PERMISSIONDENIED;
62               ghc_errstr  = "Connection rejected";
63               break;
64           case GHC_EFAULT:
65               ghc_errtype = ERR_INVALIDARGUMENT;
66               ghc_errstr  = "Address not in valid part of process address space";
67               break;
68           case GHC_EINVAL:
69               ghc_errtype = ERR_SYSTEMERROR;
70               ghc_errstr  = "Specified size of structure not equal valid address for family";
71               break;
72               break;
73           case GHC_ENETUNREACH:
74               ghc_errtype = ERR_PERMISSIONDENIED;
75               ghc_errstr  = "Network not reachable from host";
76               break;
77           case GHC_ENOTSOCK:
78               ghc_errtype = ERR_INAPPROPRIATETYPE;
79               ghc_errstr  = "Descriptor for file, not a socket";
80               break;
81           case GHC_ETIMEDOUT:
82               ghc_errtype = ERR_TIMEEXPIRED;
83               ghc_errstr  = "Connection attempt timed out";
84               break;
85           case GHC_EIO:
86               ghc_errtype = ERR_SYSTEMERROR;
87               ghc_errstr  = "Could not make directory entry or alloc inode";
88               break;
89           case GHC_EISDIR:
90               ghc_errtype = ERR_INVALIDARGUMENT;
91               ghc_errstr  = "A null path name was given";
92               break;
93           case GHC_ELOOP:
94               ghc_errtype = ERR_SYSTEMERROR;
95               ghc_errstr  = "Too many symbolic links encountered";
96               break;
97           case GHC_ENAMETOOLONG:
98               ghc_errtype = ERR_INVALIDARGUMENT;
99               ghc_errstr  = "Max length of path name exceeded";
100               break;
101           case GHC_ENOENT:
102               ghc_errtype = ERR_INVALIDARGUMENT;
103               ghc_errstr  = "Component in path prefix does not exist";
104               break;
105           case GHC_ENOTDIR:
106               ghc_errtype = ERR_INVALIDARGUMENT;
107               ghc_errstr  = "Component in path prefix is not a directory";
108               break;
109           case GHC_EPROTOTYPE:
110               ghc_errtype = ERR_INVALIDARGUMENT;
111               ghc_errstr  = "File referred to is a socket of differing type";
112               break;
113           }
114           return -1;
115       }
116     }
117     return 0;
118 }
119
120 \end{code}