1 | #ifndef READ_SOCKET
2 | #define READ_SOCKET
3 | /***************************************
4 | $Revision: 1.7 $
5 |
6 | Socket module (sk)
7 |
8 | Status: NOT REVUED, NOT TESTED
9 |
10 | ******************/ /******************
11 | Copyright (c) 1999 RIPE NCC
12 |
13 | All Rights Reserved
14 |
15 | Permission to use, copy, modify, and distribute this software and its
16 | documentation for any purpose and without fee is hereby granted,
17 | provided that the above copyright notice appear in all copies and that
18 | both that copyright notice and this permission notice appear in
19 | supporting documentation, and that the name of the author not be
20 | used in advertising or publicity pertaining to distribution of the
21 | software without specific, written prior permission.
22 |
23 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
25 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
26 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 | ***************************************/
30 |
31 | #include "stubs.h"
32 |
33 | #ifdef HAVE_SYS_TIME_H
34 | #include <sys/time.h>
35 | #endif /* HAVE_SYS_TIME_H */
36 |
37 | #ifdef HAVE_UNISTD_H
38 | #include <unistd.h>
39 | #endif /* HAVE_UNISTD_H */
40 |
41 | /* #include <arpa/inet.h> */
42 | #include <sys/types.h>
43 | #include <sys/socket.h>
44 | #include <netinet/in.h>
45 | #include <netdb.h>
46 |
47 | #include <pthread.h>
48 | #include <stdlib.h>
49 | #include <errno.h>
50 | #include <fcntl.h>
51 |
52 | #include <signal.h>
53 | #include <stdio.h>
54 |
55 | #include "iproutines.h"
56 |
57 | /* connection data -> helps keep track of all errors etc */
58 | typedef struct {
59 | int sock; /* socket descriptor # */
60 | struct timeval rd_timeout; /* preset timeout values */
61 | struct timeval wr_timeout;
62 | unsigned short rtc; /* RTC flags (reason-to-close) */
63 |
64 | pthread_t watchdog; /* thread id of the watchdog associated */
65 | pthread_t killthis; /* thread to be killed by watchdog */
66 | void * (*execthis)(void *); /* function to be called if watchdog triggers */
67 | void * execargs; /* argument to be passed to that function */
68 | pthread_mutex_t watchmutex;
69 |
70 | unsigned char lasterr; /* timeout, interrupt, etc. */
71 | ip_addr_t rIP; /* real IP */
72 | ip_addr_t eIP; /* effective IP */
73 | char *ip; /* text of the eIP */
74 | } sk_conn_st;
75 |
76 | /* reasons to close: socket-wise .... */
77 | #define SK_DISCONNECT 0x0001
78 | #define SK_INTERRUPT 0x0002
79 | #define SK_TIMEOUT 0x0004
80 |
81 | /* ... and user-wise: */
82 | #define SK_NOTEXT 0x0100
83 |
84 | int SK_atoport(const char *service, const char *proto);
85 | int SK_close(int socket);
86 | int SK_getsock(int socket_type, unsigned port, int backlog, uint32_t bind_address);
87 | er_ret_t SK_connect(int *sock, char *hostname, unsigned port, unsigned timeout);
88 | int SK_accept_connection(int listening_socket);
89 | int SK_read(int sockfd, char *buf, size_t count);
90 | int SK_write(int sockfd, const char *buf, size_t count);
91 | int SK_gets(int sockfd, char *str, size_t count);
92 | int SK_puts(int sockfd, const char *str);
93 | int SK_putc(int sockfd, char ch);
94 | int SK_getc(int sockfd);
95 | char *SK_getpeername(int sockfd);
96 | int SK_getpeerip(int sockfd, ip_addr_t *ip);
97 |
98 | void SK_cd_make(sk_conn_st *condat, int sock, unsigned timeout);
99 | void SK_cd_free(sk_conn_st *condat);
100 | int SK_cd_puts(sk_conn_st *condat, const char *str);
101 | int SK_cd_gets(sk_conn_st *condat, char *str, size_t count);
102 | int SK_cd_close(sk_conn_st *condat);
103 | int SK_cd_printf(sk_conn_st *condat, char *txt, ...)
104 | #ifdef __GNUC__ /* let gcc check the format string for problems */
105 | __attribute__ ((format (printf, 2, 3)))
106 | #endif
107 | ;
108 | void SK_init(void);
109 |
110 | er_ret_t SK_watchstart(sk_conn_st *condat);
111 | er_ret_t SK_watchstop(sk_conn_st *condat);
112 | void SK_watch_setkill(sk_conn_st *condat, pthread_t killthis);
113 | void SK_watch_setexec( sk_conn_st *condat, void *(*function)(void *), void *args);
114 | void SK_watch_setclear(sk_conn_st *condat);
115 | void SK_watchexec(sk_conn_st *condat);
116 | void SK_watchkill(sk_conn_st *condat);
117 | void SK_watchtrigger(sk_conn_st *condat);
118 |
119 | #endif /* READ_SOCKET */