New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port plugin extension #2253
Closed
Closed
Port plugin extension #2253
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1a64007
network: Started port plugin
ZronekM f78a7d7
network: Implemented core port functionality
ZronekM 49bfd25
network: Implemented port functionality
ZronekM 1ddefa8
network: Minor Cleanup
ZronekM 2f0a8d7
network: Minor Cleanup
ZronekM de603fb
Merge conflicts & Formatting
ZronekM fbfd1ec
Formatting
ZronekM 2a8eab8
Formatting
ZronekM 42892a9
Formatting
ZronekM 7f69d5a
Formatting
ZronekM c1478ae
Formatting
ZronekM be49d6e
Implicit function declaration removed
ZronekM 5bdb8f0
Merge remote-tracking branch 'origin/port-plugin-extension' into port…
ZronekM d141ec6
Formatting
ZronekM 95d50bb
FUCKING formatting
ZronekM e5ebf73
Formatting
ZronekM 3d25ff8
Network: Reformat source code
sanssecours 5220150
testFix
ZronekM e6d5926
plugin: Formatting fixes for port
ZronekM df45c69
Merge branch 'port-plugin-extension' of github.com:Piankero/libelektr…
ZronekM 5e43708
Merge branch 'master' of github.com:ElektraInitiative/libelektra into…
ZronekM 6a90152
network: reduced error messages in specification, changed connect cal…
ZronekM 02451d9
network: correct bind error return handling
ZronekM 27b216e
network: removed confusing comment
ZronekM bee7be9
Merge
ZronekM ad43a18
network-plugin: After merge fix for elektra set error
ZronekM 7cd5e4a
network: Removed fragile test and added comment for it
ZronekM aab7226
network: Clang formatting errors
ZronekM File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,70 @@ int elektraNetworkAddrInfo (Key * toCheck) | |
return 0; | ||
} | ||
|
||
int elektraPortInfo(Key * toCheck, Key * parentKey) { | ||
const Key * meta = keyGetMeta (toCheck, "check/port"); | ||
const Key * listenMeta = keyGetMeta (toCheck, "check/port/listen"); | ||
if (!meta && !listenMeta) return 0; /* No check to do */ | ||
char *endptr = NULL; | ||
long portNumber = strtol(keyString(toCheck), &endptr, 10); | ||
int portNumberNetworkByteOrder; | ||
|
||
if (*endptr == '\0') { | ||
if (portNumber < 0 || portNumber > 65535 || *endptr != 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can remove the check if (*endptr == '\0') makes sure that this is the case. |
||
ELEKTRA_SET_ERRORF(201, parentKey, "Port %d on key %s was not within 0 - 65535", | ||
portNumber, keyName(toCheck)); | ||
return -1; | ||
} | ||
portNumberNetworkByteOrder = htons(portNumber); | ||
} else { | ||
struct servent* service; | ||
service = getservbyname(keyString(toCheck), NULL); //NULL means we accept both tcp and udp | ||
if (service == NULL) { | ||
ELEKTRA_SET_ERRORF(202, parentKey, "Could not find service with name %s on key %s", | ||
keyString(toCheck), keyName(toCheck)); | ||
return -1; | ||
} | ||
portNumberNetworkByteOrder = service->s_port; | ||
} | ||
|
||
//Check if we can connect to it | ||
if (!listenMeta) return 0; /* No check to do */ | ||
|
||
char *hostname = "localhost"; | ||
|
||
int sockfd; | ||
struct sockaddr_in serv_addr; | ||
struct hostent *server; | ||
sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (sockfd < 0) { | ||
ELEKTRA_SET_ERROR(204, parentKey, "Could not open a socket"); | ||
} | ||
|
||
server = gethostbyname(hostname); | ||
if (server == NULL) { | ||
ELEKTRA_SET_ERRORF(204, parentKey, "Could not connect to %s: No such host", | ||
hostname); | ||
return -1; | ||
} | ||
|
||
bzero((char *) &serv_addr, sizeof(serv_addr)); | ||
serv_addr.sin_family = AF_INET; | ||
bcopy((char *)server->h_addr, | ||
(char *)&serv_addr.sin_addr.s_addr, | ||
server->h_length); | ||
|
||
serv_addr.sin_port = (in_port_t) portNumberNetworkByteOrder; | ||
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) == 0) { | ||
close(sockfd); | ||
ELEKTRA_SET_ERRORF(203, parentKey, "Port %s is already in use which was specified on key %s", | ||
keyString(toCheck), keyName(toCheck)); | ||
return -1; | ||
} | ||
close(sockfd); | ||
|
||
return 0; | ||
} | ||
|
||
int elektraNetworkGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey ELEKTRA_UNUSED) | ||
{ | ||
/* configuration only */ | ||
|
@@ -61,6 +125,8 @@ int elektraNetworkGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * | |
keyNew ("system/elektra/modules/network/exports/set", KEY_FUNC, elektraNetworkSet, KEY_END), | ||
keyNew ("system/elektra/modules/network/exports/elektraNetworkAddrInfo", KEY_FUNC, elektraNetworkAddrInfo, | ||
KEY_END), | ||
keyNew ("system/elektra/modules/network/exports/elektraPortInfo", KEY_FUNC, elektraNetworkAddrInfo, | ||
KEY_END), | ||
#include "readme_network.c" | ||
keyNew ("system/elektra/modules/network/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END)); | ||
ksDel (n); | ||
|
@@ -72,7 +138,6 @@ int elektraNetworkSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * | |
{ | ||
/* check all keys */ | ||
Key * cur; | ||
|
||
ksRewind (returned); | ||
while ((cur = ksNext (returned)) != 0) | ||
{ | ||
|
@@ -92,6 +157,10 @@ int elektraNetworkSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * | |
elektraFree (errmsg); | ||
return -1; | ||
} | ||
int p = elektraPortInfo(cur, parentKey); | ||
if (p != 0) { | ||
return -1; | ||
} | ||
} | ||
|
||
return 1; /* success */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need so many errors? Aren't the descriptions and the reasons somewhat overlapping?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I just do an "Port error" and reference to the specific error in the reason metadata?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should generally redesign the errors to be more user friendly. Think about what applications and users care about.
Only if there are situations where applications want to distinguish one error from the other, the error number should be different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could also introduce some inheritance for error messages and something like
kdbContainsError (KDB *key, int whicherror)
(or similar for the higher-level API).We can discuss this tomorrow if you already want to work on that topic. But maybe you should finish the tasks you started already before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored the errors and are only using a single one which I did not see anywhere else in the specification file. I also reused another error number from the range plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error messages will be dealt with another issue #1789