summaryrefslogtreecommitdiff
path: root/ghc/lib/misc/cbits/createSocket.c
blob: ccab6887da3cca78f0480122b2e07862a732bf11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#if 0
%
% (c) The GRASP/AQUA Project, Glasgow University, 1995
%
\subsection[createSocket.lc]{Create a socket file descriptor}

\begin{code}
#endif

#define NON_POSIX_SOURCE
#include "rtsdefs.h"
#include "ghcSockets.h"

StgInt
createSocket(I_ family, I_ type, I_ protocol)
{
    int fd;

    if ((fd = socket((int)family, (int)type, (int)protocol)) < 0) {
      if (errno != EINTR) {
	  cvtErrno();
	  switch (ghc_errno) {
	  default:
	      stdErrno();
	      break;
	  case GHC_EACCES:
	      ghc_errtype = ERR_PERMISSIONDENIED;
	      ghc_errstr  = "cannot create socket";
	      break;
	  case GHC_EMFILE:
	      ghc_errtype = ERR_RESOURCEEXHAUSTED;
	      ghc_errstr  = "Too many open files";
	      break;
	  case GHC_ENFILE:
	      ghc_errtype = ERR_RESOURCEEXHAUSTED;
	      ghc_errstr  = "System file table overflow";
	      break;
	  case GHC_EPROTONOSUPPORT:
	      ghc_errtype = ERR_UNSUPPORTEDOPERATION;
	      ghc_errstr  = "Protocol type not supported";
	      break;
	  case GHC_EPROTOTYPE:
	      ghc_errtype = ERR_INAPPROPRIATETYPE;
	      ghc_errstr  = "Protocol wrong type for socket";
	      break;
	  }
	  return (StgInt)-1;
      }
    }
    return (StgInt)fd;
}