• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <sys/socket.h>
2 #include <sys/time.h>
3 #include <errno.h>
4 #include "syscall.h"
5 
6 #define IS32BIT(x) !((x)+0x80000000ULL>>32)
7 #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
8 
setsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)9 int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
10 {
11 	const struct timeval *tv;
12 	time_t s;
13 	suseconds_t us;
14 
15 	int r = __socketcall(setsockopt, fd, level, optname, optval, optlen, 0);
16 
17 	if (r==-ENOPROTOOPT) switch (level) {
18 	case SOL_SOCKET:
19 		switch (optname) {
20 		case SO_RCVTIMEO:
21 		case SO_SNDTIMEO:
22 			if (SO_RCVTIMEO == SO_RCVTIMEO_OLD) break;
23 			if (optlen < sizeof *tv) return __syscall_ret(-EINVAL);
24 			tv = optval;
25 			s = tv->tv_sec;
26 			us = tv->tv_usec;
27 			if (!IS32BIT(s)) return __syscall_ret(-ENOTSUP);
28 
29 			if (optname==SO_RCVTIMEO) optname=SO_RCVTIMEO_OLD;
30 			if (optname==SO_SNDTIMEO) optname=SO_SNDTIMEO_OLD;
31 
32 			r = __socketcall(setsockopt, fd, level, optname,
33 				((long[]){s, CLAMP(us)}), 2*sizeof(long), 0);
34 			break;
35 		case SO_TIMESTAMP:
36 		case SO_TIMESTAMPNS:
37 			if (SO_TIMESTAMP == SO_TIMESTAMP_OLD) break;
38 			if (optname==SO_TIMESTAMP) optname=SO_TIMESTAMP_OLD;
39 			if (optname==SO_TIMESTAMPNS) optname=SO_TIMESTAMPNS_OLD;
40 			r = __socketcall(setsockopt, fd, level,
41 				optname, optval, optlen, 0);
42 			break;
43 		}
44 	}
45 	return __syscall_ret(r);
46 }
47