'cleanup'. it's still as ugly as before
[record-dvb] / common.c
CommitLineData
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
11struct dvb_host *parse(char *urlpart, char *defport)
12{
90d8d87b 13 static struct dvb_host *dvbhost = NULL;
5c2e3e44 14 char *pos;
15
90d8d87b 16 if (!dvbhost) {
17 if (!(dvbhost = malloc(sizeof(struct dvb_host)))) {
18 perror("malloc");
19 exit(EXIT_FAILURE);
20 }
5c2e3e44 21 }
22
23 bzero(dvbhost, sizeof(struct dvb_host));
24
25 if (!(dvbhost->hostname = strdup(urlpart))) {
26 perror("strdup");
27 exit(EXIT_FAILURE);
28 }
29
30 /* Unneded, but better readablity: */
31 dvbhost->location = NULL;
32 dvbhost->port = NULL;
33
34 pos = dvbhost->hostname;
35
36 while(*pos != '\0' && *pos != ':' && *pos != '/')
37 pos++;
38
39 if (*pos == '/')
40 dvbhost->location = pos + 1;
41
42 if (*pos == ':')
43 dvbhost->port = pos + 1;
44
45 *pos = 0;
46
47 if (dvbhost->port) {
48 pos++;
49
50 while(*pos != '\0' && *pos != '/')
51 pos ++;
52
53 if(*pos == '/')
54 dvbhost->location = pos + 1;
55
56 *pos = 0;
57 }
58
59 if (!dvbhost->port)
60 dvbhost->port = strdup(defport);
61
62 if (!dvbhost->location)
63 if(!(dvbhost->location = strdup(""))) {
64 perror("strdup");
65 exit(EXIT_FAILURE);
66 }
67
68 return dvbhost;
69}
fbc24ab0 70
71int resolve(struct dvb_host *dvbhost, struct sockaddr_in *server)
72{
73 struct addrinfo *s_addrinfo, addrhints;
74 int res;
75
76 bzero(&addrhints, sizeof(struct addrinfo));
77 addrhints.ai_family = PF_INET;
78 addrhints.ai_socktype = dvbhost->socktype;
79
80 if ((res = getaddrinfo(dvbhost->hostname, dvbhost->port, &addrhints, &s_addrinfo))) {
81 fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(res));
82 return -1;
83 }
84
85 bzero(server, sizeof(struct sockaddr_in));
86 server->sin_family = AF_INET;
87 server->sin_addr.s_addr = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_addr.s_addr;
88 server->sin_port = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_port;
89
90 freeaddrinfo(s_addrinfo);
91
92 return 0;
93}
94
c0833e33 95int is_url(char *string)
96{
97 char *pos;
98
99 if (!(strlen(string)))
100 return 0;
101
102 pos = string;
103
104 while(*pos != ':' && *pos != 0)
105 pos++;
106
107 if (!strncmp("://", pos, 3))
108 return 1;
109
110 return 0;
111}
Impressum, Datenschutz