Selaa lähdekoodia

error codes cleaned

mom040267 10 vuotta sitten
vanhempi
sitoutus
ce19cf3cee
3 muutettua tiedostoa jossa 73 lisäystä ja 96 poistoa
  1. 68 70
      src/client/ns_turn_msg.c
  2. 2 0
      src/client/ns_turn_msg.h
  3. 3 26
      src/server/ns_turn_server.c

+ 68 - 70
src/client/ns_turn_msg.c

@@ -591,63 +591,79 @@ void old_stun_init_success_response_str(u16bits method, u08bits* buf, size_t *le
   }
 }
 
+const u08bits* get_default_reason(int error_code)
+{
+	const u08bits* reason = (const u08bits *) "Unknown error";
+
+	switch (error_code){
+	case 300:
+		reason = (const u08bits *) "Try Alternate";
+		break;
+	case 400:
+		reason = (const u08bits *) "Bad Request";
+		break;
+	case 401:
+		reason = (const u08bits *) "Unauthorized";
+		break;
+	case 403:
+		reason = (const u08bits *) "Forbidden";
+		break;
+	case 404:
+		reason = (const u08bits *) "Not Found";
+		break;
+	case 420:
+		reason = (const u08bits *) "Unknown Attribute";
+		break;
+	case 437:
+		reason = (const u08bits *) "Allocation Mismatch";
+		break;
+	case 438:
+		reason = (const u08bits *) "Stale Nonce";
+		break;
+	case 440:
+		reason = (const u08bits *) "Address Family not Supported";
+		break;
+	case 441:
+		reason = (const u08bits *) "Wrong Credentials";
+		break;
+	case 442:
+		reason = (const u08bits *) "Unsupported Transport Protocol";
+		break;
+	case 443:
+		reason = (const u08bits *) "Peer Address Family Mismatch";
+		break;
+	case 446:
+		reason = (const u08bits *) "Connection Already Exists";
+		break;
+	case 447:
+		reason = (const u08bits *) "Connection Timeout or Failure";
+		break;
+	case 486:
+		reason = (const u08bits *) "Allocation Quota Reached";
+		break;
+	case 487:
+		reason = (const u08bits *) "Role Conflict";
+		break;
+	case 500:
+		reason = (const u08bits *) "Server Error";
+		break;
+	case 508:
+		reason = (const u08bits *) "Insufficient Capacity";
+		break;
+	default:
+		;
+	};
+
+	return reason;
+}
+
 static void stun_init_error_response_common_str(u08bits* buf, size_t *len,
 				u16bits error_code, const u08bits *reason,
 				stun_tid* id)
 {
 
 	if (!reason) {
-
-		switch (error_code){
-		case 300:
-			reason = (const u08bits *) "Try Alternate";
-			break;
-		case 400:
-			reason = (const u08bits *) "Bad Request";
-			break;
-		case 401:
-			reason = (const u08bits *) "Unauthorized";
-			break;
-		case 403:
-			reason = (const u08bits *) "Forbidden";
-			break;
-		case 404:
-			reason = (const u08bits *) "Not Found";
-			break;
-		case 420:
-			reason = (const u08bits *) "Unknown Attribute";
-			break;
-		case 437:
-			reason = (const u08bits *) "Allocation Mismatch";
-			break;
-		case 438:
-			reason = (const u08bits *) "Stale Nonce";
-			break;
-		case 440:
-			reason = (const u08bits *) "Address Family not Supported";
-			break;
-		case 441:
-			reason = (const u08bits *) "Wrong Credentials";
-			break;
-		case 442:
-			reason = (const u08bits *) "Unsupported Transport Protocol";
-			break;
-		case 443:
-			reason = (const u08bits *) "Peer Address Family Mismatch";
-			break;
-		case 486:
-			reason = (const u08bits *) "Allocation Quota Reached";
-			break;
-		case 500:
-			reason = (const u08bits *) "Server Error";
-			break;
-		case 508:
-			reason = (const u08bits *) "Insufficient Capacity";
-			break;
-		default:
-			reason = (const u08bits *) "Unknown Error";
-			break;
-		};
+		reason = get_default_reason(error_code);
 	}
 
 	u08bits avalue[513];
@@ -1452,25 +1468,7 @@ int stun_attr_add_bandwidth_str(u08bits* buf, size_t *len, band_limit_t bps0) {
 
 int stun_attr_add_address_error_code(u08bits* buf, size_t *len, int requested_address_family, int error_code)
 {
-	const u08bits *reason = NULL;
-
-	switch (error_code){
-	case 440:
-		reason = (const u08bits *) "Address Family not Supported";
-		break;
-	case 486:
-		reason = (const u08bits *) "Allocation Quota Reached";
-		break;
-	case 500:
-		reason = (const u08bits *) "Server Error";
-		break;
-	case 508:
-		reason = (const u08bits *) "Insufficient Capacity";
-		break;
-	default:
-		reason = (const u08bits *) "Unknown Error";
-		break;
-	};
+	const u08bits *reason = get_default_reason(error_code);
 
 	u08bits avalue[513];
 	avalue[0] = (u08bits)requested_address_family;

+ 2 - 0
src/client/ns_turn_msg.h

@@ -134,6 +134,8 @@ void stun_tid_generate_in_message_str(u08bits* buf, stun_tid* id);
 
 int stun_get_command_message_len_str(const u08bits* buf, size_t len);
 
+const u08bits* get_default_reason(int error_code);
+
 int stun_attr_is_addr(stun_attr_ref attr);
 int stun_attr_get_type(stun_attr_ref attr);
 int stun_attr_get_len(stun_attr_ref attr);

+ 3 - 26
src/server/ns_turn_server.c

@@ -1047,7 +1047,6 @@ static int handle_turn_allocate(turn_turnserver *server,
 						transport = get_transport_value(value);
 						if (!transport) {
 							*err_code = 442;
-							*reason = (const u08bits *)"Unsupported Transport Protocol";
 						}
 						if((transport == STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE) && *(server->no_tcp_relay)) {
 							*err_code = 442;
@@ -1159,7 +1158,6 @@ static int handle_turn_allocate(turn_turnserver *server,
 						break;
 					default:
 						*err_code = 440;
-						*reason = (const u08bits *)"Unsupported address family requested";
 					}
 				}
 			}
@@ -1182,8 +1180,6 @@ static int handle_turn_allocate(turn_turnserver *server,
 		} else if (*ua_num > 0) {
 
 		  *err_code = 420;
-		  if(!(*reason))
-		    *reason = (const u08bits *)"Unknown attribute";
 
 		} else if (*err_code) {
 
@@ -1209,7 +1205,6 @@ static int handle_turn_allocate(turn_turnserver *server,
 			if(inc_quota(ss, username)<0) {
 
 				*err_code = 486;
-				*reason = (const u08bits *)"Allocation Quota Reached";
 
 			} else {
 
@@ -1561,7 +1556,6 @@ static int handle_turn_refresh(turn_turnserver *server,
 		if (*ua_num > 0) {
 
 			*err_code = 420;
-			*reason = (const u08bits *)"Unknown attribute";
 
 		} else if (*err_code) {
 
@@ -1980,8 +1974,7 @@ static void tcp_peer_connection_completed_callback(int success, void *arg)
 				addr_to_string(&(tc->peer_addr),(u08bits*)rs);
 				TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: failure to connect from %s to %s\n", __FUNCTION__, ls,rs);
 			}
-			const u08bits *reason = (const u08bits *)"Connection Timeout or Failure";
-			stun_init_error_response_str(STUN_METHOD_CONNECT, ioa_network_buffer_data(nbh), &len, err_code, reason, &(tc->tid));
+			stun_init_error_response_str(STUN_METHOD_CONNECT, ioa_network_buffer_data(nbh), &len, err_code, NULL, &(tc->tid));
 		}
 
 		ioa_network_buffer_set_size(nbh,len);
@@ -2051,7 +2044,6 @@ static int tcp_start_connection_to_peer(turn_turnserver *server, ts_ur_super_ses
 	tcp_connection *tc = get_tcp_connection_by_peer(a, peer_addr);
 	if(tc) {
 		*err_code = 446;
-		*reason = (const u08bits *)"Connection Already Exists";
 		FUNCEND;
 		return -1;
 	}
@@ -2219,7 +2211,6 @@ static int handle_turn_connect(turn_turnserver *server,
 		*reason = (const u08bits *)"Connect cannot be used with UDP relay";
 	} else if (!is_allocation_valid(a)) {
 		*err_code = 437;
-		*reason = (const u08bits *)"Allocation mismatch";
 	} else {
 
 		stun_attr_ref sar = stun_attr_get_first_str(ioa_network_buffer_data(in_buffer->nbh),
@@ -2258,7 +2249,6 @@ static int handle_turn_connect(turn_turnserver *server,
 		if (*ua_num > 0) {
 
 			*err_code = 420;
-			*reason = (const u08bits *)"Unknown attribute";
 
 		} else if (*err_code) {
 
@@ -2296,7 +2286,6 @@ static int handle_turn_connection_bind(turn_turnserver *server,
 	if(ss->to_be_closed) {
 
 		*err_code = 400;
-		*reason = (const u08bits *)"Bad request";
 
 	} else if (is_allocation_valid(a)) {
 
@@ -2343,7 +2332,6 @@ static int handle_turn_connection_bind(turn_turnserver *server,
 		if (*ua_num > 0) {
 
 			*err_code = 420;
-			*reason = (const u08bits *)"Unknown attribute";
 
 		} else if (*err_code) {
 
@@ -2584,7 +2572,6 @@ static int handle_turn_channel_bind(turn_turnserver *server,
 		if (*ua_num > 0) {
 
 			*err_code = 420;
-			*reason = (const u08bits *)"Unknown attribute";
 
 		} else if (*err_code) {
 
@@ -2798,7 +2785,6 @@ static int handle_turn_binding(turn_turnserver *server,
 	if (*ua_num > 0) {
 
 		*err_code = 420;
-		*reason = (const u08bits *)"Unknown attribute";
 
 	} else if (*err_code) {
 
@@ -3286,7 +3272,6 @@ static int check_stun_auth(turn_turnserver *server,
 
 	if(!sar) {
 		*err_code = 401;
-		*reason = (const u08bits*)"Unauthorised";
 		return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
 	}
 
@@ -3354,7 +3339,6 @@ static int check_stun_auth(turn_turnserver *server,
 
 		if(!sar) {
 			*err_code = 400;
-			*reason = (const u08bits*)"Bad request";
 			return -1;
 		}
 
@@ -3386,7 +3370,6 @@ static int check_stun_auth(turn_turnserver *server,
 
 	if(!sar) {
 		*err_code = 400;
-		*reason = (const u08bits*)"Bad request";
 		return -1;
 	}
 
@@ -3405,7 +3388,6 @@ static int check_stun_auth(turn_turnserver *server,
 					*reason = (const u08bits*)"Allocation mismatch: wrong credentials";
 				} else {
 					*err_code = 441;
-					*reason = (const u08bits*)"Wrong credentials";
 				}
 				return -1;
 			}
@@ -3424,7 +3406,6 @@ static int check_stun_auth(turn_turnserver *server,
 
 		if(!sar) {
 			*err_code = 400;
-			*reason = (const u08bits*)"Bad request";
 			return -1;
 		}
 
@@ -3461,7 +3442,6 @@ static int check_stun_auth(turn_turnserver *server,
 				"%s: Cannot find credentials of user <%s>\n",
 				__FUNCTION__, (char*)usname);
 		*err_code = 401;
-		*reason = (const u08bits*)"Unauthorised";
 		return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
 	}
 
@@ -3480,7 +3460,6 @@ static int check_stun_auth(turn_turnserver *server,
 									__FUNCTION__, (char*)usname);
 					*err_code = SHA_TOO_WEAK_ERROR_CODE;
 					*reason = SHA_TOO_WEAK_ERROR_REASON;
-					*reason = (const u08bits*)"Unauthorised: weak SHA function is used";
 					return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
 		}
 
@@ -3495,7 +3474,6 @@ static int check_stun_auth(turn_turnserver *server,
 				"%s: user %s credentials are incorrect\n",
 				__FUNCTION__, (char*)usname);
 		*err_code = 401;
-		*reason = (const u08bits*)"Unauthorised";
 		return create_challenge_response(ss,tid,resp_constructed,err_code,reason,nbh,method);
 	}
 
@@ -3528,7 +3506,6 @@ static void set_alternate_server(turn_server_addrs_list_t *asl, const ioa_addr *
 			if(addr->ss.sa_family == local_addr->ss.sa_family) {
 
 				*err_code = 300;
-				*reason = (const u08bits *)"Redirect";
 
 				size_t len = ioa_network_buffer_get_size(nbh);
 				stun_init_error_response_str(method, ioa_network_buffer_data(nbh), &len, *err_code, *reason, tid);
@@ -4329,7 +4306,7 @@ static int create_relay_connection(turn_turnserver* server,
 				ioa_socket_tobeclosed(s)) {
 
 				IOA_CLOSE_SOCKET(s);
-				*err_code = 508;
+				*err_code = 404;
 				*reason = (const u08bits *)"Cannot find reserved socket";
 				return -1;
 			}
@@ -4389,7 +4366,7 @@ static int create_relay_connection(turn_turnserver* server,
 			} else {
 				IOA_CLOSE_SOCKET(newelem->s);
 				IOA_CLOSE_SOCKET(rtcp_s);
-				*err_code = 508;
+				*err_code = 500;
 				*reason = (const u08bits *)"Wrong reservation tokens (internal error)";
 				return -1;
 			}