473,472 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling inet_addr from a C++ udf

Hello -

I want to write a very simple User Defined Function in C++ that takes
in a VARCHAR(15) and runs it through inet_addr to return the integer
representation of an IP. My trouble is that I can't seem to call
inet_addr without getting: SQL0430N User defined function
"SECUREWORKS.IP_TO_INT" (specific name
"SQL040617075607700") has abnormally terminated. SQLSTATE=38503

It truly seems to be in the call itself and not my parameters because,
for the sake of testing, I have hard coded an integer return and then
just called inet_addr "on the side" with a simple statement something
like

long lAddress = inet_addr("172.16.55.55");

Has anyone successfully done this before or does anyone have any
helpful suggestions?

Thanks very much
Michael J. Hubbard
Nov 12 '05 #1
4 6462
"Mike Hubbard" <mh******@secureworks.com> wrote in message
news:f3**************************@posting.google.c om...
Hello -

I want to write a very simple User Defined Function in C++ that takes
in a VARCHAR(15) and runs it through inet_addr to return the integer
representation of an IP. My trouble is that I can't seem to call
inet_addr without getting: SQL0430N User defined function
"SECUREWORKS.IP_TO_INT" (specific name
"SQL040617075607700") has abnormally terminated. SQLSTATE=38503


Could just do it in SQL, although remember that SQL only has signed integers
and the below only works if you keep leading zeros in the character
representation.

CREATE FUNCTION F.DSPLY2IPV4 (IP VARCHAR(15))
RETURNS INTEGER
LANGUAGE SQL CONTAINS SQL NO EXTERNAL ACTION DETERMINISTIC
RETURN
(INT(SUBSTR(ip,1,3)) - 128) * 16777216
+ INT(SUBSTR(ip,5,3)) * 65536
+ INT(SUBSTR(ip,9,3)) * 256
+ (INT(SUBSTR(ip,13,3)))
;
COMMENT ON FUNCTION "F"."DSPLY2IPV4" IS
'Coverts an IP address from the standard character representaion of 4
decimal group [0-255] to a signed integer representation. Only works if
leading zeros are kept'
;
C:\SQL>db2 values F.DSPLY2IPV4 ('000.000.000.000'), F.DSPLY2IPV4
('255.255.255.255')

1
-----------
-2147483648
2147483647

2 record(s) selected.
Regards
Paul Vernon
Business Intelligence, IBM Global Services
Nov 12 '05 #2
Mike Hubbard wrote:
Hello -

I want to write a very simple User Defined Function in C++ that takes
in a VARCHAR(15) and runs it through inet_addr to return the integer
representation of an IP. My trouble is that I can't seem to call
inet_addr without getting: SQL0430N User defined function
"SECUREWORKS.IP_TO_INT" (specific name
"SQL040617075607700") has abnormally terminated. SQLSTATE=38503


A SQL0430 means that your UDF code is faulty and crashed. Could you please
show the C code that you used so that we can have a look at it and probably
find the issue that way?
--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3
Knut Stolze <st****@de.ibm.com> wrote in message news:<cb**********@fsuj29.rz.uni-jena.de>...
Mike Hubbard wrote:
Hello -

I want to write a very simple User Defined Function in C++ that takes
in a VARCHAR(15) and runs it through inet_addr to return the integer
representation of an IP. My trouble is that I can't seem to call
inet_addr without getting: SQL0430N User defined function
"SECUREWORKS.IP_TO_INT" (specific name
"SQL040617075607700") has abnormally terminated. SQLSTATE=38503


A SQL0430 means that your UDF code is faulty and crashed. Could you please
show the C code that you used so that we can have a look at it and probably
find the issue that way?


extern "C" {
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sqludf.h>
};

#include "sqlsystm.h"

int CharToInt(char cCharToConvert);
char IntToChar(short iCharToConvert);

#define ASCII_ZERO = 48

extern "C" void SQL_API_FN ip_to_int(SQLUDF_VARCHAR *IPAddress,
long *IPOut,
short *input_ind,
short *output_ind,
char sqlstate[6],
char fname[28],
char finst[19],
char msgtext[71])
{
// long lTestLong = inet_addr("172.111.111.55");
*IPOut = 5555;
return;
}

If I compile the program as you see it and run the function, I get
lots of 5's. If I comment back in the call to inet_addr (which, as
written, has no baring on any of the parameters), I crash.
Nov 12 '05 #4
Mike Hubbard wrote:
Knut Stolze <st****@de.ibm.com> wrote in message
news:<cb**********@fsuj29.rz.uni-jena.de>...
Mike Hubbard wrote:
> Hello -
>
> I want to write a very simple User Defined Function in C++ that takes
> in a VARCHAR(15) and runs it through inet_addr to return the integer
> representation of an IP. My trouble is that I can't seem to call
> inet_addr without getting: SQL0430N User defined function
> "SECUREWORKS.IP_TO_INT" (specific name
> "SQL040617075607700") has abnormally terminated. SQLSTATE=38503
A SQL0430 means that your UDF code is faulty and crashed. Could you
please show the C code that you used so that we can have a look at it and
probably find the issue that way?


extern "C" {
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sqludf.h>
};

#include "sqlsystm.h"


You don't need that. As long as you include <sqludf.h>, you have sqlsystm.h
already. (I hope you didn't modify the header file manually? That would
be a rather stupid idea...)
int CharToInt(char cCharToConvert);
char IntToChar(short iCharToConvert);

#define ASCII_ZERO = 48

extern "C" void SQL_API_FN ip_to_int(SQLUDF_VARCHAR *IPAddress,
long *IPOut,
short *input_ind,
short *output_ind,
char sqlstate[6],
char fname[28],
char finst[19],
char msgtext[71])
I would use the following function declaration instead:

extern "C"
void SQL_API_FN ip_to_int(
SQLUDF_VARCHAR *IPAddress,
SQLUDF_INTEGER *IPOut,
SQLUDF_NULLIND *input_ind,
SQLUDF_NULLIND *output_ind,
SQLUDF_TRAIL_ARGS)

The advantage is simply that you will not have any further problems with the
data types and you will also have the correct parameters. Note that if
there is a parameter missing or if you have too many parameters, then DB2
will not put the right number of values on the stack, causing a stack
corruption. All sorts of weird problems can come into play then.
{
// long lTestLong = inet_addr("172.111.111.55");
You are aware that "inet_addr" is an obsolete function and you might want to
consider "inet_aton" instead?
*IPOut = 5555;
return;
}

If I compile the program as you see it and run the function, I get
lots of 5's.
"Lots of 5's" means four 5's? If not, then you already have a problem
there.
If I comment back in the call to inet_addr (which, as
written, has no baring on any of the parameters), I crash.


I assume that your CREATE FUNCTION statement looks like this?

CREATE FUNCTION ip_to_int(ip VARCHAR(15))
RETURNS INTEGER
EXTERNAL NAME 'test_lib!ip_to_int'
LANGUAGE C
PARAMETER STYLE SQL
DETERMINISTIC -- seems to be a very good idea here
FENCED THREADSAFE
RETURNS NULL ON NULL INPUT
NO SQL
NO EXTERNAL ACTION
NO SCRATCHPAD -- !!!
NO FINAL CALL -- !!!
ALLOW PARALLEL
NO DBINFO -- !!!

If your function definition includes any of the scratchpad/final call/dbinfo
clauses, then you run stright into the problem with the corrupted stack I
mentioned above because your function prototype doesn't match with what DB2
expects.

With this CREATE FUNCTION statement, I could run your code and it worked
very well. So I suspect a problem with your parameters.

Also, you did restart DB2 to make sure that the new version of your library
was picked up after you modified the source code and recompiled it?
Otherwise, DB2 will simply reuse the library it already loaded before.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
2
by: Maydogg6 | last post by:
I need a hand with some stubborn link errors. I'm trying to recreate and old program from 6.0 into .NET, but for some reason when I try to compile I'm getting linking errors for all my function...
1
by: brad | last post by:
Does Python have an equivalent to C's inet_addr()? Thanks, Brad
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.