*** /dev/null Sat Nov 23 02:31:21 2024
--- - Sat Nov 23 02:31:32 2024
***************
*** 0 ****
--- 1,129 ----
+ /*
+ ** L2 - OSSP Logging Library
+ ** Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
+ ** Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
+ **
+ ** This file is part of OSSP L2, a flexible logging library which
+ ** can be found at http://www.ossp.org/pkg/l2/.
+ **
+ ** Permission to use, copy, modify, and distribute this software for
+ ** any purpose with or without fee is hereby granted, provided that
+ ** the above copyright notice and this permission notice appear in all
+ ** copies.
+ **
+ ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ ** SUCH DAMAGE.
+ **
+ ** l2_sockmon.c: Socket monitor for use with l2_test.c
+ */
+
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <stdio.h>
+ #include <errno.h>
+
+ #define BACKLOG 1024 /* max # pending connections */
+ #define BUFFSIZ 256 /* max string size */
+
+ void do_work(int sock)
+ {
+ char buf[BUFFSIZ];
+ int cc;
+
+ /* ASSERT(sock); */
+
+ while (1) { /* Exits when read finishes */
+ cc = read(sock, buf, sizeof (buf));
+ if (cc == -1) {
+ perror("read");
+ exit(1);
+ }
+
+ if (cc == 0) {
+ /* EOF */
+ (void) close(sock);
+ (void) printf("Connection closed\n");
+ return;
+ }
+
+ buf[cc + 1] = '\0';
+ (void) printf("Read: %s", buf);
+
+ if (write(sock, buf, cc) == -1) {
+ perror("write");
+ exit(1);
+ }
+ }
+ }
+
+ int myserver(int port)
+ {
+ struct sockaddr_in laddr, faddr ;
+ int sock, new_sock, sock_opt;
+ socklen_t faddrlen ;
+
+ /* ASSERT(port); */
+ /* Set up an IPv4 TCP socket to listen on for connections. */
+ laddr.sin_family = AF_INET;
+ laddr.sin_port = port;
+ laddr.sin_addr.s_addr = INADDR_ANY;
+ sock = socket(PF_INET, SOCK_STREAM, 0);
+
+ if (sock == -1) {
+ perror("socket");
+ return (-1);
+ }
+
+ /* Tell the system to allow local addresses to be reused. */
+ sock_opt = 1;
+ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&sock_opt, sizeof (sock_opt)) == -1) {
+ perror("setsockopt(SO_REUSEADDR)");
+ (void) close(sock);
+ return (-1);
+ }
+ if (bind(sock, (struct sockaddr *)&laddr, sizeof (laddr)) == -1) {
+ perror("bind"); /* Bad bind */
+ (void) close(sock);
+ return (-1);
+ }
+ if (listen(sock, BACKLOG) == -1) {
+ perror("listen"); /* Listen just blew up */
+ (void) close(sock);
+ return (-1);
+ }
+ /* Wait for a connection request. */
+ for (;;) {
+ faddrlen = sizeof (faddr);
+ new_sock = accept(sock, (struct sockaddr *)&faddr, &faddrlen);
+ if (new_sock == -1) {
+ if (errno != EINTR && errno != ECONNABORTED) {
+ perror("accept");
+ }
+ continue;
+ }
+ (void) printf("Connection from %s/%d\n", inet_ntoa(faddr.sin_addr), ntohs(faddr.sin_port));
+ do_work(new_sock); /* do some sockwork */
+ }
+ /*NOTREACHED*/
+ }
+
+ int main(int argc, char *argv[])
+ {
+ if (argc != 2) {
+ (void) fprintf(stderr, "Usage: %s <port>\n", argv[0]);
+ exit(1); /* Dink donk if the user doesn't know how to use this */
+ }
+ (void) myserver(htons(atoi(argv[1])));
+ return 0;
+ }
|