1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
/*
* Generic UDP connection code.
*
* Written by Larry Schwimmer
* Copyright 1997, 2008, 2012, 2013
* The Board of Trustees of the Leland Stanford Junior University
*
* See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/socket.h>
#include <portable/system.h>
#include <ctype.h>
#include <errno.h>
#include <modules/modules.h>
/*
* Connect to a host with specified protocol using UDP.
*
* Input:
* host name of server to connect to
* protocol protocol to use
* [port] port to use if protocol is unknown (use 0 if
* default port is not wanted)
* Returns:
* file descriptor number of the socket connection on success
* -1 on failure
*/
int
udp_connect(const char *host, const char *protocol, int port)
{
struct servent *se;
unsigned int addr;
struct hostent *he;
struct sockaddr_in serv_addr;
int sd;
/* Assign port. */
memset(&serv_addr, 0, sizeof(serv_addr));
if ((protocol != NULL
&& (se = getservbyname (protocol, "udp")) != NULL)
|| (port != 0 && (se = getservbyport(htons(port), "udp")) != NULL))
serv_addr.sin_port = se->s_port;
else if (port != 0)
serv_addr.sin_port = htons(port);
endservent();
/* First check if valid IP address. Otherwise check if valid name. */
addr = inet_addr(host);
if (addr != (in_addr_t) -1) {
he = gethostbyaddr(&addr, sizeof(unsigned int), AF_INET);
if (he == NULL)
return -1;
} else {
he = gethostbyname(host);
if (he == NULL)
return -1;
}
/* Set up socket connection. */
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr, he->h_addr, sizeof(serv_addr.sin_addr));
sd = socket(PF_INET, SOCK_DGRAM, 0);
if (sd < 0)
return -1;
if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
close(sd);
return -1;
}
return sd;
}
|