summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/protocol.cc
blob: d859de2f6bbc008814113c19c2471b600450bfe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Copyright (C) 2003 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "protocol.h"

#include "messages.h"

#include <mysql_com.h>
#include <m_string.h>


static char eof_buff[1]= { (char) 254 };        /* Marker for end of fields */
static const char ERROR_PACKET_CODE= (char) 255;


int net_send_ok(struct st_net *net, unsigned long connection_id,
                const char *message)
{
  /*
            The format of a packet
    1                             packet type code
    1-9                           affected rows count
    1-9                           connection id
    2                             thread return status
    2                             warning count
    1-9 + message length          message to send (isn't stored if no message)
  */
  Buffer buff;
  char *pos= buff.buffer;

  /* check that we have space to hold mandatory fields */
  buff.reserve(0, 23);

  enum { OK_PACKET_CODE= 0 };
  *pos++= OK_PACKET_CODE;
  pos= net_store_length(pos, (ulonglong) 0);
  pos= net_store_length(pos, (ulonglong) connection_id);
  int2store(pos, *net->return_status);
  pos+= 2;
  /* We don't support warnings, so store 0 for total warning count */
  int2store(pos, 0);
  pos+= 2;

  uint position= pos - buff.buffer; /* we might need it for message */

  if (message != NULL)
  {
    buff.reserve(position, 9 + strlen(message));
    store_to_protocol_packet(&buff, message, &position);
  }

  return my_net_write(net, buff.buffer, position) || net_flush(net);
}


int net_send_error(struct st_net *net, uint sql_errno)
{
  const char *err= message(sql_errno);
  char buff[1 +                                 // packet type code
            2 +                                 // sql error number
            1 + SQLSTATE_LENGTH +               // sql state
            MYSQL_ERRMSG_SIZE];                 // message
  char *pos= buff;

  *pos++= ERROR_PACKET_CODE;
  int2store(pos, sql_errno);
  pos+= 2;
  /* The first # is to make the protocol backward compatible */
  *pos++= '#';
  memcpy(pos, errno_to_sqlstate(sql_errno), SQLSTATE_LENGTH);
  pos+= SQLSTATE_LENGTH;
  pos= strmake(pos, err, MYSQL_ERRMSG_SIZE - 1) + 1;
  return my_net_write(net, buff, pos - buff) || net_flush(net);
}


int net_send_error_323(struct st_net *net, uint sql_errno)
{
  const char *err= message(sql_errno);
  char buff[1 +                                 // packet type code
            2 +                                 // sql error number
            MYSQL_ERRMSG_SIZE];                 // message
  char *pos= buff;

  *pos++= ERROR_PACKET_CODE;
  int2store(pos, sql_errno);
  pos+= 2;
  pos= strmake(pos, err, MYSQL_ERRMSG_SIZE - 1) + 1;
  return my_net_write(net, buff, pos - buff) || net_flush(net);
}

char *net_store_length(char *pkg, uint length)
{
  uchar *packet=(uchar*) pkg;
  if (length < 251)
  {
    *packet=(uchar) length;
    return (char*) packet+1;
  }
  *packet++=252;
  int2store(packet,(uint) length);
  return (char*) packet+2;
}


int store_to_protocol_packet(Buffer *buf, const char *string, uint *position,
                             uint string_len)
{
  uint currpos;

  /* reserve max amount of bytes needed to store length */
  if (buf->reserve(*position, 9))
    goto err;
  currpos= (net_store_length(buf->buffer + *position,
                             (ulonglong) string_len) - buf->buffer);
  if (buf->append(currpos, string, string_len))
    goto err;
  *position= *position + string_len + (currpos - *position);

  return 0;
err:
  return 1;
}


int store_to_protocol_packet(Buffer *buf, const char *string, uint *position)
{
  uint string_len;

  string_len= strlen(string);
  return store_to_protocol_packet(buf, string, position, string_len);
}


int send_eof(struct st_net *net)
{
  uchar buff[1 + /* eof packet code */
	     2 + /* warning count */
	     2]; /* server status */

  buff[0]=254;
  int2store(buff+1, 0);
  int2store(buff+3, 0);
  return my_net_write(net, (char*) buff, sizeof buff);
}

int send_fields(struct st_net *net, LIST *fields)
{
  LIST *tmp= fields;
  Buffer send_buff;
  char small_buff[4];
  uint position= 0;
  LEX_STRING *field;

  /* send the number of fileds */
  net_store_length(small_buff, (uint) list_length(fields));
  if (my_net_write(net, small_buff, (uint) 1))
    goto err;

  while (tmp)
  {
    position= 0;
    field= (LEX_STRING *) tmp->data;

    store_to_protocol_packet(&send_buff,
                             (char*) "", &position);    /* catalog name */
    store_to_protocol_packet(&send_buff,
                             (char*) "", &position);    /* db name */
    store_to_protocol_packet(&send_buff,
                             (char*) "", &position);    /* table name */
    store_to_protocol_packet(&send_buff,
                             (char*) "", &position);    /* table name alias */
    store_to_protocol_packet(&send_buff,
                             field->str, &position);    /* column name */
    store_to_protocol_packet(&send_buff,
                             field->str, &position);    /* column name alias */
    send_buff.reserve(position, 12);
    if (send_buff.is_error())
      goto err;
    send_buff.buffer[position++]= 12;
    int2store(send_buff.buffer + position, 1);          /* charsetnr */
    int4store(send_buff.buffer + position + 2,
              field->length);                           /* field length */
    send_buff.buffer[position+6]= (char) MYSQL_TYPE_STRING;    /* type */
    int2store(send_buff.buffer + position + 7, 0);      /* flags */
    send_buff.buffer[position + 9]= (char) 0;           /* decimals */
    send_buff.buffer[position + 10]= 0;
    send_buff.buffer[position + 11]= 0;
    position+= 12;
    if (my_net_write(net, send_buff.buffer, (uint) position+1))
      goto err;
    tmp= list_rest(tmp);
  }

  if (my_net_write(net, eof_buff, 1))
    goto err;
  return 0;

err:
  return 1;
}