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