diff options
author | William Harrington <kb0iic@berzerkula.org> | 2025-01-14 16:06:02 -0600 |
---|---|---|
committer | William Harrington <kb0iic@berzerkula.org> | 2025-01-14 16:06:02 -0600 |
commit | 0cc9b20c15460213e488bf5e70963b941482f628 (patch) | |
tree | bb0143245583ec846630f39bfa2258dba640ccd7 /sdk/net/getsocket.c | |
parent | 0e084ade5069756d487b5c948c48b777e37c00c9 (diff) |
Add source.
Diffstat (limited to 'sdk/net/getsocket.c')
-rw-r--r-- | sdk/net/getsocket.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/sdk/net/getsocket.c b/sdk/net/getsocket.c new file mode 100644 index 0000000..83ca68a --- /dev/null +++ b/sdk/net/getsocket.c @@ -0,0 +1,43 @@ +#include <net/socket.h> +#include <std/string.h> + +SOCKET getsocket(const char *host, int port, int socktype) +{ + SOCKET so; + struct hostent *hp; + struct sockaddr_in saddr; + struct in_addr *aptr; + char *sp, *p; + char *dup = strdup(host); + + so = socket(AF_INET, socktype, 0); + if(so == INVALID_SOCKET) + { + free(dup); + return so; + } + + sp = dup; + while(NULL != (p = strtok(sp, " ;:\t"))) + { + sp = NULL; + hp = gethost(p); + if(!hp) + continue; + + saddr.sin_family = hp->h_addrtype; + saddr.sin_port = htons(port); + while( (aptr = (struct in_addr *)*(hp->h_addr_list)++) != NULL) + saddr.sin_addr = *aptr; + + if(connect(so, (struct sockaddr *)&saddr, sizeof(saddr))) + continue; + + free(dup); + return so; + } + endsocket(so); + free(dup); + return INVALID_SOCKET; +} + |