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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* DDNS v3 Client
*
* By: Alan Yates <alany@ay.com.au>
* Date: 27-08-2000
*/
#ifdef WIN32
#include <windows.h>
#else
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#endif
#include <stdio.h>
#include "sockio.h"
int
ddns3_sockio_resolve(char *str, struct sockaddr_in *sai) {
struct hostent *he;
char buf[500];
char err[100];
short port;
int ret, i;
for(i = 0; str[i]; i++) if(str[i] == ':') str[i] = ' ';
ret = sscanf(str, "%s%hd", buf, &port);
if(ret < 2) return -1;
if((inet_aton(buf, &sai->sin_addr)) == 0) {
if(!(he = gethostbyname(buf))) {
//perror("gethostbyname()");
ddns3_sockio_error(err,100);
fprintf(stderr,"gethostbyname(): %s\n", err);
return -1;
}
memcpy(&sai->sin_addr.s_addr, he->h_addr, he->h_length);
}
sai->sin_port = htons(port);
sai->sin_family = AF_INET;
return 0;
}
int
ddns3_sockio_connect(char *host) {
int ret;
int sock;
struct sockaddr_in sai;
char err[100];
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0) {
//perror("socket(PF_INET, SOCK_STREAM, 0)");
ddns3_sockio_error(err,100);
fprintf(stderr, "socket(): %s\n",err);
return -1;
}
ret = ddns3_sockio_resolve(host, &sai);
if(ret < 0) {
return -1;
}
ret = connect(sock, (struct sockaddr *)&sai, sizeof(struct sockaddr_in));
if(ret < 0) {
//perror("connect()");
ddns3_sockio_error(err,100);
fprintf(stderr, "connect(): %s\n",err);
return -1;
}
return sock;
}
int
ddns3_sockio_write(int sock, char *buf, int len) {
int ret;
char err[100];
/* FIXME: we pretend short writes never happen! */
ret = send(sock, buf, len, 0);
if(ret < 0) {
//perror("write()");
ddns3_sockio_error(err,100);
fprintf(stderr, "send(): %s\n", err);
}
return ret;
}
int
ddns3_sockio_read(int sock, char *buf, int len) {
int ret;
char err[100];
ret = recv(sock, buf, len, 0);
if(ret < 0){
//perror("read()");
ddns3_sockio_error(err,100);
fprintf(stderr, "recv(): %s\n", err);
}
return ret;
}
int
ddns3_sockio_close(int sock) {
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return 0;
}
int
ddns3_sockio_init(void) {
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return -1;
}
#endif
return 0;
}
int
ddns3_sockio_cleanup(void) {
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
int
ddns3_sockio_error(char *buf, int len) {
int code;
#ifdef WIN32
code = WSAGetLastError();
sprintf(buf,"%d",code);
#else
code = errno;
sprintf(buf,"%s",strerror(code));
#endif
return code;
}
int
ddns3_sockio_getlocalip(int sock, char *buf, int len) {
int ret;
char err[100];
struct sockaddr_in sai;
int sailen = sizeof(struct sockaddr_in);
ret = getsockname(sock, (struct sockaddr *)&sai, &sailen);
if(ret < 0) {
ddns3_sockio_error(err,100);
fprintf(stderr, "getsockname(): %s\n", err);
}
if(!buf || len < 16) return -1;
return sprintf(buf, "%s", inet_ntoa(sai.sin_addr));
}
|