mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-30 15:13:03 +00:00 
			
		
		
		
	Support bindport.
This commit is contained in:
		| @@ -369,13 +369,12 @@ JANET_CORE_FN(cfun_net_connect, | |||||||
|     /* Check arguments */ |     /* Check arguments */ | ||||||
|     int socktype = janet_get_sockettype(argv, argc, 2); |     int socktype = janet_get_sockettype(argv, argc, 2); | ||||||
|     int is_unix = 0; |     int is_unix = 0; | ||||||
|     const char *bindhost = janet_optcstring(argv, argc, 3, NULL); |     char *bindhost = (char *) janet_optcstring(argv, argc, 3, NULL); | ||||||
|     int bindport = 0; |     char *bindport = NULL; | ||||||
|     if (janet_checkint(argv[4])) { |     if (janet_checkint(argv[4])) { | ||||||
|         bindport = janet_unwrap_integer(argv[4]); |         bindport = (char *)janet_to_string(argv[4]); | ||||||
|     } else { |     } else { | ||||||
|         const char *portstring = (char *)janet_optcstring(argv, argc, 4, "0"); |         bindport = (char *)janet_optcstring(argv, argc, 4, NULL); | ||||||
|         bindport = atoi(portstring); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /* Where we're connecting to */ |     /* Where we're connecting to */ | ||||||
| @@ -391,10 +390,10 @@ JANET_CORE_FN(cfun_net_connect, | |||||||
|         /* getaddrinfo */ |         /* getaddrinfo */ | ||||||
|         struct addrinfo hints; |         struct addrinfo hints; | ||||||
|         memset(&hints, 0, sizeof(hints)); |         memset(&hints, 0, sizeof(hints)); | ||||||
|         hints.ai_family = AF_INET; |         hints.ai_family = AF_UNSPEC; | ||||||
|         hints.ai_socktype = socktype; |         hints.ai_socktype = socktype; | ||||||
|         hints.ai_flags = 0; |         hints.ai_flags = 0; | ||||||
|         int status = getaddrinfo(bindhost, NULL, &hints, &binding); |         int status = getaddrinfo(bindhost, bindport, &hints, &binding); | ||||||
|         if (status) { |         if (status) { | ||||||
|             freeaddrinfo(ai); |             freeaddrinfo(ai); | ||||||
|             janet_panicf("could not get address info for bindhost: %s", gai_strerror(status)); |             janet_panicf("could not get address info for bindhost: %s", gai_strerror(status)); | ||||||
| @@ -433,24 +432,30 @@ JANET_CORE_FN(cfun_net_connect, | |||||||
|         } |         } | ||||||
|         if (NULL == addr) { |         if (NULL == addr) { | ||||||
|             Janet v = janet_ev_lasterr(); |             Janet v = janet_ev_lasterr(); | ||||||
|  |             if (binding) freeaddrinfo(binding); | ||||||
|             freeaddrinfo(ai); |             freeaddrinfo(ai); | ||||||
|             janet_panicf("could not create socket: %V", v); |             janet_panicf("could not create socket: %V", v); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /* Bind to bindhost and bindport if given */ |     /* Bind to bindhost and bindport if given */ | ||||||
|     if (bindhost != NULL) { |     if (binding) { | ||||||
|         /* Bind to a specific network interface (and optionally a specific local port) */ |         struct addrinfo *rp = NULL; | ||||||
|         struct sockaddr_in localaddr; |         int did_bind = 0; | ||||||
|         memset(&localaddr, 0, sizeof(localaddr)); |         for (rp = ai; rp != NULL; rp = rp->ai_next) { | ||||||
|         localaddr.sin_family = AF_INET; |             if (bind(sock, rp->ai_addr, (int) rp->ai_addrlen) == 0) { | ||||||
|         localaddr.sin_addr.s_addr = inet_addr(bindhost); |                 did_bind = 1; | ||||||
|         localaddr.sin_port = bindport; |                 break; | ||||||
|         if (0 == bind(sock, (struct sockaddr *)&localaddr, sizeof(localaddr))) { |             } | ||||||
|             Janet lasterr = janet_ev_lasterr(); |         } | ||||||
|  |         if (!did_bind) { | ||||||
|  |             Janet v = janet_ev_lasterr(); | ||||||
|  |             freeaddrinfo(binding); | ||||||
|             freeaddrinfo(ai); |             freeaddrinfo(ai); | ||||||
|             JSOCKCLOSE(sock); |             JSOCKCLOSE(sock); | ||||||
|             janet_panicf("could not bind outgoing address: %V", lasterr); |             janet_panicf("could not bind outgoing address: %V", v); | ||||||
|  |         } else { | ||||||
|  |             freeaddrinfo(binding); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 bakpakin
					bakpakin