Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 Cisco and/or its affiliates. |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __sock_test_h__ |
| 17 | #define __sock_test_h__ |
| 18 | |
| 19 | #include <netdb.h> |
| 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 23 | #include <vcl/vcl_test.h> |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 24 | |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 25 | #define SOCK_TEST_AF_UNIX_FILENAME "/tmp/ldp_server_af_unix_socket" |
| 26 | #define SOCK_TEST_MIXED_EPOLL_DATA "Hello, world! (over an AF_UNIX socket)" |
| 27 | #define SOCK_TEST_AF_UNIX_ACCEPT_DATA 0xaf0000af |
| 28 | #define SOCK_TEST_AF_UNIX_FD_MASK 0x00af0000 |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 29 | #define SOCK_TEST_BANNER_STRING \ |
| 30 | "============================================\n" |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 31 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 32 | static inline int |
| 33 | sock_test_read (int fd, uint8_t *buf, uint32_t nbytes, |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 34 | vcl_test_stats_t *stats) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 35 | { |
| 36 | int rx_bytes, errno_val; |
| 37 | |
| 38 | do |
| 39 | { |
| 40 | if (stats) |
| 41 | stats->rx_xacts++; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 42 | rx_bytes = read (fd, buf, nbytes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 43 | if (stats) |
| 44 | { |
| 45 | if ((rx_bytes == 0) || |
| 46 | ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))) |
| 47 | stats->rx_eagain++; |
| 48 | else if (rx_bytes < nbytes) |
| 49 | stats->rx_incomp++; |
| 50 | } |
| 51 | } |
| 52 | while ((rx_bytes == 0) || |
| 53 | ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))); |
| 54 | |
| 55 | if (rx_bytes < 0) |
| 56 | { |
| 57 | errno_val = errno; |
| 58 | perror ("ERROR in sock_test_read()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 59 | fprintf (stderr, "SOCK_TEST: ERROR: socket read " |
| 60 | "failed (errno = %d)!\n", errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 61 | errno = errno_val; |
| 62 | } |
| 63 | else if (stats) |
| 64 | stats->rx_bytes += rx_bytes; |
| 65 | |
| 66 | return (rx_bytes); |
| 67 | } |
| 68 | |
| 69 | static inline int |
| 70 | sock_test_write (int fd, uint8_t *buf, uint32_t nbytes, |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 71 | vcl_test_stats_t *stats, uint32_t verbose) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 72 | { |
| 73 | int tx_bytes = 0; |
| 74 | int nbytes_left = nbytes; |
| 75 | int rv, errno_val; |
| 76 | |
| 77 | do |
| 78 | { |
| 79 | if (stats) |
| 80 | stats->tx_xacts++; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 81 | rv = write (fd, buf, nbytes_left); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 82 | if (rv < 0) |
| 83 | { |
| 84 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 85 | { |
| 86 | if (stats) |
| 87 | stats->tx_eagain++; |
| 88 | continue; |
| 89 | } |
| 90 | else |
| 91 | break; |
| 92 | } |
| 93 | tx_bytes += rv; |
| 94 | |
| 95 | if (tx_bytes != nbytes) |
| 96 | { |
| 97 | nbytes_left = nbytes_left - rv; |
| 98 | if (stats) |
| 99 | stats->tx_incomp++; |
| 100 | if (verbose) |
| 101 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 102 | printf ("SOCK_TEST: WARNING: bytes written (%d) " |
| 103 | "!= bytes to write (%d)!\n", tx_bytes, nbytes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | } while (tx_bytes != nbytes); |
| 108 | |
| 109 | if (tx_bytes < 0) |
| 110 | { |
| 111 | errno_val = errno; |
| 112 | perror ("ERROR in sock_test_write()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 113 | fprintf (stderr, "SOCK_TEST: ERROR: socket write failed " |
| 114 | "(errno = %d)!\n", errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 115 | } |
| 116 | else if (stats) |
| 117 | stats->tx_bytes += tx_bytes; |
| 118 | |
| 119 | return (tx_bytes); |
| 120 | } |
| 121 | |
| 122 | #endif /* __sock_test_h__ */ |