summaryrefslogtreecommitdiff
path: root/gopher.c
blob: 28caab0322cff17bab31056cd9bd15fbef2a7d1c (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Gopher protocol routines

   Copyright (C) 2002 James A. Morrison <ja2morri@uwaterloo.ca>
   Copyright (C) 2000 Igor Khavkine <igor@twu.net>
   This file is part of Gopherfs.

   Gopherfs is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   Gopherfs is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <stdio.h>
#include <error.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "gopherfs.h"

#include <hurd/hurd_types.h>
#include <hurd/netfs.h>

/* do a DNS lookup for NAME:PORT and store results in ENT and error in H_ERR */
error_t
lookup_host (char *name, unsigned short port, struct addrinfo **ai)
{
  /* XXX: cache host lookups. */
  struct addrinfo hints;
  char *service;
  error_t err;

  memset (&hints, 0, sizeof(hints));
  hints.ai_family = PF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_IP;
  hints.ai_flags = AI_NUMERICSERV;

  err = asprintf (&service, "%hu", port);
  if (err == -1)
    return ENOMEM;

  err = getaddrinfo (name, service, &hints, ai);

  free (service);

  return err;
}

/* open ENTRY and store the remote socket in *FD */
error_t
gopher_open (struct gopher_entry *entry, int *fd)
{
  struct addrinfo *addr;
  error_t err;
  ssize_t written;
  size_t towrite;
  
  *fd = socket (PF_INET, SOCK_STREAM, 0);
  if (*fd < 0)
    {
      debug ("failed to open socket (errno: %s)", strerror(errno));
      return errno;
    }

  err = lookup_host (entry->server, entry->port, &addr);
  if (err)
    {
      debug ("looking up host failed: %s", gai_strerror(err));
      return err;
    }

  /* XXX: cache host lookups. */
  do
    {
      debug ("connecting to %s:%hu...", 
	     inet_ntoa(((struct sockaddr_in *)addr->ai_addr)->sin_addr), 
	     ntohs(((struct sockaddr_in *)addr->ai_addr)->sin_port));

      err = connect (*fd, (struct sockaddr_in *) addr->ai_addr, addr->ai_addrlen);
      if (err)
	debug ("connect() failed! errno: %s", strerror(errno));

      addr = addr->ai_next;
    }
  while (addr && err);

  freeaddrinfo(addr);
  if (err)
    {
      close (*fd);
      return errno;
    }

  /* Write selector to *FD. */

  towrite = strlen (entry->selector);
  /* guard against EINTR failures */
  written = TEMP_FAILURE_RETRY (write (*fd, entry->selector, towrite));
  written += TEMP_FAILURE_RETRY (write (*fd, "\r\n", 2));
  if (written == -1 || written < (towrite + 2))
    return errno;

  return 0;
}

/* List all entries from ENTRY and store them in *MAP. */
error_t
gopher_list_entries (struct gopher_entry *entry, struct gopher_entry **map)
{
  FILE *sel;
  char *line = NULL;
  size_t line_len = 0;
  struct gopher_entry *prev = NULL;
  error_t err = 0;

  {
    int fd;
    err = gopher_open (entry, &fd);
    if (err)
	return err;

    sel = fdopen (fd, "r");
    if (!sel)
      {
	close (fd);
	return errno;
      }
  }
  
  while (getline (&line, &line_len, sel) >= 0)
    {
      /* Parse gopher entry. */
      struct gopher_entry *cur;
      char *tmp;

      debug ("%s", line);

      if (*line == '.' || err)
	break;

      cur = malloc (sizeof (struct gopher_entry));
      if (!cur)
	{
	 err = ENOMEM;
	 break;
	}

      cur->type = line[0];
      tmp = line + 1;
      cur->name = strdup(strsep (&tmp, "\t"));
      cur->selector = strdup(strsep (&tmp, "\t"));
      cur->server = strdup(strsep (&tmp, "\t"));
      cur->port = (unsigned short) atoi (strsep (&tmp, "\t"));

      if (!*map)
	/* First item. */
	*map = cur;
      else
	prev->next = cur;

      cur->prev = prev;
      cur->next = NULL;
      prev = cur;
    }

  free (line);
  fclose (sel);

  return err;
}