summaryrefslogtreecommitdiff
path: root/sys/src/cmd/aquarela/smbresponse.c
blob: a07ce8d705f9f86ab56ca65e4ea5bef8a6107a38 (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
#include "headers.h"

void
smbresponsereset(SmbSession *s)
{
	smbbufferreset(s->response);
}

void
smbresponseinit(SmbSession *s, ushort maxlen)
{
	smbbufferfree(&s->response);
	s->response = smbbuffernew(maxlen);
}

int
smbresponsealignl2(SmbSession *s, int l2a)
{
	return smbbufferalignl2(s->response, l2a);
}

int
smbresponseputheader(SmbSession *s, SmbHeader *h, uchar errclass, ushort error)
{
	h->errclass = errclass;
	h->error = error;
	return smbbufferputheader(s->response, h, &s->peerinfo);
}

int
smbresponseputb(SmbSession *s, uchar b)
{
	return smbbufferputb(s->response, b);
}

ushort
smbresponsespace(SmbSession *sess)
{
	return smbbufferwritespace(sess->response);
}

int
smbresponseskip(SmbSession *sess, ushort amount)
{
	return smbbufferputbytes(sess->response, nil, amount);
}

int
smbresponseoffsetputs(SmbSession *sess, ushort offset, ushort s)
{
	return smbbufferoffsetputs(sess->response, offset, s);
}

int
smbresponseputs(SmbSession *sess, ushort s)
{
	return smbbufferputs(sess->response, s);
}

int
smbresponseputl(SmbSession *s, ulong l)
{
	return smbbufferputl(s->response, l);
}

int
smbresponsecpy(SmbSession *s, uchar *data, ushort datalen)
{
	return smbbufferputbytes(s->response, data, datalen);
}

int
smbresponseputstring(SmbSession *s, int mustalign, char *string)
{
	return smbbufferputstring(s->response, &s->peerinfo, mustalign ? 0 : SMB_STRING_UNALIGNED, string);
}

int
smbresponseputstr(SmbSession *s, char *string)
{
	return smbbufferputstring(s->response, nil, SMB_STRING_ASCII, string);
}

ushort
smbresponseoffset(SmbSession *s)
{
	return smbbufferwriteoffset(s->response);
}

SmbProcessResult
smbresponsesend(SmbSession *s)
{
	uchar cmd;
	SmbProcessResult pr;

	assert(smbbufferoffsetgetb(s->response, 4, &cmd));
smbloglock();
smblogprint(cmd, "sending:\n");
smblogdata(cmd, smblogprint, smbbufferreadpointer(s->response), smbbufferreadspace(s->response), 256);
smblogunlock();
	if (s->nbss) {
		NbScatterGather a[2];
		a[0].p = smbbufferreadpointer(s->response);
		a[0].l = smbbufferreadspace(s->response);
		a[1].p = nil;
		nbssgatherwrite(s->nbss, a);
		pr = SmbProcessResultOk;
	}
	else if (s->cifss) {
		ulong l = smbbufferreadspace(s->response);
		uchar nl[4];
		hnputl(nl, l);
		write(s->cifss->fd, nl, 4);
		write(s->cifss->fd, smbbufferreadpointer(s->response), l);
		pr = SmbProcessResultOk;
	}
	else
		pr = SmbProcessResultDie;
	smbbufferreset(s->response);
	return pr;
}

int
smbresponseputandxheader(SmbSession *s, SmbHeader *h, ushort andxcommand, ulong *andxoffsetfixupp)
{
	return smbbufferputandxheader(s->response, h, &s->peerinfo, andxcommand, andxoffsetfixupp);
}

int
smbresponseputerror(SmbSession *s, SmbHeader *h, uchar errclass, ushort error)
{
	h->wordcount = 0;
	return smbresponseputheader(s, h, errclass, error)
		&& smbresponseputs(s, 0);
}