Browse code

- fixed some header files - found a bug in add_rule (bad mallocs) - added SO_REUSEPORT

Andrei Pelinescu-Onciul authored on 05/09/2001 21:21:02
Showing 4 changed files
... ...
@@ -4,6 +4,8 @@
4 4
 
5 5
 
6 6
 #include <string.h>
7
+#include <sys/types.h>
8
+#include <sys/socket.h>
7 9
 #include <netdb.h>
8 10
 #include <netinet/in.h>
9 11
 
... ...
@@ -31,7 +31,7 @@ void receive_stdin_loop()
31 31
 }
32 32
 */
33 33
 
34
-#define NAME "dorian.fokus.gmd.de"
34
+#define NAME "0.0.0.0"
35 35
 
36 36
 
37 37
 int main(int argc, char** argv)
... ...
@@ -116,7 +116,7 @@ int add_rule(struct cfg_line* cl, struct route_elem** head)
116 116
 	/* start copying the host entry.. */
117 117
 	/* copy h_name */
118 118
 	len=strlen(he->h_name)+1;
119
-	re->host.h_name=(char*)malloc(len);
119
+	re->host.h_name=(char*)malloc(sizeof(char) * len);
120 120
 	if (re->host.h_name) strncpy(re->host.h_name, he->h_name, len);
121 121
 	else{
122 122
 		ret=E_OUT_OF_MEM;
... ...
@@ -125,7 +125,7 @@ int add_rule(struct cfg_line* cl, struct route_elem** head)
125 125
 
126 126
 	/* copy h_aliases */
127 127
 	for (len=0;he->h_aliases[len];len++);
128
-	re->host.h_aliases=(char**)malloc(len+1);
128
+	re->host.h_aliases=(char**)malloc(sizeof(char*)*(len+1));
129 129
 	if (re->host.h_aliases==0){
130 130
 		ret=E_OUT_OF_MEM;
131 131
 		goto error;
... ...
@@ -133,7 +133,7 @@ int add_rule(struct cfg_line* cl, struct route_elem** head)
133 133
 	memset((void*)re->host.h_aliases, 0, sizeof(char*) * (len+1) );
134 134
 	for (i=0;i<len;i++){
135 135
 		len2=strlen(he->h_aliases[i])+1;
136
-		re->host.h_aliases[i]=(char*)malloc(len2);
136
+		re->host.h_aliases[i]=(char*)malloc(sizeof(char)*len2);
137 137
 		if (re->host.h_aliases==0){
138 138
 			ret=E_OUT_OF_MEM;
139 139
 			goto error;
... ...
@@ -142,20 +142,21 @@ int add_rule(struct cfg_line* cl, struct route_elem** head)
142 142
 	}
143 143
 	/* copy h_addr_list */
144 144
 	for (len=0;he->h_addr_list[len];len++);
145
-	re->host.h_addr_list=(char**)malloc(len+1);
145
+	re->host.h_addr_list=(char**)malloc(sizeof(char*)*(len+1));
146 146
 	if (re->host.h_addr_list==0){
147 147
 		ret=E_OUT_OF_MEM;
148 148
 		goto error;
149 149
 	}
150 150
 	memset((void*)re->host.h_addr_list, 0, sizeof(char*) * (len+1) );
151 151
 	for (i=0;i<len;i++){
152
-		re->host.h_addr_list[i]=(char*)malloc(he->h_length+1);
153
-		if (re->host.h_addr_list==0){
152
+		re->host.h_addr_list[i]=(char*)malloc(sizeof(char)*he->h_length);
153
+		if (re->host.h_addr_list[i]==0){
154 154
 			ret=E_OUT_OF_MEM;
155 155
 			goto error;
156 156
 		}
157
-		memcpy(re->host.h_addr_list[i], he->h_addr_list[i], he->h_length+1);
157
+		memcpy(re->host.h_addr_list[i], he->h_addr_list[i], he->h_length);
158 158
 	}
159
+
159 160
 	/* copy h_addr_type & length */
160 161
 	re->host.h_addrtype=he->h_addrtype;
161 162
 	re->host.h_length=he->h_length;
... ...
@@ -213,15 +214,16 @@ void print_rl()
213 213
 		DPrint("%2d.to=%s ; route ok=%d\n", i,
214 214
 				t->host.h_name, t->ok);
215 215
 		DPrint("   ips: ");
216
-		for (j=0; t->host.h_addr_list[j]; j++)
217
-			DPrint("%d.%d.%d.%d ",
216
+		for (j=0; t->host.h_addr_list[j]; j++){
217
+			DPrint("%d.%d.%d.%d ", 
218 218
 				(unsigned char) t->host.h_addr_list[j][0],
219 219
 				(unsigned char) t->host.h_addr_list[j][1],
220 220
 			    (unsigned char) t->host.h_addr_list[j][2],
221 221
 				(unsigned char) t->host.h_addr_list[j][3]
222 222
 				  );
223
-				
224
-		DPrint("\n   port:%d\n", (unsigned short)t->port);
223
+		}
224
+		DPrint("\n");
225
+		DPrint("   port:%d\n", (unsigned short)t->port);
225 226
 		DPrint("   Statistics: tx=%d, errors=%d, tx_bytes=%d, idx=%d\n",
226 227
 				t->tx, t->errors, t->tx_bytes, t->current_addr_idx);
227 228
 	}
... ...
@@ -24,6 +24,7 @@ unsigned short our_port;
24 24
 int udp_init(unsigned long ip, unsigned short port)
25 25
 {
26 26
 	struct sockaddr_in addr;
27
+	int optval;
27 28
 
28 29
 	addr.sin_family=AF_INET;
29 30
 	addr.sin_port=htons(port);
... ...
@@ -34,13 +35,20 @@ int udp_init(unsigned long ip, unsigned short port)
34 34
 		DPrint("ERROR: udp_init: socket: %s\n", strerror());
35 35
 		goto error;
36 36
 	}
37
+	/* set sock opts? */
38
+	optval=1;
39
+	if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEPORT,
40
+					&optval, sizeof(optval)) ==-1)
41
+	{
42
+		DPrint("ERROR: udp_init: setsockopt: %s\n", strerror());
43
+		goto error;
44
+	}
37 45
 
38 46
 	if (bind(udp_sock, (struct sockaddr*) &addr, sizeof(addr))==-1){
39
-		DPrint("ERROR: udp_init: socket: %s\n", strerror());
47
+		DPrint("ERROR: udp_init: bind: %s\n", strerror());
40 48
 		goto error;
41 49
 	}
42 50
 
43
-	/* set sock opts? */
44 51
 
45 52
 	return 0;
46 53