source: trunk/minix/commands/urlget/net.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.9 KB
RevLine 
[9]1/* net.c Copyright 2000 by Michael Temari All Rights Reserved */
2/* 04/05/2000 Michael Temari <Michael@TemWare.Com> */
3
4#include <sys/types.h>
5#include <sys/ioctl.h>
6#include <sys/wait.h>
7#include <stdio.h>
8#include <ctype.h>
9#include <stdlib.h>
10#include <string.h>
11#include <errno.h>
12#include <fcntl.h>
13#include <signal.h>
14#include <unistd.h>
15#include <time.h>
16#include <net/netlib.h>
17#include <net/hton.h>
18#include <net/gen/in.h>
19#include <net/gen/inet.h>
20#include <net/gen/tcp.h>
21#include <net/gen/tcp_io.h>
22#include <net/gen/socket.h>
23#include <net/gen/netdb.h>
24
25#include "net.h"
26
27int connect(host, port)
28char *host;
29int port;
30{
31nwio_tcpconf_t tcpconf;
32nwio_tcpcl_t tcpcopt;
33char *tcp_device;
34int netfd;
35ipaddr_t nethost;
36tcpport_t netport;
37struct hostent *hp;
38struct servent *sp;
39char *p;
40int s;
41int tries;
42
43 if((hp = gethostbyname(host)) == (struct hostent *)NULL) {
44 fprintf(stderr, "Unknown host %s!\n", host);
45 return(-1);
46 } else
47 memcpy((char *) &nethost, (char *) hp->h_addr, hp->h_length);
48
49 netport = htons(port);
50
51 /* Connect to the host */
52 if((tcp_device = getenv("TCP_DEVICE")) == NULL)
53 tcp_device = TCP_DEVICE;
54
55 if((netfd = open(tcp_device, O_RDWR)) < 0) {
56 perror("httpget: opening tcp");
57 return(-1);
58 }
59
60 tcpconf.nwtc_flags = NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
61 tcpconf.nwtc_remaddr = nethost;
62 tcpconf.nwtc_remport = netport;
63
64 s = ioctl(netfd, NWIOSTCPCONF, &tcpconf);
65 if(s < 0) {
66 perror("httpget: NWIOSTCPCONF");
67 close(netfd);
68 return(-1);
69 }
70
71 s = ioctl(netfd, NWIOGTCPCONF, &tcpconf);
72 if(s < 0) {
73 perror("httpget: NWIOGTCPCONF");
74 close(netfd);
75 return(-1);
76 }
77
78 tcpcopt.nwtcl_flags = 0;
79
80 tries = 0;
81 do {
82 s = ioctl(netfd, NWIOTCPCONN, &tcpcopt);
83 if(s == -1 && errno == EAGAIN) {
84 if(tries++ >= 10)
85 break;
86 sleep(10);
87 } else
88 break;
89 } while(1);
90
91 if(s < 0) {
92 perror("httpget: NWIOTCPCONN");
93 close(netfd);
94 return(-1);
95 }
96
97 return(netfd);
98}
Note: See TracBrowser for help on using the repository browser.