im a real beginner with android (and phones in general, except linux and my nintendo ds, i never coded anything else), and im having an error saying "Bad file number" when executing my app.
My app is a socket client using SSL, coded in C.
Here is the code of the client made for android :
- Code: Select all
/* ssl_client.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
/*** ssl_client.c ***/
/*** ***/
/*** Demonstrate an SSL client. ***/
/*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <resolv.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <arpa/inet.h>
#define FAIL -1
/*---------------------------------------------------------------------*/
/*--- OpenConnection - create socket and connect to server. ---*/
/*---------------------------------------------------------------------*/
int OpenConnection(const char *hostname, int port)
{
int sd;
struct hostent *host;
struct sockaddr_in addr;
if ( (host = gethostbyname(hostname)) == NULL )
{
perror(hostname);
abort();
}
sd = socket(PF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *(long*)(host->h_addr);
if ( connect(sd, &addr, sizeof(addr)) != 0 )
{
close(sd);
perror(hostname);
abort();
}
return sd;
}
/*---------------------------------------------------------------------*/
/*--- InitCTX - initialize the SSL engine. ---*/
/*---------------------------------------------------------------------*/
SSL_CTX* InitCTX(void)
{
SSL_METHOD *method;
SSL_CTX *ctx;
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
SSL_load_error_strings(); /* Bring in and register error messages */
method = TLSv1_client_method(); /* Create new client-method instance */
ctx = SSL_CTX_new(method); /* Create new context */
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
abort();
}
return ctx;
}
/*---------------------------------------------------------------------*/
/*--- ShowCerts - print out the certificates. ---*/
/*---------------------------------------------------------------------*/
void ShowCerts(SSL* ssl)
{
X509 *cert;
char *line;
cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
if ( cert != NULL )
{
printf("Server certificates:n");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %sn", line);
free(line); /* free the malloc'ed string */
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %sn", line);
free(line); /* free the malloc'ed string */
X509_free(cert); /* free the malloc'ed certificate copy */
}
else
printf("No certificates.n");
}
/*---------------------------------------------------------------------*/
/*--- main - create SSL context and connect ---*/
/*---------------------------------------------------------------------*/
int main(int count, char *strings[])
{
SSL_CTX *ctx;
int server;
SSL *ssl;
char buf[1024];
int bytes;
char *hostname, *portnum;
if ( count != 3 )
{
printf("usage: %s <hostname> <portnum>n", strings[0]);
exit(0);
}
hostname=strings[1];
portnum=strings[2];
SSL_library_init();
ctx = InitCTX();
server = OpenConnection(hostname, atoi(portnum));
ssl = SSL_new(ctx); /* create new SSL connection state */
SSL_set_fd(ssl, server); /* attach the socket descriptor */
if ( SSL_connect(ssl) == FAIL ) /* perform the connection */
ERR_print_errors_fp(stderr);
else
{
char *msg = "Hello from Bryan";
printf("Connected with %s encryptionn", SSL_get_cipher(ssl));
ShowCerts(ssl); /* get any certs */
SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
buf[bytes] = 0;
printf("Received: "%s"n", buf);
SSL_free(ssl); /* release connection state */
}
close(server); /* close socket */
SSL_CTX_free(ctx); /* release context */
}
i compile it this way :
- Code: Select all
arm-eabi-gcc -o ssl_client -I/home/kuri/Apps/android/system/core/include -I/home/kuri/Apps/android/hardware/libhardware/include -I/home/kuri/Apps/android/hardware/ril/include -I/home/kuri/Apps/android/dalvik/libnativehelper/include -I/home/kuri/Apps/android/frameworks/base/include -I/home/kuri/Apps/android/external/skia/include -I/home/kuri/Apps/android/out/target/product/generic/obj/include -I/home/kuri/Apps/android/bionic/libc/arch-arm/include -I/home/kuri/Apps/android/bionic/libc/include -I/home/kuri/Apps/android/bionic/libstdc++/include -I/home/kuri/Apps/android/bionic/libc/kernel/common -I/home/kuri/Apps/android/bionic/libc/kernel/arch-arm -I/home/kuri/Apps/android/bionic/libm/include -I/home/kuri/Apps/android/bionic/libm/include/arch/arm -I/home/kuri/Apps/android/bionic/libthread_db/include -I/home/kuri/Apps/android/bionic/libm/arm -I/home/kuri/Apps/android/bionic/libm -I/home/kuri/Apps/android/external/openssl/include -I/home/kuri/Apps/android/out/target/product/generic/obj/SHARED_LIBRARIES/libm_intermediates -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DSK_RELEASE -DNDEBUG -include /home/kuri/Apps/android/system/core/include/arch/linux-arm/AndroidConfig.h -UDEBUG -march=armv5te -mtune=xscale -msoft-float -mthumb-interwork -fpic -fno-exceptions -ffunction-sections -funwind-tables -fstack-protector -fmessage-length=0 -Bdynamic -Wl,-T,/home/kuri/Apps/android/build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -Wl,--no-undefined -Wl,-rpath-link=/home/kuri/Apps/android/out/target/product/generic/obj/lib -L/home/kuri/Apps/android/out/target/product/generic/obj/lib -nostdlib /home/kuri/Apps/android/out/target/product/generic/obj/lib/crtend_android.o /home/kuri/Apps/android/out/target/product/generic/obj/lib/crtbegin_dynamic.o /home/kuri/Apps/android/out/target/product/generic/obj/lib/libssl.so /home/kuri/Apps/android/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/lib/gcc/arm-eabi/4.3.1/libgcc.a -lc -lm client.c
if i do :
kuri[@]Jetdail(14:33:51)[~/Work/Android/C/SSL_Client_Demo]
$ adb shell
# cd /data/local
# ls
ssl_client
tmp
test
# chmod 755 ssl_client
# ./ssl_client 192.168.2.89 12345
Connected with AES256-SHA encryption
Server certificates:
Subject: /C=FR/ST=xxxx/L=xxxxt/O=xxxx/CN=None/emailAddress=xxxx@xxxx.com
Issuer: /C=FR/ST=xxxx/L=xxxx/O=xxxx/CN=None/emailAddress=xxxx@xxxx.com
Received: "Hello Bryan"
If i do the same using the emulator (menu/devtools/terminal)
i cant chmod (permission denied), i cant ls /data/local (denied too), and executing ssl_client gives me "Bad file number".
here is the code of the ssl server, so you can test it :
- Code: Select all
/* ssl_server.c
* *
* * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* * whole or in part in accordance to the General Public License (GPL).
* *
* * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS `AS IS'' AND
* * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* * SUCH DAMAGE.
* */
/*****************************************************************************/
/*** ssl_server.c ***/
/*** ***/
/*** Demonstrate an SSL server. ***/
/*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define FAIL -1
/*---------------------------------------------------------------------*/
/*--- OpenListener - create server socket ---*/
/*---------------------------------------------------------------------*/
int OpenListener(int port)
{
int sd;
struct sockaddr_in addr;
sd = socket(PF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if ( bind(sd, &addr, sizeof(addr)) != 0 )
{
perror("can't bind port");
abort();
}
if ( listen(sd, 10) != 0 )
{
perror("Can't configure listening port");
abort();
}
return sd;
}
/*---------------------------------------------------------------------*/
/*--- InitServerCTX - initialize SSL server and create context ---*/
/*---------------------------------------------------------------------*/
SSL_CTX* InitServerCTX(void)
{
SSL_METHOD *method;
SSL_CTX *ctx;
OpenSSL_add_all_algorithms();/* load & register all cryptos, etc. */
SSL_load_error_strings();/* load all error messages */
method = TLSv1_server_method();/* create new server-method instance */
ctx = SSL_CTX_new(method);/* create new context from method */
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
abort();
}
return ctx;
}
/*---------------------------------------------------------------------*/
/*--- LoadCertificates - load from files. ---*/
/*---------------------------------------------------------------------*/
void LoadCertificates(SSL_CTX* ctx, char* CertFile, char* KeyFile)
{
/* set the local certificate from CertFile */
if ( SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
abort();
}
/* set the private key from KeyFile (may be the same as CertFile) */
if ( SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM) <= 0 )
{
ERR_print_errors_fp(stderr);
abort();
}
/* verify private key */
if ( !SSL_CTX_check_private_key(ctx) )
{
fprintf(stderr, "Private key does not match the public certificaten");
abort();
}
}
/*---------------------------------------------------------------------*/
/*--- ShowCerts - print out certificates. ---*/
/*---------------------------------------------------------------------*/
void ShowCerts(SSL* ssl)
{
X509 *cert;
char *line;
cert = SSL_get_peer_certificate(ssl);/* Get certificates (if available) */
if ( cert != NULL )
{
printf("Server certificates:n");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
printf("Subject: %sn", line);
free(line);
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
printf("Issuer: %sn", line);
free(line);
X509_free(cert);
}
else
printf("No certificates.n");
}
/*---------------------------------------------------------------------*/
/*--- Servlet - SSL servlet (contexts can be shared) ---*/
/*---------------------------------------------------------------------*/
void Servlet(SSL* ssl)/* Serve the connection -- threadable */
{
char buf[1024];
char reply[1024];
int sd, bytes;
const char* answer="Hello %s";
char name[512];
memset(name, 0, sizeof(name));
if ( SSL_accept(ssl) == FAIL )/* do SSL-protocol accept */
ERR_print_errors_fp(stderr);
else
{
ShowCerts(ssl);/* get any certificates */
bytes = SSL_read(ssl, buf, sizeof(buf));/* get request */
if ( bytes > 0 )
{
buf[bytes] = 0;
printf("Client msg: "%s"n", buf);
sscanf(buf, "%*s %*s %s", name);
sprintf(reply, answer, name);/* construct reply */
SSL_write(ssl, reply, strlen(reply));/* send reply */
}
else
ERR_print_errors_fp(stderr);
}
sd = SSL_get_fd(ssl);/* get socket connection */
SSL_free(ssl);/* release SSL state */
close(sd);/* close connection */
}
/*---------------------------------------------------------------------*/
/*--- main - create SSL socket server. ---*/
/*---------------------------------------------------------------------*/
int main(int argc, char **argv)
{
SSL_CTX *ctx;
int server;
char *portnum;
if ( argc != 2 )
{
printf("Usage: %s <portnum>n", argv[0]);
exit(0);
}
portnum = argv[1];
SSL_library_init();
ctx = InitServerCTX();/* initialize SSL */
LoadCertificates(ctx, "mycert.pem", "mykey.pem");/* load certs */
server = OpenListener(atoi(portnum));/* create server socket */
while (1)
{
struct sockaddr_in addr;
int len = sizeof(addr);
SSL *ssl;
int client = accept(server, &addr, &len);/* accept connection as usual */
printf("Connection: %s:%dn",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
ssl = SSL_new(ctx); /* get new SSL state with context */
SSL_set_fd(ssl, client);/* set connection socket to SSL state */
Servlet(ssl);/* service connection */
}
close(server);/* close server socket */
SSL_CTX_free(ctx);/* release context */
}
so if anyone can explain me why i dont have rights (am i not root ?), and why saying "bad file number" (normally it happens if my file descriptor is wrong, which isnt in this case)

