commit package to go along with the last commit, thanks fofware
[packages.git] / net / remotectrl / src / Socket_Cliente.c
1 /* Javier Abellán, 20 Junio 2000
2  *
3  * Funciones para abrir/establecer sockets de un cliente con un servidor.
4  *
5  * MODIFICACIONES:
6  * 4 Septiembre 2003. Añadida función Abre_Conexion_Udp()
7  */
8
9
10 /*
11 * Includes del sistema
12 */
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <netinet/in.h>
17 #include <netdb.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <unistd.h>
21
22 /*
23 * Conecta con un servidor Unix, en la misma maquina.
24 *       Devuelve descriptor de socket si todo es correcto, -1 si hay error.
25 */
26 int Abre_Conexion_Unix (char *Servicio)
27 {
28         struct sockaddr_un Direccion;
29         int Descriptor;
30
31         strcpy (Direccion.sun_path, Servicio);
32         Direccion.sun_family = AF_UNIX;
33
34         /* Se abre el descriptor del socket */
35         Descriptor = socket (AF_UNIX, SOCK_STREAM, 0);
36         if (Descriptor == -1)
37                 return -1;
38
39         /* Se establece la conexion.
40          * Devuelve 0 si todo va bien, -1 en caso de error */
41         if (connect (
42                         Descriptor, 
43                         (struct sockaddr *)&Direccion, 
44                         strlen (Direccion.sun_path) + sizeof (Direccion.sun_family)) == -1)
45         {
46                 return -1;
47         }
48
49         return Descriptor;
50 }
51
52 /*
53 * Conecta con un servidor remoto a traves de socket INET
54 */
55 int Abre_Conexion_Inet (
56         char *Host_Servidor, 
57         char *Servicio)
58 {
59         struct sockaddr_in Direccion;
60         struct servent *Puerto;
61         struct hostent *Host;
62         int Descriptor;
63
64 /*
65         Puerto = getservbyname (Servicio, "tcp");
66         Puerto = 15557;
67         if (Puerto == NULL)
68                 return -1;
69 */
70
71         Host = gethostbyname (Host_Servidor);
72         if (Host == NULL)
73                 return -1;
74
75         Direccion.sin_family = AF_INET;
76         Direccion.sin_addr.s_addr = ((struct in_addr *)(Host->h_addr))->s_addr;
77 //      Direccion.sin_port = Puerto->s_port;
78         Direccion.sin_port = 15557;
79         
80         Descriptor = socket (AF_INET, SOCK_STREAM, 0);
81         if (Descriptor == -1)
82                 return -1;
83
84         if (connect (
85                         Descriptor, 
86                         (struct sockaddr *)&Direccion, 
87                         sizeof (Direccion)) == -1)
88         {
89                 return -1;
90         }
91
92         return Descriptor;
93 }
94
95
96 /*
97  * Prepara un socket para un cliente UDP.
98  * Asocia un socket a un cliente UDP en un servicio cualquiera elegido por el sistema,
99  * de forma que el cliente tenga un sitio por el que enviar y recibir mensajes.
100  * Devuelve el descriptor del socket que debe usar o -1 si ha habido algún error.
101  */
102 int Abre_Conexion_Udp ()
103 {
104         struct sockaddr_in Direccion;
105         int Descriptor;
106
107         /* Se abre el socket UDP (DataGRAM) */
108         Descriptor = socket (AF_INET, SOCK_DGRAM, 0);
109         if (Descriptor == -1)
110         {
111                 return -1;
112         }
113
114         /* Se rellena la estructura de datos necesaria para hacer el bind() */
115         Direccion.sin_family = AF_INET;            /* Socket inet */
116         Direccion.sin_addr.s_addr = htonl(INADDR_ANY);    /* Cualquier dirección IP */
117         Direccion.sin_port = htons(0);                    /* Dejamos que linux eliga el servicio */
118
119         /* Se hace el bind() */
120         if (bind (
121                         Descriptor, 
122                         (struct sockaddr *)&Direccion, 
123                         sizeof (Direccion)) == -1)
124         {
125                 close (Descriptor);
126                 return -1;
127         }
128
129         /* Se devuelve el Descriptor */
130         return Descriptor;
131 }