1 /* 2 * Copyright (c) 2003 Rinet Corp., Novosibirsk, Russia 3 * 4 * Redistribution and use in source forms, with and without modification, 5 * are permitted provided that this entire comment appears intact. 6 * 7 * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND. 8 */ 9 10 #ifndef _SESSION_H_ 11 #define _SESSION_H_ 12 13 #include <sys/types.h> 14 #include <sys/socket.h> 15 #include <sys/time.h> 16 #include <sys/select.h> 17 #include <netinet/in.h> 18 19 /* 20 * Session handler. 21 */ 22 23 /* currently supported session types */ 24 typedef enum { 25 PlainFile, /* simple I/O with a file descriptor */ 26 TextStream, /* CRLFed text lines exchange through TCP */ 27 DataSequence /* raw binary data exchange through UDP */ 28 } SessionType; 29 30 struct session_binder_ent; 31 32 #ifndef HAVE_SOCKADDR_STORAGE 33 34 /* 35 * RFC 2553: protocol-independent placeholder for socket addresses 36 */ 37 #define _SS_MAXSIZE 128 38 #define _SS_ALIGNSIZE (sizeof(u_int64_t)) 39 #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(u_char) * 2) 40 #define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(u_char) * 2 - \ 41 _SS_PAD1SIZE - _SS_ALIGNSIZE) 42 43 struct sockaddr_storage { 44 u_char ss_len; /* address length */ 45 u_char ss_family; /* address family */ 46 char __ss_pad1[_SS_PAD1SIZE]; 47 u_int64_t __ss_align; /* force desired structure storage alignment */ 48 char __ss_pad2[_SS_PAD2SIZE]; 49 }; 50 51 #endif /* HAVE_SOCKADDR_STORAGE */ 52 53 typedef struct session_ent { 54 u_long sid; /* session id (must not be zero!) */ 55 56 /* user supplied parameters */ 57 int sock; /* socket file descriptor */ 58 struct sockaddr_storage peer; /* remote peer address and port */ 59 struct sockaddr_storage from; /* recvfrom peer */ 60 SessionType type; /* session type, see above */ 61 unsigned timeout; /* reply timeout in seconds */ 62 63 /* internal */ 64 struct timeval expire; /* time until first timeout */ 65 char *buf; /* temporary I/O buffer */ 66 67 /* user callback functions */ 68 void (*connected)(struct session_ent *sd); 69 void (*read_error)(struct session_ent *sd, int error); 70 void (*read_data)(struct session_ent *sd, const unsigned char *data, int len); 71 72 const void *cookie; /* user defined container, cast it yourself */ 73 74 struct session_binder_ent *sb; /* session binder container */ 75 76 struct session_ent *next; 77 } SESSION; 78 79 SESSION *session_open(int sock, const struct sockaddr *peer, SessionType type); 80 int session_sock(SESSION *sd); 81 int session_start(SESSION *sd); 82 void session_stop(SESSION *sd); 83 int session_idle(SESSION *sd); 84 void session_free(SESSION *sd); 85 void session_setcallback(SESSION *sd, 86 void (*connected)(SESSION *sd), 87 void (*read_error)(SESSION *sd, int error), 88 void (*read_data)(SESSION *sd, const unsigned char *data, int len)); 89 void session_setcookie(SESSION *sd, const void *cookie); 90 const void *session_cookie(SESSION *sd); 91 unsigned session_settimeout(SESSION *sd, unsigned timeout); 92 int session_send(SESSION *sd, const unsigned char *data, int len); 93 int session_select(int *nfds, fd_set *readfds, fd_set *writefds, 94 struct timeval *timeout, int *block); 95 void session_operate(fd_set *readfds, fd_set *writefds); 96 void session_timeout(); 97 const struct sockaddr *session_peer(SESSION *sd); 98 const struct sockaddr *session_from(SESSION *sd); 99 SESSION *session_find(const struct sockaddr *peer, SessionType type); 100 101 int session_bind(SESSION *sd, void (*notify)(void *arg), void *arg); 102 void session_unbind(SESSION *sd, void (*notify)(void *arg), void *arg); 103 104 int socket_nonblock(int sock, int on); 105 int socket_keepalive(int sock, int on); 106 int socket_error(int sock); 107 int socket_peer(struct sockaddr *peer, int sock); 108 int socket_name(struct sockaddr *name, int sock); 109 110 #endif /* _SESSION_H_ */ 111