summaryrefslogtreecommitdiff
path: root/src/include/libpq/libpq-be.h
blob: 4c65e7769b8ec6300c03e8e459cbb3e1ed5652e0 (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
/*-------------------------------------------------------------------------
 *
 * libpq-be.h--
 *	  This file contains definitions for structures and
 *	  externs for functions used by the POSTGRES backend.
 *
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 * $Id: libpq-be.h,v 1.13 1999/01/11 03:56:11 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#ifndef LIBPQ_BE_H
#define LIBPQ_BE_H

#include <stdio.h>
#include <sys/types.h>

#include "libpq/pqcomm.h"
#include "libpq/hba.h"


/* Protocol v0 password packet. */

typedef struct PasswordPacketV0
{
	uint32		unused;
	char		data[288];		/* User and password as strings. */
} PasswordPacketV0;


/*
 * Password packet.  The length of the password can be changed without
 * affecting anything.
 */

typedef struct PasswordPacket
{
	char		passwd[100];	/* The password. */
} PasswordPacket;


/* Error message packet. */

typedef struct ErrorMessagePacket
{
	char		data[1 + 100];	/* 'E' + the message. */
} ErrorMessagePacket;


/* Authentication request packet. */

typedef struct AuthRequestPacket
{
	char		data[1 + sizeof(AuthRequest) + 2];		/* 'R' + the request +
														 * optional salt. */
} AuthRequestPacket;


/* These are used by the packet handling routines. */

typedef enum
{
	Idle,
	ReadingPacketLength,
	ReadingPacket,
	WritingPacket
} PacketState;

typedef int (*PacketDoneProc) (void *arg, PacketLen pktlen, void *pktdata);

typedef struct Packet
{
	PacketState state;			/* What's in progress. */
	PacketLen	len;			/* Actual length */
	int			nrtodo;			/* Bytes still to transfer */
	char	   *ptr;			/* Buffer pointer */
	PacketDoneProc iodone;		/* I/O complete callback */
	void	   *arg;			/* Argument to callback */

	/*
	 * We declare the data buffer as a union of the allowed packet types,
	 * mainly to ensure that enough space is allocated for the largest
	 * one.
	 */

	union
	{
		/* These are outgoing so have no packet length prepended. */

		ErrorMessagePacket em;
		AuthRequestPacket ar;

		/* These are incoming and have a packet length prepended. */

		StartupPacket si;
		CancelRequestPacket canc;
		PasswordPacketV0 pwv0;
		PasswordPacket pw;
	}			pkt;
} Packet;


/*
 * This is used by the postmaster in its communication with frontends.	It is
 * contains all state information needed during this communication before the
 * backend is run.
 */

typedef struct Port
{
	int			sock;			/* File descriptor */
	Packet		pktInfo;		/* For the packet handlers */
	SockAddr	laddr;			/* local addr (us) */
	SockAddr	raddr;			/* remote addr (them) */
	char		salt[2];		/* Password salt */

	/*
	 * Information that needs to be held during the fe/be authentication
	 * handshake.
	 */

	ProtocolVersion proto;
	char		database[SM_DATABASE + 1];
	char		user[SM_USER + 1];
	char		options[SM_OPTIONS + 1];
	char		tty[SM_TTY + 1];
	char		auth_arg[MAX_AUTH_ARG];
	UserAuth	auth_method;
} Port;


extern ProtocolVersion FrontendProtocol;


/*
 * prototypes for functions in pqpacket.c
 */
void		PacketReceiveSetup(Packet *pkt, PacketDoneProc iodone, void *arg);
int			PacketReceiveFragment(Packet *pkt, int sock);
void		PacketSendSetup(Packet *pkt, int nbytes, PacketDoneProc iodone, void *arg);
int			PacketSendFragment(Packet *pkt, int sock);
void		PacketSendError(Packet *pkt, char *errormsg);

#endif	 /* LIBPQ_BE_H */