Enable Lua tcp tap userdata.
[obnox/wireshark/wip.git] / epan / dissectors / packet-pgsql.c
1 /* packet-pgsql.c
2  * Routines for PostgreSQL v3 protocol dissection.
3  * <http://www.postgresql.org/docs/current/static/protocol.html>
4  * Copyright 2004 Abhijit Menon-Sen <ams@oryx.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/conversation.h>
34 #include <epan/prefs.h>
35
36 #include "packet-tcp.h"
37
38
39 static int proto_pgsql = -1;
40 static int hf_frontend = -1;
41 static int hf_type = -1;
42 static int hf_length = -1;
43 static int hf_parameter_name = -1;
44 static int hf_parameter_value = -1;
45 static int hf_query = -1;
46 static int hf_authtype = -1;
47 static int hf_passwd = -1;
48 static int hf_salt = -1;
49 static int hf_statement = -1;
50 static int hf_portal = -1;
51 static int hf_tag = -1;
52 static int hf_status = -1;
53 static int hf_copydata = -1;
54 static int hf_error = -1;
55 static int hf_pid = -1;
56 static int hf_key = -1;
57 static int hf_condition = -1;
58 static int hf_text = -1;
59 static int hf_tableoid = -1;
60 static int hf_typeoid = -1;
61 static int hf_oid = -1;
62 static int hf_format = -1;
63 static int hf_val_name = -1;
64 static int hf_val_idx = -1;
65 static int hf_val_length = -1;
66 static int hf_val_data = -1;
67 static int hf_val_mod = -1;
68 static int hf_severity = -1;
69 static int hf_code = -1;
70 static int hf_message = -1;
71 static int hf_detail = -1;
72 static int hf_hint = -1;
73 static int hf_position = -1;
74 static int hf_where = -1;
75 static int hf_file = -1;
76 static int hf_line = -1;
77 static int hf_routine = -1;
78
79 static gint ett_pgsql = -1;
80 static gint ett_values = -1;
81
82 static guint pgsql_port = 5432;
83 static gboolean pgsql_desegment = TRUE;
84 static gboolean first_message = TRUE;
85
86 static void dissect_pgsql_fe_msg(guchar, guint, tvbuff_t *, gint, proto_tree *);
87 static void dissect_pgsql_be_msg(guchar, guint, tvbuff_t *, gint, proto_tree *);
88 static void dissect_pgsql_msg(tvbuff_t *, packet_info *, proto_tree *);
89 static void dissect_pgsql(tvbuff_t *, packet_info *, proto_tree *);
90 static guint pgsql_length(packet_info *, tvbuff_t *, int);
91
92 static const value_string fe_messages[] = {
93     { 'p', "Password message" },
94     { 'Q', "Simple query" },
95     { 'P', "Parse" },
96     { 'B', "Bind" },
97     { 'E', "Execute" },
98     { 'D', "Describe" },
99     { 'C', "Close" },
100     { 'H', "Flush" },
101     { 'S', "Sync" },
102     { 'F', "Function call" },
103     { 'd', "Copy data" },
104     { 'c', "Copy completion" },
105     { 'f', "Copy failure" },
106     { 'X', "Termination" },
107     { 0, NULL }
108 };
109
110 static const value_string be_messages[] = {
111     { 'R', "Authentication request" },
112     { 'K', "Backend key data" },
113     { 'S', "Parameter status" },
114     { '1', "Parse completion" },
115     { '2', "Bind completion" },
116     { '3', "Close completion" },
117     { 'C', "Command completion" },
118     { 't', "Parameter description" },
119     { 'T', "Row description" },
120     { 'D', "Data row" },
121     { 'I', "Empty query" },
122     { 'n', "No data" },
123     { 'E', "Error" },
124     { 'N', "Notice" },
125     { 's', "Portal suspended" },
126     { 'Z', "Ready for query" },
127     { 'A', "Notification" },
128     { 'V', "Function call response" },
129     { 'G', "CopyIn response" },
130     { 'H', "CopyOut response" },
131     { 'd', "Copy data" },
132     { 'c', "Copy completion" },
133     { 0, NULL }
134 };
135
136
137 static const value_string auth_types[] = {
138     { 0, "Success" },
139     { 1, "Kerberos V4" },
140     { 2, "Kerberos V5" },
141     { 3, "Plaintext password" },
142     { 4, "crypt()ed password" },
143     { 5, "MD5 password" },
144     { 6, "SCM credentials" },
145     { 0, NULL }
146 };
147
148 static const value_string status_vals[] = {
149     { 'I', "Idle" },
150     { 'T', "In a transaction" },
151     { 'E', "In a failed transaction" },
152     { 0, NULL }
153 };
154
155 static const value_string format_vals[] = {
156     { 0, "Text" },
157     { 1, "Binary" },
158     { 0, NULL }
159 };
160
161 /* This function is called once per TCP packet. It sets COL_PROTOCOL and
162  * identifies FE/BE messages by adding a ">" or "<" to COL_INFO. Then it
163  * arranges for each message to be dissected individually. */
164
165 static void
166 dissect_pgsql(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
167 {
168     conversation_t *cv;
169
170     first_message = TRUE;
171
172     /* We don't use conversation data yet, but... */
173     cv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
174                            pinfo->srcport, pinfo->destport, 0);
175     if (!cv) {
176         cv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
177                               pinfo->srcport, pinfo->destport, 0);
178     }
179
180     if (check_col(pinfo->cinfo, COL_PROTOCOL))
181         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PGSQL");
182     if (check_col(pinfo->cinfo, COL_INFO))
183         col_set_str(pinfo->cinfo, COL_INFO,
184                     (pinfo->match_port == pinfo->destport) ?
185                      ">" : "<");
186
187     tcp_dissect_pdus(tvb, pinfo, tree, pgsql_desegment, 5,
188                      pgsql_length, dissect_pgsql_msg);
189 }
190
191
192 /* This function is called by tcp_dissect_pdus() to find the size of the
193    message starting at tvb[offset]. */
194
195 static guint
196 pgsql_length(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
197 {
198     gint n = 0;
199     guchar type;
200     guint length;
201
202     /* The length is either the four bytes after the type, or, if the
203        type is 0, the first four bytes. */
204     type = tvb_get_guint8(tvb, offset);
205     if (type != '\0')
206         n = 1;
207     length = tvb_get_ntohl(tvb, offset+n);
208     return length+n;
209 }
210
211
212 /* This function is responsible for dissecting a single message. */
213
214 static void
215 dissect_pgsql_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
216 {
217     proto_item *ti, *hidden_item;
218     proto_tree *ptree;
219
220     gint n;
221     guchar type;
222     const char *typestr;
223     guint length;
224     gboolean info = check_col(pinfo->cinfo, COL_INFO);
225     gboolean fe = (pinfo->match_port == pinfo->destport);
226
227     n = 0;
228     type = tvb_get_guint8(tvb, 0);
229     if (type != '\0')
230         n += 1;
231     length = tvb_get_ntohl(tvb, n);
232
233     /* This is like specifying VALS(messages) for hf_type, which we can't do
234        directly because of messages without type bytes, and because the type
235        interpretation depends on fe. */
236     if (fe) {
237         /* There are a few frontend messages that have no leading type byte.
238            We identify them by the fact that the first byte of their length
239            must be zero, and that the next four bytes are a unique tag. */
240         if (type == '\0') {
241             guint tag = tvb_get_ntohl(tvb, 4);
242
243             if (length == 16 && tag == 80877102)
244                 typestr = "Cancel request";
245             else if (length == 8 && tag == 80877103)
246                 typestr = "SSL request";
247             else if (tag == 196608)
248                 typestr = "Startup message";
249             else
250                 typestr = "Unknown";
251         } else
252             typestr = val_to_str(type, fe_messages, "Unknown");
253     }
254     else {
255         typestr = val_to_str(type, be_messages, "Unknown");
256     }
257
258     if (info) {
259         /* This is a terrible hack. It makes the "Info" column reflect
260            the contents of every message in a TCP packet. Could it be
261            done any better? */
262         col_append_fstr(pinfo->cinfo, COL_INFO, "%s%c",
263                         ( first_message ? "" : "/" ), type);
264         first_message = FALSE;
265     }
266
267     if (tree) {
268         ti = proto_tree_add_item(tree, proto_pgsql, tvb, 0, -1, FALSE);
269         ptree = proto_item_add_subtree(ti, ett_pgsql);
270
271         n = 1;
272         if (type == '\0')
273             n = 0;
274         proto_tree_add_text(ptree, tvb, 0, n, "Type: %s", typestr);
275         hidden_item = proto_tree_add_item(ptree, hf_type, tvb, 0, n, FALSE);
276         PROTO_ITEM_SET_HIDDEN(hidden_item);
277         proto_tree_add_item(ptree, hf_length, tvb, n, 4, FALSE);
278         hidden_item = proto_tree_add_boolean(ptree, hf_frontend, tvb, 0, 0, fe);
279         PROTO_ITEM_SET_HIDDEN(hidden_item);
280         n += 4;
281
282         if (fe)
283             dissect_pgsql_fe_msg(type, length, tvb, n, ptree);
284         else
285             dissect_pgsql_be_msg(type, length, tvb, n, ptree);
286     }
287 }
288
289
290 static void dissect_pgsql_fe_msg(guchar type, guint length, tvbuff_t *tvb,
291                                  gint n, proto_tree *tree)
292 {
293     guchar c;
294     gint i, l;
295     char *s, *t;
296     proto_item *ti, *hidden_item;
297     proto_tree *shrub;
298
299     switch (type) {
300     /* Password */
301     case 'p':
302         s = tvb_get_ephemeral_stringz(tvb, n, &l);
303         proto_tree_add_string(tree, hf_passwd, tvb, n, l, s);
304         break;
305
306     /* Simple query */
307     case 'Q':
308         s = tvb_get_ephemeral_stringz(tvb, n, &l);
309         proto_tree_add_string(tree, hf_query, tvb, n, l, s);
310         break;
311
312     /* Parse */
313     case 'P':
314         s = tvb_get_ephemeral_stringz(tvb, n, &l);
315         proto_tree_add_string(tree, hf_statement, tvb, n, l, s);
316         n += l;
317
318         s = tvb_get_ephemeral_stringz(tvb, n, &l);
319         proto_tree_add_string(tree, hf_query, tvb, n, l, s);
320         n += l;
321
322         i = tvb_get_ntohs(tvb, n);
323         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameters: %d", i);
324         shrub = proto_item_add_subtree(ti, ett_values);
325         n += 2;
326         while (i-- > 0) {
327             proto_tree_add_item(shrub, hf_typeoid, tvb, n, 4, FALSE);
328             n += 4;
329         }
330         break;
331
332     /* Bind */
333     case 'B':
334         s = tvb_get_ephemeral_stringz(tvb, n, &l);
335         proto_tree_add_string(tree, hf_portal, tvb, n, l, s);
336         n += l;
337
338         s = tvb_get_ephemeral_stringz(tvb, n, &l);
339         proto_tree_add_string(tree, hf_statement, tvb, n, l, s);
340         n += l;
341
342         i = tvb_get_ntohs(tvb, n);
343         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter formats: %d", i);
344         shrub = proto_item_add_subtree(ti, ett_values);
345         n += 2;
346         while (i-- > 0) {
347             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
348             n += 2;
349         }
350
351         i = tvb_get_ntohs(tvb, n);
352         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter values: %d", i);
353         shrub = proto_item_add_subtree(ti, ett_values);
354         n += 2;
355         while (i-- > 0) {
356             l = tvb_get_ntohl(tvb, n);
357             proto_tree_add_int(shrub, hf_val_length, tvb, n, 4, l);
358             n += 4;
359             if (l > 0) {
360                 proto_tree_add_item(shrub, hf_val_data, tvb, n, l, FALSE);
361                 n += l;
362             }
363         }
364
365         i = tvb_get_ntohs(tvb, n);
366         ti = proto_tree_add_text(tree, tvb, n, 2, "Result formats: %d", i);
367         shrub = proto_item_add_subtree(ti, ett_values);
368         n += 2;
369         while (i-- > 0) {
370             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
371             n += 2;
372         }
373         break;
374
375     /* Execute */
376     case 'E':
377         s = tvb_get_ephemeral_stringz(tvb, n, &l);
378         proto_tree_add_string(tree, hf_portal, tvb, n, l, s);
379         n += l;
380
381         ti = proto_tree_add_text(tree, tvb, n, 4, "Returns: ");
382         i = tvb_get_ntohl(tvb, n);
383         if (i == 0)
384             proto_item_append_text(ti, "all");
385         else
386             proto_item_append_text(ti, "%d", i);
387         proto_item_append_text(ti, " rows");
388         break;
389
390     /* Describe, Close */
391     case 'D':
392     case 'C':
393         i = 0;
394         c = tvb_get_guint8(tvb, n);
395         if (c == 'P')
396             i = hf_portal;
397         else
398             i = hf_statement;
399
400         if (i != 0) {
401             n += 1;
402             s = tvb_get_ephemeral_stringz(tvb, n, &l);
403             hidden_item = proto_tree_add_string(tree, i, tvb, n, l, s);
404             PROTO_ITEM_SET_HIDDEN(hidden_item);
405             proto_tree_add_text(
406                 tree, tvb, n-1, l, "%s: %s",
407                 (c == 'P' ? "Portal" : "Statement"), s
408             );
409         }
410         break;
411
412     /* Messages without a type identifier */
413     case '\0':
414         i = tvb_get_ntohl(tvb, n);
415         n += 4;
416         length -= n;
417         switch (i) {
418         /* Startup message */
419         case 196608:
420             while (length > 0) {
421                 s = tvb_get_ephemeral_stringz(tvb, n, &l);
422                 length -= l;
423                 if (length <= 0) {
424                     break;
425                 }
426                 t = tvb_get_ephemeral_stringz(tvb, n+l, &i);
427                 proto_tree_add_text(tree, tvb, n, l+i, "%s: %s", s, t);
428                 n += l+i;
429                 length -= i;
430                 if (length == 1 && tvb_get_guint8(tvb, n) == 0)
431                     break;
432             }
433             break;
434
435         /* SSL request */
436         case 80877103:
437             /* There's nothing to parse here, but what do we do if the
438                SSL negotiation succeeds? */
439             break;
440
441         /* Cancellation request */
442         case 80877102:
443             proto_tree_add_item(tree, hf_pid, tvb, n, 4, FALSE);
444             proto_tree_add_item(tree, hf_key, tvb, n+4, 4, FALSE);
445             break;
446         }
447         break;
448
449     /* Copy data */
450     case 'd':
451         proto_tree_add_item(tree, hf_copydata, tvb, n, length-n+1, FALSE);
452         break;
453
454     /* Copy failure */
455     case 'f':
456         s = tvb_get_ephemeral_stringz(tvb, n, &l);
457         proto_tree_add_string(tree, hf_error, tvb, n, l, s);
458         break;
459
460     /* Function call */
461     case 'F':
462         proto_tree_add_item(tree, hf_oid, tvb, n, 4, FALSE);
463         n += 4;
464
465         i = tvb_get_ntohs(tvb, n);
466         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter formats: %d", i);
467         shrub = proto_item_add_subtree(ti, ett_values);
468         n += 2;
469         while (i-- > 0) {
470             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
471             n += 2;
472         }
473
474         i = tvb_get_ntohs(tvb, n);
475         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter values: %d", i);
476         shrub = proto_item_add_subtree(ti, ett_values);
477         n += 2;
478         while (i-- > 0) {
479             l = tvb_get_ntohl(tvb, n);
480             proto_tree_add_item(shrub, hf_val_length, tvb, n, 4, FALSE);
481             n += 4;
482             if (l > 0) {
483                 proto_tree_add_item(shrub, hf_val_data, tvb, n, l, FALSE);
484                 n += l;
485             }
486         }
487
488         proto_tree_add_item(tree, hf_format, tvb, n, 2, FALSE);
489         break;
490     }
491 }
492
493
494 static void dissect_pgsql_be_msg(guchar type, guint length, tvbuff_t *tvb,
495                                  gint n, proto_tree *tree)
496 {
497     guchar c;
498     gint i, l;
499     char *s, *t;
500     proto_item *ti, *hidden_item;
501     proto_tree *shrub;
502
503     switch (type) {
504     /* Authentication request */
505     case 'R':
506         proto_tree_add_item(tree, hf_authtype, tvb, n, 4, FALSE);
507         i = tvb_get_ntohl(tvb, n);
508         if (i == 4 || i == 5) {
509             /* i -= (6-i); :-) */
510             n += 4;
511             l = (i == 4 ? 2 : 4);
512             proto_tree_add_item(tree, hf_salt, tvb, n, l, FALSE);
513         }
514         break;
515
516     /* Key data */
517     case 'K':
518         proto_tree_add_item(tree, hf_pid, tvb, n, 4, FALSE);
519         proto_tree_add_item(tree, hf_key, tvb, n+4, 4, FALSE);
520         break;
521
522     /* Parameter status */
523     case 'S':
524         s = tvb_get_ephemeral_stringz(tvb, n, &l);
525         hidden_item = proto_tree_add_string(tree, hf_parameter_name, tvb, n, l, s);
526         PROTO_ITEM_SET_HIDDEN(hidden_item);
527         n += l;
528         t = tvb_get_ephemeral_stringz(tvb, n, &i);
529         hidden_item = proto_tree_add_string(tree, hf_parameter_value, tvb, n, i, t);
530         PROTO_ITEM_SET_HIDDEN(hidden_item);
531         proto_tree_add_text(tree, tvb, n-l, l+i, "%s: %s", s, t);
532         break;
533
534     /* Parameter description */
535     case 't':
536         i = tvb_get_ntohs(tvb, n);
537         proto_tree_add_text(tree, tvb, n, 2, "Parameters: %d", i);
538         n += 2;
539         while (i-- > 0) {
540             proto_tree_add_item(tree, hf_typeoid, tvb, n, 4, FALSE);
541             n += 4;
542         }
543         break;
544
545     /* Row description */
546     case 'T':
547         i = tvb_get_ntohs(tvb, n);
548         ti = proto_tree_add_text(tree, tvb, n, 2, "Columns: %d", i);
549         shrub = proto_item_add_subtree(ti, ett_values);
550         n += 2;
551         while (i-- > 0) {
552             proto_tree *twig;
553             s = tvb_get_ephemeral_stringz(tvb, n, &l);
554             ti = proto_tree_add_string(shrub, hf_val_name, tvb, n, l, s);
555             twig = proto_item_add_subtree(ti, ett_values);
556             n += l;
557             proto_tree_add_item(twig, hf_tableoid, tvb, n, 4, FALSE);
558             n += 4;
559             proto_tree_add_item(twig, hf_val_idx, tvb, n, 2, FALSE);
560             n += 2;
561             proto_tree_add_item(twig, hf_typeoid, tvb, n, 4, FALSE);
562             n += 4;
563             proto_tree_add_item(twig, hf_val_length, tvb, n, 2, FALSE);
564             n += 2;
565             proto_tree_add_item(twig, hf_val_mod, tvb, n, 4, FALSE);
566             n += 4;
567             proto_tree_add_item(twig, hf_format, tvb, n, 2, FALSE);
568             n += 2;
569         }
570         break;
571
572     /* Data row */
573     case 'D':
574         i = tvb_get_ntohs(tvb, n);
575         ti = proto_tree_add_text(tree, tvb, n, 2, "Columns: %d", i);
576         shrub = proto_item_add_subtree(ti, ett_values);
577         n += 2;
578         while (i-- > 0) {
579             l = tvb_get_ntohl(tvb, n);
580             proto_tree_add_int(shrub, hf_val_length, tvb, n, 4, l);
581             n += 4;
582             if (l > 0) {
583                 proto_tree_add_item(shrub, hf_val_data, tvb, n, l, FALSE);
584                 n += l;
585             }
586         }
587         break;
588
589     /* Command completion */
590     case 'C':
591         s = tvb_get_ephemeral_stringz(tvb, n, &l);
592         proto_tree_add_string(tree, hf_tag, tvb, n, l, s);
593         break;
594
595     /* Ready */
596     case 'Z':
597         proto_tree_add_item(tree, hf_status, tvb, n, 1, FALSE);
598         break;
599
600     /* Error, Notice */
601     case 'E':
602     case 'N':
603         length -= 4;
604         while (length > 0) {
605             c = tvb_get_guint8(tvb, n);
606             if (c == '\0')
607                 break;
608             s = tvb_get_ephemeral_stringz(tvb, n+1, &l);
609             i = hf_text;
610             switch (c) {
611             case 'S': i = hf_severity; break;
612             case 'C': i = hf_code; break;
613             case 'M': i = hf_message; break;
614             case 'D': i = hf_detail; break;
615             case 'H': i = hf_hint; break;
616             case 'P': i = hf_position; break;
617             case 'W': i = hf_where; break;
618             case 'F': i = hf_file; break;
619             case 'L': i = hf_line; break;
620             case 'R': i = hf_routine; break;
621             }
622             proto_tree_add_string(tree, i, tvb, n, l+1, s);
623             n += l+1;
624         }
625         break;
626
627     /* NOTICE response */
628     case 'A':
629         proto_tree_add_item(tree, hf_pid, tvb, n, 4, FALSE);
630         n += 4;
631         s = tvb_get_ephemeral_stringz(tvb, n, &l);
632         proto_tree_add_string(tree, hf_condition, tvb, n, l, s);
633         n += l;
634         s = tvb_get_ephemeral_stringz(tvb, n, &l);
635         if (l > 1)
636             proto_tree_add_string(tree, hf_text, tvb, n, l, s);
637         break;
638
639     /* Copy in/out */
640     case 'G':
641     case 'H':
642         proto_tree_add_item(tree, hf_format, tvb, n, 1, FALSE);
643         n += 1;
644         i = tvb_get_ntohs(tvb, n);
645         ti = proto_tree_add_text(tree, tvb, n, 2, "Columns: %d", i);
646         shrub = proto_item_add_subtree(ti, ett_values);
647         n += 2;
648         while (i-- > 2) {
649             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
650             n += 2;
651         }
652         break;
653
654     /* Copy data */
655     case 'd':
656         proto_tree_add_item(tree, hf_copydata, tvb, n, length-n+1, FALSE);
657         break;
658
659     /* Function call response */
660     case 'V':
661         l = tvb_get_ntohl(tvb, n);
662         proto_tree_add_int(tree, hf_val_length, tvb, n, 4, l);
663         if (l > 0)
664             proto_tree_add_item(tree, hf_val_data, tvb, n+4, l, FALSE);
665         break;
666     }
667 }
668
669 void
670 proto_reg_handoff_pgsql(void);
671
672 void
673 proto_register_pgsql(void)
674 {
675     static hf_register_info hf[] = {
676         { &hf_frontend,
677           { "Frontend", "pgsql.frontend", FT_BOOLEAN, BASE_NONE, NULL, 0,
678             "True for messages from the frontend, false otherwise.",
679             HFILL }
680         },
681         { &hf_type,
682           { "Type", "pgsql.type", FT_STRING, BASE_NONE, NULL, 0,
683             "A one-byte message type identifier.", HFILL }
684         },
685         { &hf_length,
686           { "Length", "pgsql.length", FT_UINT32, BASE_DEC, NULL, 0,
687             "The length of the message (not including the type).",
688             HFILL }
689         },
690         { &hf_parameter_name,
691           { "Parameter name", "pgsql.parameter_name", FT_STRINGZ,
692             BASE_NONE, NULL, 0, "The name of a database parameter.",
693             HFILL }
694         },
695         { &hf_parameter_value,
696           { "Parameter value", "pgsql.parameter_value", FT_STRINGZ,
697             BASE_NONE, NULL, 0, "The value of a database parameter.",
698             HFILL }
699         },
700         { &hf_query,
701           { "Query", "pgsql.query", FT_STRINGZ, BASE_NONE, NULL, 0,
702             "A query string.", HFILL }
703         },
704         { &hf_passwd,
705           { "Password", "pgsql.password", FT_STRINGZ, BASE_NONE, NULL, 0,
706             "A password.", HFILL }
707         },
708         { &hf_authtype,
709           { "Authentication type", "pgsql.authtype", FT_INT32, BASE_DEC,
710             VALS(auth_types), 0,
711             "The type of authentication requested by the backend.", HFILL }
712         },
713         { &hf_salt,
714           { "Salt value", "pgsql.salt", FT_BYTES, BASE_HEX, NULL, 0,
715             "The salt to use while encrypting a password.", HFILL }
716         },
717         { &hf_statement,
718           { "Statement", "pgsql.statement", FT_STRINGZ, BASE_NONE, NULL, 0,
719             "The name of a prepared statement.", HFILL }
720         },
721         { &hf_portal,
722           { "Portal", "pgsql.portal", FT_STRINGZ, BASE_NONE, NULL, 0,
723             "The name of a portal.", HFILL }
724         },
725         { &hf_tag,
726           { "Tag", "pgsql.tag", FT_STRINGZ, BASE_NONE, NULL, 0,
727             "A completion tag.", HFILL }
728         },
729         { &hf_status,
730           { "Status", "pgsql.status", FT_UINT8, BASE_DEC, VALS(status_vals),
731             0, "The transaction status of the backend.", HFILL }
732         },
733         { &hf_copydata,
734           { "Copy data", "pgsql.copydata", FT_BYTES, BASE_NONE, NULL, 0,
735             "Data sent following a Copy-in or Copy-out response.", HFILL }
736         },
737         { &hf_error,
738           { "Error", "pgsql.error", FT_STRINGZ, BASE_NONE, NULL, 0,
739             "An error message.", HFILL }
740         },
741         { &hf_pid,
742           { "PID", "pgsql.pid", FT_UINT32, BASE_DEC, NULL, 0,
743             "The process ID of a backend.", HFILL }
744         },
745         { &hf_key,
746           { "Key", "pgsql.key", FT_UINT32, BASE_DEC, NULL, 0,
747             "The secret key used by a particular backend.", HFILL }
748         },
749         { &hf_condition,
750           { "Condition", "pgsql.condition", FT_STRINGZ, BASE_NONE, NULL, 0,
751             "The name of a NOTIFY condition.", HFILL }
752         },
753         { &hf_text,
754           { "Text", "pgsql.text", FT_STRINGZ, BASE_NONE, NULL, 0,
755             "Text from the backend.", HFILL }
756         },
757         { &hf_tableoid,
758           { "Table OID", "pgsql.oid.table", FT_UINT32, BASE_DEC, NULL, 0,
759             "The object identifier of a table.", HFILL }
760         },
761         { &hf_typeoid,
762           { "Type OID", "pgsql.oid.type", FT_UINT32, BASE_DEC, NULL, 0,
763             "The object identifier of a type.", HFILL }
764         },
765         { &hf_oid,
766           { "OID", "pgsql.oid", FT_UINT32, BASE_DEC, NULL, 0,
767             "An object identifier.", HFILL }
768         },
769         { &hf_format,
770           { "Format", "pgsql.format", FT_UINT16, BASE_DEC, VALS(format_vals),
771             0, "A format specifier.", HFILL }
772         },
773         { &hf_val_name,
774           { "Column name", "pgsql.col.name", FT_STRINGZ, BASE_NONE, NULL, 0,
775             "The name of a column.", HFILL }
776         },
777         { &hf_val_idx,
778           { "Column index", "pgsql.col.index", FT_UINT32, BASE_DEC, NULL, 0,
779             "The position of a column within a row.", HFILL }
780         },
781         { &hf_val_length,
782           { "Column length", "pgsql.val.length", FT_INT32, BASE_DEC, NULL, 0,
783             "The length of a parameter value, in bytes. -1 means NULL.",
784             HFILL }
785         },
786         { &hf_val_data,
787           { "Data", "pgsql.val.data", FT_BYTES, BASE_NONE, NULL, 0,
788             "Parameter data.", HFILL }
789         },
790         { &hf_val_mod,
791           { "Type modifier", "pgsql.col.typemod", FT_INT32, BASE_DEC, NULL, 0,
792             "The type modifier for a column.", HFILL }
793         },
794         { &hf_severity,
795           { "Severity", "pgsql.severity", FT_STRINGZ, BASE_NONE, NULL, 0,
796             "Message severity.", HFILL }
797         },
798         { &hf_code,
799           { "Code", "pgsql.code", FT_STRINGZ, BASE_NONE, NULL, 0,
800             "SQLState code.", HFILL }
801         },
802         { &hf_message,
803           { "Message", "pgsql.message", FT_STRINGZ, BASE_NONE, NULL, 0,
804             "Error message.", HFILL }
805         },
806         { &hf_detail,
807           { "Detail", "pgsql.detail", FT_STRINGZ, BASE_NONE, NULL, 0,
808             "Detailed error message.", HFILL }
809         },
810         { &hf_hint,
811           { "Hint", "pgsql.hint", FT_STRINGZ, BASE_NONE, NULL, 0,
812             "A suggestion to resolve an error.", HFILL }
813         },
814         { &hf_position,
815           { "Position", "pgsql.position", FT_STRINGZ, BASE_NONE, NULL, 0,
816             "The index of the error within the query string.", HFILL }
817         },
818         { &hf_where,
819           { "Context", "pgsql.where", FT_STRINGZ, BASE_NONE, NULL, 0,
820             "The context in which an error occurred.", HFILL }
821         },
822         { &hf_file,
823           { "File", "pgsql.file", FT_STRINGZ, BASE_NONE, NULL, 0,
824             "The source-code file where an error was reported.", HFILL }
825         },
826         { &hf_line,
827           { "Line", "pgsql.line", FT_STRINGZ, BASE_NONE, NULL, 0,
828             "The line number on which an error was reported.", HFILL }
829         },
830         { &hf_routine,
831           { "Routine", "pgsql.routine", FT_STRINGZ, BASE_NONE, NULL, 0,
832             "The routine that reported an error.", HFILL }
833         }
834     };
835
836     static gint *ett[] = {
837         &ett_pgsql,
838         &ett_values
839     };
840
841     module_t *mod_pgsql;
842
843     proto_pgsql = proto_register_protocol("PostgreSQL", "PGSQL", "pgsql");
844     proto_register_field_array(proto_pgsql, hf, array_length(hf));
845     proto_register_subtree_array(ett, array_length(ett));
846
847     mod_pgsql = prefs_register_protocol(proto_pgsql, proto_reg_handoff_pgsql);
848     prefs_register_uint_preference(
849         mod_pgsql, "tcp.port", "PGSQL TCP port", "Set the port for PGSQL "
850         "messages (if different from the default of 5432)", 10, &pgsql_port
851     );
852 }
853
854 void
855 proto_reg_handoff_pgsql(void)
856 {
857     static gboolean initialized = FALSE;
858     static dissector_handle_t pgsql_handle;
859     static guint saved_pgsql_port;
860
861     if (!initialized) {
862         pgsql_handle = create_dissector_handle(dissect_pgsql, proto_pgsql);
863         initialized = TRUE;
864     } else {
865         dissector_delete("tcp.port", saved_pgsql_port, pgsql_handle);
866     }
867
868     dissector_add("tcp.port", pgsql_port, pgsql_handle);
869     saved_pgsql_port = pgsql_port;
870 }
871
872