Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 1 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | ** Copyright 2006, 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 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stddef.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <sys/socket.h> |
| 24 | #include <sys/select.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <netinet/in.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 28 | #include <cutils/sockets.h> |
| 29 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #define LISTEN_BACKLOG 4 |
| 31 | |
| 32 | /* open listen() port on any interface */ |
| 33 | int socket_inaddr_any_server(int port, int type) |
| 34 | { |
Erik Kline | c40da92 | 2015-12-07 16:09:39 +0900 | [diff] [blame] | 35 | struct sockaddr_in6 addr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | int s, n; |
| 37 | |
| 38 | memset(&addr, 0, sizeof(addr)); |
Erik Kline | c40da92 | 2015-12-07 16:09:39 +0900 | [diff] [blame] | 39 | addr.sin6_family = AF_INET6; |
| 40 | addr.sin6_port = htons(port); |
| 41 | addr.sin6_addr = in6addr_any; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Erik Kline | c40da92 | 2015-12-07 16:09:39 +0900 | [diff] [blame] | 43 | s = socket(AF_INET6, type, 0); |
| 44 | if (s < 0) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
| 46 | n = 1; |
Mark Salyzyn | 02a7c3a | 2014-05-05 08:49:13 -0700 | [diff] [blame] | 47 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof(n)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
Erik Kline | c40da92 | 2015-12-07 16:09:39 +0900 | [diff] [blame] | 49 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | close(s); |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | if (type == SOCK_STREAM) { |
| 55 | int ret; |
| 56 | |
| 57 | ret = listen(s, LISTEN_BACKLOG); |
| 58 | |
| 59 | if (ret < 0) { |
| 60 | close(s); |
| 61 | return -1; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return s; |
| 66 | } |