File: nConsvr.c

package info (click to toggle)
conquest 8.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,984 kB
  • ctags: 3,086
  • sloc: ansic: 39,393; sh: 8,540; yacc: 446; makefile: 296; lex: 146
file content (193 lines) | stat: -rw-r--r-- 3,932 bytes parent folder | download
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
/* 
 * server connect node
 *
 * $Id: nConsvr.c,v 1.4 2004/08/09 00:45:36 jon Exp $
 *
 * Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
 */

#include "c_defs.h"
#include "context.h"
#include "global.h"
#include "datatypes.h"
#include "color.h"
#include "conf.h"
#include "gldisplay.h"
#include "node.h"
#include "client.h"
#include "nConsvr.h"
#include "nAuth.h"
#include "udp.h"
#include "glmisc.h"


#define CLIENTNAME "ConquestGL"

static char *rhost = NULL;
static Unsgn16 rport;

static int err = FALSE;

#define ERR_BUFSZ 128
static char errbuf1[ERR_BUFSZ];
static char errbuf2[ERR_BUFSZ];

static int nConsvrDisplay(dspConfig_t *);
static int nConsvrIdle(void);
static int nConsvrInput(int ch);

static scrNode_t nConsvrNode = {
  nConsvrDisplay,               /* display */
  nConsvrIdle,                  /* idle */
  nConsvrInput,                  /* input */
  NULL
};


void nConsvrInit(char *remotehost, Unsgn16 remoteport)
{
  rhost = remotehost;
  rport = remoteport;
  errbuf1[0] = 0;
  errbuf2[0] = 0;
  err = FALSE;

  setNode(&nConsvrNode);

  return;
}

static int nConsvrDisplay(dspConfig_t *dsp)
{
  int lin;

  if (!rhost)
    return NODE_EXIT;

  /* display the logo */
  mglConqLogo(dsp);

  lin = 12;

  cprintf(lin++, 0, ALIGN_CENTER, "Connecting to %s:%d",
          rhost, rport);

  
  if (err)
    {
      if (strlen(errbuf1))
        cprintf(lin++, 0, ALIGN_CENTER, "%s", errbuf1);
      
      if (strlen(errbuf2))
        cprintf(lin++, 0, ALIGN_CENTER, "%s", errbuf2);

      cprintf( MSG_LIN2, 0, ALIGN_CENTER,"#%d#%s", InfoColor, MTXT_DONE);
 
    }

  return NODE_OK;
}  
  
static int nConsvrIdle(void)
{
  int s;
  struct sockaddr_in sa;
  struct hostent *hp;

  /* no point in trying again... */
  if (err)
    return NODE_ERR;

  if (!rhost)
    return NODE_EXIT;

  /* should not happen - debugging */
  if (!cInfo.serverDead)
    {
      clog("ALREADY CONNECTED! I AM READY TO TRANSFER TO AUTH NODE!!!");      
      return NODE_OK;
    }

  if ((hp = gethostbyname(rhost)) == NULL) 
    {
      clog("conquest: %s: no such host\n", rhost);

      snprintf(errbuf1, sizeof(errbuf1) - 1, "%s: no such host", 
              rhost);
      err = TRUE;

      return NODE_ERR;
    }

  /* put host's address and address type into socket structure */
  memcpy((char *)&sa.sin_addr, (char *)hp->h_addr, hp->h_length);
  sa.sin_family = hp->h_addrtype;

  sa.sin_port = htons(rport);

  if ((s = socket(AF_INET, SOCK_STREAM, 0 )) < 0) 
    {
      clog("socket: %s", strerror(errno));
      snprintf(errbuf1, sizeof(errbuf1) - 1, "socket: %s", rhost);
      err = TRUE;

      return NODE_ERR;
    }

  if (cInfo.tryUDP)
    {
      clog("NET: Opening UDP...");
      if ((cInfo.usock = udpOpen(0, &cInfo.servaddr)) < 0) 
        {
          clog("NET: udpOpen: %s", strerror(errno));
          cInfo.tryUDP = FALSE;
        }
    }

  clog("Connecting to host: %s, port %d\n",
       rhost, rport);

  /* connect to the remote server */
  if (connect(s, (const struct sockaddr *)&sa, sizeof(sa)) < 0) 
    {
      clog("connect %s:%d: %s", 
           rhost, rport, strerror(errno));

      snprintf(errbuf1, sizeof(errbuf1) - 1, "connect %s:%d: %s",
               rhost, rport, strerror(errno));
      snprintf(errbuf2, sizeof(errbuf2) - 1, 
               "Is there a conquestd server running there?");

      err = TRUE;

      return NODE_ERR;
    }

  cInfo.serverDead = FALSE;
  cInfo.sock = s;
  cInfo.servaddr = sa;

  pktSetNodelay(cInfo.sock);

  if (!clientHello(CLIENTNAME))
    {
      clog("conquestgl: hello() failed\n");
      printf("conquestgl: hello() failed, check log\n");

      cInfo.serverDead = TRUE;
      return NODE_EXIT;
    }

  nAuthInit();                  /* transfer to Auth */

  return TRUE;
}

static int nConsvrInput(int ch)
{
  if (err)                      /* then we just exit */
    return NODE_EXIT;

  return NODE_OK;
}