1 /* 2 ** server.h - IDENT TCP/IP socket server code 3 ** 4 ** Copyright (c) 1997-2000 Peter Eriksson <pen@lysator.liu.se> 5 ** 6 ** This program is free software; you can redistribute it and/or 7 ** modify it as you wish - as long as you don't claim that you wrote 8 ** it. 9 ** 10 ** This program is distributed in the hope that it will be useful, 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 */ 14 15 #ifndef PLIB_SERVER_H 16 #define PLIB_SERVER_H 17 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <netinet/in.h> 21 22 #include "plib/threads.h" 23 #include "plib/sockaddr.h" 24 25 struct server_info; 26 27 typedef struct 28 { 29 int fd; 30 struct sockaddr_gen rsin; /* Remote address */ 31 struct sockaddr_gen lsin; /* Local address */ 32 struct server_info *sp; 33 } CLIENT; 34 35 typedef struct server_info 36 { 37 int fd; 38 struct sockaddr_gen sin; 39 void (*handler)(CLIENT *cp); 40 41 pthread_mutex_t mtx; 42 int state; /* 0 = not running, 1 = starting/started, 2 = stopping, 3 = failure */ 43 int error; /* Failure reason (errno) */ 44 pthread_t tid; 45 46 int clients_max; 47 int clients_cur; 48 pthread_mutex_t clients_mtx; 49 pthread_cond_t clients_cv; 50 pthread_attr_t ca_detached; 51 } SERVER; 52 53 54 extern SERVER * 55 server_init(int fd, 56 const struct sockaddr_gen *sg, 57 int listen_backlog, 58 int max_clients, 59 void (*handler)(CLIENT *cp)); 60 61 extern int 62 server_start(SERVER *sp); 63 64 extern int 65 server_stop(SERVER *sp); 66 67 extern int 68 server_run_all(SERVER *sp); 69 70 extern int 71 server_run_one(SERVER *sp, 72 int fd, 73 int nofork); 74 75 extern void 76 server_cancel(SERVER *sp); 77 78 #endif 79