diff options
author | Colin Cross <ccross@android.com> | 2014-03-21 16:59:20 -0700 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2014-03-21 17:05:29 -0700 |
commit | a63a9ec8085f43a9eb49ada8d50cac1431944b18 (patch) | |
tree | 13582ece0bc8542614f9a10e0ef8b1f0fadeda35 /libnl_2/socket.c | |
parent | f8f0531bb69a90e87f1bafa875e665e3935610b1 (diff) | |
download | system_core-a63a9ec8085f43a9eb49ada8d50cac1431944b18.zip system_core-a63a9ec8085f43a9eb49ada8d50cac1431944b18.tar.gz system_core-a63a9ec8085f43a9eb49ada8d50cac1431944b18.tar.bz2 |
DO NOT MERGE: Revert "delete libnl_2"
This reverts commit 7097f052d946bc9fbe298c7a88e1d943f54f684e.
libnl_2 needs to stay in AOSP for now for compatibility with
GPL test builds.
Diffstat (limited to 'libnl_2/socket.c')
-rw-r--r-- | libnl_2/socket.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/libnl_2/socket.c b/libnl_2/socket.c new file mode 100644 index 0000000..e94eb9e --- /dev/null +++ b/libnl_2/socket.c @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* NOTICE: This is a clean room re-implementation of libnl */ + +#include <errno.h> +#include <unistd.h> +#include <malloc.h> +#include <sys/time.h> +#include <sys/socket.h> +#include "netlink-types.h" + +/* Join group */ +int nl_socket_add_membership(struct nl_sock *sk, int group) +{ + return setsockopt(sk->s_fd, SOL_NETLINK, + NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)); +} + +/* Allocate new netlink socket. */ +static struct nl_sock *_nl_socket_alloc(void) +{ + struct nl_sock *sk; + struct timeval tv; + struct nl_cb *cb; + + sk = (struct nl_sock *) malloc(sizeof(struct nl_sock)); + if (!sk) + return NULL; + memset(sk, 0, sizeof(*sk)); + + /* Get current time */ + + if (gettimeofday(&tv, NULL)) + goto fail; + else + sk->s_seq_next = (int) tv.tv_sec; + + /* Create local socket */ + sk->s_local.nl_family = AF_NETLINK; + sk->s_local.nl_pid = 0; /* Kernel fills in pid */ + sk->s_local.nl_groups = 0; /* No groups */ + + /* Create peer socket */ + sk->s_peer.nl_family = AF_NETLINK; + sk->s_peer.nl_pid = 0; /* Kernel */ + sk->s_peer.nl_groups = 0; /* No groups */ + + return sk; +fail: + free(sk); + return NULL; +} + +/* Allocate new netlink socket. */ +struct nl_sock *nl_socket_alloc(void) +{ + struct nl_sock *sk = _nl_socket_alloc(); + struct nl_cb *cb; + + if (!sk) + return NULL; + + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) + goto cb_fail; + sk->s_cb = cb; + return sk; +cb_fail: + free(sk); + return NULL; +} + +/* Allocate new socket with custom callbacks. */ +struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb) +{ + struct nl_sock *sk = _nl_socket_alloc(); + + if (!sk) + return NULL; + + sk->s_cb = cb; + nl_cb_get(cb); + + return sk; +} + +/* Free a netlink socket. */ +void nl_socket_free(struct nl_sock *sk) +{ + nl_cb_put(sk->s_cb); + close(sk->s_fd); + free(sk); +} + +/* Sets socket buffer size of netlink socket */ +int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf) +{ + if (setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF, \ + &rxbuf, (socklen_t) sizeof(rxbuf))) + goto error; + + if (setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF, \ + &txbuf, (socklen_t) sizeof(txbuf))) + goto error; + + return 0; +error: + return -errno; + +} + +int nl_socket_get_fd(struct nl_sock *sk) +{ + return sk->s_fd; +} + +void nl_socket_set_cb(struct nl_sock *sk, struct nl_cb *cb) +{ + nl_cb_put(sk->s_cb); + sk->s_cb = cb; + nl_cb_get(cb); +} + +struct nl_cb *nl_socket_get_cb(struct nl_sock *sk) +{ + return nl_cb_get(sk->s_cb); +} |