File: inet.c

package info (click to toggle)
xtel 3.1.4-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,988 kB
  • ctags: 4,359
  • sloc: ansic: 14,674; makefile: 3,931; sh: 41
file content (124 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download | duplicates (3)
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
/*	
 *   xtel - Emulateur MINITEL sous X11
 *
 *   Copyright (C) 1991-1996  Lectra Systemes & Pierre Ficheux
 *
 *   This program 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 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*
 *
 *	Routines standard de creation de sockets cote client.
 *
 *	Permet de creer des socket tcp sur un service donne).
 *
 *      D'apres Ronan KERYELL (Ecole Normale Superieure, Paris)
 *               keryell@ens.fr
 */
static char rcsid[] = "$Id: inet.c,v 1.3 1996/10/04 17:04:11 pierre Exp $";

#ifndef NO_NETWORK

#include <sys/types.h>
#define _TYPE
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#if defined(SVR4) || defined(hpux)
#define bcopy(src,dest,len) (memcpy(dest,src,len))
#endif
 
#ifndef u_short
#define u_short ushort
#endif

c_socket (host, port)
char *host;
int port;
{
  int sock;
  struct sockaddr_in addr;
  struct hostent *hp;

  /* Recupere l'adresse de l'hote cible */
  if (!(hp = gethostbyname (host))){
    perror("gethostbyname");
    return (-1);
  }
  /* Et on cree la socket */
  if ((sock = socket(AF_INET, SOCK_STREAM,0)) < 0){
    perror("socket");
    return (-1);
  }
  /* Preparons l'adresse pour le connect */
  /* Le type d'adresse */
  addr.sin_family = hp->h_addrtype;
  /* Le port (recupere dans le getservbyname) */
  addr.sin_port = port;
  /* L'adresse de l'hote cible */
  bcopy(hp->h_addr, (caddr_t)&addr.sin_addr, hp->h_length);

  /* Et zoup : on se connecte */
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect");
    return (-1);
  }
  /* Ok ! : on a une socket en tcp de bon gout */
  return (sock);
}

/*
 *	Cree une socket vers un hote donne, sur un service donne,
 *
 *	Ex : sock = c_clientbyname("aneth", "courier")
 *
 */
c_clientbyname (host, name)
char *host, *name;
{
  struct servent *sv;  

  /* Description du service recherche (essentiellement, le numero de port) */
  if (!(sv = getservbyname (name, "tcp"))) {
    perror ("getservbyname");
    return (-1);
  }

  return (c_socket (host, sv->s_port));
}

/*
 *	Cree une socket vers un hote donne, sur un port donne,
 *
 *	Ex : sock = c_clientbyport ("aneth", 1313)
 *
 */
c_clientbyport (host, port)
char *host;
int port;
{
  struct servent *sv;  

  /* Description du service recherche (essentiellement, le numero de port) */
  if (!(sv = getservbyport (htons(port), "tcp"))) {
    perror ("getservbyport");
    return (-1);
  }

  return (c_socket (host, sv->s_port));
}

#endif /* NO_NETWORK */