|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/nettest/tcp/test_tcp_peername.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <errno.h> |
| 28 | +#include <netinet/in.h> |
| 29 | +#include <poll.h> |
| 30 | +#include <setjmp.h> |
| 31 | +#include <stdarg.h> |
| 32 | +#include <stddef.h> |
| 33 | +#include <stdint.h> |
| 34 | +#include <stdlib.h> |
| 35 | +#include <string.h> |
| 36 | +#include <unistd.h> |
| 37 | +#include <sys/socket.h> |
| 38 | +#include <cmocka.h> |
| 39 | + |
| 40 | +#include "test_tcp.h" |
| 41 | +#include "utils.h" |
| 42 | + |
| 43 | +/**************************************************************************** |
| 44 | + * Pre-processor Definitions |
| 45 | + ****************************************************************************/ |
| 46 | + |
| 47 | +#define TEST_POLL 1000 /* ms, loopback answers at once */ |
| 48 | + |
| 49 | +/**************************************************************************** |
| 50 | + * Private Types |
| 51 | + ****************************************************************************/ |
| 52 | + |
| 53 | +struct tcp_peername_state_s |
| 54 | +{ |
| 55 | + int fds[3]; |
| 56 | +}; |
| 57 | + |
| 58 | +/**************************************************************************** |
| 59 | + * Public Functions |
| 60 | + ****************************************************************************/ |
| 61 | + |
| 62 | +/**************************************************************************** |
| 63 | + * Name: test_tcp_peername_setup |
| 64 | + ****************************************************************************/ |
| 65 | + |
| 66 | +int test_tcp_peername_setup(FAR void **state) |
| 67 | +{ |
| 68 | + FAR struct tcp_peername_state_s *st = malloc(sizeof(*st)); |
| 69 | + |
| 70 | + assert_non_null(st); |
| 71 | + st->fds[0] = -1; |
| 72 | + st->fds[1] = -1; |
| 73 | + st->fds[2] = -1; |
| 74 | + *state = st; |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +/**************************************************************************** |
| 79 | + * Name: test_tcp_peername_teardown |
| 80 | + ****************************************************************************/ |
| 81 | + |
| 82 | +int test_tcp_peername_teardown(FAR void **state) |
| 83 | +{ |
| 84 | + FAR struct tcp_peername_state_s *st = *state; |
| 85 | + int i; |
| 86 | + |
| 87 | + for (i = 0; i < 3; i++) |
| 88 | + { |
| 89 | + if (st->fds[i] >= 0) |
| 90 | + { |
| 91 | + close(st->fds[i]); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + free(st); |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | +/**************************************************************************** |
| 100 | + * Name: test_tcp_peername_ipv6 |
| 101 | + * |
| 102 | + * Description: |
| 103 | + * On a connection over ::1, getpeername() of each end gives the address |
| 104 | + * and the port that getsockname() of the other end gives. |
| 105 | + * |
| 106 | + ****************************************************************************/ |
| 107 | + |
| 108 | +void test_tcp_peername_ipv6(FAR void **state) |
| 109 | +{ |
| 110 | + FAR struct tcp_peername_state_s *st = *state; |
| 111 | + struct sockaddr_in6 listen_addr; |
| 112 | + struct sockaddr_in6 local; |
| 113 | + struct sockaddr_in6 peer; |
| 114 | + socklen_t addrlen; |
| 115 | + struct pollfd pfd; |
| 116 | + |
| 117 | + st->fds[0] = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 118 | + assert_return_code(st->fds[0], errno); |
| 119 | + nettest_lo_addr((FAR struct sockaddr *)&listen_addr, AF_INET6); |
| 120 | + listen_addr.sin6_port = 0; |
| 121 | + assert_return_code(bind(st->fds[0], (FAR struct sockaddr *)&listen_addr, |
| 122 | + sizeof(listen_addr)), errno); |
| 123 | + assert_return_code(listen(st->fds[0], 1), errno); |
| 124 | + addrlen = sizeof(listen_addr); |
| 125 | + assert_return_code(getsockname(st->fds[0], |
| 126 | + (FAR struct sockaddr *)&listen_addr, |
| 127 | + &addrlen), errno); |
| 128 | + |
| 129 | + st->fds[1] = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 130 | + assert_return_code(st->fds[1], errno); |
| 131 | + assert_return_code(connect(st->fds[1], |
| 132 | + (FAR struct sockaddr *)&listen_addr, |
| 133 | + sizeof(listen_addr)), errno); |
| 134 | + pfd.fd = st->fds[0]; |
| 135 | + pfd.events = POLLIN; |
| 136 | + pfd.revents = 0; |
| 137 | + assert_int_equal(poll(&pfd, 1, TEST_POLL), 1); |
| 138 | + st->fds[2] = accept4(st->fds[0], NULL, NULL, SOCK_CLOEXEC); |
| 139 | + assert_return_code(st->fds[2], errno); |
| 140 | + |
| 141 | + /* The client: its peer is the listener's address and port */ |
| 142 | + |
| 143 | + addrlen = sizeof(local); |
| 144 | + assert_return_code(getsockname(st->fds[1], (FAR struct sockaddr *)&local, |
| 145 | + &addrlen), errno); |
| 146 | + addrlen = sizeof(peer); |
| 147 | + memset(&peer, 0, sizeof(peer)); |
| 148 | + assert_return_code(getpeername(st->fds[1], (FAR struct sockaddr *)&peer, |
| 149 | + &addrlen), errno); |
| 150 | + assert_int_equal(addrlen, sizeof(peer)); |
| 151 | + assert_int_equal(peer.sin6_family, AF_INET6); |
| 152 | + assert_true(IN6_IS_ADDR_LOOPBACK(&peer.sin6_addr)); |
| 153 | + assert_int_equal(ntohs(peer.sin6_port), ntohs(listen_addr.sin6_port)); |
| 154 | + assert_int_not_equal(ntohs(peer.sin6_port), ntohs(local.sin6_port)); |
| 155 | + |
| 156 | + /* The accepted end: its peer is the client's port */ |
| 157 | + |
| 158 | + addrlen = sizeof(peer); |
| 159 | + memset(&peer, 0, sizeof(peer)); |
| 160 | + assert_return_code(getpeername(st->fds[2], (FAR struct sockaddr *)&peer, |
| 161 | + &addrlen), errno); |
| 162 | + assert_true(IN6_IS_ADDR_LOOPBACK(&peer.sin6_addr)); |
| 163 | + assert_int_equal(ntohs(peer.sin6_port), ntohs(local.sin6_port)); |
| 164 | +} |
0 commit comments