1*eabc0478Schristos /* $NetBSD: test-eof.c,v 1.6 2024/08/18 20:47:23 christos Exp $ */ 28585484eSchristos 38585484eSchristos /* 48585484eSchristos * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> 58585484eSchristos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 68585484eSchristos * 78585484eSchristos * Redistribution and use in source and binary forms, with or without 88585484eSchristos * modification, are permitted provided that the following conditions 98585484eSchristos * are met: 108585484eSchristos * 1. Redistributions of source code must retain the above copyright 118585484eSchristos * notice, this list of conditions and the following disclaimer. 128585484eSchristos * 2. Redistributions in binary form must reproduce the above copyright 138585484eSchristos * notice, this list of conditions and the following disclaimer in the 148585484eSchristos * documentation and/or other materials provided with the distribution. 158585484eSchristos * 3. The name of the author may not be used to endorse or promote products 168585484eSchristos * derived from this software without specific prior written permission. 178585484eSchristos * 188585484eSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198585484eSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208585484eSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218585484eSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228585484eSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238585484eSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248585484eSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258585484eSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268585484eSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278585484eSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288585484eSchristos */ 29*eabc0478Schristos #include "../util-internal.h" 308585484eSchristos #include "event2/event-config.h" 318585484eSchristos 328585484eSchristos #ifdef _WIN32 338585484eSchristos #include <winsock2.h> 348585484eSchristos #else 358585484eSchristos #include <unistd.h> 368585484eSchristos #endif 378585484eSchristos #include <sys/types.h> 388585484eSchristos #include <sys/stat.h> 398585484eSchristos #ifdef EVENT__HAVE_SYS_TIME_H 408585484eSchristos #include <sys/time.h> 418585484eSchristos #endif 428585484eSchristos #ifdef EVENT__HAVE_SYS_SOCKET_H 438585484eSchristos #include <sys/socket.h> 448585484eSchristos #endif 458585484eSchristos #include <fcntl.h> 468585484eSchristos #include <stdlib.h> 478585484eSchristos #include <stdio.h> 488585484eSchristos #include <string.h> 498585484eSchristos #include <errno.h> 508585484eSchristos 518585484eSchristos #include <event.h> 528585484eSchristos #include <evutil.h> 538585484eSchristos 548585484eSchristos int test_okay = 1; 558585484eSchristos int called = 0; 568585484eSchristos struct timeval timeout = {60, 0}; 578585484eSchristos 588585484eSchristos static void 598585484eSchristos read_cb(evutil_socket_t fd, short event, void *arg) 608585484eSchristos { 618585484eSchristos char buf[256]; 628585484eSchristos int len; 638585484eSchristos 648585484eSchristos if (EV_TIMEOUT & event) { 658585484eSchristos printf("%s: Timeout!\n", __func__); 668585484eSchristos exit(1); 678585484eSchristos } 688585484eSchristos 698585484eSchristos len = recv(fd, buf, sizeof(buf), 0); 708585484eSchristos 718585484eSchristos printf("%s: read %d%s\n", __func__, 728585484eSchristos len, len ? "" : " - means EOF"); 738585484eSchristos 748585484eSchristos if (len) { 758585484eSchristos if (!called) 768585484eSchristos event_add(arg, &timeout); 778585484eSchristos } else if (called == 1) 788585484eSchristos test_okay = 0; 798585484eSchristos 808585484eSchristos called++; 818585484eSchristos } 828585484eSchristos 838585484eSchristos int 848585484eSchristos main(int argc, char **argv) 858585484eSchristos { 868585484eSchristos struct event ev; 878585484eSchristos const char *test = "test string"; 888585484eSchristos evutil_socket_t pair[2]; 898585484eSchristos 908585484eSchristos #ifdef _WIN32 918585484eSchristos WORD wVersionRequested; 928585484eSchristos WSADATA wsaData; 938585484eSchristos 948585484eSchristos wVersionRequested = MAKEWORD(2, 2); 958585484eSchristos 968585484eSchristos (void) WSAStartup(wVersionRequested, &wsaData); 978585484eSchristos #endif 988585484eSchristos 998585484eSchristos if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1008585484eSchristos return (1); 1018585484eSchristos 1028585484eSchristos 1038585484eSchristos if (send(pair[0], test, (int)strlen(test)+1, 0) < 0) 1048585484eSchristos return (1); 105*eabc0478Schristos shutdown(pair[0], EVUTIL_SHUT_WR); 1068585484eSchristos 107*eabc0478Schristos /* Initialize the event library */ 1088585484eSchristos event_init(); 1098585484eSchristos 110*eabc0478Schristos /* Initialize one event */ 1118585484eSchristos event_set(&ev, pair[1], EV_READ | EV_TIMEOUT, read_cb, &ev); 1128585484eSchristos 1138585484eSchristos event_add(&ev, &timeout); 1148585484eSchristos 1158585484eSchristos event_dispatch(); 1168585484eSchristos 1178585484eSchristos return (test_okay); 1188585484eSchristos } 1198585484eSchristos 120