/
ropen.c
64 lines (51 loc) · 1.36 KB
/
ropen.c
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
/*
Copyright (C) 2006 by Jonas Kramer
Published under the terms of the GNU General Public License (GPL).
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "ropen.h"
FILE * ropen(const char * host, unsigned short port) {
int sck;
struct hostent * entry;
struct sockaddr_in addr;
FILE * fd;
if(!host || !port) {
fputs("No host/port given.\n", stderr);
return NULL;
}
if(!(entry = gethostbyname(host)) || !entry->h_addr) {
fprintf(stderr, "%s.\n", hstrerror(h_errno));
return NULL;
}
if(-1 == (sck = socket(PF_INET, SOCK_STREAM, PF_UNSPEC))) {
fprintf(stderr, "Couldn't create socket. %s.\n", strerror(errno));
return NULL;
}
memset(& addr, 0, sizeof(struct sockaddr_in));
addr.sin_addr.s_addr = * (unsigned *) entry->h_addr;
addr.sin_port = htons(port);
addr.sin_family = PF_INET;
if(connect(sck, (struct sockaddr *) & addr, sizeof(struct sockaddr_in))) {
fprintf(stderr, "Connection failed. %s.\n", strerror(errno));
return NULL;
}
if(!(fd = fdopen(sck, "w+"))) {
fprintf(stderr, "Couldn't create file handle. %s.\n", strerror(errno));
shutdown(sck, SHUT_RDWR);
close(sck);
}
return fd;
}
void fshutdown(FILE ** fd) {
shutdown(fileno(* fd), SHUT_RDWR);
fclose(* fd);
* fd = NULL;
}