Commit 5a6efecf78112f2e882473e6610c9b1f2e1cdada

Authored by Stéphane Raimbault
1 parent f807767f

Replace inet_addr by inet_pton calls

Showing 1 changed file with 21 additions and 2 deletions
src/modbus-tcp.c
... ... @@ -351,7 +351,16 @@ static int _modbus_tcp_connect(modbus_t *ctx)
351 351  
352 352 addr.sin_family = AF_INET;
353 353 addr.sin_port = htons(ctx_tcp->port);
354   - addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
  354 + rc = inet_pton(addr.sin_family, ctx_tcp->ip, &(addr.sin_addr));
  355 + if (rc <= 0) {
  356 + if (ctx->debug) {
  357 + fprintf(stderr, "Invalid IP address: %s\n", ctx_tcp->ip);
  358 + }
  359 + close(ctx->s);
  360 + ctx->s = -1;
  361 + return -1;
  362 + }
  363 +
355 364 rc =
356 365 _connect(ctx->s, (struct sockaddr *) &addr, sizeof(addr), &ctx->response_timeout);
357 366 if (rc == -1) {
... ... @@ -495,6 +504,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
495 504 int flags;
496 505 struct sockaddr_in addr;
497 506 modbus_tcp_t *ctx_tcp;
  507 + int rc;
498 508  
499 509 if (ctx == NULL) {
500 510 errno = EINVAL;
... ... @@ -536,8 +546,17 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
536 546 addr.sin_addr.s_addr = htonl(INADDR_ANY);
537 547 } else {
538 548 /* Listen only specified IP address */
539   - addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
  549 + rc = inet_pton(addr.sin_family, ctx_tcp->ip, &(addr.sin_addr));
  550 + if (rc <= 0) {
  551 + if (ctx->debug) {
  552 + fprintf(stderr, "Invalid IP address: %s\n", ctx_tcp->ip);
  553 + }
  554 + close(ctx->s);
  555 + ctx->s = -1;
  556 + return -1;
  557 + }
540 558 }
  559 +
541 560 if (bind(new_s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
542 561 close(new_s);
543 562 return -1;
... ...