e8d5d843c1626c78e436ebfac57f9f79036c8a83
[ghc-hetmet.git] / ghc / lib / misc / cbits / socketOpt.c
1 #if 0
2 %
3 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 %
5 \subsection[socketOpt.lc]{Setting/Getting socket opts}
6
7 \begin{code}
8 #endif
9
10 #define NON_POSIX_SOURCE
11 #include "rtsdefs.h"
12 #include "ghcSockets.h"
13
14 StgInt
15 getSocketOption__ (fd, opt)
16 StgInt fd;
17 StgInt opt;
18 {
19   int level,optval, sz_optval,rc;
20
21   if ( opt == TCP_MAXSEG || opt == TCP_NODELAY ) {
22      level = IPPROTO_TCP;
23   } else {
24      level = SOL_SOCKET;
25   }
26
27   sz_optval = sizeof(int);
28
29   while ( (rc = getsockopt((int)fd, level, opt, &optval, &sz_optval)) < 0 ) {
30       if (errno != EINTR) {
31           cvtErrno();
32           stdErrno();
33           return -1;
34       }
35    }
36    return optval;
37 }
38
39 StgInt
40 setSocketOption__ (fd, opt, val)
41 StgInt fd;
42 StgInt opt;
43 StgInt val;
44 {
45   int level, optval,rc;
46
47   if ( opt == TCP_MAXSEG || opt == TCP_NODELAY ) {
48      level = IPPROTO_TCP;
49   } else {
50      level = SOL_SOCKET;
51   }
52   
53   optval = val;
54
55   while ( (rc = setsockopt((int)fd, level, opt, &optval, sizeof(optval))) < 0 ) {
56       if (errno != EINTR) {
57           cvtErrno();
58           stdErrno();
59           return -1;
60       }
61    }
62    return 0;
63 }