1 /*
2 * Copyright (C) 1999 Uwe Ohse
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "config.h"
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #ifdef HAVE_ARPA_INET_H
24 #include <arpa/inet.h>
25 #endif
26
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include "str2num.h"
32 #include "uosock.h"
33
34 static int
lookup_host(const char * host,struct in_addr * ina)35 lookup_host(const char *host, struct in_addr *ina)
36 {
37 int v;
38 struct hostent *he;
39 v = inet_aton (host, ina);
40 if (0 == v) {
41 /* XXX use real DNS! */
42 he = gethostbyname (host);
43 if (!he) return -1;
44 memcpy (&ina->s_addr, he->h_addr, sizeof (ina->s_addr));
45 }
46 return 0;
47 }
48
49 /*
50 * a function to parse HOST:PORT specifications, which can deal with:
51 * HOST:PORT
52 * :PORT
53 * HOST:
54 * HOST
55 * :
56 * HOST may be an IP address or a host name (which will be resolved through
57 * gethostbyname).
58 * PORT may be a number or a service name (only in that case to "proto" parameter
59 * will be used).
60 *
61 * Note that you should initialize "ina" and "port" to useful default values before
62 * calling this function.
63 */
64 int
parse_ip_port(const char * input,struct in_addr * ina,unsigned short * port,const char * proto)65 parse_ip_port(const char *input, struct in_addr *ina, unsigned short *port, const char *proto)
66 {
67 char *dp;
68 int r;
69 long lo;
70 dp=strchr(input,':');
71 if (!dp) /* HOST */
72 return lookup_host(input,ina);
73 if (dp==input) /* :PORT */
74 ina->s_addr=INADDR_ANY;
75 else { /* HOST: or HOST:port */
76 char *cop;
77 cop=(char *)malloc(dp-input+1);
78 if (!cop) {errno=ENOMEM; return -1; }
79 memcpy(cop,input,dp-input);
80 cop[dp-input]=0;
81 r=lookup_host(cop,ina);
82 free(cop);
83 if (r==-1)
84 return r;
85 }
86 dp++;
87 if (!*dp)
88 return 0;
89 if (str2long(dp,&lo,10) > 0) {
90 *port=htons(lo);
91 } else {
92 struct servent *se;
93 se=getservbyname(dp,proto);
94 if (se)
95 *port=se->s_port;
96 else
97 return -1;
98 }
99 return 0;
100 }
101