Zeng Linggang | 370a3a4 | 2015-10-21 11:15:23 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Fujitsu Ltd. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef __SAFE_NET_H__ |
| 20 | #define __SAFE_NET_H__ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
| 25 | #include <arpa/inet.h> |
| 26 | #include <sys/un.h> |
| 27 | |
| 28 | char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen, char *res, |
| 29 | size_t len); |
| 30 | |
| 31 | int safe_socket(const char *file, const int lineno, void (cleanup_fn)(void), |
| 32 | int domain, int type, int protocol); |
| 33 | #define SAFE_SOCKET(cleanup_fn, domain, type, protocol) \ |
| 34 | safe_socket(__FILE__, __LINE__, (cleanup_fn), domain, type, protocol) |
| 35 | |
| 36 | int safe_bind(const char *file, const int lineno, void (cleanup_fn)(void), |
| 37 | int socket, const struct sockaddr *address, |
| 38 | socklen_t address_len); |
| 39 | #define SAFE_BIND(cleanup_fn, socket, address, address_len) \ |
| 40 | safe_bind(__FILE__, __LINE__, (cleanup_fn), socket, address, \ |
| 41 | address_len) |
| 42 | |
| 43 | int safe_listen(const char *file, const int lineno, void (cleanup_fn)(void), |
| 44 | int socket, int backlog); |
| 45 | #define SAFE_LISTEN(cleanup_fn, socket, backlog) \ |
| 46 | safe_listen(__FILE__, __LINE__, (cleanup_fn), socket, backlog) |
| 47 | |
| 48 | int safe_connect(const char *file, const int lineno, void (cleanup_fn)(void), |
| 49 | int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
| 50 | #define SAFE_CONNECT(cleanup_fn, sockfd, addr, addrlen) \ |
| 51 | safe_connect(__FILE__, __LINE__, (cleanup_fn), sockfd, addr, addrlen) |
| 52 | |
| 53 | int safe_getsockname(const char *file, const int lineno, |
| 54 | void (cleanup_fn)(void), int sockfd, struct sockaddr *addr, |
| 55 | socklen_t *addrlen); |
| 56 | #define SAFE_GETSOCKNAME(cleanup_fn, sockfd, addr, addrlen) \ |
| 57 | safe_getsockname(__FILE__, __LINE__, (cleanup_fn), sockfd, addr, \ |
| 58 | addrlen) |
| 59 | |
| 60 | #endif /* __SAFE_NET_H__ */ |