5c2e3e44 |
1 | #include <strings.h> |
2 | #include <string.h> |
3 | #include <stdio.h> |
4 | #include <stdlib.h> |
fbc24ab0 |
5 | #include <netinet/in.h> |
6 | #include <arpa/inet.h> |
7 | #include <netdb.h> |
5c2e3e44 |
8 | |
9 | #include "common.h" |
10 | |
11 | struct dvb_host *parse(char *urlpart, char *defport) |
12 | { |
13 | struct dvb_host *dvbhost; |
14 | char *pos; |
15 | |
16 | if (!(dvbhost = malloc(sizeof(struct dvb_host)))) { |
17 | perror("malloc"); |
18 | exit(EXIT_FAILURE); |
19 | } |
20 | |
21 | bzero(dvbhost, sizeof(struct dvb_host)); |
22 | |
23 | if (!(dvbhost->hostname = strdup(urlpart))) { |
24 | perror("strdup"); |
25 | exit(EXIT_FAILURE); |
26 | } |
27 | |
28 | /* Unneded, but better readablity: */ |
29 | dvbhost->location = NULL; |
30 | dvbhost->port = NULL; |
31 | |
32 | pos = dvbhost->hostname; |
33 | |
34 | while(*pos != '\0' && *pos != ':' && *pos != '/') |
35 | pos++; |
36 | |
37 | if (*pos == '/') |
38 | dvbhost->location = pos + 1; |
39 | |
40 | if (*pos == ':') |
41 | dvbhost->port = pos + 1; |
42 | |
43 | *pos = 0; |
44 | |
45 | if (dvbhost->port) { |
46 | pos++; |
47 | |
48 | while(*pos != '\0' && *pos != '/') |
49 | pos ++; |
50 | |
51 | if(*pos == '/') |
52 | dvbhost->location = pos + 1; |
53 | |
54 | *pos = 0; |
55 | } |
56 | |
57 | if (!dvbhost->port) |
58 | dvbhost->port = strdup(defport); |
59 | |
60 | if (!dvbhost->location) |
61 | if(!(dvbhost->location = strdup(""))) { |
62 | perror("strdup"); |
63 | exit(EXIT_FAILURE); |
64 | } |
65 | |
66 | return dvbhost; |
67 | } |
fbc24ab0 |
68 | |
69 | int resolve(struct dvb_host *dvbhost, struct sockaddr_in *server) |
70 | { |
71 | struct addrinfo *s_addrinfo, addrhints; |
72 | int res; |
73 | |
74 | bzero(&addrhints, sizeof(struct addrinfo)); |
75 | addrhints.ai_family = PF_INET; |
76 | addrhints.ai_socktype = dvbhost->socktype; |
77 | |
78 | if ((res = getaddrinfo(dvbhost->hostname, dvbhost->port, &addrhints, &s_addrinfo))) { |
79 | fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(res)); |
80 | return -1; |
81 | } |
82 | |
83 | bzero(server, sizeof(struct sockaddr_in)); |
84 | server->sin_family = AF_INET; |
85 | server->sin_addr.s_addr = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_addr.s_addr; |
86 | server->sin_port = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_port; |
87 | |
88 | freeaddrinfo(s_addrinfo); |
89 | |
90 | return 0; |
91 | } |
92 | |