00001
00002
00003
00004
00005 #include "sockets.h"
00006
00007 static char *get_doted_ip (unsigned long int ip);
00008
00009
00010
00011
00012 #define SOCKET_ERROR_SIZE 4096
00013
00014
00015
00016
00017 #define MAX_INTERFACE 16
00018
00019
00020
00021
00022 static char last_error[SOCKET_ERROR_SIZE];
00023
00024
00025
00026
00027
00028
00029
00030 char *get_last_socket_error() { return last_error; }
00031
00032
00033
00034
00035
00036 static int my_fake_syslog (const char *file, const char * fmt,...) { return 0; }
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 int create_socket (
00068 int port,
00069 struct sockaddr_in *add,
00070 char *host,
00071 char *interface,
00072 int len,
00073 unsigned long int *broadcast,
00074 unsigned long int *netmask,
00075 unsigned long int *myip,
00076 int (*syslog)(const char *file, const char * fmt,...),
00077 char *log
00078 )
00079 {
00080 int desc, longueur, on, i, number_of_interfaces, length;
00081 struct ifreq *ifr, if_req;
00082 struct hostent *h;
00083 struct ifconf if_conf;
00084 unsigned long int ip, fip;
00085 char *buffer, interfaces_names[MAX_INTERFACE][IFNAMSIZ];
00086 char *log_file;
00087 int (*my_syslog) (const char *file, const char * fmt,...);
00088
00089
00090
00091 if ((syslog != NULL) && (log != NULL))
00092 {
00093 my_syslog = syslog;
00094 log_file = log;
00095 }
00096 else { my_syslog = my_fake_syslog; }
00097
00098 memset ((void*)last_error, 0, SOCKET_ERROR_SIZE);
00099 for (i=0; i<MAX_INTERFACE; i++) { memset ((void*)&(interfaces_names[i][0]), 0, IFNAMSIZ); }
00100 *myip = 0;
00101
00102
00103
00104
00105
00106 if (len < IFNAMSIZ)
00107 {
00108 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00109 "Invalid lenght for interface's buffer (segmentation fault avoided!)");
00110 return -1;
00111 }
00112
00113 memset ((void*)interface, 0, len);
00114
00115
00116
00117
00118
00119 h = gethostbyname(host);
00120
00121 if (h == NULL)
00122 {
00123 snprintf (last_error, SOCKET_ERROR_SIZE - 1, "System call gethostbyname(%s) failed - %s)",
00124 host,
00125 strerror(errno));
00126 return -1;
00127 }
00128
00129 if (h->h_length != 4)
00130 {
00131 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00132 "The size of the Internet address returned by gethostbyname(%s) - %d - is not valid",
00133 host,
00134 h->h_length);
00135 return -1;
00136 }
00137
00138 memcpy ((void*)&ip, h->h_addr_list[0], h->h_length);
00139
00140 my_syslog (log_file, "[INFO] [%s:%d] IP address of host %s is %s",
00141 __FILE__, __LINE__,
00142 host,
00143 get_doted_ip(ip));
00144
00145 *myip = ip;
00146
00147
00148
00149
00150
00151 if ((desc = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
00152 {
00153 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00154 "System call socket(INET, SOCK_DGRAM, IPPROTO_UDP) failed - %s", strerror(errno));
00155 return -1;
00156 }
00157
00158
00159
00160
00161
00162 memset ((void*)&if_conf, 0, sizeof(struct ifconf));
00163
00164 buffer = (char*)malloc(MAX_INTERFACE * sizeof(struct ifreq));
00165 if (buffer == NULL)
00166 {
00167 snprintf (last_error, SOCKET_ERROR_SIZE - 1, "Could not allocate memory (this should never happen)");
00168 return -1;
00169 }
00170
00171 if_conf.ifc_len = MAX_INTERFACE * sizeof(struct ifreq);
00172 if_conf.ifc_buf = buffer;
00173
00174 if (ioctl(desc, SIOCGIFCONF, &if_conf) == -1)
00175 {
00176 snprintf (last_error, SOCKET_ERROR_SIZE - 1, "System call ioctl(SIOCGIFCONF) failed - %s", strerror(errno));
00177 free (buffer);
00178 return -1;
00179 }
00180
00181
00182
00183
00184
00185 number_of_interfaces = 0;
00186 for (i=0; i<(if_conf.ifc_len)/sizeof(struct ifreq); i++)
00187 {
00188 ifr = ((struct ifreq*)buffer) + i;
00189
00190
00191
00192
00193
00194 if (ifr->ifr_addr.sa_family != AF_INET) { continue; }
00195
00196
00197
00198
00199
00200 memcpy (interfaces_names[number_of_interfaces++], ifr->ifr_name, IFNAMSIZ);
00201 }
00202
00203 free (buffer);
00204
00205
00206
00207
00208
00209 for (i=0; i<number_of_interfaces; i++)
00210 {
00211
00212
00213
00214
00215
00216
00217
00218
00219 memset ((void*)&if_req, 0, sizeof(struct ifreq));
00220 strcpy (if_req.ifr_name, interfaces_names[i]);
00221
00222 if (ioctl(desc, SIOCGIFADDR, &if_req) == -1)
00223 {
00224 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00225 "System call ioctl(SIOCGIFADDR) failed - %s", strerror(errno));
00226 return -1;
00227 }
00228
00229 fip = ((struct sockaddr_in*)(&(if_req.ifr_addr)))->sin_addr.s_addr;
00230
00231 if (fip == ip) { strcpy (interface, interfaces_names[i]); }
00232
00233 my_syslog (log_file, "[INFO] [%s:%d] Found interface %s (%s)",
00234 __FILE__, __LINE__,
00235 interfaces_names[i],
00236 get_doted_ip(fip));
00237 }
00238
00239
00240
00241
00242
00243 if (interface[0] == 0)
00244 {
00245 snprintf (last_error, SOCKET_ERROR_SIZE - 1, "No interface found for alias %s (%s)", host, get_doted_ip(ip));
00246 return -1;
00247 }
00248
00249
00250
00251
00252
00253 memset ((void*)&if_req, 0, sizeof(struct ifreq));
00254 strcpy (if_req.ifr_name, interface);
00255
00256 if (ioctl(desc, SIOCGIFNETMASK, &if_req) == -1)
00257 {
00258 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00259 "System call ioctl(SIOCGIFNETMASK) failed - %s", strerror(errno));
00260 return -1;
00261 }
00262
00263 *netmask = ((struct sockaddr_in*)(&(if_req.ifr_netmask)))->sin_addr.s_addr;
00264
00265 my_syslog (log_file, "[INFO] [%s:%d] Netmask assigned to %s is: %s",
00266 __FILE__, __LINE__,
00267 interface,
00268 get_doted_ip(*netmask));
00269
00270
00271
00272
00273
00274 memset ((void*)&if_req, 0, sizeof(struct ifreq));
00275 strcpy (if_req.ifr_name, interface);
00276
00277 if (ioctl(desc, SIOCGIFBRDADDR, &if_req) == -1)
00278 {
00279 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00280 "System call ioctl(SIOCGIFNETMASK) failed - %s", strerror(errno));
00281 return -1;
00282 }
00283
00284 *broadcast = ((struct sockaddr_in*)(&(if_req.ifr_broadaddr)))->sin_addr.s_addr;
00285
00286 my_syslog (log_file, "[INFO] [%s:%d] Broadcast address assigned to %s is: %s",
00287 __FILE__, __LINE__,
00288 interface,
00289 get_doted_ip(*broadcast));
00290
00291
00292
00293
00294
00295 on = 1;
00296 if (setsockopt(desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
00297 {
00298 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00299 "System call setsockopt(SO_REUSEADDR) failed - %s", strerror(errno));
00300 return -1;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 longueur = (int)sizeof (struct sockaddr_in);
00330
00331 memset ((void*)add, 0, longueur);
00332 add->sin_family = AF_INET;
00333 add->sin_port = htons((unsigned short)port);
00334 add->sin_addr.s_addr = INADDR_ANY;
00335
00336 if (bind(desc, (struct sockaddr*)add, longueur) == -1)
00337 {
00338 snprintf (last_error, SOCKET_ERROR_SIZE - 1, "System call bind() failed - %s", strerror(errno));
00339 return -1;
00340 }
00341
00342
00343
00344
00345
00346 if (setsockopt(desc, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)+1) < 0)
00347 {
00348 snprintf (last_error, SOCKET_ERROR_SIZE - 1, "System call setsockopt(SO_BINDTODEVICE, %s) failed - %s",
00349 interface,
00350 strerror(errno));
00351 return -1;
00352 }
00353
00354 my_syslog (log_file,
00355 "[INFO] [%s:%d] Binding server to IP address %s - (%s) on port %d - %s (%s)",
00356 __FILE__, __LINE__,
00357 get_doted_ip(add->sin_addr.s_addr),
00358 interface,
00359 port,
00360 host,
00361 get_doted_ip(ip));
00362
00363
00364
00365
00366
00367 on = 1;
00368 if (setsockopt(desc, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int)) < 0)
00369 {
00370 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00371 "System call setsockopt(SO_BROADCAST) failed - %s", strerror(errno));
00372 return -1;
00373 }
00374
00375
00376
00377
00378
00379 on = 0;
00380 length = sizeof(int);
00381 if (getsockopt(desc, SOL_SOCKET, SO_BROADCAST, &on, &length) < 0)
00382 {
00383 snprintf (last_error, SOCKET_ERROR_SIZE - 1,
00384 "System call getsockopt(SO_BROADCAST) failed - %s", strerror(errno));
00385 return -1;
00386 }
00387
00388 if (on == 1)
00389 {
00390 my_syslog (log_file, "[INFO] [%s:%d] Broadcast is activated", __FILE__, __LINE__);
00391 }
00392 else
00393 {
00394 my_syslog (log_file, "[INFO] [%s:%d] Broadcast is __NOT__ activated",
00395 __FILE__, __LINE__);
00396 }
00397
00398 return (desc);
00399 };
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 void get_remote_address (struct sockaddr_in *sender, char *ip, int *port)
00410 {
00411 int i;
00412 char *a;
00413
00414 a = inet_ntoa(sender->sin_addr);
00415 for (i=0; i<MAX_IP_ADDRESS_STR_SIZE-1; i++) { ip[i] = *(a+i); };
00416 ip[MAX_IP_ADDRESS_STR_SIZE-1] = 0;
00417 *port = ntohs (sender->sin_port);
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 static char *get_doted_ip (unsigned long int ip)
00431 {
00432 struct in_addr add;
00433
00434 add.s_addr = ip;
00435
00436 return inet_ntoa(add);
00437 }
00438