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
|
/*
* 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.5 2001/02/13 09:36:43 pierre Exp $";
#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;
{
return (c_socket (host, htons(port)));
}
|