123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /* This file is part of libepistle.
- *
- * libepistle is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * libepistle is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with libepistle. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <unistd.h>
- #include "network.h"
- int
- network_server(uint16_t port, int backlog, Err *err)
- {
- int reuseaddr = 1;
- struct sockaddr_in6 addr = { 0 };
- int sock = socket(AF_INET6, SOCK_STREAM, 0);
- if (sock < 0) {
- err_std(err);
- return -1;
- }
- addr.sin6_family = AF_INET6;
- addr.sin6_port = htons(port);
- addr.sin6_addr = in6addr_any;
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
- &reuseaddr, sizeof(reuseaddr));
- if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0 ||
- listen(sock, backlog < 0 ? SOMAXCONN : backlog) < 0) {
- err_std(err);
- close(sock);
- return -1;
- }
- return sock;
- }
- int
- network_client(struct in6_addr in6_addr, uint16_t port, Err *err)
- {
- struct sockaddr_in6 addr = { 0 };
- int sock = socket(AF_INET6, SOCK_STREAM, 0);
- if (sock < 0) {
- err_std(err);
- return -1;
- }
- addr.sin6_family = AF_INET6;
- addr.sin6_port = htons(port);
- addr.sin6_addr = in6_addr;
- if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- err_std(err);
- close(sock);
- return -1;
- }
- return sock;
- }
|