• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <conscrypt/netutil.h>
18 #include <conscrypt/trace.h>
19 
20 #ifdef _WIN32
21 #pragma comment(lib, "ws2_32.lib")
22 #include <winsock2.h>
23 #else  // !_WIN32
24 #include <arpa/inet.h>
25 #include <fcntl.h>
26 #include <poll.h>
27 #include <sys/socket.h>
28 #include <sys/syscall.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31 #ifdef CONSCRYPT_UNBUNDLED
32 #include <dlfcn.h>
33 #endif  // CONSCRYPT_UNBUNDLED
34 #endif  // !_WIN32
35 
36 namespace conscrypt {
37 namespace netutil {
38 
39 /**
40  * Copied from libnativehelper NetworkUtilites.cpp
41  */
setBlocking(int fd,bool blocking)42 bool setBlocking(int fd, bool blocking) {
43 #ifdef _WIN32
44     unsigned long flag = blocking ? 0UL : 1UL;  // NOLINT(runtime/int)
45     int res = ioctlsocket(fd, FIONBIO, &flag);
46     if (res != NO_ERROR) {
47         JNI_TRACE("ioctlsocket %d failed with error: %d", fd, WSAGetLastError());
48     }
49     return res == NO_ERROR;
50 #else
51     int flags = fcntl(fd, F_GETFL);
52     if (flags == -1) {
53         return false;
54     }
55 
56     if (!blocking) {
57         flags |= O_NONBLOCK;
58     } else {
59         flags &= ~O_NONBLOCK;
60     }
61 
62     return fcntl(fd, F_SETFL, flags) != -1;
63 #endif
64 }
65 
66 }  // namespace netutil
67 }  // namespace conscrypt
68