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 138
 	sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
139 139
 
140 140
 #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);
141
+	if (dst->proto == PROTO_TCP
144 142
 #ifdef USE_TLS
145
-		else if (dst->proto == PROTO_TLS)
146
-			con = tcpconn_get(dst->id, 0, 0, 0, 0);
143
+		|| dst->proto == PROTO_TLS
147 144
 #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);
145
+	) {
146
+		if (unlikely(dst->send_flags.f & SND_F_FORCE_SOCKET
147
+				&& dst->send_sock)) {
148
+			local_addr = dst->send_sock->su;
149
+			su_setport(&local_addr, 0); /* any local port will do */
150
+			from = &local_addr;
151
+		}
152
+
153
+		if (unlikely(sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
154
+			port = su_getport(&dst->to);
155
+			if (likely(port)) {
156
+				su2ip_addr(&ip, &dst->to);
157
+				con = tcpconn_get(dst->id, &ip, port, from, 0);
158
+			}
159
+			else if (likely(dst->id))
160
+				con = tcpconn_get(dst->id, 0, 0, 0, 0);
161
+			else {
162
+				LM_CRIT("BUG: msg_send called with null_id & to\n");
163
+				return -1;
164
+			}
165
+
166
+			if (con && con->flags & F_CONN_WS)
167
+			{
168
+				memset(&wsev, 0, sizeof(ws_event_info_t));
169
+				wsev.type = SREV_TCP_WS_FRAME_OUT;
170
+				wsev.buf = outb.s;
171
+				wsev.len = outb.len;
172
+				wsev.id = con->id;
173
+				return sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
174
+			}
156 175
 		}
157 176
 	}
158 177
 #endif
... ...
@@ -182,13 +203,6 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
182 182
 					" support is disabled\n");
183 183
 			goto error;
184 184
 		}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 185
 			if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
193 186
 				STATS_TX_DROPS;
194 187
 				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 204
 					" support is disabled\n");
205 205
 			goto error;
206 206
 		}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 207
 			if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
215 208
 				STATS_TX_DROPS;
216 209
 				LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");