Bug Tracker – Attachment #186: Patch that allows you to bind to an address for bug #587

View | Details | Raw Unified | Return to bug 587
Collapse All | Expand All

(-)common/i_net.cpp (-16 / +48 lines)
Lines 95-100 Link Here
95
lzo_byte wrkmem[LZO1X_1_MEM_COMPRESS];
95
lzo_byte wrkmem[LZO1X_1_MEM_COMPRESS];
96
96
97
EXTERN_CVAR(port)
97
EXTERN_CVAR(port)
98
EXTERN_CVAR(bindtohost)
98
99
99
msg_info_t clc_info[clc_max];
100
msg_info_t clc_info[clc_max];
100
msg_info_t svc_info[svc_max];
101
msg_info_t svc_info[svc_max];
Lines 114-138 Link Here
114
	return s;
115
	return s;
115
}
116
}
116
117
118
void NetadrToSockadr (netadr_t *a, struct sockaddr_in *s);
119
void SockadrToNetadr (struct sockaddr_in *s, netadr_t *a);
120
117
//
121
//
118
// BindToLocalPort
122
// BindToLocalPort
119
//
123
//
120
void BindToLocalPort (SOCKET s, u_short wanted)
124
void BindToLocalPort (SOCKET s, u_short wanted)
121
{
125
{
122
	int v;
126
	int v;
127
	struct hostent* h;
123
	struct sockaddr_in address;
128
	struct sockaddr_in address;
129
	netadr_t na;
124
130
125
	memset (&address, 0, sizeof(address));
131
	memset (&address, 0, sizeof(address));
126
	address.sin_family = AF_INET;
132
	address.sin_family = AF_INET;
127
	address.sin_addr.s_addr = INADDR_ANY;
133
	
134
	// GhostlyDeath <July 3, 2010> -- Bind to local IP
135
	if (serverside)
136
	{
137
		// Just use our existing function
138
		if (!NET_StringToAdr(bindtohost.cstring(), &na))
139
			address.sin_addr.s_addr = INADDR_ANY;	// Failure
140
		else
141
			NetadrToSockadr(&na, &address);
142
	}
143
	else	// Client should use any address
144
		address.sin_addr.s_addr = INADDR_ANY;
128
	u_short next = wanted;
145
	u_short next = wanted;
129
146
130
	// denis - try several ports
147
	// denis - try several ports
131
	do
148
	do
132
	{
149
	{
133
		address.sin_port = htons(next++);
150
		address.sin_port = htons(next++);
134
151
		
135
		v = bind (s, (sockaddr *)&address, sizeof(address));
152
		v = bind (s, (sockaddr *)&address, sizeof(address));
153
		
154
		// GhostlyDeath <July 3, 2010> -- Fallback to any address
155
		if (address.sin_addr.s_addr != INADDR_ANY && (errno == EACCES || errno == EADDRNOTAVAIL))
156
		{
157
			Printf (PRINT_HIGH, "BindToLocalPort: %s\n", strerror(errno));
158
			next--;
159
			address.sin_addr.s_addr = INADDR_ANY;
160
			continue;
161
		}
136
162
137
		if(next > wanted + 16)
163
		if(next > wanted + 16)
138
		{
164
		{
Lines 145-151 Link Here
145
	sprintf(tmp, "%d", next - 1);
171
	sprintf(tmp, "%d", next - 1);
146
	port.ForceSet(tmp);
172
	port.ForceSet(tmp);
147
173
148
	Printf(PRINT_HIGH, "Bound to local port %d\n", next - 1);
174
	if (address.sin_addr.s_addr == INADDR_ANY)
175
		Printf(PRINT_HIGH, "Bound to local port %d\n", next - 1);
176
	else
177
	{
178
		SockadrToNetadr(&address, &na);
179
		
180
		Printf(PRINT_HIGH, "Bound to local address %s\n", NET_AdrToString(na));
181
	}
149
}
182
}
150
183
151
184
Lines 207-224 Link Here
207
             *colon = 0;
240
             *colon = 0;
208
             sadr.sin_port = htons(atoi(colon+1));
241
             sadr.sin_port = htons(atoi(colon+1));
209
          }
242
          }
243
	
244
	// GhostlyDeath <July 3, 2010> -- Just use gethostbyname(), because if you
245
	// give it an IP address it will just do a inet_addr() anyway or resolve the
246
	// host if not. Also, the check before was really bad, checking the first
247
	// character if it contains a number is bad. inet_addr() would not be able
248
	// to handle something such as 127-0-0-1.localhost. gethostbyname() can
249
	// handle hostnames and addresses inet_addr() would take.
250
	if (!(h = gethostbyname(copy)))
251
		return false;
252
	*(int *)&sadr.sin_addr = *(int *)h->h_addr_list[0];
210
253
211
     if (copy[0] >= '0' && copy[0] <= '9')
212
     {
213
          *(int *)&sadr.sin_addr = inet_addr(copy);
214
     }
215
     else
216
     {
217
          if (! (h = gethostbyname(copy)) )
218
                return 0;
219
          *(int *)&sadr.sin_addr = *(int *)h->h_addr_list[0];
220
     }
221
222
     SockadrToNetadr (&sadr, a);
254
     SockadrToNetadr (&sadr, a);
223
255
224
     return true;
256
     return true;
Lines 286-297 Link Here
286
{
318
{
287
    int                   ret;
319
    int                   ret;
288
    struct sockaddr_in    addr;
320
    struct sockaddr_in    addr;
289
321
   	
290
    NetadrToSockadr (&to, &addr);
322
    NetadrToSockadr (&to, &addr);
291
323
292
	ret = sendto (net_socket, (const char *)buf.ptr(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr));
324
	ret = sendto (net_socket, (const char *)buf.ptr(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr));
293
325
294
	buf.clear();
326
	buf.clear();	
295
327
296
    if (ret == -1)
328
    if (ret == -1)
297
    {
329
    {
(-)common/c_cvarlist.cpp (+2 lines)
Lines 90-94 Link Here
90
CVAR (lookspring,		"1", CVAR_CLIENTARCHIVE)
90
CVAR (lookspring,		"1", CVAR_CLIENTARCHIVE)
91
// Allows players to walk through other players
91
// Allows players to walk through other players
92
CVAR (sv_unblockplayers, "0", CVAR_ARCHIVE | CVAR_LATCH | CVAR_SERVERINFO)
92
CVAR (sv_unblockplayers, "0", CVAR_ARCHIVE | CVAR_LATCH | CVAR_SERVERINFO)
93
// GhostlyDeath <July 3, 2010> -- Bind to host
94
CVAR (bindtohost, "", CVAR_SERVERARCHIVE | CVAR_NOENABLEDISABLE)
93
95
94
VERSION_CONTROL (c_cvarlist_cpp, "$Id$")
96
VERSION_CONTROL (c_cvarlist_cpp, "$Id$")

Return to bug 587