1 #ifndef AS_SOCKET_H_HEADER_INCLUDED 2 #define AS_SOCKET_H_HEADER_INCLUDED 3 4 #include "aslist.h" 5 struct ASBiDirList; 6 7 /* socket setup code : */ 8 int socket_connect_client (const char *socket_name); 9 int socket_listen (const char *socket_name); 10 11 /* Network to local 32bit binary conversion : (Network is Big Endian) */ 12 13 #ifdef WORDS_BIGENDIAN 14 #define as_ntohl(ui32) (ui32) 15 #define as_hlton(ui32) (ui32) 16 #define as_ntohl16(ui16) (ui16) 17 #define as_hlton16(ui16) (ui16) 18 #else 19 #define as_ntohl(ui32) ((((ui32)&0x000000FF)<<24)|(((ui32)&0x0000FF00)<<8)|(((ui32)&0x00FF0000)>>8)|(((ui32)&0xFF000000)>>24)) 20 #define as_hlton(ui32) as_ntohl(ui32) /* conversion is symmetrical */ 21 #define as_ntohl16(ui16) ((((ui16)&0x00FF)<<8)|(((ui16)&0xFF00)>>8)) 22 #define as_hlton16(ui16) as_ntohl(ui16) /* conversion is symmetrical */ 23 #endif 24 25 /* simple buffered write operations : */ 26 typedef struct ASSocketBuffer 27 { 28 int fd ; 29 #define AS_SOCK_BUFFER_SIZE 2048 /* single page */ 30 int bytes_in ; 31 CARD8 buffer[AS_SOCK_BUFFER_SIZE] ; 32 }ASSocketBuffer; 33 34 void socket_buffered_write (ASSocketBuffer *sb, const void *data, int size); 35 void socket_write_int32 (ASSocketBuffer *sb, CARD32 *data, size_t items ); 36 void socket_write_int16 (ASSocketBuffer *sb, CARD16 *data, size_t items ); 37 void socket_write_string (ASSocketBuffer *sb, const char *string); 38 void socket_write_flush ( ASSocketBuffer *sb ); 39 40 /* More complex signal safe and overflow safe socket write operations (FIFO): */ 41 typedef struct ASFIFOPacket 42 { 43 int ref_count ; /* number of queues we are participating in */ 44 time_t added_time ; /* so we can implement per-packet timeouts */ 45 size_t bytes_in, bytes_out ; 46 CARD8 *buffer ; 47 }ASFIFOPacket; 48 49 typedef struct ASFIFOQueue 50 { 51 /* output pipe : */ 52 int fd ; 53 enum { 54 ASFS_Empty = 0, 55 ASFS_WritePending, 56 ASFS_Closed 57 } state ; 58 /* actuall queue : */ 59 struct ASBiDirList queue ; 60 }ASFIFOQueue; 61 62 ASFIFOPacket *form_fifo_packet( CARD8* buffer, size_t size ); 63 void dereference_packet( ASFIFOPacket **ppacket ); /* destroys packet when its ref_count reaches 0 */ 64 65 ASFIFOQueue *create_fifo( int fd ); 66 size_t add_fifo_packet( ASFIFOQueue *fifo, ASFIFOPacket *packet ); 67 size_t send_fifo( ASFIFOQueue *fifo ); /* attempts to send next packet */ 68 void purge_fifo( ASFIFOQueue *fifo ); 69 void destroy_fifo( ASFIFOQueue **pfifo); 70 71 /* signal safe socket read operations : */ 72 73 typedef struct ASProtocolItemSpec 74 { 75 #define AS_PROTOCOL_ITEM_NONE -1 76 #define AS_PROTOCOL_ITEM_STRING 0 77 #define AS_PROTOCOL_ITEM_BYTE 1 78 #define AS_PROTOCOL_ITEM_INT16 2 79 #define AS_PROTOCOL_ITEM_INT32 4 80 int type ; /* one of the above items */ 81 size_t max_size ; 82 }ASProtocolItemSpec; 83 84 typedef struct ASProtocolSpec 85 { 86 ASProtocolItemSpec *items ; /* list terminated by element AS_PROTOCOL_ITEM_NONE */ 87 size_t items_num ; 88 time_t timeout ; 89 }ASProtocolSpec; 90 91 typedef struct ASProtocolItem 92 { 93 size_t size, size_bytes ; 94 size_t bytes_allocated, bytes_read; 95 union { 96 void *memory; 97 char *string; 98 CARD8 *byte ; 99 CARD16 *int16 ; 100 CARD32 *int32 ; 101 }d; 102 }ASProtocolItem; 103 104 typedef struct ASProtocolState 105 { 106 struct ASProtocolSpec *specs; 107 struct ASProtocolItem *items ; 108 unsigned int curr_item ; 109 time_t last_read_time ; 110 int fd ; 111 }ASProtocolState; 112 113 /* returns : */ 114 typedef enum { 115 ASP_SocketError = -3, 116 ASP_Timeout = -2, 117 ASP_BadData = -1, 118 ASP_WaitData = 0, 119 ASP_Success = 1 120 }ASProtocolResult; 121 122 ASProtocolResult socket_read_proto_item( ASProtocolState *ps ); 123 ASProtocolResult socket_read_proto( ASProtocolState *ps ); 124 void socket_read_proto_reset( ASProtocolState *ps ); 125 void *socket_read_steal_buffer( ASProtocolState *ps ); 126 127 #endif /* AS_SOCKET_H_HEADER_INCLUDED */ 128 129