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
|
$NetBSD: patch-ai,v 1.4 2001/03/10 22:05:54 tron Exp $
--- src/server.c.orig Fri Jan 9 18:16:22 1998
+++ src/server.c Sat Mar 10 23:00:44 2001
@@ -2,19 +2,21 @@
* Copyright (C) 1995, 1996 Karl-Johan Johnsson.
*/
+#include <sys/types.h>
+#include <sys/socket.h>
+
#include "global.h"
#include "child.h"
#include "codes.h"
#include "connect.h"
#include "file.h"
#include "resource.h"
+#include "sysdeps.h"
#include "server.h"
#include "util.h"
#include "widgets.h"
#include "xutil.h"
-#include "sysdeps.h"
-
struct SERVER {
int fd;
char *buffer;
@@ -128,37 +130,68 @@
* tell != 0 means tell about errors
* tell > 1 means say what you're doing
*/
-int server_open(SERVER *server, struct SERV_ADDR *addr, int tell)
+int server_open(SERVER *server, SERV_ADDR *addr, int tell)
{
- long tmp;
+ int tmp;
server_close(server);
- server->fd = open_socket();
- if (server->fd < 0) {
- set_message("Error: Failed to create socket!", True);
- return -1;
- }
+ while (addr) {
+ server->fd = open_socket(addr);
+ if (server->fd < 0) {
+ set_message("Error: Failed to create socket!", True);
+ return -1;
+ }
- if (tell > 1)
- set_message("Server contacted, waiting for response...", False);
+ if (tell > 1)
+ set_message("Server contacted, waiting for response...", False);
- tmp = connect_socket(server->fd, addr);
+ if (connect_socket(server->fd, addr) == 0)
+ break;
- if (tmp < 0 && would_block(server->fd, errno)) {
- if (tell == 0)
- return 0; /* don't block */
-
- tmp = 0;
- do_wait(&server->fd, True, server->quit_func, server);
- if (server->fd < 0)
- return -1;
+ if (would_block(server->fd, errno)) {
+ if (tell == 0)
+ return 0; /* don't block */
+
+ do_wait(&server->fd, True, server->quit_func, server);
+ if (server->fd >= 0) {
+#ifdef __NetBSD__
+ socklen_t length;
+ int error;
+#else
+ int length, error;
+#endif
+ length = sizeof(error);
+ if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR,
+ (char *)&error, &length) == 0) {
+ if (error == 0)
+ break;
+ else
+ errno = error;
+ }
+ }
+ }
+
+ server_close(server);
+ addr = next_addr(addr);
+ }
+
+ if (server->fd < 0) {
+ if (tell) {
+ char message[128];
+ char *tmp = error_string(errno);
+
+ if (!tmp)
+ tmp = "Connection failed";
+ sprintf(message, "Error: %s!", tmp);
+ set_message(message, True);
+ }
+ return -1;
}
server->buffer[0] = '\0';
- if (tmp >= 0)
- do {
- tmp = read(server->fd, server->buffer, server->len);
- } while (tmp < 0 && errno == EINTR);
+ do {
+ tmp = read(server->fd, server->buffer, server->len);
+ } while (tmp < 0 && errno == EINTR);
if (tmp >= 0) {
server->buffer[tmp] = '\0';
|