~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to tags/mysql-proxy-0.6.1/src/network-socket.h

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2007 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */ 
 
15
 
 
16
#ifndef _NETWORK_SOCKET_H_
 
17
#define _NETWORK_SOCKET_H_
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
#include "config.h"
 
21
#endif
 
22
 
 
23
#ifdef HAVE_SYS_TIME_H
 
24
/**
 
25
 * event.h needs struct timeval and doesn't include sys/time.h itself
 
26
 */
 
27
#include <sys/time.h>
 
28
#endif
 
29
 
 
30
#include <sys/types.h>      /** u_char */
 
31
#ifndef _WIN32
 
32
#include <sys/socket.h>     /** struct sockaddr */
 
33
 
 
34
#ifdef HAVE_NETINET_IN_H
 
35
#include <netinet/in.h>     /** struct sockaddr_in */
 
36
#endif
 
37
#include <netinet/tcp.h>
 
38
 
 
39
#ifdef HAVE_SYS_UN_H
 
40
#include <sys/un.h>         /** struct sockaddr_un */
 
41
#endif
 
42
/**
 
43
 * use closesocket() to close sockets to be compatible with win32
 
44
 */
 
45
#define closesocket(x) close(x)
 
46
#else
 
47
#include <winsock2.h>
 
48
 
 
49
#define socklen_t int
 
50
#endif
 
51
#include <glib.h>
 
52
#include <event.h>
 
53
 
 
54
/* a input or output stream */
 
55
typedef struct {
 
56
        GQueue *chunks;
 
57
 
 
58
        size_t len; /* len in all chunks */
 
59
        size_t offset; /* offset over all chunks */
 
60
} network_queue;
 
61
 
 
62
typedef struct {
 
63
        union {
 
64
                struct sockaddr_in ipv4;
 
65
#ifdef HAVE_SYS_UN_H
 
66
                struct sockaddr_un un;
 
67
#endif
 
68
                struct sockaddr common;
 
69
        } addr;
 
70
 
 
71
        gchar *str;
 
72
 
 
73
        socklen_t len;
 
74
} network_address;
 
75
 
 
76
typedef struct {
 
77
        int fd;             /**< socket-fd */
 
78
        struct event event; /**< events for this fd */
 
79
 
 
80
        network_address addr;
 
81
 
 
82
        guint32 packet_len; /**< the packet_len is a 24bit unsigned int */
 
83
        guint8  packet_id;  /**< id which increments for each packet in the stream */
 
84
        
 
85
 
 
86
        network_queue *recv_queue;
 
87
        network_queue *send_queue;
 
88
 
 
89
        GString *header; /** raw buffer for the packet_len and packet_id */
 
90
        off_t header_read;
 
91
        off_t to_read;
 
92
        
 
93
        /**
 
94
         * data extracted from the handshake  
 
95
         *
 
96
         * all server-side only
 
97
         */
 
98
        guint32 mysqld_version;  /**< numberic version of the version string */
 
99
        guint32 thread_id;       /**< connection-id, set in the handshake packet */
 
100
        GString *scramble_buf;   /**< the 21byte scramble-buf */
 
101
        GString *username;       /**< username of the authed connection */
 
102
        GString *scrambled_password;  /**< scrambled_pass used to auth */
 
103
 
 
104
        /**
 
105
         * store the auth-handshake packet to simulate a login
 
106
         */
 
107
        GString *auth_handshake_packet;
 
108
        int is_authed;           /** did a client already authed this connection */
 
109
 
 
110
        /**
 
111
         * store the default-db of the socket
 
112
         *
 
113
         * the client might have a different default-db than the server-side due to
 
114
         * statement balancing
 
115
         */     
 
116
        GString *default_db;     /** default-db of this side of the connection */
 
117
} network_socket;
 
118
 
 
119
 
 
120
network_queue *network_queue_init(void);
 
121
void network_queue_free(network_queue *queue);
 
122
int network_queue_append(network_queue *queue, const char *data, size_t len, int packet_id);
 
123
int network_queue_append_chunk(network_queue *queue, GString *chunk);
 
124
 
 
125
network_socket *network_socket_init(void);
 
126
void network_socket_free(network_socket *s);
 
127
 
 
128
#endif
 
129