[project @ 1998-12-02 13:17:09 by simonm]
[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 "Rts.h"
12 #include "ghcSockets.h"
13 #include "stgio.h"
14
15 StgInt
16 getSocketOption__ (fd, opt)
17 StgInt fd;
18 StgInt opt;
19 {
20   int level,optval, sz_optval,rc;
21
22   if ( opt == TCP_MAXSEG || opt == TCP_NODELAY ) {
23      level = IPPROTO_TCP;
24   } else {
25      level = SOL_SOCKET;
26   }
27
28   sz_optval = sizeof(int);
29
30   while ( (rc = getsockopt((int)fd, level, opt, &optval, &sz_optval)) < 0 ) {
31       if (errno != EINTR) {
32           cvtErrno();
33           stdErrno();
34           return -1;
35       }
36    }
37    return optval;
38 }
39
40 StgInt
41 setSocketOption__ (fd, opt, val)
42 StgInt fd;
43 StgInt opt;
44 StgInt val;
45 {
46   int level, optval,rc;
47
48   if ( opt == TCP_MAXSEG || opt == TCP_NODELAY ) {
49      level = IPPROTO_TCP;
50   } else {
51      level = SOL_SOCKET;
52   }
53   
54   optval = val;
55
56   while ( (rc = setsockopt((int)fd, level, opt, &optval, sizeof(optval))) < 0 ) {
57       if (errno != EINTR) {
58           cvtErrno();
59           stdErrno();
60           return -1;
61       }
62    }
63    return 0;
64 }