Browse code

core: Fix to msg_send() so that requests destined for a WebSocket connection go through the websocket module

- responses already worked

Peter Dunkley authored on 28/06/2012 16:54:45
Showing 1 changed files
... ...
@@ -127,7 +127,9 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
127 127
 	struct dest_info new_dst;
128 128
 	str outb;
129 129
 #ifdef USE_TCP 
130
-	union sockaddr_union* from;
130
+	int port;
131
+	struct ip_addr ip;
132
+	union sockaddr_union* from = NULL;
131 133
 	union sockaddr_union local_addr;
132 134
 	struct tcp_connection *con = NULL;
133 135
 	struct ws_event_info wsev;
... ...
@@ -138,21 +140,40 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
138 140
 	sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
139 141
 
140 142
 #ifdef USE_TCP
141
-	if (unlikely(sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
142
-		if (dst->proto == PROTO_TCP)
143
-			con = tcpconn_get(dst->id, 0, 0, 0, 0);
143
+	if (dst->proto == PROTO_TCP
144 144
 #ifdef USE_TLS
145
-		else if (dst->proto == PROTO_TLS)
146
-			con = tcpconn_get(dst->id, 0, 0, 0, 0);
145
+		|| dst->proto == PROTO_TLS
147 146
 #endif
148
-		if (con && con->flags & F_CONN_WS)
149
-		{
150
-			memset(&wsev, 0, sizeof(ws_event_info_t));
151
-			wsev.type = SREV_TCP_WS_FRAME_OUT;
152
-			wsev.buf = outb.s;
153
-			wsev.len = outb.len;
154
-			wsev.id = dst->id;
155
-			return sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
147
+	) {
148
+		if (unlikely(dst->send_flags.f & SND_F_FORCE_SOCKET
149
+				&& dst->send_sock)) {
150
+			local_addr = dst->send_sock->su;
151
+			su_setport(&local_addr, 0); /* any local port will do */
152
+			from = &local_addr;
153
+		}
154
+
155
+		if (unlikely(sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
156
+			port = su_getport(&dst->to);
157
+			if (likely(port)) {
158
+				su2ip_addr(&ip, &dst->to);
159
+				con = tcpconn_get(dst->id, &ip, port, from, 0);
160
+			}
161
+			else if (likely(dst->id))
162
+				con = tcpconn_get(dst->id, 0, 0, 0, 0);
163
+			else {
164
+				LM_CRIT("BUG: msg_send called with null_id & to\n");
165
+				return -1;
166
+			}
167
+
168
+			if (con && con->flags & F_CONN_WS)
169
+			{
170
+				memset(&wsev, 0, sizeof(ws_event_info_t));
171
+				wsev.type = SREV_TCP_WS_FRAME_OUT;
172
+				wsev.buf = outb.s;
173
+				wsev.len = outb.len;
174
+				wsev.id = con->id;
175
+				return sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
176
+			}
156 177
 		}
157 178
 	}
158 179
 #endif
... ...
@@ -182,13 +203,6 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
182 203
 					" support is disabled\n");
183 204
 			goto error;
184 205
 		}else{
185
-			from=0;
186
-			if (unlikely((dst->send_flags.f & SND_F_FORCE_SOCKET) &&
187
-						dst->send_sock)) {
188
-				local_addr=dst->send_sock->su;
189
-				su_setport(&local_addr, 0); /* any local port will do */
190
-				from=&local_addr;
191
-			}
192 206
 			if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
193 207
 				STATS_TX_DROPS;
194 208
 				LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
... ...
@@ -204,13 +218,6 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
204 218
 					" support is disabled\n");
205 219
 			goto error;
206 220
 		}else{
207
-			from=0;
208
-			if (unlikely((dst->send_flags.f & SND_F_FORCE_SOCKET) &&
209
-						dst->send_sock)) {
210
-				local_addr=dst->send_sock->su;
211
-				su_setport(&local_addr, 0); /* any local port will do */
212
-				from=&local_addr;
213
-			}
214 221
 			if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
215 222
 				STATS_TX_DROPS;
216 223
 				LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");