98c324f450f8c2baf25c266ffc2376eb50d29852
[framework/uifw/xorg/lib/libxdmcp.git] / Fill.c
1 /*
2 Copyright 1989, 1998  The Open Group
3
4 Permission to use, copy, modify, distribute, and sell this software and its
5 documentation for any purpose is hereby granted without fee, provided that
6 the above copyright notice appear in all copies and that both that
7 copyright notice and this permission notice appear in supporting
8 documentation.
9
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
16 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20 Except as contained in this notice, the name of The Open Group shall not be
21 used in advertising or otherwise to promote the sale, use or other dealings
22 in this Software without prior written authorization from The Open Group.
23  * *
24  * Author:  Keith Packard, MIT X Consortium
25  */
26
27 #ifdef WIN32
28 #define _WILLWINSOCK_
29 #endif
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <X11/Xos.h>
34 #include <X11/X.h>
35 #include <X11/Xmd.h>
36 #include <X11/Xdmcp.h>
37 #include <stdlib.h>
38
39 #ifdef STREAMSCONN
40 #include <tiuser.h>
41 #else
42 #ifdef WIN32
43 #include <X11/Xwinsock.h>
44 #else
45 #include <sys/socket.h>
46 #endif
47 #endif
48
49 int
50 XdmcpFill (int fd, XdmcpBufferPtr buffer, XdmcpNetaddr from, int *fromlen)
51 {
52     BYTE    *newBuf;
53 #ifdef STREAMSCONN
54     struct t_unitdata dataunit;
55     int gotallflag, result;
56 #endif
57
58     if (buffer->size < XDM_MAX_MSGLEN)
59     {
60         newBuf = (BYTE *) malloc(XDM_MAX_MSGLEN);
61         if (newBuf)
62         {
63             free(buffer->data);
64             buffer->data = newBuf;
65             buffer->size = XDM_MAX_MSGLEN;
66         }
67     }
68     buffer->pointer = 0;
69 #ifdef STREAMSCONN
70     dataunit.addr.buf = from;
71     dataunit.addr.maxlen = *fromlen;
72     dataunit.opt.maxlen = 0;    /* don't care to know about options */
73     dataunit.udata.buf = (char *)buffer->data;
74     dataunit.udata.maxlen = buffer->size;
75     result = t_rcvudata (fd, &dataunit, &gotallflag);
76     if (result < 0) {
77         return FALSE;
78     }
79     buffer->count = dataunit.udata.len;
80     *fromlen = dataunit.addr.len;
81 #else
82     buffer->count = recvfrom (fd, (char*)buffer->data, buffer->size, 0,
83                               (struct sockaddr *)from, (void *)fromlen);
84 #endif
85     if (buffer->count < 6) {
86         buffer->count = 0;
87         return FALSE;
88     }
89     return TRUE;
90 }