X-Git-Url: https://git.jankratochvil.net/?p=gnokii.git;a=blobdiff_plain;f=common%2Fdevices%2Ftcp.c;fp=common%2Fdevices%2Ftcp.c;h=ea4918e586213b2769f6404252da98d2528b00ad;hp=0000000000000000000000000000000000000000;hb=56289d787a96f7d74273e0d1d1875b487f6cfe39;hpb=e1413f172e2c080b63889db8a957f7f7c984a33e diff --git a/common/devices/tcp.c b/common/devices/tcp.c new file mode 100644 index 0000000..ea4918e --- /dev/null +++ b/common/devices/tcp.c @@ -0,0 +1,205 @@ +/* + + $Id$ + + G N O K I I + + A Linux/Unix toolset and driver for Nokia mobile phones. + + Copyright (C) 2002 Jan Kratochvil + + Released under the terms of the GNU GPL, see file COPYING for more details. + +*/ + +#include "misc.h" +#include "cfgreader.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if __unices__ +# include +#endif + +#include +#include "devices/tcp.h" +#include "devices/unixserial.h" + +#ifdef HAVE_SYS_IOCTL_COMPAT_H + #include +#endif + +#ifdef HAVE_SYS_SELECT_H +#include +#endif + +#ifndef O_NONBLOCK + #define O_NONBLOCK 0 +#endif + +/* Open the serial port and store the settings. */ + +int tcp_open(__const char *__file) { + + int __fd; + int i; + struct sockaddr_in addr; + static bool atexit_registered=false; + char *filedup,*portstr,*end; + unsigned long portul; + struct hostent *hostent; + + if (!atexit_registered) { + memset(serial_close_all_openfds,-1,sizeof(serial_close_all_openfds)); +#if 0 /* Disabled for now as atexit() functions are then called multiple times for pthreads! */ + signal(SIGINT,unixserial_interrupted); +#endif + atexit(serial_close_all); + atexit_registered=true; + } + + __fd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); + if (__fd == -1) { + perror("Gnokii tcp_open: socket()"); + return (-1); + } + if (!(filedup=strdup(__file))) { +fail_close: + close(__fd); + return (-1); + } + if (!(portstr=strchr(filedup,':'))) { + fprintf(stderr,"Gnokii tcp_open: colon (':') not found in connect strings \"%s\"!\n",filedup); +fail_free: + free(filedup); + goto fail_close; + } + *portstr++='\0'; + portul=strtoul(portstr,&end,0); + if ((end && *end) || portul>=0x10000) { + fprintf(stderr,"Gnokii tcp_open: Port string \"%s\" not valid for IPv4 connection!\n",portstr); + goto fail_free; + } + if (!(hostent=gethostbyname(filedup))) { + fprintf(stderr,"Gnokii tcp_open: Unknown host \"%s\"!\n",filedup); + goto fail_free; + } + if (hostent->h_addrtype!=AF_INET || hostent->h_length!=sizeof(addr.sin_addr) || !hostent->h_addr_list[0]) { + fprintf(stderr,"Gnokii tcp_open: Address resolve for host \"%s\" not compatible!\n",filedup); + goto fail_free; + } + free(filedup); + + addr.sin_family=AF_INET; + addr.sin_port=htons(portul); + memcpy(&addr.sin_addr,hostent->h_addr_list[0],sizeof(addr.sin_addr)); + + if (connect(__fd,(struct sockaddr *)&addr,sizeof(addr))) { + perror("Gnokii tcp_open: connect()"); + goto fail_close; + } + + for (i=0;i